tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
lu.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_LU_H
2#define FUSED_ALGORITHMS_LU_H
3
4#include "config.h"
6#include "matrix_traits.h"
9#include "math/math_utils.h"
10
47namespace matrix_algorithms
48{
49
51
52 // ========================================================================
53 // LU Result
54 // ========================================================================
55
66 template <typename T, my_size_t N>
67 struct LUResult
68 {
71 int sign;
72
79 {
80 FusedMatrix<T, N, N> result(T(0));
81
82 for (my_size_t i = 0; i < N; ++i)
83 {
84 result(i, i) = T(1); // unit diagonal
85
86 for (my_size_t j = 0; j < i; ++j)
87 {
88 result(i, j) = LU(i, j);
89 }
90 }
91
92 return result;
93 }
94
101 {
102 FusedMatrix<T, N, N> result(T(0));
103
104 for (my_size_t i = 0; i < N; ++i)
105 {
106 for (my_size_t j = i; j < N; ++j)
107 {
108 result(i, j) = LU(i, j);
109 }
110 }
111
112 return result;
113 }
114 };
115
116 // ========================================================================
117 // LU Decomposition
118 // ========================================================================
119
150 template <typename T, my_size_t N>
152 const FusedMatrix<T, N, N> &A,
153 T tol = T(PRECISION_TOLERANCE))
154 {
155 static_assert(is_floating_point_v<T>,
156 "lu requires a floating-point scalar type");
157
158 LUResult<T, N> result;
159 result.LU = A; // work on a copy
160 result.sign = 1;
161
162 // Initialize permutation to identity
163 for (my_size_t i = 0; i < N; ++i)
164 {
165 result.perm(i) = i;
166 }
167
168 for (my_size_t j = 0; j < N; ++j)
169 {
170 // 1. Find pivot: row with max |A(p,j)| for p >= j
171 my_size_t pivot = j;
172 T max_val = math::abs(result.LU(j, j));
173
174 for (my_size_t p = j + 1; p < N; ++p)
175 {
176 T val = math::abs(result.LU(p, j));
177
178 if (val > max_val)
179 {
180 max_val = val;
181 pivot = p;
182 }
183 }
184
185 // 2. Swap rows j and pivot
186 if (pivot != j)
187 {
188 // Swap entire rows in LU
189 for (my_size_t k = 0; k < N; ++k)
190 {
191 T tmp = result.LU(j, k);
192 result.LU(j, k) = result.LU(pivot, k);
193 result.LU(pivot, k) = tmp;
194 }
195
196 // Swap permutation entries
197 my_size_t tmp_perm = result.perm(j);
198 result.perm(j) = result.perm(pivot);
199 result.perm(pivot) = tmp_perm;
200
201 result.sign = -result.sign;
202 }
203
204 // 3. Check for singularity
205 T diag = result.LU(j, j);
206
207 if (math::abs(diag) <= tol)
208 {
209 return Unexpected{MatrixStatus::Singular};
210 }
211
212 // 4. Eliminate below pivot
213 for (my_size_t i = j + 1; i < N; ++i)
214 {
215 T factor = result.LU(i, j) / diag;
216 result.LU(i, j) = factor; // store L factor
217
218 for (my_size_t k = j + 1; k < N; ++k)
219 {
220 result.LU(i, k) -= factor * result.LU(j, k);
221 }
222 }
223 }
224
225 return move(result);
226 }
227
238 template <typename T, my_size_t N>
240 {
241 auto result = lu(A);
242
243 if (!result.has_value())
244 {
245 MyErrorHandler::error("LU decomposition failed");
246 }
247
248 return move(result.value());
249 }
250
251} // namespace matrix_algorithms
252
253#endif // FUSED_ALGORITHMS_LU_H
static void error(const T &msg)
Definition error_handler.h:30
A discriminated union holding either a success value or an error.
Definition expected.h:86
Definition fused_matrix.h:12
Definition fused_vector.h:9
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
A minimal, STL-free expected/result type for failable operations.
Runtime property descriptors and error codes for matrices.
constexpr T abs(T x) noexcept
Compute the absolute value of a numeric value.
Definition math_utils.h:48
Definition cholesky.h:52
Expected< LUResult< T, N >, MatrixStatus > lu(const FusedMatrix< T, N, N > &A, T tol=T(PRECISION_TOLERANCE))
Compute the LU decomposition of a square matrix with partial pivoting.
Definition lu.h:151
LUResult< T, N > lu_or_die(const FusedMatrix< T, N, N > &A)
LU decomposition — abort on failure.
Definition lu.h:239
MatrixStatus
Error codes for matrix decomposition and solver algorithms.
Definition matrix_traits.h:33
constexpr remove_reference_t< T > && move(T &&t) noexcept
Cast to rvalue reference (replacement for std::move).
Definition simple_type_traits.h:178
Tag type for constructing an Expected in the error state.
Definition expected.h:30
Result of LU decomposition with partial pivoting.
Definition lu.h:68
FusedMatrix< T, N, N > U() const
Extract the upper-triangular factor U.
Definition lu.h:100
FusedMatrix< T, N, N > LU
Compact L+U storage.
Definition lu.h:69
FusedVector< my_size_t, N > perm
Row permutation: perm(i) = original row index.
Definition lu.h:70
int sign
Permutation sign: +1 (even) or -1 (odd).
Definition lu.h:71
FusedMatrix< T, N, N > L() const
Extract the lower-triangular factor L with unit diagonal.
Definition lu.h:78