![Circle CI](https://camo.githubusercontent.com/4167034bb8166458be48127f59ce94910e67e10a47d9e2f76e95400e2c4999e0/68747470733a2f2f636972636c6563692e636f6d2f67682f6c69626d69722f6d69722d616c676f726974686d2e7376673f7374796c653d737667)
![Bountysource](https://camo.githubusercontent.com/25fda9a0a63cc24264f0bbdad2df9415d28c6cbb528074d109accdb0bbc581fc/68747470733a2f2f7777772e626f756e7479736f757263652e636f6d2f62616467652f7465616d3f7465616d5f69643d313435333939267374796c653d626f756e746965735f7265636569766564)
/+dub.sdl:
dependency "mir-algorithm" version="~>2.0.0"
+/
void main()
{
import mir.ndslice;
auto matrix = slice!double(3, 4);
matrix[] = 0;
matrix.diagonal[] = 1;
auto row = matrix[2];
row[3] = 6;
assert(matrix[2, 3] == 6); // D & C index order
import mir.stdio;
matrix.writeln;
// prints [[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 6.0]]
}
![Open on run.dlang.io](https://camo.githubusercontent.com/0323f0330bd86fa5aa546d2488dcf142c56c68e665d7be9178367f7c42bad86b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72756e2e646c616e672e696f2d6f70656e2d626c75652e737667)
/+dub.sdl:
dependency "mir-algorithm" version="~>2.0.0"
+/
void main()
{
import mir.ndslice;
import std.stdio : writefln;
enum fmt = "%(%(%.2f %)\n%)\n";
// Magic sqaure.
// `a` is lazy, each element is computed on-demand.
auto a = magic(5).as!float;
writefln(fmt, a);
// 5x5 grid on sqaure [1, 2] x [0, 1] with values x * x + y.
// `b` is lazy, each element is computed on-demand.
auto b = linspace!float([5, 5], [1f, 2f], [0f, 1f]).map!"a * a + b";
writefln(fmt, b);
// allocate a 5 x 5 contiguous matrix
auto c = slice!float(5, 5);
c[] = transposed(a + b / 2); // no memory allocations here
// 1. b / 2 - lazy element-wise operation with scalars
// 2. a + (...) - lazy element-wise operation with other slices
// Both slices must be `contiguous` or one-dimensional.
// 3. transposed(...) - trasposes matrix view. The result is `universal` (numpy-like) matrix.
// 5. c[] = (...) -- performs element-wise assignment.
writefln(fmt, c);
}
![Open on run.dlang.io](https://camo.githubusercontent.com/0323f0330bd86fa5aa546d2488dcf142c56c68e665d7be9178367f7c42bad86b/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f72756e2e646c616e672e696f2d6f70656e2d626c75652e737667)