Report a bug
If you spot a problem with this page, click here to create a GitHub 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.

mir.ndslice.fuse

This is a submodule of mir.ndslice.
Allocation routines that construct ndslices from ndranges.
License:
Authors:
Ilya Yaroshenko
See Also:
concatenation submodule.
template fuse(Dimensions...)

template rcfuse(Dimensions...)
Fuses ndrange r into GC-allocated (fuse) or RC-allocated (rcfuse) ndslice. Can be used to join rows or columns into a matrix.
Parameters:
Dimensions (optional) indices of dimensions to be brought to the first position
Returns:
ndslice
Examples:
import mir.ndslice.fuse;
import mir.ndslice.slice : Contiguous, Slice;
import mir.ndslice.topology: iota;
import mir.rc.array: RCI;

enum ror = [
        [0, 1, 2, 3],
        [4, 5, 6, 7],
        [8, 9,10,11]];

//  0  1  2  3
//  4  5  6  7
//  8  9 10 11
auto matrix = ror.fuse;

auto rcmatrix = ror.rcfuse; // nogc version

assert(matrix == [3, 4].iota);
assert(rcmatrix == [3, 4].iota);
static assert(ror.fuse == [3, 4].iota); // CTFE-able

// matrix is contiguos
static assert(is(typeof(matrix) == Slice!(int*, 2)));
static assert(is(typeof(rcmatrix) == Slice!(RCI!int, 2)));
Examples:
Transposed
import mir.ndslice.fuse;
import mir.ndslice.topology: iota;
import mir.ndslice.dynamic: transposed;
import mir.ndslice.slice : Contiguous, Slice;

enum ror = [
    [0, 1, 2, 3],
    [4, 5, 6, 7],
    [8, 9,10,11]];

//  0  4  8
//  1  5  9
//  2  6 10
//  3  7 11

// `!1` brings dimensions under index 1 to the front (0 index).
auto matrix = ror.fuse!1;

assert(matrix == [3, 4].iota.transposed!1);
// TODO: CTFE
// static assert(ror.fuse!1 == [3, 4].iota.transposed!1); // CTFE-able
// matrix is contiguos
static assert(is(typeof(matrix) == Slice!(int*, 2)));
Examples:
3D
import mir.ndslice.fuse;
import mir.ndslice.topology: iota;
import mir.ndslice.dynamic: transposed;

auto ror =
  [[[ 0, 1, 2, 3],
    [ 4, 5, 6, 7]],
   [[ 8, 9,10,11],
    [12,13,14,15]]];

auto nd = [2, 2, 4].iota;

assert(ror.fuse == nd);
assert(ror.fuse!2 == nd.transposed!2);
assert(ror.fuse!(1, 2) == nd.transposed!(1, 2));
assert(ror.fuse!(2, 1) == nd.transposed!(2, 1));
Examples:
Work with RC Arrays of RC Arrays
import mir.ndslice.fuse;
import mir.ndslice.slice;
import mir.ndslice.topology: map;
import mir.rc.array;

Slice!(const(double)*, 2) conv(RCArray!(const RCArray!(const double)) a)
{
    return a[].map!"a[]".fuse;
}
template fuseAs(T, Dimensions...)

template rcfuseAs(T, Dimensions...)
Fuses ndrange r into GC-allocated (fuseAs) or RC-allocated (rcfuseAs) ndslice. Can be used to join rows or columns into a matrix.
Parameters:
T output type of ndslice elements
Dimensions (optional) indices of dimensions to be brought to the first position
Returns:
ndslice
Examples:
import mir.ndslice.fuse;
import mir.ndslice.slice : Contiguous, Slice;
import mir.ndslice.topology: iota;
import mir.rc.array: RCI;

enum ror = [
        [0, 1, 2, 3],
        [4, 5, 6, 7],
        [8, 9,10,11]];

//  0  1  2  3
//  4  5  6  7
//  8  9 10 11
auto matrix = ror.fuseAs!double;

auto rcmatrix = ror.rcfuseAs!double; // nogc version

assert(matrix == [3, 4].iota);
assert(rcmatrix == [3, 4].iota);
static assert(ror.fuseAs!double == [3, 4].iota); // CTFE-able

// matrix is contiguos
static assert(is(typeof(matrix) == Slice!(double*, 2)));
static assert(is(typeof(rcmatrix) == Slice!(RCI!double, 2)));
template fuseImpl(bool RC, T_, Dimensions...)
auto fuseImpl(NDRange)(NDRange r)
if (hasShape!NDRange);
Parameters:
NDRange r parallelotope (ndrange) with length/shape and input range primitives.
auto fuseCells(S)(S cells);
Fuses cells into GC-allocated ndslice.
Parameters:
S cells ndrange of ndcells, ndrange and ndcell should have shape and multidimensional input range primivies (front!d, empty!d, popFront!d).
Returns:
ndslice composed of fused cells.
See Also:
Examples:
1D
import mir.ndslice.topology: iota;
enum ar = [[0, 1], [], [2, 3, 4, 5], [6], [7, 8, 9]];
static assert ([[0, 1], [], [2, 3, 4, 5], [6], [7, 8, 9]].fuseCells == 10.iota);
assert (ar.fuseCells == 10.iota);
Examples:
2D
import mir.ndslice.topology: iota;
import mir.ndslice.chunks;

auto sl = iota(11, 17);
assert(sl.chunks!(0, 1)(3, 4).fuseCells == sl);