Matrix

An N by M Matrix of T.

struct Matrix (
T
size_t N
size_t M = N
) {}

Constructors

this
this(const(T)[N * M] v)
Undocumented in source.
this
this(Matrix!(T2, N, M) m)
Undocumented in source.
this
this(const(T)[N2] v)
Undocumented in source.
this
this(Matrix!(T2, N2, 1) v)
Undocumented in source.

Members

Aliases

height
alias height = N
Undocumented in source.
width
alias width = M
Undocumented in source.

Functions

adjugate
Matrix adjugate()
Undocumented in source. Be warned that the author may not have intended to support it.
cofactor
T cofactor(size_t n, size_t m)
Undocumented in source. Be warned that the author may not have intended to support it.
cofactor_matrix
Matrix cofactor_matrix()
Undocumented in source. Be warned that the author may not have intended to support it.
column
Matrix!(T, N, 1) column(size_t k)
Undocumented in source. Be warned that the author may not have intended to support it.
determinant
T determinant()
Undocumented in source. Be warned that the author may not have intended to support it.
inverse
Matrix inverse()
Undocumented in source. Be warned that the author may not have intended to support it.
invert
void invert()
Undocumented in source. Be warned that the author may not have intended to support it.
length
auto length()
Undocumented in source. Be warned that the author may not have intended to support it.
normalize
void normalize()
Undocumented in source. Be warned that the author may not have intended to support it.
normalized
auto normalized()
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
Matrix opAssign(const(T)[N * M] v)
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
Matrix opAssign(Matrix!(T2, N, M) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
Matrix opAssign(const(T)[N2] v)
Undocumented in source. Be warned that the author may not have intended to support it.
opAssign
Matrix opAssign(Matrix!(T2, N2, 1) v)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
auto opBinary(Matrix!(T2, N, M) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
auto opBinary(Matrix!(T2, N2, 1) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
auto opBinary(T2 v)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
auto opBinary(Matrix!(T2, N2, 1) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinary
auto opBinary(Matrix!(T2, M, M2) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opBinaryRight
auto opBinaryRight(T2 v)
Undocumented in source. Be warned that the author may not have intended to support it.
opDollar
size_t opDollar()
Undocumented in source. Be warned that the author may not have intended to support it.
opIndex
auto ref opIndex(size_t i)
Undocumented in source. Be warned that the author may not have intended to support it.
opIndex
auto ref opIndex(size_t n, size_t m)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Matrix opOpAssign(Matrix!(T2, N, M) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Matrix opOpAssign(Matrix!(T2, N2, 1) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Matrix opOpAssign(T2 v)
Undocumented in source. Be warned that the author may not have intended to support it.
opOpAssign
Matrix opOpAssign(Matrix!(T2, N, N) m)
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
auto opSlice()
Undocumented in source. Be warned that the author may not have intended to support it.
opSlice
auto opSlice(size_t i, size_t j)
Undocumented in source. Be warned that the author may not have intended to support it.
opSliceAssign
auto opSliceAssign(T2 v)
Undocumented in source. Be warned that the author may not have intended to support it.
opSliceAssign
auto opSliceAssign(T2 v, size_t i, size_t j)
Undocumented in source. Be warned that the author may not have intended to support it.
opSliceOpAssign
auto opSliceOpAssign(T2 v)
Undocumented in source. Be warned that the author may not have intended to support it.
opSliceOpAssign
auto opSliceOpAssign(T2 v, size_t i, size_t j)
Undocumented in source. Be warned that the author may not have intended to support it.
opUnary
Matrix opUnary()
Undocumented in source. Be warned that the author may not have intended to support it.
row
Matrix!(T, 1, M) row(size_t k)
Undocumented in source. Be warned that the author may not have intended to support it.
toString
string toString()
Undocumented in source. Be warned that the author may not have intended to support it.
transpose
void transpose()
Undocumented in source. Be warned that the author may not have intended to support it.
transposed
auto transposed()
Undocumented in source. Be warned that the author may not have intended to support it.
without_column
Matrix!(T, N, M - 1) without_column(size_t k)
Undocumented in source. Be warned that the author may not have intended to support it.
without_row
Matrix!(T, N - 1, M) without_row(size_t k)
Undocumented in source. Be warned that the author may not have intended to support it.
without_row_column
Matrix!(T, N - 1, M - 1) without_row_column(size_t r, size_t c)
Undocumented in source. Be warned that the author may not have intended to support it.

Properties

identity
Matrix identity [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
ptr
auto ptr [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.
zero
Matrix zero [@property getter]
Undocumented in source. Be warned that the author may not have intended to support it.

Examples

1 // Matrix!(T=ElementType, N=Height, M=Width) can be initialized
2 // with an array of N*M elements, or by passing N*M values to the constructor.
3 auto m = Matrix!(float, 2, 3)(
4 	7, 3, 2,
5 	9, 1, 5,
6 );
7 
8 // Elements can be accessed with [i] (i=0..N*M) and [row, column] (row=0..N, column=0..M)
9 assert(m[4] == 1);
10 assert(m[1, 1] == 1);
11 assert(m[0, 2] == 2);
12 
13 // A matrix is default-initialied to just N*M default-initialized Ts.
14 {
15 	Matrix!(float, 3, 3) a;
16 	Matrix!(int, 2, 3) b;
17 	assert(isNaN(a[2]));
18 	assert(b[2] == 0);
19 }
20 
21 // .zero gives a zero-filled matrix.
22 {
23 	auto a = Matrix!(float, 3, 3).zero;
24 	assert(a[3] == 0);
25 }
26 
27 // For square matrices, .identity gives the identity matrix.
28 {
29 	auto a = Matrix!(float, 2, 2).identity;
30 	// The identity is also accessible as a.identity.
31 	assert(a[0, 0] == 1);
32 	assert(a[0, 1] == 0);
33 }
34 
35 // .width and .height are aliases for N and M.
36 assert(Matrix!(int, 13, 37).height == 13);
37 assert(m.width == 3);
38 
39 // [] gives a T[] slice of all N*M elements.
40 assert(m[] == [7, 3, 2, 9, 1, 5]);
41 m[] = 0;
42 assert(m == m.zero);
43 
44 // .transposed gives the transposed M*N matrix.
45 assert(m.transposed.width == m.height);
46 assert(m.transposed.height == m.width);
47 m[0, 2] = 4;
48 assert(m.transposed[2, 0] == 4);
49 
50 // For square matrices, .transpose() transposes the matrix in place.
51 {
52 	auto a = Matrix!(float, 2, 2)(1, 2, 3, 4);
53 	a.transpose();
54 	assert(a[] == [1, 3, 2, 4]);
55 }
56 
57 // Vectors are just matrices with a width of 1. Vector!(T, N) is just an alias.
58 assert(is(Vector!(float, 3) == Matrix!(float, 3, 1)));
59 
60 // For vectors, [i..j] can be used to get/set/modify a part of the vector.
61 auto a = Vector!(float, 4).zero;
62 a[0..2] = 1;
63 a[1..3] += 2;
64 assert(a[0..3] == [1, 3, 2]);
65 
66 // Vectors of different sizes can be used together, they're padded with zeros.
67 a += Vector!(float, 2)(1, 2);
68 assert(a[] == [2, 5, 2, 0]);
69 assert((a - Vector!(float, 5)(0, 0, 0, 0, 1))[] == [2, 5, 2, 0, -1]);
70 
71 // .column(i) and .row(i) give you a specific row or column as N*1 or 1*M matrix, respectively.
72 assert(m.row(0)[] == [0, 0, 4]);
73 assert(m.column(2)[] == [4, 0]);
74 assert(m.row(0).height == 1);
75 assert(m.column(0).width == 1);
76 
77 // .without_row(i), .without_column(i), and .without_row_column(r, c) do as they say.
78 assert(m.without_column(1) == Matrix!(float, 2, 2)(0, 4, 0, 0));
79 assert(m.without_row(1) == Matrix!(float, 1, 3)(0, 0, 4));
80 assert(m.without_row_column(1, 1) == Matrix!(float, 1, 2)(0, 4));
81 
82 // For (column) vectors, .length gives the Euclidian length,
83 // .normalized and .normalize do what you want.
84 Vector!(float, 2) v = [3, 4];
85 assert(v.length == 5);
86 assert(v.normalized == Vector!(float, 2)(0.6, 0.8));
87 v.normalize();
88 assert(v.length == 1);
89 
90 // For square matrices, there is .determinant, .cofactor(row, column),
91 // .cofactor_matrix, .adjugate, .inverse and .invert.
92 Matrix!(float, 3, 3) x = [
93 	1, 2, 3,
94 	0, 6, 1,
95 	0, 5, 0,
96 ];
97 assert(x.determinant == -5);
98 assert(x.cofactor(1, 0) == -x.without_row_column(1, 0).determinant);
99 assert(x.cofactor_matrix[1, 0] == x.cofactor(1, 0));
100 assert(x.adjugate == x.cofactor_matrix.transposed);
101 assert(x.inverse == x.adjugate / x.determinant);
102 x.invert();
103 assert(x.column(1)[] == [-3, 0, 1]);
104 
105 // You can add and subtract same-sized matrices with +=, -=, + and -,
106 // and scale them with *=, /=, *, and /.
107 auto y = x.without_row(2) + m;
108 y -= -m * 2;
109 y /= 0.5;
110 assert(y[1] == -6);
111 
112 // Matrix multiplication is done with * and *=.
113 assert(x * x.inverse == x.identity);
114 x *= x.inverse;
115 assert(x == x.identity);
116 
117 // For vectors, * gives you the dot product.
118 assert(v * v == 1);
119 
120 // There are aliases available for the most common matrices and vectors.
121 // (1..4 in size, for types int, float, double and real.)
122 assert(is(Matrix2x3f == Matrix!(float, 2, 3)));
123 assert(is(Matrix3d == Matrix!(double, 3, 3)));
124 assert(is(Vector2i == Vector!(int, 2)));
125 assert(is(HVector4r == HVector!(real, 4)));

Meta