d2arrayIndices
Creates a 2D array of the given shape, where each element is computed from its row (i) and column (j) indices. Useful when the element value depends on its position in the matrix.
Signature
inline fun <reified T : Any> Multik.d2arrayIndices(
sizeD1: Int,
sizeD2: Int,
init: (i: Int, j: Int) -> T
): D2Array<T>
Parameters
Parameter | Type | Description |
|---|---|---|
|
| Number of rows. Must be positive. |
|
| Number of columns. Must be positive. |
|
| Lambda that receives row index |
Returns: D2Array<T>
Example
val m = mk.d2arrayIndices<Int>(3, 3) { i, j -> i + j }
// [[0, 1, 2],
// [1, 2, 3],
// [2, 3, 4]]
val identity = mk.d2arrayIndices<Double>(4, 4) { i, j ->
if (i == j) 1.0 else 0.0
}
28 February 2026