Nexus Engine  v0.0.1
Loading...
Searching...
No Matches
Mat.inl
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2
3#pragma once
4
5namespace Nexus {
6
7 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
8 template <typename... Args>
9 requires(sizeof...(Args) == RowCount * ColCount) && (std::convertible_to<Args, T> && ...)
10 constexpr Mat<T, RowCount, ColCount, Layout>::Mat(Args&&... args) {
11 const std::array<T, RowCount * ColCount> values{static_cast<T>(std::forward<Args>(args))...};
12
13 for (size_type row = 0; row < RowCount; ++row) {
14 for (size_type col = 0; col < ColCount; ++col) {
15 (*this)[row, col] = values[row * ColCount + col];
16 }
17 }
18 }
19
20 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
21 template <Numeric U, typename OtherLayout>
23 for (size_type row = 0; row < RowCount; ++row) {
24 for (size_type col = 0; col < ColCount; ++col) {
25 (*this)[row, col] = static_cast<T>(other[row, col]);
26 }
27 }
28 }
29
30 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
32 return m_data[m_mapping(row, col)];
33 }
34
35 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
37 return m_data[m_mapping(row, col)];
38 }
39
40 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
42 return (*this)[row, col];
43 }
44
45 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
47 return (*this)[row, col];
48 }
49
50 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
52 return m_data.data();
53 }
54
55 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
56 constexpr const T* Mat<T, RowCount, ColCount, Layout>::Data() const {
57 return m_data.data();
58 }
59
60 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
62 return mdspan_type{m_data.data()};
63 }
64
65 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
67 return const_mdspan_type{m_data.data()};
68 }
69
70 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
72 return RowCount;
73 }
74
75 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
77 return ColCount;
78 }
79
80 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
82 return RowCount * ColCount;
83 }
84
85 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
86 template <Numeric U>
87 constexpr bool
89 for (size_type row = 0; row < RowCount; ++row) {
90 for (size_type col = 0; col < ColCount; ++col) {
91 if ((*this)[row, col] != other[row, col]) {
92 return false;
93 }
94 }
95 }
96
97 return true;
98 }
99
100 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
101 template <Numeric U, typename OtherLayout>
102 constexpr bool
104 for (size_type row = 0; row < RowCount; ++row) {
105 for (size_type col = 0; col < ColCount; ++col) {
106 if ((*this)[row, col] != other[row, col]) {
107 return false;
108 }
109 }
110 }
111
112 return true;
113 }
114
115 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
116 template <Numeric U, typename OtherLayout>
119 for (size_type row = 0; row < RowCount; ++row) {
120 for (size_type col = 0; col < ColCount; ++col) {
121 (*this)[row, col] += other[row, col];
122 }
123 }
124
125 return *this;
126 }
127
128 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
129 template <Numeric U, typename OtherLayout>
132 for (size_type row = 0; row < RowCount; ++row) {
133 for (size_type col = 0; col < ColCount; ++col) {
134 (*this)[row, col] -= other[row, col];
135 }
136 }
137
138 return *this;
139 }
140
141 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
142 template <Numeric U>
144 for (auto& value : m_data) {
145 value *= scalar;
146 }
147
148 return *this;
149 }
150
151 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
152 template <Numeric U>
154 for (auto& value : m_data) {
155 value /= scalar;
156 }
157
158 return *this;
159 }
160
161 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
165
166 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
168 Mat result;
169
170 for (size_type row = 0; row < RowCount; ++row) {
171 for (size_type col = 0; col < ColCount; ++col) {
172 result[row, col] = -(*this)[row, col];
173 }
174 }
175
176 return result;
177 }
178
179 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
182
183 for (size_type row = 0; row < RowCount; ++row) {
184 for (size_type col = 0; col < ColCount; ++col) {
185 result[col, row] = (*this)[row, col];
186 }
187 }
188
189 return result;
190 }
191
192 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
193 template <usize N>
194 requires(RowCount == ColCount && N == RowCount)
196 Mat result;
197
198 for (size_type i = 0; i < RowCount; ++i) {
199 result[i, i] = T{1};
200 }
201
202 return result;
203 }
204
205 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
206 template <usize N>
207 requires(RowCount == ColCount && N == RowCount)
209 static_assert(RowCount == ColCount);
210
212 T result = T{1};
213
214 for (size_type i = 0; i < RowCount; ++i) {
215 const T pivot = temp[i, i];
216
217 if (pivot == T{}) {
218 return T{};
219 }
220
221 result *= pivot;
222
223 for (size_type row = i + 1; row < RowCount; ++row) {
224 const T factor = temp[row, i] / pivot;
225
226 for (size_type col = i; col < ColCount; ++col) {
227 temp[row, col] -= factor * temp[i, col];
228 }
229 }
230 }
231
232 return result;
233 }
234
235 template <Numeric T, usize RowCount, usize ColCount, typename Layout>
236 template <usize N>
237 requires(RowCount == ColCount && N == RowCount)
239 static_assert(RowCount == ColCount);
240
242
243 for (size_type row = 0; row < RowCount; ++row) {
244 for (size_type col = 0; col < ColCount; ++col) {
245 augmented[row, col] = (*this)[row, col];
246 }
247
248 for (size_type col = 0; col < ColCount; ++col) {
249 augmented[row, ColCount + col] = row == col ? T{1} : T{};
250 }
251 }
252
253 for (size_type i = 0; i < RowCount; ++i) {
254 const T pivot = augmented[i, i];
255
256 if (pivot == T{}) {
257 return Mat{};
258 }
259
260 for (size_type col = 0; col < ColCount * 2; ++col) {
261 augmented[i, col] /= pivot;
262 }
263
264 for (size_type row = 0; row < RowCount; ++row) {
265 if (row == i) {
266 continue;
267 }
268
269 const T factor = augmented[row, i];
270
271 for (size_type col = 0; col < ColCount * 2; ++col) {
272 augmented[row, col] -= factor * augmented[i, col];
273 }
274 }
275 }
276
277 Mat result;
278
279 for (size_type row = 0; row < RowCount; ++row) {
280 for (size_type col = 0; col < ColCount; ++col) {
281 result[row, col] = augmented[row, ColCount + col];
282 }
283 }
284
285 return result;
286 }
287
288 template <Numeric T, Numeric U, usize RowCount, usize ColCount, typename Layout>
291 using R = std::common_type_t<T, U>;
292
294
295 for (usize row = 0; row < RowCount; ++row) {
296 for (usize col = 0; col < ColCount; ++col) {
297 result[row, col] = static_cast<R>(a[row, col]) + static_cast<R>(b[row, col]);
298 }
299 }
300
301 return result;
302 }
303
304 template <Numeric T, Numeric U, usize RowCount, usize ColCount, typename Layout>
307 using R = std::common_type_t<T, U>;
308
310
311 for (usize row = 0; row < RowCount; ++row) {
312 for (usize col = 0; col < ColCount; ++col) {
313 result[row, col] = static_cast<R>(a[row, col]) - static_cast<R>(b[row, col]);
314 }
315 }
316
317 return result;
318 }
319
320 template <Numeric T, Numeric U, usize RowCount, usize ColCount, typename Layout>
321 constexpr auto operator*(const Mat<T, RowCount, ColCount, Layout>& mat, U scalar)
323 using R = std::common_type_t<T, U>;
324
326
327 for (usize row = 0; row < RowCount; ++row) {
328 for (usize col = 0; col < ColCount; ++col) {
329 result[row, col] = static_cast<R>(mat[row, col]) * static_cast<R>(scalar);
330 }
331 }
332
333 return result;
334 }
335
336 template <Numeric T, Numeric U, usize RowCount, usize ColCount, typename Layout>
337 constexpr auto operator*(U scalar, const Mat<T, RowCount, ColCount, Layout>& mat)
339 return mat * scalar;
340 }
341
342 template <Numeric T, Numeric U, usize RowCount, usize ColCount, typename Layout>
343 constexpr auto operator/(const Mat<T, RowCount, ColCount, Layout>& mat, U scalar)
345 using R = std::common_type_t<T, U>;
346
348
349 for (usize row = 0; row < RowCount; ++row) {
350 for (usize col = 0; col < ColCount; ++col) {
351 result[row, col] = static_cast<R>(mat[row, col]) / static_cast<R>(scalar);
352 }
353 }
354
355 return result;
356 }
357
358 template <Numeric T, Numeric U, usize A, usize B, usize C, typename Layout>
359 constexpr auto operator*(const Mat<T, A, B, Layout>& lhs, const Mat<U, B, C, Layout>& rhs)
361 using R = std::common_type_t<T, U>;
362
364
365 for (usize row = 0; row < A; ++row) {
366 for (usize col = 0; col < C; ++col) {
367 R value{};
368
369 for (usize i = 0; i < B; ++i) {
370 value += static_cast<R>(lhs[row, i]) * static_cast<R>(rhs[i, col]);
371 }
372
373 result[row, col] = value;
374 }
375 }
376
377 return result;
378 }
379
380 template <Numeric T, usize RowCount, usize ColCount, typename FromLayout, typename ToLayout>
383
384 for (usize row = 0; row < RowCount; ++row) {
385 for (usize col = 0; col < ColCount; ++col) {
386 result[row, col] = mat[row, col];
387 }
388 }
389
390 return result;
391 }
392
393} // namespace Nexus
394
395template <Nexus::Numeric T, Nexus::usize RowCount, Nexus::usize ColCount, typename Layout>
396struct std::formatter<Nexus::Mat<T, RowCount, ColCount, Layout>> {
397 std::formatter<T> underlying;
398
399 constexpr auto parse(std::format_parse_context& ctx) {
400 return underlying.parse(ctx);
401 }
402
403 auto format(const Nexus::Mat<T, RowCount, ColCount, Layout>& obj, std::format_context& ctx) const {
404 auto out = ctx.out();
405
406 out = std::format_to(out, "[");
407
408 for (Nexus::usize row = 0; row < RowCount; ++row) {
409 if (row != 0) {
410 out = std::format_to(out, ", ");
411 }
412
413 out = std::format_to(out, "[");
414
415 for (Nexus::usize col = 0; col < ColCount; ++col) {
416 if (col != 0) {
417 out = std::format_to(out, ", ");
418 }
419
420 out = underlying.format(obj[row, col], ctx);
421 }
422
423 out = std::format_to(out, "]");
424 }
425
426 return std::format_to(out, "]");
427 }
428};
Definition Mat.cppm:20
constexpr Mat()=default
static constexpr size_type Size()
Definition Mat.inl:81
static constexpr size_type Cols()
Definition Mat.inl:76
constexpr bool operator==(const Mat< U, RowCount, ColCount, Layout > &other) const
Definition Mat.inl:88
std::mdspan< T, extents_type, Layout > mdspan_type
Definition Mat.cppm:28
constexpr Mat & operator/=(U scalar)
constexpr Mat operator+() const
Definition Mat.inl:162
static constexpr Mat Identity()
constexpr Mat operator-() const
Definition Mat.inl:167
constexpr T * Data()
Definition Mat.inl:51
constexpr T & operator[](size_type row, size_type col)
Definition Mat.inl:31
static constexpr size_type Rows()
Definition Mat.inl:71
constexpr Mat & operator+=(const Mat< U, RowCount, ColCount, OtherLayout > &other)
constexpr Mat Inverse() const
constexpr Mat & operator*=(U scalar)
constexpr Mat< T, ColCount, RowCount, Layout > Transpose() const
Definition Mat.inl:180
std::mdspan< const T, extents_type, Layout > const_mdspan_type
Definition Mat.cppm:29
constexpr auto Determinant() const
Definition Mat.inl:208
usize size_type
Definition Mat.cppm:23
constexpr T & operator()(size_type row, size_type col)
Definition Mat.inl:41
constexpr Mat & operator-=(const Mat< U, RowCount, ColCount, OtherLayout > &other)
constexpr auto MDSpan()
Definition Mat.inl:61
Definition Config.cppm:11
constexpr auto operator+(const Mat< T, RowCount, ColCount, Layout > &a, const Mat< U, RowCount, ColCount, Layout > &b) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:289
std::size_t usize
Definition Types.cppm:25
Mat< std::common_type_t< T, U >, RowCount, ColCount, Layout > MatCommon
Definition Mat.cppm:99
constexpr auto operator*(const Mat< T, RowCount, ColCount, Layout > &mat, U scalar) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:321
constexpr auto operator/(const Mat< T, RowCount, ColCount, Layout > &mat, U scalar) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:343
constexpr auto operator-(const Mat< T, RowCount, ColCount, Layout > &a, const Mat< U, RowCount, ColCount, Layout > &b) -> MatCommon< T, U, RowCount, ColCount, Layout >
Definition Mat.inl:305
constexpr Mat< T, RowCount, ColCount, ToLayout > MatCastLayout(const Mat< T, RowCount, ColCount, FromLayout > &mat)
Definition Mat.inl:381
std::formatter< T > underlying
Definition Mat.inl:397
auto format(const Nexus::Mat< T, RowCount, ColCount, Layout > &obj, std::format_context &ctx) const
Definition Mat.inl:403
constexpr auto parse(std::format_parse_context &ctx)
Definition Mat.inl:399