Libmir Archive
mir-algorithmmir-algorithm

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

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?

FeatureD [][] arraysndslice
Memory layoutJagged (array of arrays)Contiguous or strided
TransposeCopy requiredZero-copy view
ReshapeManualreshape, flattened
Lazy mapstd.algorithm.map (range)mir.ndslice.topology.map (slice)
BLAS interopNeeds manual pointerDirect via mir-blas
BetterC compatibleNoYes (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);
}
  • 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

On this page