Multik 0.3.0 Help

ndarray

Overview

mk.ndarray() is the most common way to create arrays. It infers type and dimension from the input. Use mk[...] to build nested list literals inline.

From nested lists with mk[]

val v = mk.ndarray(mk[1, 2, 3]) // D1Array<Int> val m = mk.ndarray(mk[mk[1.0, 2.0], mk[3.0, 4.0]]) // D2Array<Double> val t = mk.ndarray(mk[mk[mk[1, 2], mk[3, 4]]]) // D3Array<Int>

Overloads exist for 1D–4D with both Number and Complex element types.

From collection with shape

val a = mk.ndarray<Int, D2>(listOf(1, 2, 3, 4), intArrayOf(2, 2)) // D2Array<Int>, shape (2, 2)

Parameter

Type

Description

elements

Collection<T>

Flat collection of values.

shape

IntArray

Target shape. Product must equal elements.size.

Dimension can be reified or passed explicitly:

val b = mk.ndarray(listOf(1, 2, 3, 4), intArrayOf(2, 2), D2)

From collection with dimension sizes

val m = mk.ndarray(listOf(1, 2, 3, 4, 5, 6), 2, 3) // D2Array, shape (2, 3) val t = mk.ndarray(listOf(1, 2, 3, 4, 5, 6), 1, 2, 3) // D3Array, shape (1, 2, 3)

Overloads for 2D, 3D, 4D, and N-D (5+ dimensions via varargs).

From primitive arrays

1D:

val a = mk.ndarray(intArrayOf(1, 2, 3)) // D1Array<Int> val b = mk.ndarray(doubleArrayOf(1.0, 2.0)) // D1Array<Double>

With shape:

val m = mk.ndarray(intArrayOf(1, 2, 3, 4), 2, 2) // D2Array<Int>

Supported types: ByteArray, ShortArray, IntArray, LongArray, FloatArray, DoubleArray, ComplexFloatArray, ComplexDoubleArray.

From Array of primitive arrays

val m = mk.ndarray(arrayOf(intArrayOf(1, 2), intArrayOf(3, 4))) // D2Array<Int>

Pitfalls

  • The product of the shape must exactly equal the number of elements. A mismatch throws IllegalArgumentException.

  • Nested list inputs are validated for consistent sizes — jagged lists are rejected.

28 February 2026