The Units Indexing Function
Description
Examples
A Unit of measurement can be added as the indexing function when constructing an rtable. Subsequent queries into the rtable will return the underlying quantity times the given Unit. This indexing function allows you to keep the Unit and data separate, notably allowing datatype=float[8] on an array of quantities involving units.
This indexing function is designed to work identically to a rtable without a Unit indexing function, so it can be used as a starting point initially with a fixed unit, and not trigger errors when some or all entries deviate from that unit. The underlying mechanism keeps the unit separate from the data. When an entry is assigned that does not match the default unit, a new mirror-rtable containing only units is created and added to the indexing function. The rtable_split_unit command allows you to extract and separate the data from the unit.
A consequence of this design is that all assignments need to have the Unit specified in the assigned value, even at initialization. Initializing an rtable with another rtable that is purely numeric (with no units) will result in a purely numeric rtable with all entries marked as unitless (unless those entries are zero). The rtable_set_indfn can be used to add a units indexing function to an existing rtable, but be advised that this function always works in-place.
In this example we separate the data from the unit in order to perform a fast operation (for simplicity we raise to the power of 4), and then create a result with a new unit attached.
M≔Matrix⁡2,i,j↦10⋅i+j⋅Unit⁡m,shape=Unit⁡m,datatype=float8
M≔11.⁢m12.⁢m21.⁢m22.⁢m
split≔rtable_split_unit⁡M
split≔m,11.12.21.22.
default_unit≔split1
default_unit≔m
data≔split2
data≔11.12.21.22.
newdata≔data4
newdata≔413557.439164.768537.816124.
newM≔rtable_set_indfn⁡newdata,Units:-Simple:-simplify⁡default_unit4
newM≔413557.⁢m4439164.⁢m4768537.⁢m4816124.⁢m4
See Also
Array
Matrix
rtable
rtable_indexfcn
rtable_set_indfn
rtable_split_unit
Units
Units:-Simple
Download Help Document