tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
linear_solve.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_LINEAR_SOLVE_H
2#define FUSED_ALGORITHMS_LINEAR_SOLVE_H
3
4#include "config.h"
6#include "matrix_traits.h"
10
53namespace matrix_algorithms
54{
55
57
70 template <typename T, my_size_t N>
72 const FusedMatrix<T, N, N> &A,
73 const FusedVector<T, N> &b)
74 {
75 static_assert(is_floating_point_v<T>,
76 "lu_solve requires a floating-point scalar type");
77
78 // 1. Decompose P·A = L·U
79 auto lu_result = lu(A);
80
81 if (!lu_result.has_value())
82 {
83 return Unexpected{lu_result.error()};
84 }
85
86 auto &decomp = lu_result.value();
87
88 // 2. Permute b
89 FusedVector<T, N> b_perm(T(0));
90
91 for (my_size_t i = 0; i < N; ++i)
92 {
93 b_perm(i) = b(decomp.perm(i));
94 }
95
96 // 3. Extract L and U
97 auto L = decomp.L();
98 auto U = decomp.U();
99
100 // 4. Solve L·y = b_perm (forward substitution, unit diagonal)
101 auto fwd_result = forward_substitute<true>(L, b_perm);
102
103 if (!fwd_result.has_value())
104 {
105 return Unexpected{fwd_result.error()};
106 }
107
108 auto &y = fwd_result.value();
109
110 // 5. Solve U·x = y (back substitution)
111 return back_substitute(U, y);
112 }
113
141 template <typename T, my_size_t N>
143 const FusedMatrix<T, N, N> &A,
144 const FusedVector<T, N> &b)
145 {
146 static_assert(is_floating_point_v<T>,
147 "solve requires a floating-point scalar type");
148
149 // Try Cholesky path first (O(N³/3) — cheaper if SPD)
150 auto chol_result = cholesky_solve(A, b);
151
152 if (chol_result.has_value())
153 {
154 return chol_result;
155 }
156
157 // Fall back to LU path (O(2N³/3) — works for any non-singular A)
158 return lu_solve(A, b);
159 }
160
161} // namespace matrix_algorithms
162
163#endif // FUSED_ALGORITHMS_LINEAR_SOLVE_H
Solve Ax = b for symmetric positive-definite A via Cholesky decomposition.
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
A minimal, STL-free expected/result type for failable operations.
LU decomposition with partial pivoting for square matrices.
Runtime property descriptors and error codes for matrices.
Definition cholesky.h:52
Expected< FusedVector< T, N >, MatrixStatus > back_substitute(const FusedMatrix< T, N, N > &U, const FusedVector< T, N > &b)
Solve the upper-triangular system Ux = b by back substitution.
Definition triangular_solve.h:216
Expected< FusedVector< T, N >, MatrixStatus > solve(const FusedMatrix< T, N, N > &A, const FusedVector< T, N > &b)
Solve Ax = b with automatic algorithm selection.
Definition linear_solve.h:142
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
Expected< FusedVector< T, N >, MatrixStatus > lu_solve(const FusedMatrix< T, N, N > &A, const FusedVector< T, N > &b)
Solve Ax = b via LU decomposition with partial pivoting.
Definition linear_solve.h:71
Expected< FusedVector< T, N >, MatrixStatus > cholesky_solve(const FusedMatrix< T, N, N > &A, const FusedVector< T, N > &b)
Solve Ax = b for symmetric positive-definite A via Cholesky decomposition.
Definition cholesky_solve.h:81
MatrixStatus
Error codes for matrix decomposition and solver algorithms.
Definition matrix_traits.h:33
Tag type for constructing an Expected in the error state.
Definition expected.h:30
E error
The error value.
Definition expected.h:31
Forward/back substitution for triangular systems.