-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmatrix.h
More file actions
166 lines (146 loc) · 4.94 KB
/
Copy pathmatrix.h
File metadata and controls
166 lines (146 loc) · 4.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#pragma once
#include <array>
#include <cstddef>
#include <ostream>
#include <type_traits>
#include <utility>
// Fixed-size M x N matrix (row-major).
//
// Dimensions are part of the type, so dimension mismatches in addition and
// multiplication are caught at compile time. Storage is a flat std::array for
// cache-friendly, contiguous layout.
template <class T, std::size_t M, std::size_t N>
class matrix
{
static_assert(std::is_arithmetic<T>::value,
"matrix<T, M, N> requires an arithmetic element type T");
static_assert(M > 0 && N > 0, "matrix dimensions must be positive");
std::array<T, M * N> data_{}; // value-initialised to zero
public:
using value_type = T;
matrix() = default;
static constexpr std::size_t rows() noexcept { return M; }
static constexpr std::size_t cols() noexcept { return N; }
static constexpr bool isSquare() noexcept { return M == N; }
// ----- element access (row, col) ------------------------------------
T& operator()(std::size_t row, std::size_t col)
{
return data_[row * N + col];
}
const T& operator()(std::size_t row, std::size_t col) const
{
return data_[row * N + col];
}
// ----- factories ----------------------------------------------------
// Identity matrix (square types only).
static matrix identity()
{
static_assert(M == N, "identity() is only defined for square matrices");
matrix result;
for (std::size_t i = 0; i < M; ++i)
result(i, i) = T{1};
return result;
}
// ----- matrix arithmetic --------------------------------------------
matrix operator+(const matrix& other) const
{
matrix result;
for (std::size_t i = 0; i < M * N; ++i)
result.data_[i] = data_[i] + other.data_[i];
return result;
}
matrix operator-(const matrix& other) const
{
matrix result;
for (std::size_t i = 0; i < M * N; ++i)
result.data_[i] = data_[i] - other.data_[i];
return result;
}
matrix operator*(const T& scalar) const
{
matrix result;
for (std::size_t i = 0; i < M * N; ++i)
result.data_[i] = data_[i] * scalar;
return result;
}
// Matrix product: (M x N) * (N x P) -> (M x P).
template <std::size_t P>
matrix<T, M, P> operator*(const matrix<T, N, P>& other) const
{
matrix<T, M, P> result;
for (std::size_t i = 0; i < M; ++i)
for (std::size_t k = 0; k < N; ++k)
{
const T lhs = (*this)(i, k);
for (std::size_t j = 0; j < P; ++j)
result(i, j) += lhs * other(k, j);
}
return result;
}
// Transpose: (M x N) -> (N x M).
matrix<T, N, M> transpose() const
{
matrix<T, N, M> result;
for (std::size_t i = 0; i < M; ++i)
for (std::size_t j = 0; j < N; ++j)
result(j, i) = (*this)(i, j);
return result;
}
// Determinant (square types only), via Laplace cofactor expansion.
T det() const
{
static_assert(M == N, "det() is only defined for square matrices");
return determinant(*this, M);
}
// ----- comparison ---------------------------------------------------
bool operator==(const matrix& other) const { return data_ == other.data_; }
bool operator!=(const matrix& other) const { return !(*this == other); }
// ----- streaming ----------------------------------------------------
friend std::ostream& operator<<(std::ostream& os, const matrix& m)
{
for (std::size_t i = 0; i < M; ++i)
{
for (std::size_t j = 0; j < N; ++j)
os << m(i, j) << (j + 1 < N ? ' ' : '\n');
}
return os;
}
private:
// Determinant of the leading n x n block of a square matrix.
// Recurses on a dynamically sized minor so it works for any n <= M.
static T determinant(const matrix& m, std::size_t n)
{
if (n == 1)
return m(0, 0);
if (n == 2)
return m(0, 0) * m(1, 1) - m(0, 1) * m(1, 0);
T result = T{};
int sign = 1;
for (std::size_t col = 0; col < n; ++col)
{
matrix minor;
std::size_t mi = 0;
for (std::size_t r = 1; r < n; ++r)
{
std::size_t mj = 0;
for (std::size_t c = 0; c < n; ++c)
{
if (c == col)
continue;
minor(mi, mj) = m(r, c);
++mj;
}
++mi;
}
result += static_cast<T>(sign) * m(0, col) * determinant(minor, n - 1);
sign = -sign;
}
return result;
}
};
// Scalar-on-the-left multiplication.
template <class T, std::size_t M, std::size_t N>
matrix<T, M, N> operator*(const T& scalar, const matrix<T, M, N>& m)
{
return m * scalar;
}