mir-algorithm
The core Mir library for D — multidimensional arrays (ndslice), functional iteration, and numerical algorithms. Replaces the D standard library's std.algorithm for numerical computing workloads.
📖 Full API reference → mir-algorithm.libmir.org — every module and symbol of
mir-algorithm, regenerated from current source.
mir-algorithm is the foundation of the Mir ecosystem. Its centrepiece is ndslice — a multidimensional array abstraction that combines D's range/slice semantics with strided memory layouts, lazy views, and functional pipelines.
Install
// dub.sdl
dependency "mir-algorithm" version="~>3.0"Latest release: v3.22.4 (June 2025) — DUB registry · GitHub
Modules
ndslice
N-dimensional array slices — create, index, reshape, iterate.
ndslice topology
Lazy views: reshape, transposed, diagonal, iota, repeat, zip.
ndslice field
Custom field types for ndslice — map arbitrary memory layouts.
Algorithms
map, reduce, each, fold, all, any — functional operations on ndslice.
Hello, ndslice
import mir.ndslice;
import std.stdio;
void main()
{
// 3×4 matrix, row-major (C order)
auto m = slice!double(3, 4);
// Fill with the product of indices
m.each!((ref a, size_t i, size_t j) { a = i * 4 + j; });
writeln(m);
// [[0, 1, 2, 3],
// [4, 5, 6, 7],
// [8, 9, 10, 11]]
// Transpose is a lazy view — no copy
writeln(m.transposed);
// [[0, 4, 8],
// [1, 5, 9],
// [2, 6, 10],
// [3, 7, 11]]
}Why ndslice over built-in arrays?
| Feature | D [][] arrays | ndslice |
|---|---|---|
| Memory layout | Jagged (array of arrays) | Contiguous or strided |
| Transpose | Copy required | Zero-copy view |
| Reshape | Manual | reshape, flattened |
| Lazy map | std.algorithm.map (range) | mir.ndslice.topology.map (slice) |
| BLAS interop | Needs manual pointer | Direct via mir-blas |
| BetterC compatible | No | Yes (with Slice directly) |
BetterC support
mir-algorithm is designed for @safe, @nogc, and betterC code:
extern(C) void compute(double* data, size_t rows, size_t cols) @nogc nothrow
{
import mir.ndslice;
// Wrap existing memory — no allocation
auto s = data.sliced(rows, cols);
s[] = s.map!(x => x * 2.0);
}Related libraries
- mir-core — algebraic types and primitives used throughout mir-algorithm
- mir-blas / mir-lapack — BLAS/LAPACK bindings that accept ndslice directly
- lubeck (kaleidicassociates) — high-level linear algebra on top of mir-blas
- mir-random — distributions that produce ndslice outputs
Related guides
Libmir Archive
Community documentation archive for Mir — the D language numerical computing library ecosystem.
ndslice — Multidimensional Arrays
mir.ndslice is the core data structure in mir-algorithm. It provides N-dimensional array slices with contiguous or strided memory, lazy views, and full BetterC support.