akreon-linalg
Fixed-size linear algebra and numerical methods in C++26.
Loading...
Searching...
No Matches
cholesky.hpp
Go to the documentation of this file.
1/*
2 * Copyright © 2025-2026 Akreon LLC
3 * SPDX-License-Identifier: Apache-2.0
4 */
5
6#pragma once
7
11
12namespace linalg {
13
19template <SquareMatrix Mat>
20struct Cholesky : SymmetricOperator<Mat, Cholesky<Mat>> {
26
29 using SymmetricBase::N;
30
31 static_assert(N > 1, "GG^T requires as least 2x2 matrix");
32
38 constexpr explicit Cholesky(const Mat& A) : SymmetricBase(A) { factor(A); }
39
46 constexpr explicit Cholesky(const Mat& A,
49 factor(A);
50 }
51
57 constexpr auto G() const { return make_lower_triangle<Mat>(G_vec); }
58
59private:
65 constexpr void factor(Mat A) {
66 auto chol = [&]<std::size_t j = 0>(this auto&& self) {
67 if constexpr (j >= SymmetricBase::N) {
68 return;
69 } else {
70 constexpr std::size_t next = j + 1;
71 // Subtract contributions of the previously computed columns of G.
72 if constexpr (j > 0) {
75 const auto A_j_0j = get_block<j, next, 0, j>(A);
76 const auto lhs = A_jn_j - A_jn_0j * transpose(A_j_0j);
78 }
80 // Take the positive pivot root, then scale the remaining column.
81 const auto a_jj = constexpr_sqrt_safe(A(j, j));
83 return self.template operator()<next>();
84 }
85 };
86
87 chol();
88
89 // Pack the completed lower factor, including its diagonal.
91
93 }
94
97 Vector<T, DIM> G_vec;
98};
99
100} // namespace linalg
constexpr auto get_diag(const Mat &A)
get the diagonal of a matrix A
constexpr auto transpose(const Vec &x)
tranpose of a vector
constexpr auto get_lower_triangle_with_diag(const Mat &A)
get lower triangle with diag
constexpr T constexpr_sqrt_safe(T x, T floor=T(0), T g=1e-10, T tol=1e-10)
Return a scalar square root with a sanitized fallback radicand.
constexpr std::size_t mat_rows_v
Registered compile-time row count after cv/ref normalization.
Configuration for generic numerical factorizations.
Factors a real positive-definite matrix as A = G G^T.
Definition cholesky.hpp:20
constexpr Cholesky(const Mat &A)
Factor A as G G^T using Cholesky column updates.
Definition cholesky.hpp:38
constexpr Cholesky(const Mat &A, const config::FactorConfig< T > &config_in)
Compute Cholesky factors with explicit settings.
Definition cholesky.hpp:46
constexpr auto G() const
Reconstruct the lower-triangular Cholesky factor.
Definition cholesky.hpp:57
std::complex< ScalarT > ComplexT
matrix_scalar_t< Mat > T
alias helpers for types
static constexpr std::size_t N
helper to extract N
real_type_t< T > ScalarT
forward declaration for symmetric operator
static constexpr std::size_t DIM
helpers to get DIM from mat
constexpr void compute_definiteness(const Vector< DiagT, N > &diag, const ScalarT &tol)
compute definiteness
constexpr const Cholesky< Mat > & self() const
self