numir.core.utility

Various slice utility (e.g., ndim, view, type functions)

  • Declaration

    template rank(R)

    Returns the number of dimensions of type R.

    Examples

    1. static assert(rank!(int[2]) == 1); static assert(rank!(int[2][3]) == 2);

  • Declaration

    template NestedElementType(T)

    Returns the ElementType of a nested type T.

    Examples

    1. static assert(is(NestedElementType!(int[][]) == int));

  • Declaration

    pure nothrow size_t[rank!T] shapeNested(T)(T array);

    Returns the shape of a nested array length. Note that the array should have compile-time nested lengths. Dynamic arrays get compile errors.

    Parameters

    T array

    input array

    Return Value

    shape

    Examples

    1. assert([1].shapeNested == [1]); assert([1, 2].shapeNested == [2]); assert([[1,2],[3,4],[5,6]].shapeNested == [3, 2]);

  • Declaration

    pure auto dtype(S)(S s);

    Returns the typeid of the element type of S.

    Examples

    1. import mir.ndslice.topology : iota, as; auto e = iota([2, 3, 1, 3]).as!double; assert(e.dtype == typeid(double)); assert(e.dtype != typeid(float));

  • Declaration

    pure size_t[] byteStrides(S)(S s);

    Return the number of bytes to step in each dimension when traversing a slice.

    Parameters

    S s

    n-dimensional slice

    Return Value

    array of byte strides

    Examples

    1. import mir.ndslice.topology : iota, as; auto e = iota(2, 3, 1, 3); assert(e.as!int.byteStrides == [36, 12, 12, 4]); assert(e.as!double.byteStrides == [72, 24, 24, 8]);

  • Declaration

    pure auto size(S)(S s);

    Returns the total number of elements in a slice

    Parameters

    S s

    array

    Return Value

    total number of elements in a slice

    Examples

    1. import mir.ndslice.topology : iota; auto e = iota(2, 3, 1, 3); assert(e.size == (2 * 3 * 1 * 3));

  • Declaration

    pure auto view(S, size_t N)(S sl, ptrdiff_t[N] lengths...);

    Returns a new view of a slice with the same data, but reshaped to have shape equal to lengths.

    Parameters

    S sl

    n-dimensional slice

    ptrdiff_t[N] lengths

    A list of lengths for each dimension

    Return Value

    new view of a slice with the same data

    Examples

    1. import std.string : split; import mir.ndslice.topology : universal, iota; import mir.ndslice.allocation : slice; import mir.ndslice.dynamic : transposed; assert(iota(3, 4).slice.view(-1, 1).shape == [12, 1]); assert(iota(3, 4).slice.universal.transposed.view(-1, 6).shape == [2, 6]);

    Examples

    Invalid views generate ReshapeError

    1. import std.string : split; import mir.ndslice.topology : iota; import mir.ndslice.allocation : slice; try { iota(3, 4).slice.view(2, 1); } catch (Exception e) { assert(e.msg.split(":")[0] == "ReshapeError"); } try { iota(0).slice.view(2, 1); } catch (Exception e) { assert(e.msg.split(":")[0] == "ReshapeError"); }

  • Declaration

    size_t ndim(S)(S s) if (isSlice!S);

    Returns the number of dimensions of a slice.

    Parameters

    S s

    n-dimensional slice

    Return Value

    number of dimensions

    Examples

    1. import mir.ndslice.topology : iota; auto e = iota(2, 3, 1, 3); assert(e.ndim == 4);