numir.core.creation

Various slice creation (e.g., zeros, ones, zeros_like, diag)

  • Declaration

    pure auto empty(E = double, size_t N)(size_t[N] length...);

    Construct new uninitialized slice of element type E and shape(length ...).

    Parameters

    size_t[N] length

    elements of shape

    Return Value

    new uninitialized slice

    Examples

    1. assert(empty(2, 3).shape == [2, 3]); assert(empty([2, 3]).shape == [2, 3]);

  • Declaration

    pure auto like(alias initializer, S)(S slice);

    Construct new slice having the same element type and shape to given slice.

    Parameters

    initializer

    template function(ElementType)(shape) that initializes slice

    S slice

    n-dimensional slice to refer shape and element type

    Return Value

    new initialized slice with Initializer alias

    Examples

    1. import mir.ndslice.topology : iota; import mir.ndslice.slice : DeepElementType; // ------- // | 1 2 3 | // | 4 5 6 | // ------- auto s = iota([2, 3], 1); // ------- // | 0 1 2 | // | 3 4 5 | // ------- auto e = s.like!iota; static assert(is(typeof(e) == typeof(s))); assert(e.shape == s.shape); assert(e != s); alias S = DeepElementType!(typeof(s)); alias E = DeepElementType!(typeof(e)); static assert(is(S == E));

  • Declaration

    pure auto empty_like(S)(S slice);

    Construct new empty slice having the same element type and shape to given slice.

    Parameters

    S slice

    n-dimensional slice to refer shape and element type

    Return Value

    new empty slice

    Examples

    1. auto s = empty!int(2, 3); auto e = empty_like(s); static assert(is(typeof(e) == typeof(s))); assert(e.shape == s.shape); s[0, 0] += 1; assert(e != s);

  • Declaration

    pure auto ones(E = double, size_t N)(size_t[N] lengths...);

    Construct a new slice, filled with ones, of element type E and shape(lengths ...).

    Parameters

    size_t[N] lengths

    elements of shape

    Return Value

    new ones slice

    Examples

    1. import mir.algorithm.iteration : all; // ------- // | 1 1 1 | // | 1 1 1 | // ------- auto o = ones(2, 3); assert(o.all!(x => x == 1)); assert(o.shape == [2, 3]); assert(o == ones([2, 3]));

  • Declaration

    pure auto ones_like(S)(S slice);

    Construct new ones slice having the same element type and shape to given slice.

    Parameters

    S slice

    n-dimensional slice to refer shape and element type

    Return Value

    new ones slice

    Examples

    1. import mir.ndslice.topology : iota; // ------- // | 0 1 2 | // | 3 4 5 | // ------- auto e = iota(2, 3); assert(e.ones_like == ones(2, 3));

  • Declaration

    pure auto zeros(E = double, size_t N)(size_t[N] lengths...);

    Construct a new slice, filled with zeroes, of element type E and shape(lengths ...).

    Parameters

    size_t[N] lengths

    elements of shape

    Return Value

    new zeroes slice

    Examples

    1. import mir.algorithm.iteration : all; // ------- // | 0 0 0 | // | 0 0 0 | // ------- auto z = zeros(2, 3); assert(z.all!(x => x == 0)); assert(z.shape == [2, 3]); assert(z == zeros([2, 3]));

  • Declaration

    pure auto zeros_like(S)(S slice);

    Construct new zeroes slice having the same element type and shape to given slice.

    Parameters

    S slice

    n-dimensional slice to refer shape and element type

    Return Value

    new zeroes slice

    Examples

    1. import mir.ndslice.topology : iota; // ------- // | 0 1 2 | // | 3 4 5 | // ------- auto e = iota(2, 3); assert(e.zeros_like == zeros(2, 3));

  • eye

    Declaration

    pure auto eye(E = double, size_t dimension = 1)(size_t m, size_t n = 0, size_t k = 0);

    Construct a new slice with ones along a diagonal and zeroes elsewhere of element type E.

    Discussion

    The diagonal is set through the interaction of dimension and k. k determines the offset of the diagonal and dimension controls the dimension of that this offset proceeds in. For instance, if dimension = 1 and k = 1, then the diagonal after the first column is what is filled with ones.

    Parameters

    dimension

    axis to apply k offset

    size_t m

    number of rows of output

    size_t n

    number of columns of output (default = 0, sets n = m)

    size_t k

    offset for start of diagonal (default = 0)

    Return Value

    new eye slice

  • eye

    Declaration

    pure auto eye(size_t dimension)(size_t m, size_t n = 0, size_t k = 0);

    Returns a double eye slice.

    Parameters

    dimension

    axis to apply k offset

    size_t m

    number of rows of output

    size_t n

    number of columns of output (default = 0, sets n = m)

    size_t k

    offset for start of diagonal (default = 0)

    Return Value

    new eye slice

    Examples

    1. assert(eye(2) == [[1.0, 0.0], [0.0, 1.0]]);

    Examples

    1. assert(eye(2, 3) == [[1.0, 0.0, 0.0], [0.0, 1.0, 0.0]]); assert(eye(2, 3, 1) == [[0.0, 1.0, 0.0], [0.0, 0.0, 1.0]]); assert(eye(2, 3, 2) == [[0.0, 0.0, 1.0], [0.0, 0.0, 0.0]]); assert(eye!0(2, 3, 1) == [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]);

  • Declaration

    pure auto identity(E = double)(size_t n);

    Returns a square slice of element type E with ones on the main diagonal and zeros elsewhere.

    Parameters

    size_t n

    number of rows and columns

    Return Value

    new identity slice

    Examples

    1. assert(identity(2) == [[1.0, 0.0], [0.0, 1.0]]);

  • Declaration

    auto arange(size_t size);

    Returns a 1-dimensional slice whose elements are equal to its indices.

    Parameters

    size_t size

    length

    Return Value

    1-dimensional slice composed of indices

  • Declaration

    pure auto arange(E)(E start, E end, E step = 1);

    Returns a 1-dimensional slice whose elements are equal to its indices.

    Parameters

    E start

    value of the first index

    E end

    value of the last index

    E step

    value between indices (default = 1)

    Return Value

    1-dimensional slice composed of indices

    Examples

    1. assert(arange(3) == [0, 1, 2]); assert(arange(2, 3, 0.3) == [2.0, 2.3, 2.6, 2.9]);

  • Declaration

    auto linspace(E = double)(E start, E stop, size_t num = 50);

    Returns a slice with evenly spaced numbers over an interval.

    Parameters

    E start

    value of the first element

    E stop

    value of the last element

    size_t num

    number of points in the interval (default = 50)

    Return Value

    slice with evenly spaced numbers over an interval

    Examples

    1. assert(linspace(1, 2, 3) == [1.0, 1.5, 2.0]);

  • Declaration

    pure auto steppedIota(E)(size_t num, E step, E start = 0);

    An alternate, 1-dimensional version of iota (and different API).

    Parameters

    size_t num

    number of values to return

    E step

    value between indices

    E start

    value of the first index

    Return Value

    1-dimensional slice composed of indices

    Examples

    1. assert(steppedIota!double(4, 0.3, 2.0) == [2.0, 2.3, 2.6, 2.9]);

  • Declaration

    auto logspace(E = double)(E start, E stop, size_t num = 50, E base = 10);

    Returns a slice with numbers evenly spaced over a log scale.

    Discussion

    In linear space, the sequence starts at base ^^ start (base to the power of start) and ends with base ^^ stop.

    Parameters

    E start

    base ^^ start is the value of the first element

    E stop

    base ^^ stop is the value of the last element

    size_t num

    number of points in the interval (default = 50)

    E base

    the base of the log space (default = 10)

    Return Value

    slice with numbers evenly spaced over a log scale

    Examples

    1. assert(logspace(1, 2, 3, 10) == [10. ^^ 1.0, 10. ^^ 1.5, 10. ^^ 2.0]);

  • Declaration

    template diag(size_t dimension = 1)

    Extract the diagonal of a 2-dimensional slice.

    Parameters

    dimension

    dimension to apply the offset, k (default = 1)

    S s

    2-dimensional slice

    size_t k

    offset from the main diagonal, k > 0 for diagonals above the main diagonal, and k < 0 for diagonals below the main diagonal

    Return Value

    the extracted diagonal slice

    Examples

    1. import mir.ndslice.topology : iota; // ------- // | 0 1 2 | // | 3 4 5 | // ------- auto a = iota(2, 3); assert(a.diag == [0, 4]); assert(a.diag!1(1) == [1, 5]); assert(a.diag!0(1) == [3]);

    Examples

    example from https://docs.scipy.org/doc/numpy/reference/generated/numpy.apply_along_axis.html

    1. import mir.ndslice : iota, map, alongDim; static immutable d33 = [[[1, 0, 0], [0, 2, 0], [0, 0, 3]], [[4, 0, 0], [0, 5, 0], [0, 0, 6]], [[7, 0, 0], [0, 8, 0], [0, 0, 9]]]; assert(iota([3, 3], 1).alongDim!(-1).map!diag == d33);

  • Declaration

    pure auto diag(S)(S s) if (isVector!S);

    Create a diagonal 2-dimensional slice from a 1-dimensional slice.

    Parameters

    S s

    1-dimensional slice

    Return Value

    diagonal 2-dimensional slice

    Examples

    1. import mir.ndslice.topology : iota; import mir.ndslice.slice : sliced; // ------- // | 0 1 2 | // ------- auto a = iota(3).diag; auto result = [[0, 0, 0], [0, 1, 0], [0, 0, 2]].sliced; assert(a[0] == result[0]); assert(a[1] == result[1]); assert(a[2] == result[2]);