cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
cholesky.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_ALGORITHMS_CHOLESKY_H
4#define pRC_ALGORITHMS_CHOLESKY_H
5
8
9namespace pRC
10{
11 template<Operator::Hint H = Operator::Hint::None, class X,
13 requires(R::Dimension == 2) && (R::size(0) == R::size(1))
14 static inline constexpr auto cholesky(X &&a)
15 {
16 constexpr auto N = R::size(0);
17
18 decltype(auto) u = [&a]() -> decltype(auto)
19 {
20 if constexpr(H == Operator::Hint::UpperTriangular)
21 {
22 if constexpr(cDebugLevel >= DebugLevel::High)
23 {
24 if(!isUpperTriangular(a))
25 {
27 "Cholesky decomposition failed: Input is not upper "
28 "triangular part of self-adjoint matrix.");
29 }
30
31 return copy(eval(a));
32 }
33 else
34 {
35 return copy<!(!IsReference<X> && !IsConst<R>)>(eval(a));
36 }
37 }
38 else if constexpr(H == Operator::Hint::LowerTriangular)
39 {
40 if constexpr(cDebugLevel >= DebugLevel::High)
41 {
42 if(!isLowerTriangular(a))
43 {
45 "Cholesky decomposition failed: Input is not lower "
46 "triangular part of self-adjoint matrix.");
47 }
48 }
49
50 return eval(transpose(a));
51 }
52 else if constexpr(H == Operator::Hint::None)
53 {
54 if constexpr(cDebugLevel >= DebugLevel::High)
55 {
56 if(!isSelfAdjoint(a))
57 {
59 "Cholesky decomposition failed: Input is not "
60 "self-adjoint.");
61 }
62 }
63
64 return eval(upperTriangular(a));
65 }
66 else
67 {
68 static_assert(H != H,
69 "Unsupported cholesky decomposition hint.");
70 }
71 }();
72
74 [&u](auto const i)
75 {
76 auto x = real(u(i, i));
77 for(Index k = 0; k < i; ++k)
78 {
79 x -= norm<2, 1>(u(k, i));
80 }
81 x = sqrt(x);
82 u(i, i) = x;
83 for(Index j = i + 1; j < N; ++j)
84 {
85 for(Index k = 0; k < i; ++k)
86 {
87 u(i, j) -= innerProduct(u(k, i), u(k, j));
88 }
89 if(u(i, j) != zero())
90 {
91 u(i, j) /= x;
92 }
93 }
94 });
95
96 if constexpr(cDebugLevel >= DebugLevel::High)
97 {
98 if(!isUpperTriangular(u))
99 {
101 "Cholesky decomposition failed: U is not upper "
102 "triangular.");
103 }
104
105 if(!isSelfAdjoint(adjoint(u) * u))
106 {
108 "Cholesky decomposition failed: Product is not self "
109 "adjoint.");
110 }
111
113 {
114 Logging::error("Cholesky decomposition failed.");
115 }
116 }
117
118 if constexpr(IsReference<decltype(u)>)
119 {
120 return forward<X>(a);
121 }
122 else
123 {
124 return u;
125 }
126 }
127}
128#endif // pRC_ALGORITHMS_CHOLESKY_H
Definition concepts.hpp:25
Definition concepts.hpp:19
Definition declarations.hpp:45
int i
Definition gmock-matchers-comparisons_test.cc:603
int x
Definition gmock-matchers-containers_test.cc:376
static void error(Xs &&...args)
Definition log.hpp:14
Hint
Definition hint.hpp:9
Definition cholesky.hpp:10
static constexpr auto sqrt(T const &a)
Definition sqrt.hpp:11
static constexpr Conditional< C, RemoveConstReference< X >, RemoveConst< X > > copy(X &&a)
Definition copy.hpp:13
static constexpr auto upperTriangular(X &&a)
Definition upper_triangular.hpp:14
Size Index
Definition basics.hpp:32
std::remove_reference_t< T > RemoveReference
Definition basics.hpp:41
static constexpr auto innerProduct(TA const &a, TB const &b)
Definition inner_product.hpp:11
static constexpr auto cholesky(X &&a)
Definition cholesky.hpp:14
static constexpr auto transpose(JacobiRotation< T > const &a)
Definition jacobi_rotation.hpp:306
static constexpr auto adjoint(JacobiRotation< T > const &a)
Definition jacobi_rotation.hpp:312
static constexpr auto isLowerTriangular(X &&a, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_lower_triangular.hpp:14
static constexpr auto isApprox(XE &&expected, XA &&approx, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_approx.hpp:14
constexpr auto cDebugLevel
Definition config.hpp:48
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
static constexpr decltype(auto) real(X &&a)
Definition real.hpp:12
static constexpr auto isUpperTriangular(X &&a, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_upper_triangular.hpp:14
static constexpr auto zero()
Definition zero.hpp:12
static constexpr decltype(auto) eval(X &&a)
Definition eval.hpp:12
static constexpr auto norm(T const &a)
Definition norm.hpp:12
static constexpr auto isSelfAdjoint(X &&a, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_self_adjoint.hpp:14