Type casting
Overview
Type casting changes the dtype of an ndarray while preserving its shape and dimension. Multik provides two primary APIs:
asType<T>()onNDArraytoType<T>()onMultiArray
Both create a new array with converted element values.
asType on NDArray
asType is a simple way to cast an NDArray to a new numeric type. It preserves shape and dimension.
You can also cast using a DataType:
toType on MultiArray
toType works for any MultiArray and supports copy strategies. It can be useful when you are working with views or with arrays that are not consistent.
Copy strategies
toType accepts a CopyStrategy:
CopyStrategy.FULL(default) copies the entire underlying storage.CopyStrategy.MEANINGFULcopies only the visible elements of the array.
Use MEANINGFUL when you want to materialize just the data visible through a view. Use FULL when you want to preserve the full underlying storage layout.
Casting to complex types
Numeric arrays can be converted to complex types; the imaginary part is set to zero:
Conversions between complex types are supported as well.
Notes and tips
Casting always creates a new array; it does not modify the original.
When the dtype is already the target type,
toTypereturns a copy based on the chosen strategy.