Creating multidimensional arrays
Overview
Multik offers multiple ways to build ndarrays, depending on how much control you need. You can rely on type and dimension inference for quick experiments, or specify dtype and shape explicitly for production-grade, predictable behavior.
Literal-style construction with mk[]
The fastest way to create an ndarray is to use the mk[] literal builder and pass it to mk.ndarray. The element type and dimension are inferred from the nested structure.
When you need a specific dimension type, you can state it explicitly:
From collections and primitive arrays
Use mk.ndarray or helper extensions to convert Kotlin collections and primitive arrays into ndarrays. This is convenient when you already have data in memory.
If you have a flat primitive array and know the shape, pass it directly:
Factory functions
Multik provides a set of factory helpers for common patterns:
For ranges and evenly spaced values:
Lambda-based creation
Use lambda factories when you want values computed from indices:
For 3D or higher, use the corresponding helpers (d3array, d4array, or ndarray with a shape).
Controlling dtype and dimension
Most creation functions infer dtype and dimension, but you can also be explicit for clarity and type-safety. This helps avoid accidental promotions or dimension mismatches later in your pipeline.
If you need to change element types, see Type casting.