tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
cholesky_solve.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_CHOLESKY_SOLVE_H
2#define FUSED_ALGORITHMS_CHOLESKY_SOLVE_H
3
4#include "config.h"
6#include "matrix_traits.h"
10
44namespace matrix_algorithms
45{
46
48
80 template <typename T, my_size_t N>
82 const FusedMatrix<T, N, N> &A,
83 const FusedVector<T, N> &b)
84 {
85 static_assert(is_floating_point_v<T>,
86 "cholesky_solve requires a floating-point scalar type");
87
88 // 1. Decompose A = LLᵀ
89 auto chol_result = cholesky(A);
90
91 if (!chol_result.has_value())
92 {
93 return Unexpected{chol_result.error()};
94 }
95
96 auto &L = chol_result.value();
97
98 // 2. Solve Ly = b (forward substitution)
99 // L comes from cholesky — guaranteed non-singular, but forward_substitute
100 // checks anyway; no overhead for the unrolled paths.
101 auto fwd_result = forward_substitute(L, b);
102
103 if (!fwd_result.has_value())
104 {
105 return Unexpected{fwd_result.error()};
106 }
107
108 auto &y = fwd_result.value();
109
110 // 3. Solve Lᵀx = y (back substitution on L transposed)
111 // Access L(k,i) instead of U(i,k) to avoid creating a transpose.
112 // L diagonal is guaranteed positive from cholesky — no singular check needed.
113 FusedVector<T, N> x(T(0));
114
115 for (my_size_t i = N; i-- > 0;)
116 {
117 T sum = y(i);
118
119 for (my_size_t k = i + 1; k < N; ++k)
120 {
121 sum -= L(k, i) * x(k); // L(k,i) == Lᵀ(i,k)
122 }
123
124 x(i) = sum / L(i, i);
125 }
126
127 return move(x);
128 }
129
130} // namespace matrix_algorithms
131
132#endif // FUSED_ALGORITHMS_CHOLESKY_SOLVE_H
Cholesky decomposition for symmetric positive-definite matrices.
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.
Runtime property descriptors and error codes for matrices.
Definition cholesky.h:52
Expected< MatrixType, MatrixStatus > cholesky(const MatrixType &A, typename MatrixType::value_type tol=typename MatrixType::value_type(PRECISION_TOLERANCE))
Compute the Cholesky decomposition of a symmetric positive-definite matrix.
Definition cholesky.h:87
Expected< FusedVector< T, N >, MatrixStatus > forward_substitute(const FusedMatrix< T, N, N > &L, const FusedVector< T, N > &b)
Solve the lower-triangular system Lx = b by forward substitution.
Definition triangular_solve.h:74
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
Expr::value_type sum(const BaseExpr< Expr > &expr)
Definition reductions.h:30
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
E error
The error value.
Definition expected.h:31
Forward/back substitution for triangular systems.