tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
tridiagonal.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_TRIDIAGONAL_H
2#define FUSED_ALGORITHMS_TRIDIAGONAL_H
3
4#include "config.h"
6#include "matrix_traits.h"
8#include "math/math_utils.h"
9
60namespace matrix_algorithms
61{
62
64
88 template <typename T, my_size_t N>
90 const FusedVector<T, N> &a,
91 const FusedVector<T, N> &d,
92 const FusedVector<T, N> &c,
93 const FusedVector<T, N> &b)
94 {
95 static_assert(is_floating_point_v<T>,
96 "thomas_solve requires a floating-point scalar type");
97
98 // Work on copies (modified during forward sweep)
99 FusedVector<T, N> dp = d;
100 FusedVector<T, N> bp = b;
101
102 // Forward sweep
103 for (my_size_t i = 1; i < N; ++i)
104 {
105 T diag = dp(i - 1);
106
107 if (math::abs(diag) <= T(PRECISION_TOLERANCE))
108 {
109 return Unexpected{MatrixStatus::Singular};
110 }
111
112 T w = a(i) / diag;
113 dp(i) = dp(i) - w * c(i - 1);
114 bp(i) = bp(i) - w * bp(i - 1);
115 }
116
117 // Check last pivot
118 if (math::abs(dp(N - 1)) <= T(PRECISION_TOLERANCE))
119 {
120 return Unexpected{MatrixStatus::Singular};
121 }
122
123 // Back substitution
124 FusedVector<T, N> x(T(0));
125
126 x(N - 1) = bp(N - 1) / dp(N - 1);
127
128 for (my_size_t i = N - 1; i-- > 0;)
129 {
130 x(i) = (bp(i) - c(i) * x(i + 1)) / dp(i);
131 }
132
133 return move(x);
134 }
135
136} // namespace matrix_algorithms
137
138#endif // FUSED_ALGORITHMS_TRIDIAGONAL_H
A discriminated union holding either a success value or an error.
Definition expected.h:86
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< FusedVector< T, N >, MatrixStatus > thomas_solve(const FusedVector< T, N > &a, const FusedVector< T, N > &d, const FusedVector< T, N > &c, const FusedVector< T, N > &b)
Solve a tridiagonal system Ax = b using the Thomas algorithm.
Definition tridiagonal.h:89
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