tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
cholesky.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_CHOLESKY_H
2#define FUSED_ALGORITHMS_CHOLESKY_H
3
4#include "config.h"
6#include "matrix_traits.h"
7#include "math/math_utils.h" // math::sqrt
8#include "simple_type_traits.h" // for move
9
52{
53
55
86 template <typename MatrixType>
88 const MatrixType &A,
89 typename MatrixType::value_type tol = typename MatrixType::value_type(PRECISION_TOLERANCE))
90 {
91 static_assert(is_floating_point_v<typename MatrixType::value_type>,
92 "Cholesky requires a floating-point scalar type");
93 if (!A.isSymmetric())
94 {
95 return Unexpected{MatrixStatus::NotSymmetric};
96 }
97
98 MatrixType L(0);
99
100 for (my_size_t i = 0; i < A.getDim(0); ++i)
101 {
102 for (my_size_t j = 0; j <= i; ++j)
103 {
104 typename MatrixType::value_type sum = 0;
105
106 for (my_size_t k = 0; k < j; ++k)
107 {
108 sum += L(i, k) * L(j, k);
109 }
110
111 if (i == j)
112 {
113 typename MatrixType::value_type diag = A(i, i) - sum;
114
115 if (diag <= tol)
116 {
117 return Unexpected{MatrixStatus::NotPositiveDefinite};
118 }
119
120 L(i, j) = math::sqrt(diag);
121 }
122 else
123 {
124 L(i, j) = (A(i, j) - sum) / L(j, j);
125 }
126 }
127 }
128
129 return move(L);
130 }
131
143 template <typename MatrixType>
144 MatrixType cholesky_or_die(const MatrixType &A)
145 {
146 auto result = cholesky(A);
147
148 if (!result.has_value())
149 {
150 MyErrorHandler::error("cholesky decomposition failed");
151 }
152
153 return move(result.value());
154 }
155
156} // namespace matrix_algorithms
157
158#endif // FUSED_ALGORITHMS_CHOLESKY_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
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 sqrt(T x) noexcept
Compute the square root of a floating-point value.
Definition math_utils.h:29
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
MatrixType cholesky_or_die(const MatrixType &A)
Cholesky decomposition — abort on failure.
Definition cholesky.h:144
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