numir.core.manipulation

Various slice manipulation (e.g., concatenate, squeeze, resize, alongDim)

  • Declaration

    pure auto concatenate(int axis = 0, Slices...)(Slices slices);

    Join multiple slices along an axis.

    Parameters

    axis

    dimension of concatenation

    Slices slices

    slices to concatentate

    Return Value

    concatenated slices

    Examples

    1. import mir.ndslice.fuse : fuse; import mir.ndslice.topology : universal; import mir.ndslice.dynamic : transposed; auto m = fuse([[1, 2],[3, 4]]); auto u = fuse([[5, 6]]); assert(concatenate(m, u) == [[1, 2], [3, 4], [5, 6]]); assert(concatenate(u, m) == [[5, 6], [1, 2], [3, 4]]); auto uT = u.universal.transposed; assert(concatenate!1(m, uT) == [[1, 2, 5], [3, 4, 6]]);

    Examples

    1. import mir.ndslice.fuse : fuse; import mir.ndslice.topology : iota; assert(concatenate!0([[0, 1]].fuse, [[2, 3]].fuse, [[4, 5]].fuse) == iota(3, 2)); assert(concatenate!1([[0, 1]].fuse, [[2, 3]].fuse, [[4, 5]].fuse) == [iota(6)]); // axis=-1 is the same to axis=$-1 assert(concatenate!(-1)([[0, 1]].fuse, [[2, 3]].fuse, [[4, 5]].fuse) == [iota(6)]); assert(concatenate!(-1)([[0, 1]].fuse, [[2]].fuse) == [[0, 1, 2]]);

  • Declaration

    pure auto resize(S)(S s, size_t size) if (isSlice!S);

    Returns a resized n-dimensional slice s with new size. When new size is larger, new slice is padded with 0. When new size is smaller, new slice is just a subslice of s.

    Parameters

    S s

    n-dimensional slice

    size_t size

    new size of s

    Return Value

    new slice with new length

    TODO: support n-dimensional new shape

    Examples

    1. import mir.ndslice.fuse : fuse; import mir.ndslice.slice : sliced; assert([1,2].sliced.resize(3) == [1, 2, 0]); assert([1,2].sliced.resize(2) == [1, 2]); assert([1,2].sliced.resize(1) == [1]); assert([[1,2],[3,4]].fuse.resize(3) == [[1,2], [3,4], [0,0]]); assert([[1,2],[3,4]].fuse.resize(2) == [[1,2], [3,4]]); assert([[1,2],[3,4]].fuse.resize(1) == [[1,2]]); assert([1,2].sliced.resize(0) == [].sliced!int); assert([].sliced!int.resize(3) == [0,0,0]);