File I/O (JVM only)
All file I/O functions are extension functions on the Multik (mk) object. Import from org.jetbrains.kotlinx.multik.api.io.
Generic read / write
fun <T, D : Dimension> Multik.read(path: Path): NDArray<T, D>
fun Multik.write(path: Path, ndarray: NDArray<*, *>)
The format is inferred from the file extension (.npy or .csv). NPZ must be read with readNPZ explicitly.
NPY (NumPy binary format)
fun <T, D : Dimension> Multik.readNPY(path: String): NDArray<T, D>
fun <T, D : Dimension> Multik.readNPY(path: File): NDArray<T, D>
fun <T, D : Dimension> Multik.readNPY(path: Path): NDArray<T, D>
fun <T, D : Dimension> Multik.writeNPY(path: String, ndarray: NDArray<T, D>)
fun <T, D : Dimension> Multik.writeNPY(path: File, ndarray: NDArray<T, D>)
fun <T, D : Dimension> Multik.writeNPY(path: Path, ndarray: NDArray<T, D>)
Supported types: Byte, Short, Int, Long, Float, Double (no complex).
Dimensions: any.
Binary format — compact and preserves exact dtype and shape.
val a = mk.ndarray(mk[mk[1.0, 2.0], mk[3.0, 4.0]])
mk.writeNPY("matrix.npy", a)
val loaded = mk.readNPY<Double, D2>("matrix.npy")
NPZ (NumPy compressed archive)
fun Multik.readNPZ(path: String): List<NDArray<out Number, out DimN>>
fun Multik.readNPZ(path: File): List<NDArray<out Number, out DimN>>
fun Multik.readNPZ(path: Path): List<NDArray<out Number, out DimN>>
fun Multik.writeNPZ(path: Path, vararg ndArrays: NDArray<*, *>)
fun Multik.writeNPZ(path: Path, ndArrays: List<NDArray<*, *>>)
Supported types: same as NPY (numeric only).
Arrays in the archive are named arr_0, arr_1, etc.
Wrapper around ZIP compression.
val a = mk.ndarray(mk[1, 2, 3])
val b = mk.ndarray(mk[4.0, 5.0])
mk.writeNPZ(Path.of("bundle.npz"), a, b)
val arrays = mk.readNPZ("bundle.npz") // List of 2 arrays
CSV (comma-separated values)
fun <T, D : Dimension> Multik.readCSV(
fileName: String, delimiter: Char = ',', charset: Charset = Charsets.UTF_8
): NDArray<T, D>
fun <T, D : Dimension> Multik.readCSV(
file: File, delimiter: Char = ',', charset: Charset = Charsets.UTF_8
): NDArray<T, D>
fun <T, D : Dimension> Multik.writeCSV(
path: String, ndarray: NDArray<T, D>, delimiter: Char = ','
)
fun <T, D : Dimension> Multik.writeCSV(
file: File, ndarray: NDArray<T, D>, delimiter: Char = ','
)
Supported types: all 8 types including ComplexFloat and ComplexDouble.
Dimensions: D1 and D2 only.
Supports .gz and .zip compressed input files.
Complex values are stored as two space-separated numbers (e.g. "1.5 2.3").
Delimiter is configurable — use '\t' for TSV, ';' for semicolons, etc.
val m = mk.ndarray(mk[mk[1.0, 2.0], mk[3.0, 4.0]])
mk.writeCSV("data.csv", m)
val loaded = mk.readCSV<Double, D2>("data.csv")
In-memory conversions (all platforms)
To collections
fun <T> MultiArray<T, *>.toList(): List<T>
fun <T> MultiArray<T, *>.toMutableList(): MutableList<T>
fun <T> MultiArray<T, *>.toSet(): Set<T>
fun <T> MultiArray<T, *>.toMutableSet(): MutableSet<T>
To nested lists
fun <T> MultiArray<T, D2>.toListD2(): List<List<T>>
fun <T> MultiArray<T, D3>.toListD3(): List<List<List<T>>>
fun <T> MultiArray<T, D4>.toListD4(): List<List<List<List<T>>>>
To primitive arrays (1D)
fun MultiArray<Byte, D1>.toByteArray(): ByteArray
fun MultiArray<Short, D1>.toShortArray(): ShortArray
fun MultiArray<Int, D1>.toIntArray(): IntArray
fun MultiArray<Long, D1>.toLongArray(): LongArray
fun MultiArray<Float, D1>.toFloatArray(): FloatArray
fun MultiArray<Double, D1>.toDoubleArray(): DoubleArray
fun MultiArray<ComplexFloat, D1>.toComplexFloatArray(): ComplexFloatArray
fun MultiArray<ComplexDouble, D1>.toComplexDoubleArray(): ComplexDoubleArray
To nested primitive arrays (2D–4D)
fun <T> MultiArray<T, D2>.toArray(): Array<*> // e.g. Array<DoubleArray>
fun <T> MultiArray<T, D3>.toArray(): Array<*> // e.g. Array<Array<DoubleArray>>
fun <T> MultiArray<T, D4>.toArray(): Array<*> // e.g. Array<Array<Array<DoubleArray>>>
The inner array type matches the element dtype.