tesseract++ 0.0.1
N-dimensional tensor library for embedded systems
Loading...
Searching...
No Matches
least_squares.h
Go to the documentation of this file.
1#ifndef FUSED_ALGORITHMS_LEAST_SQUARES_H
2#define FUSED_ALGORITHMS_LEAST_SQUARES_H
3
4#include "config.h"
6#include "matrix_traits.h"
11
42namespace matrix_algorithms
43{
44
46
69 template <typename T, my_size_t M, my_size_t N>
71 const FusedMatrix<T, M, N> &A,
72 const FusedVector<T, M> &b)
73 {
74 static_assert(is_floating_point_v<T>,
75 "least_squares requires a floating-point scalar type");
76 static_assert(M >= N, "least_squares requires M >= N (overdetermined system)");
77
78 // 1. QR decompose A
79 auto qr = qr_householder(A);
80
81 // 2. Compute c = Qᵀ·b
82 auto c = qr.apply_Qt(b);
83
84 // 3. Extract R₁ (top N×N of R) and c₁ (top N of c)
85 FusedMatrix<T, N, N> R1(T(0));
86
87 for (my_size_t i = 0; i < N; ++i)
88 {
89 for (my_size_t j = i; j < N; ++j)
90 {
91 R1(i, j) = qr.QR(i, j);
92 }
93 }
94
95 FusedVector<T, N> c1(T(0));
96
97 for (my_size_t i = 0; i < N; ++i)
98 {
99 c1(i) = c(i);
100 }
101
102 // 4. Back-substitute R₁·x = c₁
103 return back_substitute(R1, c1);
104 }
105
106} // namespace matrix_algorithms
107
108#endif // FUSED_ALGORITHMS_LEAST_SQUARES_H
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< 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
QRResult< T, M, N > qr_householder(const FusedMatrix< T, M, N > &A)
Compute the Householder QR decomposition of a rectangular matrix.
Definition qr.h:207
Expected< FusedVector< T, N >, MatrixStatus > least_squares(const FusedMatrix< T, M, N > &A, const FusedVector< T, M > &b)
Solve the least squares problem min ‖Ax - b‖² via QR.
Definition least_squares.h:70
MatrixStatus
Error codes for matrix decomposition and solver algorithms.
Definition matrix_traits.h:33
Householder QR decomposition for rectangular matrices.
Forward/back substitution for triangular systems.