cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
lu.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_ALGORITHMS_LU_H
4#define pRC_ALGORITHMS_LU_H
5
8
9namespace pRC
10{
30 template<Operator::Hint H = Operator::Hint::None, class X,
31 IsTensorish R = RemoveReference<X>>
32 requires(R::Dimension == 2) && (R::size(0) == R::size(1))
33 static inline constexpr auto lu(X &&a)
34 {
35 constexpr auto N = R::size(0);
36
37 decltype(auto) u = copy<!(!IsReference<X> && !IsConst<R>)>(eval(a));
38
39 if constexpr(H != Operator::Hint::UpperTriangular)
40 {
41 if(u(0, 0) != pRC::zero())
42 {
43 for(Index j = 0; j < N; ++j)
44 {
45 for(Index i = 0; i <= j; ++i)
46 {
47 for(Index k = 0; k < i; ++k)
48 {
49 u(i, j) -= innerProduct(u(i, k), u(k, j));
50 }
51 }
52 for(Index i = j + 1; i < N; ++i)
53 {
54 for(Index k = 0; k < j; ++k)
55 {
56 u(i, j) -= innerProduct(u(i, k), u(k, j));
57 }
58 if(u(i, j) != pRC::zero())
59 {
60 u(i, j) /= u(j, j);
61 }
62 }
63 }
64 }
65 else
66 {
68 "LU decomposition failed: Input is has 0 at entry (0,0)");
69 }
70 }
71 else
72 {
73 if constexpr(cDebugLevel >= DebugLevel::High)
74 {
75 if(!isLowerTriangular(a))
76 {
78 "LU decomposition failed: Input is not lower "
79 "triangular part of matrix.");
80 }
81 }
82 }
83
84 return u;
85 }
86}
87#endif // pRC_ALGORITHMS_LU_H
Definition concepts.hpp:25
Definition concepts.hpp:19
int i
Definition gmock-matchers-comparisons_test.cc:603
static void error(Xs &&...args)
Definition log.hpp:14
Hint
Definition hint.hpp:9
Definition cholesky.hpp:10
static constexpr Conditional< C, RemoveConstReference< X >, RemoveConst< X > > copy(X &&a)
Definition copy.hpp:13
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 isLowerTriangular(X &&a, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_lower_triangular.hpp:14
static constexpr auto lu(X &&a)
LU decomposition of a square matrix.
Definition lu.hpp:33
constexpr auto cDebugLevel
Definition config.hpp:48
static constexpr auto zero()
Definition zero.hpp:12
static constexpr decltype(auto) eval(X &&a)
Definition eval.hpp:12