Mathter
A configurable 3D math library for game developers.
DecomposeQR.hpp
Go to the documentation of this file.
1 //==============================================================================
2 // This software is distributed under The Unlicense.
3 // For more information, please refer to <http://unlicense.org/>
4 //==============================================================================
5 
6 #pragma once
7 
8 #include "../Common/MathUtil.hpp"
9 #include "../Transforms/ZeroBuilder.hpp"
10 #include "../Transforms/IdentityBuilder.hpp"
11 
12 
13 namespace mathter {
14 
15 
18 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
21 
22 public:
25 
28 };
29 
30 
33 template <class T, int Rows, int Columns, eMatrixOrder Order, eMatrixLayout Layout, bool Packed>
35  static_assert(Rows >= Columns);
36 
39 
40  R = m;
41  Q = Identity();
42 
46 
47  for (int col = 0; col < m.ColumnCount(); ++col) {
48  u = R.Column(col);
49  for (int i = 0; i < col; ++i) {
50  u(i) = T(0);
51  }
52  T alpha = sign(R(col, col)) * LengthPrecise(u);
53  u(col) -= alpha;
54  T norm = LengthPrecise(u);
55  if (norm == 0) {
56  continue;
57  }
58  u /= norm;
59  v = u;
60  Qi = (T(-2) * v) * Transpose(v);
61  for (int i = 0; i < Q.ColumnCount(); ++i) {
62  Qi(i, i) += T(1);
63  }
64  R = Qi * R;
65  Q = Qi * Q;
66  }
67  Q = Transpose(Q);
68 
70 }
71 
72 
73 } // namespace mathter
auto Identity()
Creates an identity matrix or identity quaternion.
Definition: IdentityBuilder.hpp:42
auto DecomposeQR(Matrix< T, Rows, Columns, Order, Layout, Packed > m)
Calculates the QR decomposition of the matrix using Householder transforms.
Definition: DecomposeQR.hpp:34
Matrix< T, Rows, Rows, Order, Layout, Packed > Q
Definition: DecomposeQR.hpp:26
auto Column(int colIdx)
Return the submatrix corresponding to the specified column.
Definition: MatrixImpl.hpp:302
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
Definition: Approx.hpp:11
A utility class that can do common operations with the QR decomposition, i.e. solving equation system...
Definition: DecomposeQR.hpp:19
constexpr int ColumnCount() const
Returns the number of columns of the matrix.
Definition: MatrixImpl.hpp:27
DecompositionQR(Matrix< T, Rows, Rows, Order, Layout, Packed > Q, Matrix< T, Rows, Columns, Order, Layout, Packed > R)
Definition: DecomposeQR.hpp:23
Definition: Definitions.hpp:63
T LengthPrecise(const Vector< T, Dim, Packed > &v)
Returns the length of the vector, avoids overflow and underflow, so it&#39;s more expensive.
Definition: VectorFunction.hpp:41
Matrix< T, Rows, Columns, Order, Layout, Packed > R
Definition: DecomposeQR.hpp:27
T sign(T arg)
Definition: MathUtil.hpp:6