map
Applies a transformation function to each element and returns a new array of the same shape. Indexed variants provide the element's flat index (mapIndexed, 1D only) or multi-dimensional index (mapMultiIndexed). The result element type R must be a supported DataType.
Signatures
inline fun <T, D : Dimension, reified R : Any> MultiArray<T, D>.map(
transform: (T) -> R
): NDArray<R, D>
inline fun <T, reified R : Any> MultiArray<T, D1>.mapIndexed(
transform: (index: Int, T) -> R
): D1Array<R>
inline fun <T, D : Dimension, reified R : Any> MultiArray<T, D>.mapMultiIndexed(
transform: (index: IntArray, T) -> R
): NDArray<R, D>
inline fun <T, D : Dimension, reified R : Any> MultiArray<T, D>.mapNotNull(
transform: (T) -> R?
): NDArray<R, D>
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Maps each element to a new value. |
|
| With flat index (1D arrays only). |
|
| With multi-dimensional index. |
Returns: NDArray<R, D> — a new array with the same shape and transformed elements.
Example
val a = mk.ndarray(mk[mk[1, 2], mk[3, 4]])
val doubled = a.map { it * 2 }
// [[2, 4],
// [6, 8]]
val b = mk.ndarray(mk[10, 20, 30])
b.mapIndexed { i, v -> v + i }
// [10, 21, 32]
a.mapMultiIndexed { idx, v -> idx.sum() + v }
// [[1, 3],
// [4, 6]]
28 February 2026