numir.core.creation
Various slice creation (e.g., zeros, ones, zeros_like, diag)
-
Declaration
pure autoempty(E = double, size_t N)(size_t[N]length...);Construct new uninitialized slice of element type
Eand shape().length...Parameters
size_t[N]lengthelements of shape
Return Value
new uninitialized slice
Examples
assert(empty(2, 3).shape == [2, 3]); assert(empty([2, 3]).shape == [2, 3]);
-
Declaration
pure autolike(alias initializer, S)(Sslice);Construct new
slicehaving the same element type and shape to givenslice.Parameters
initializertemplate function(ElementType)(shape) that initializes
sliceSslicen-dimensional
sliceto refer shape and element typeReturn Value
new initialized
slicewith Initializer aliasExamples
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 autoempty_like(S)(Sslice);Construct new empty
slicehaving the same element type and shape to givenslice.Parameters
Sslicen-dimensional
sliceto refer shape and element typeReturn Value
new empty
sliceExamples
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 autoones(E = double, size_t N)(size_t[N]lengths...);Construct a new slice, filled with
ones, of element typeEand shape().lengths...Parameters
size_t[N]lengthselements of shape
Return Value
new
onessliceExamples
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 autoones_like(S)(Sslice);Construct new ones
slicehaving the same element type and shape to givenslice.Parameters
Sslicen-dimensional
sliceto refer shape and element typeReturn Value
new ones
sliceExamples
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 autozeros(E = double, size_t N)(size_t[N]lengths...);Construct a new slice, filled with zeroes, of element type
Eand shape().lengths...Parameters
size_t[N]lengthselements of shape
Return Value
new zeroes slice
Examples
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 autozeros_like(S)(Sslice);Construct new zeroes
slicehaving the same element type and shape to givenslice.Parameters
Sslicen-dimensional
sliceto refer shape and element typeReturn Value
new zeroes
sliceExamples
import mir.ndslice.topology : iota; // ------- // | 0 1 2 | // | 3 4 5 | // ------- auto e = iota(2, 3); assert(e.zeros_like == zeros(2, 3));
-
Declaration
pure autoeye(E = double, size_t dimension = 1)(size_tm, size_tn= 0, size_tk= 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
dimensionand.kdetermines the offset of the diagonal andkdimensioncontrols the dimension of that this offset proceeds in. For instance, ifdimension= 1 and= 1, then the diagonal after the first column is what is filled with ones.kParameters
dimensionaxis to apply
offsetksize_tmnumber of rows of output
size_tnnumber of columns of output (default = 0, sets
n=m)size_tkoffset for start of diagonal (default = 0)
Return Value
new
eyeslice -
Declaration
pure autoeye(size_t dimension)(size_tm, size_tn= 0, size_tk= 0);Returns a
doubleslice.eyeParameters
dimensionaxis to apply
offsetksize_tmnumber of rows of output
size_tnnumber of columns of output (default = 0, sets
n=m)size_tkoffset for start of diagonal (default = 0)
Return Value
new
eyesliceExamples
assert(eye(2) == [[1.0, 0.0], [0.0, 1.0]]);
Examples
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 autoidentity(E = double)(size_tn);Returns a square slice of element type
Ewith ones on the main diagonal and zeros elsewhere.Parameters
size_tnnumber of rows and columns
Return Value
new
identitysliceExamples
assert(identity(2) == [[1.0, 0.0], [0.0, 1.0]]);
-
Declaration
autoarange(size_tsize);Returns a 1-dimensional slice whose elements are equal to its indices.
Parameters
size_tsizelength
Return Value
1-dimensional slice composed of indices
-
Declaration
pure autoarange(E)(Estart, Eend, Estep= 1);Returns a 1-dimensional slice whose elements are equal to its indices.
Parameters
Estartvalue of the first index
Eendvalue of the last index
Estepvalue between indices (default = 1)
Return Value
1-dimensional slice composed of indices
Examples
assert(arange(3) == [0, 1, 2]); assert(arange(2, 3, 0.3) == [2.0, 2.3, 2.6, 2.9]);
-
Declaration
autolinspace(E = double)(Estart, Estop, size_tnum= 50);Returns a slice with evenly spaced numbers over an interval.
Parameters
Estartvalue of the first element
Estopvalue of the last element
size_tnumnumber of points in the interval (default = 50)
Return Value
slice with evenly spaced numbers over an interval
Examples
assert(linspace(1, 2, 3) == [1.0, 1.5, 2.0]);
-
Declaration
pure autosteppedIota(E)(size_tnum, Estep, Estart= 0);An alternate, 1-dimensional version of iota (and different API).
Parameters
size_tnumnumber of values to return
Estepvalue between indices
Estartvalue of the first index
Return Value
1-dimensional slice composed of indices
Examples
assert(steppedIota!double(4, 0.3, 2.0) == [2.0, 2.3, 2.6, 2.9]);
-
Declaration
autologspace(E = double)(Estart, Estop, size_tnum= 50, Ebase= 10);Returns a slice with numbers evenly spaced over a log scale.
Discussion
In linear space, the sequence starts at
(base^^startbaseto the power ofstart) and ends with.base^^stopParameters
Estartis the value of the first elementbase^^startEstopis the value of the last elementbase^^stopsize_tnumnumber of points in the interval (default = 50)
Ebasethe
baseof the log space (default = 10)Return Value
slice with numbers evenly spaced over a log scale
Examples
assert(logspace(1, 2, 3, 10) == [10. ^^ 1.0, 10. ^^ 1.5, 10. ^^ 2.0]);
-
Declaration
templatediag(size_t dimension = 1)Extract the diagonal of a 2-dimensional slice.
Parameters
dimensiondimension to apply the offset,
k(default = 1)S s2-dimensional slice
size_t koffset from the main diagonal,
k > 0for diagonals above the main diagonal, andk < 0for diagonals below the main diagonalReturn Value
the extracted diagonal slice
Examples
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
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 autodiag(S)(Ss) if (isVector!S);Create a diagonal 2-dimensional slice from a 1-dimensional slice.
Parameters
Ss1-dimensional slice
Return Value
diagonal 2-dimensional slice
Examples
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]);