Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using a local clone.


Multidimensional Random Access Ranges

The package provides a multidimensional array implementation. It would be well suited to creating machine learning and image processing algorithms, but should also be general enough for use anywhere with homogeneously-typed multidimensional data. In addition, it includes various functions for iteration, accessing, and manipulation.
Advanced and fast iteration algorithms, matrix multiplication, and BLAS-like functions can be found in the Mir LLVM-Accelerated Generic Numerical Library for Science and Machine Learning.

Quick Start: sliced  is a function designed to create a multidimensional view over a range. Multidimensional view is presented by Slice  type.

auto matrix = slice!double(3, 4);
matrix[] = 0;
matrix.diagonal[] = 1;

auto row = matrix[2];
row[3] = 6;
assert(matrix[2, 3] == 6); // D & C index order

Note: In many examples iotaSlice  is used instead of a regular array, which makes it possible to carry out tests without memory allocation.

Submodule Declarations
Slice , its properties, operator overloading
as  assumeSameStructure  DeepElementType  makeNdarray  makeSlice  makeUninitializedSlice  ndarray  ReplaceArrayWithPointer  shape  Slice  slice  sliced  SliceException  uninitializedSlice 
Basic iteration operators
allReversed  everted  reversed  rotated  strided  swapped  transposed  dropToHypercube  and other drop primitives
Subspace manipulations
Operators for loop free programming
blocks  byElement  byElementInStandardSimplex  diagonal  evertPack  indexSlice  iotaSlice  mapSlice  pack  repeatSlice  reshape  ReshapeException  unpack  windows 

Example: Image Processing

A median filter is implemented as an example. The function movingWindowByChannel can also be used with other filters that use a sliding window as the argument, in particular with convolution matrices such as the Sobel operator.
movingWindowByChannel iterates over an image in sliding window mode. Each window is transferred to a filter, which calculates the value of the pixel that corresponds to the given window.
This function does not calculate border cases in which a window overlaps the image partially. However, the function can still be used to carry out such calculations. That can be done by creating an amplified image, with the edges reflected from the original image, and then applying the given function to the new file.

Note: You can find the example at GitHub.

    filter = unary function. Dimension window 2D is the argument.
    image = image dimensions `(h, w, c)`,
        where с is the number of channels in the image
    nr = number of rows in the window
    nс = number of columns in the window

    image dimensions `(h - nr + 1, w - nc + 1, c)`,
        where с is the number of channels in the image.
        Dense data layout is guaranteed.

Slice!(3, C*) movingWindowByChannel(alias filter, C)
(Slice!(3, C*) image, size_t nr, size_t nc)
        // 0. 3D
        // The last dimension represents the color channel.
    return image
        // 1. 2D composed of 1D
        // Packs the last dimension.
        // 2. 2D composed of 2D composed of 1D
        // Splits image into overlapping windows.
        .windows(nr, nc)
        // 3. 5D
        // Unpacks the windows.
        // 4. 5D
        // Brings the color channel dimension to the third position.
        .transposed!(0, 1, 4)
        // 5. 3D Composed of 2D
        // Packs the last two dimensions.
        // 2D to pixel lazy conversion.
        // Creates the new image. The only memory allocation in this function.
A function that calculates the value of iterator median is also necessary.

    r = input range
    buf = buffer with length no less than the number of elements in `r`
    median value over the range `r`
T median(Range, T)(Slice!(2, Range) sl, T[] buf)
    import std.algorithm.sorting : topN;
    // copy sl to the buffer
    size_t n;
    foreach (r; sl)
        foreach (e; r)
            buf[n++] = e;
    buf[0 .. n].topN(n / 2);
    return buf[n / 2];
The main function:
void main(string[] args)
    import std.conv : to;
    import std.getopt : getopt, defaultGetoptPrinter;
    import std.path : stripExtension;

    uint nr, nc, def = 3;
    auto helpInformation = args.getopt(
        "nr", "number of rows in window, default value is " ~!string, &nr,
        "nc", "number of columns in window, default value is equal to nr", &nc);
    if (helpInformation.helpWanted)
            "Usage: median-filter [<options...>] [<file_names...>]\noptions:",
    if (!nr) nr = def;
    if (!nc) nc = nr;

    auto buf = new ubyte[nr * nc];

    foreach (name; args[1 .. $])
        import imageformats; // can be found at

        IFImage image = read_image(name);

        auto ret = image.pixels
            .sliced(cast(size_t)image.h, cast(size_t)image.w, cast(size_t)image.c)
                !(window => median(window, buf))
                 (nr, nc);

            name.stripExtension ~ "_filtered.png",
            (&ret[0, 0, 0])[0 .. ret.elementsCount]);
This program works both with color and grayscale images.
$ median-filter --help
Usage: median-filter [<options...>] [<file_names...>]
     --nr number of rows in window, default value is 3
     --nc number of columns in window default value equals to nr
-h --help This help information.

Compared with numpy.ndarray

numpy is undoubtedly one of the most effective software packages that has facilitated the work of many engineers and scientists. However, due to the specifics of implementation of Python, a programmer who wishes to use the functions not represented in numpy may find that the built-in functions implemented specifically for numpy are not enough, and their Python implementations work at a very low speed. Extending numpy can be done, but is somewhat laborious as even the most basic numpy functions that refer directly to ndarray data must be implemented in C for reasonable performance.
At the same time, while working with ndslice, an engineer has access to the whole set of standard D library, so the functions he creates will be as efficient as if they were written in C.

Ilya Yaroshenko

Acknowledgements: John Loughran Colvin