tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
qr_givens.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_QR_GIVENS_H
2#define FUSED_ALGORITHMS_QR_GIVENS_H
3
4#include "config.h"
7#include "math/math_utils.h"
8
50namespace matrix_algorithms
51{
52
63 template <typename T, my_size_t M, my_size_t N>
69
88 template <typename T, my_size_t M, my_size_t N>
90 {
91 static_assert(is_floating_point_v<T>,
92 "qr_givens requires a floating-point scalar type");
93 static_assert(M >= N, "qr_givens requires M >= N");
94
96 result.R = A;
97 result.Q = FusedMatrix<T, M, M>(T(0));
98 result.Q.setIdentity();
99
100 for (my_size_t j = 0; j < N; ++j)
101 {
102 // Zero elements below diagonal, bottom to top
103 for (my_size_t i = M - 1; i > j; --i)
104 {
105 T a = result.R(i - 1, j);
106 T b = result.R(i, j);
107
108 // Skip if already zero
109 if (math::abs(b) <= T(PRECISION_TOLERANCE))
110 continue;
111
112 // Compute Givens rotation
113 T r = math::sqrt(a * a + b * b);
114 T c = a / r;
115 T s = b / r;
116
117 // Apply rotation to rows i-1 and i of R (columns j..N-1)
118 for (my_size_t k = j; k < N; ++k)
119 {
120 T r1 = result.R(i - 1, k);
121 T r2 = result.R(i, k);
122 result.R(i - 1, k) = c * r1 + s * r2;
123 result.R(i, k) = -s * r1 + c * r2;
124 }
125
126 // Accumulate rotation into Q (columns i-1 and i)
127 for (my_size_t k = 0; k < M; ++k)
128 {
129 T q1 = result.Q(k, i - 1);
130 T q2 = result.Q(k, i);
131 result.Q(k, i - 1) = c * q1 + s * q2;
132 result.Q(k, i) = -s * q1 + c * q2;
133 }
134 }
135 }
136
137 return result;
138 }
139
140} // namespace matrix_algorithms
141
142#endif // FUSED_ALGORITHMS_QR_GIVENS_H
Definition fused_matrix.h:12
FusedMatrix & setIdentity(void)
Definition fused_matrix.h:234
Global configuration for the tesseract tensor library.
#define my_size_t
Size/index type used throughout the library.
Definition config.h:126
#define PRECISION_TOLERANCE
Tolerance for floating-point comparisons (e.g. symmetry checks, Cholesky).
Definition config.h:117
constexpr T abs(T x) noexcept
Compute the absolute value of a numeric value.
Definition math_utils.h:48
constexpr T sqrt(T x) noexcept
Compute the square root of a floating-point value.
Definition math_utils.h:29
Definition cholesky.h:52
GivensQRResult< T, M, N > qr_givens(const FusedMatrix< T, M, N > &A)
Compute the QR decomposition using Givens rotations.
Definition qr_givens.h:89
Result of Givens QR decomposition.
Definition qr_givens.h:65
FusedMatrix< T, M, M > Q
Orthogonal factor (M×M).
Definition qr_givens.h:66
FusedMatrix< T, M, N > R
Upper-triangular factor (M×N).
Definition qr_givens.h:67