toType
Converts the element type of the array to a different numeric type (e.g. Int to Double), returning a new array with the same shape. The CopyStrategy parameter controls whether data is always copied or can be shared when types already match.
Signatures
inline fun <T, reified O : Any, D : Dimension> MultiArray<T, D>.toType(
copy: CopyStrategy = CopyStrategy.FULL
): NDArray<O, D>
fun <T, O : Any, D : Dimension> MultiArray<T, D>.toType(
dtype: DataType,
copy: CopyStrategy = CopyStrategy.FULL
): NDArray<O, D>
Parameters
Parameter | Type | Description |
|---|---|---|
| (reified type) | Target element type. |
|
| Target type as a |
|
| Copy behavior. |
Returns: NDArray<O, D> — a new array with the same shape and converted element type.
Example
val ints = mk.ndarray(mk[1, 2, 3])
val doubles = ints.toType<Int, Double, D1>()
// D1Array<Double> [1.0, 2.0, 3.0]
val floats = ints.toType<Int, Float, D1>()
// D1Array<Float> [1.0, 2.0, 3.0]
Pitfalls
Narrowing conversions (e.g.
DoubletoInt) truncate values —2.9becomes2.When source and target types match with
CopyStrategy.FULL, a copy is still made.
28 February 2026