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))...};
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];
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]);
30 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
32 return m_data[m_mapping(row, col)];
35 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
37 return m_data[m_mapping(row, col)];
40 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
42 return (*
this)[row, col];
45 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
47 return (*
this)[row, col];
50 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
55 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
60 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
65 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
70 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
75 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
80 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
82 return RowCount * ColCount;
85 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
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]) {
100 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
101 template <Numeric U,
typename OtherLayout>
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]) {
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];
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];
141 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
144 for (
auto& value : m_data) {
151 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
154 for (
auto& value : m_data) {
161 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
166 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
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];
179 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
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];
192 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
194 requires(RowCount == ColCount && N == RowCount)
198 for (
size_type i = 0; i < RowCount; ++i) {
205 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
207 requires(RowCount == ColCount && N == RowCount)
209 static_assert(RowCount == ColCount);
214 for (
size_type i = 0; i < RowCount; ++i) {
215 const T pivot = temp[i, i];
223 for (
size_type row = i + 1; row < RowCount; ++row) {
224 const T factor = temp[row, i] / pivot;
226 for (
size_type col = i; col < ColCount; ++col) {
227 temp[row, col] -= factor * temp[i, col];
235 template <Numeric T, usize RowCount, usize ColCount,
typename Layout>
237 requires(RowCount == ColCount && N == RowCount)
239 static_assert(RowCount == ColCount);
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];
248 for (
size_type col = 0; col < ColCount; ++col) {
249 augmented[row, ColCount + col] = row == col ? T{1} : T{};
253 for (
size_type i = 0; i < RowCount; ++i) {
254 const T pivot = augmented[i, i];
260 for (
size_type col = 0; col < ColCount * 2; ++col) {
261 augmented[i, col] /= pivot;
264 for (
size_type row = 0; row < RowCount; ++row) {
269 const T factor = augmented[row, i];
271 for (
size_type col = 0; col < ColCount * 2; ++col) {
272 augmented[row, col] -= factor * augmented[i, col];
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];
288 template <Numeric T, Numeric U, usize RowCount, usize ColCount,
typename Layout>
291 using R = std::common_type_t<T, U>;
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]);
304 template <Numeric T, Numeric U, usize RowCount, usize ColCount,
typename Layout>
307 using R = std::common_type_t<T, U>;
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]);
320 template <Numeric T, Numeric U, usize RowCount, usize ColCount,
typename Layout>
323 using R = std::common_type_t<T, U>;
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);
336 template <Numeric T, Numeric U, usize RowCount, usize ColCount,
typename Layout>
342 template <Numeric T, Numeric U, usize RowCount, usize ColCount,
typename Layout>
345 using R = std::common_type_t<T, U>;
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);
358 template <Numeric T, Numeric U, usize A, usize B, usize C,
typename Layout>
361 using R = std::common_type_t<T, U>;
365 for (
usize row = 0; row < A; ++row) {
366 for (
usize col = 0; col < C; ++col) {
369 for (
usize i = 0; i < B; ++i) {
370 value +=
static_cast<R
>(lhs[row, i]) *
static_cast<R
>(rhs[i, col]);
373 result[row, col] = value;
380 template <Numeric T, usize RowCount, usize ColCount,
typename FromLayout,
typename ToLayout>
384 for (
usize row = 0; row < RowCount; ++row) {
385 for (
usize col = 0; col < ColCount; ++col) {
386 result[row, col] = mat[row, col];
395template <Nexus::Numeric T, Nexus::usize RowCount, Nexus::usize ColCount,
typename Layout>
396struct std::formatter<
Nexus::Mat<T, RowCount, ColCount, Layout>> {
399 constexpr auto parse(std::format_parse_context& ctx) {
404 auto out = ctx.out();
406 out = std::format_to(out,
"[");
410 out = std::format_to(out,
", ");
413 out = std::format_to(out,
"[");
417 out = std::format_to(out,
", ");
423 out = std::format_to(out,
"]");
426 return std::format_to(out,
"]");
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