-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlinear_algebra.h
306 lines (253 loc) · 10.4 KB
/
linear_algebra.h
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#if !defined LINEAR_ALGEBRA_18_07_29_15_04_10
#define LINEAR_ALGEBRA_18_07_29_15_04_10
#include <initializer_list>
namespace std::experimental::la {
////////////////////////////////////////////////////////
// matrix
////////////////////////////////////////////////////////
template <class Rep>
struct matrix
{
using scalar_t = typename Rep::scalar_t;
using matrix_t = typename Rep::matrix_t;
// Constructors
constexpr matrix() = default;
constexpr explicit matrix(const matrix_t&) noexcept;
constexpr matrix(std::initializer_list<scalar_t>) noexcept;
constexpr matrix(std::pair<size_t, size_t>) noexcept;
// Accessors
constexpr matrix_t const& data() const noexcept;
constexpr matrix_t& data() noexcept;
constexpr scalar_t operator()(size_t, size_t) const; // If the parameters are too high, what do we do? Throw, UB or error code?
constexpr scalar_t& operator()(size_t, size_t); // If the parameters are too high, what do we do? Throw, UB or error code?
// Equality operators
constexpr bool operator==(matrix<Rep> const& rhs) const noexcept;
constexpr bool operator!=(matrix<Rep> const& rhs) const noexcept;
// Scalar binary operators
constexpr matrix<Rep>& operator*=(scalar_t const& rhs) noexcept;
constexpr matrix<Rep>& operator/=(scalar_t const& rhs) noexcept;
// Matrix binary operators
constexpr matrix<Rep>& operator+=(matrix<Rep> const& rhs) noexcept;
constexpr matrix<Rep>& operator-=(matrix<Rep> const& rhs) noexcept;
matrix_t _Data;
};
// Scalar binary operators
template<class Rep>
constexpr matrix<Rep> operator*(matrix<Rep> const& lhs, typename matrix<Rep>::scalar_t const& rhs) noexcept;
template<class Rep>
constexpr matrix<Rep> operator*(typename matrix<Rep>::scalar_t const& rhs, matrix<Rep> const& lhs) noexcept;
template<class Rep>
constexpr matrix<Rep> operator/(matrix<Rep> const& lhs, typename matrix<Rep>::scalar_t const& rhs) noexcept;
// Matrix binary operators
template<class Rep>
constexpr matrix<Rep> operator+(matrix<Rep> const& lhs, matrix<Rep> const& rhs) noexcept;
template<class Rep>
constexpr matrix<Rep> operator-(matrix<Rep> const& lhs, matrix<Rep> const& rhs) noexcept;
template<class Rep1, class Rep2>
constexpr auto operator*(matrix<Rep1> const& lhs, matrix<Rep2> const& rhs) noexcept;
// Matrix Functions
template<class Rep>
constexpr auto transpose(matrix<Rep> const&) noexcept;
template<class Rep>
constexpr auto submatrix(matrix<Rep> const&, size_t p, size_t q) noexcept;
// Vector Functions
template<class Rep>
constexpr typename Rep::scalar_t inner_product(matrix<Rep> const& lhs, matrix<Rep> const& rhs) noexcept;
template<class Rep>
constexpr typename Rep::scalar_t modulus(matrix<Rep> const&) noexcept;
template<class Rep>
constexpr typename Rep::scalar_t modulus_squared(matrix<Rep> const&) noexcept;
template<class Rep>
constexpr matrix<Rep> unit(matrix<Rep> const&) noexcept;
// SquareMatrix predicates
template<class Rep>
constexpr bool is_identity(matrix<Rep> const&) noexcept;
template<class Rep>
constexpr bool is_invertible(matrix<Rep> const&) noexcept;
// SquareMatrix functions
template<class Rep>
constexpr matrix<Rep> identity() noexcept;
template<class Rep>
constexpr typename Rep::scalar_t determinant(matrix<Rep> const&) noexcept;
template<class Rep>
constexpr matrix<typename Rep::transpose_t> classical_adjoint(matrix<Rep> const&) noexcept;
template<class Rep>
constexpr matrix<Rep> inverse(matrix<Rep> const&);
}
////////////////////////////////////////////////////////
// matrix implementation
////////////////////////////////////////////////////////
// Constructors
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>::matrix(const matrix_t& dat) noexcept
: _Data(dat)
{}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>::matrix(std::initializer_list<scalar_t> il) noexcept
: _Data(il)
{}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>::matrix(std::pair<size_t, size_t> size) noexcept
: _Data(size)
{}
// Accessors
template<class Rep>
inline constexpr typename std::experimental::la::matrix<Rep>::matrix_t const& std::experimental::la::matrix<Rep>::data() const noexcept
{
return _Data;
}
template<class Rep>
inline constexpr typename std::experimental::la::matrix<Rep>::matrix_t& std::experimental::la::matrix<Rep>::data() noexcept
{
return _Data;
}
template<class Rep>
inline constexpr typename std::experimental::la::matrix<Rep>::scalar_t std::experimental::la::matrix<Rep>::operator()(size_t i, size_t j) const
{
return _Data(i, j);
}
template<class Rep>
inline constexpr typename std::experimental::la::matrix<Rep>::scalar_t& std::experimental::la::matrix<Rep>::operator()(size_t i, size_t j)
{
return _Data(i, j);
}
// Equality operators
template<class Rep>
inline constexpr bool std::experimental::la::matrix<Rep>::operator==(matrix<Rep> const& rhs) const noexcept
{
return Rep::equal(data(), rhs.data());
}
template<class Rep>
inline constexpr bool std::experimental::la::matrix<Rep>::operator!=(matrix<Rep> const& rhs) const noexcept
{
return Rep::not_equal(data(), rhs.data());
}
// Scalar member binary operators
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>& std::experimental::la::matrix<Rep>::operator*=(typename matrix<Rep>::scalar_t const& rhs) noexcept
{
Rep::scalar_multiply(_Data, rhs);
return *this;
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>& std::experimental::la::matrix<Rep>::operator/=(typename matrix<Rep>::scalar_t const& rhs) noexcept
{
Rep::divide(_Data, rhs);
return *this;
}
// Matrix member binary operators
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>& std::experimental::la::matrix<Rep>::operator+=(matrix<Rep> const& rhs) noexcept
{
Rep::add(_Data, rhs.data());
return *this;
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep>& std::experimental::la::matrix<Rep>::operator-=(matrix<Rep> const& rhs) noexcept
{
Rep::subtract(_Data, rhs.data());
return *this;
}
// Scalar non-member binary operators
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::operator*(std::experimental::la::matrix<Rep> const& lhs, typename std::experimental::la::matrix<Rep>::scalar_t const& rhs) noexcept
{
auto res(lhs);
return res *= rhs;
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::operator*(typename std::experimental::la::matrix<Rep>::scalar_t const& lhs, std::experimental::la::matrix<Rep> const& rhs) noexcept
{
auto res(rhs);
Rep::scalar_multiply(res.data(), lhs);
return res;
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::operator/(std::experimental::la::matrix<Rep> const& lhs, typename std::experimental::la::matrix<Rep>::scalar_t const& rhs) noexcept
{
auto res(lhs);
return res /= rhs;
}
// Matrix non-member binary operators
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::operator+(std::experimental::la::matrix<Rep> const& lhs, std::experimental::la::matrix<Rep> const& rhs) noexcept
{
auto res(lhs);
return res += rhs;
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::operator-(std::experimental::la::matrix<Rep> const& lhs, std::experimental::la::matrix<Rep> const& rhs) noexcept
{
auto res(lhs);
return res -= rhs;
}
template<class Rep1, class Rep2>
inline constexpr auto std::experimental::la::operator*(std::experimental::la::matrix<Rep1> const& lhs, std::experimental::la::matrix<Rep2> const& rhs) noexcept
{
return matrix<typename Rep1::template multiply_t<Rep2>>(Rep1::template matrix_multiply<Rep2>(lhs.data(), rhs.data()));
}
// Matrix functions
template<class Rep>
inline constexpr auto std::experimental::la::transpose(matrix<Rep> const& mat) noexcept
{
return matrix<typename Rep::transpose_t>(Rep::transpose(mat.data()));
}
template<class Rep>
inline constexpr auto std::experimental::la::submatrix(matrix<Rep> const& mat, size_t p, size_t q) noexcept
{
return matrix<typename Rep::submatrix_t>(Rep::submatrix(mat.data(), p, q));
}
// Vector functions
template<class Rep>
inline constexpr typename Rep::scalar_t std::experimental::la::inner_product(matrix<Rep> const& lhs, matrix<Rep> const& rhs) noexcept
{
return Rep::inner_product(lhs.data(), rhs.data());
}
template<class Rep>
inline constexpr typename Rep::scalar_t std::experimental::la::modulus(matrix<Rep> const& vec) noexcept
{
return Rep::modulus(vec.data());
}
template<class Rep>
inline constexpr typename Rep::scalar_t std::experimental::la::modulus_squared(matrix<Rep> const& vec) noexcept
{
return Rep::modulus_squared(vec.data());
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::unit(matrix<Rep> const& vec) noexcept
{
return matrix<Rep>(Rep::unit(vec.data()));
}
// Square matrix predicates
template<class Rep>
inline constexpr bool std::experimental::la::is_identity(matrix<Rep> const& mat) noexcept
{
return Rep::is_identity(mat.data());
}
template<class Rep>
inline constexpr bool std::experimental::la::is_invertible(matrix<Rep> const& mat) noexcept
{
return Rep::is_invertible(mat.data());
}
// SquareMatrix functions
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::identity() noexcept
{
return matrix<Rep>(Rep::identity());
}
template<class Rep>
inline constexpr typename Rep::scalar_t std::experimental::la::determinant(matrix<Rep> const& mat) noexcept
{
return Rep::determinant(mat.data());
}
template<class Rep>
inline constexpr std::experimental::la::matrix<typename Rep::transpose_t> std::experimental::la::classical_adjoint(matrix<Rep> const& mat) noexcept
{
return matrix<typename Rep::transpose_t>(Rep::classical_adjoint(mat.data()));
}
template<class Rep>
inline constexpr std::experimental::la::matrix<Rep> std::experimental::la::inverse(matrix<Rep> const& mat)
{
return matrix<Rep>(Rep::inverse(mat.data()));
}
#endif