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
assert(empty(2, 3).shape == [2, 3]); assert(empty([2, 3]).shape == [2, 3]);
-
Declaration
pure auto
like
(alias initializer, S)(Sslice
);Construct new
slice
having the same element type and shape to givenslice
.Parameters
initializer
template function(ElementType)(shape) that initializes
slice
S
slice
n-dimensional
slice
to refer shape and element typeReturn Value
new initialized
slice
with 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 auto
empty_like
(S)(Sslice
);Construct new empty
slice
having the same element type and shape to givenslice
.Parameters
S
slice
n-dimensional
slice
to refer shape and element typeReturn Value
new empty
slice
Examples
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 typeE
and shape(
).lengths
...Parameters
size_t[N]
lengths
elements of shape
Return Value
new
ones
sliceExamples
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)(Sslice
);Construct new ones
slice
having the same element type and shape to givenslice
.Parameters
S
slice
n-dimensional
slice
to refer shape and element typeReturn Value
new ones
slice
Examples
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
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)(Sslice
);Construct new zeroes
slice
having the same element type and shape to givenslice
.Parameters
S
slice
n-dimensional
slice
to refer shape and element typeReturn Value
new zeroes
slice
Examples
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 auto
eye
(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
dimension
and
.k
determines the offset of the diagonal andk
dimension
controls 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.k
Parameters
dimension
axis to apply
offsetk
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 -
Declaration
pure auto
eye
(size_t dimension)(size_tm
, size_tn
= 0, size_tk
= 0);Returns a
double
slice.eye
Parameters
dimension
axis to apply
offsetk
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
sliceExamples
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 auto
identity
(E = double)(size_tn
);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
sliceExamples
assert(identity(2) == [[1.0, 0.0], [0.0, 1.0]]);
-
Declaration
auto
arange
(size_tsize
);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)(Estart
, Eend
, Estep
= 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
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)(Estart
, Estop
, size_tnum
= 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
assert(linspace(1, 2, 3) == [1.0, 1.5, 2.0]);
-
Declaration
pure auto
steppedIota
(E)(size_tnum
, Estep
, Estart
= 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
assert(steppedIota!double(4, 0.3, 2.0) == [2.0, 2.3, 2.6, 2.9]);
-
Declaration
auto
logspace
(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
^^start
base
to the power ofstart
) and ends with
.base
^^stop
Parameters
E
start
is the value of the first elementbase
^^start
E
stop
is the value of the last elementbase
^^stop
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
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, andk < 0
for 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 auto
diag
(S)(Ss
) 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
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]);