Mathter
A configurable 3D math library for game developers.
MatrixFunction.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "MatrixImpl.hpp"
4 
5 
6 namespace mathter {
7 
8 
10 template <class T, int Dim, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
12  T sum = m(0, 0);
13  for (int i = 1; i < Dim; ++i) {
14  sum += m(i, i);
15  }
16  return sum;
17 }
18 
20 template <class T, int Dim, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
22  // only works if L's diagonal is 1s
23  int parity;
24  auto [L, U, P] = DecomposeLUP(m, parity);
25  T prod = U(0, 0);
26  for (int i = 1; i < U.RowCount(); ++i) {
27  prod *= U(i, i);
28  }
29  return parity * prod;
30 }
31 
33 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
36  for (int i = 0; i < m.RowCount(); ++i) {
37  for (int j = 0; j < m.ColumnCount(); ++j) {
38  result(j, i) = m(i, j);
39  }
40  }
41  return result;
42 }
43 
45 template <class T, int Dim, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
48 
49  auto LUP = DecomposeLUP(m);
50 
53  for (int col = 0; col < Dim; ++col) {
54  b(std::max(0, col - 1)) = 0;
55  b(col) = 1;
56  x = LUP.Solve(b);
57  for (int i = 0; i < Dim; ++i) {
58  ret(i, col) = x(i);
59  }
60  }
61 
62  return ret;
63 }
64 
66 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
68  T sum = T(0);
69  for (auto& stripe : m.stripes) {
70  sum += LengthSquared(stripe);
71  }
72  sum /= (m.RowCount() * m.ColumnCount());
73  return sum;
74 }
75 
77 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
79  return sqrt(NormSquared(m));
80 }
81 
82 
83 
84 } // namespace mathter
Matrix< T, Dim, Dim, Order, Layout, Packed > Inverse(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m)
Returns the inverse of the matrix.
Definition: MatrixFunction.hpp:46
Matrix< T, Columns, Rows, Order, Layout, Packed > Transpose(const Matrix< T, Rows, Columns, Order, Layout, Packed > &m)
Transposes the matrix in-place.
Definition: MatrixFunction.hpp:34
Represents a vector in N-dimensional space.
Definition: Definitions.hpp:57
T NormSquared(const Matrix< T, Rows, Columns, Order, Layout, Packed > &m)
Calculates the square of the Frobenius norm of the matrix.
Definition: MatrixFunction.hpp:67
std::array< Vector< T, StripeDim, Packed >, StripeCount > stripes
Definition: MatrixImpl.hpp:46
Definition: Approx.hpp:11
T Trace(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m)
Returns the trace (sum of diagonal elements) of the matrix.
Definition: MatrixFunction.hpp:11
auto DecomposeLUP(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m, int &parity)
Implements LU decomposition with partial pivoting.
Definition: DecomposeLU.hpp:151
constexpr int ColumnCount() const
Returns the number of columns of the matrix.
Definition: MatrixImpl.hpp:27
T Norm(const Matrix< T, Rows, Columns, Order, Layout, Packed > &m)
Calculates the Frobenius norm of the matrix.
Definition: MatrixFunction.hpp:78
T LengthSquared(const Quaternion< T, Packed > &q)
Returns the square of the absolute value.
Definition: QuaternionFunction.hpp:53
T Determinant(const Matrix< T, Dim, Dim, Order, Layout, Packed > &m)
Returns the determinant of the matrix.
Definition: MatrixFunction.hpp:21
constexpr int RowCount() const
Returns the number of rows of the matrix.
Definition: MatrixImpl.hpp:31