cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
backward_substitution.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_ALGORITHMS_SOLVER_BACKWARD_SUBSTITUTION_H
4#define pRC_ALGORITHMS_SOLVER_BACKWARD_SUBSTITUTION_H
5
6#include <prc/config.hpp>
17
18namespace pRC::Solver
19{
21 {
22 public:
26 class RA = RemoveReference<XA>, class TA = typename RA::Type,
27 class VA = typename TA::Value, class XB,
28 class RB = RemoveReference<XB>, class TB = typename RB::Type,
29 class VB = typename TB::Value,
32 If<IsSatisfied<(typename RA::Dimension() == 2)>> = 0,
33 If<IsSatisfied<(typename RB::Dimension() == 1 ||
34 typename RB::Dimension() == 2)>> = 0,
35 If<IsSatisfied<(RA::size(0) == RA::size(1))>> = 0,
36 If<IsSatisfied<(RA::size(0) == RB::size(0))>> = 0>
37 inline constexpr auto operator()(XA &&A, XB &&b) const
38 {
39 auto const U = transform<OT>(restrict<OR>(forward<XA>(A)));
40
41 if constexpr(cDebugLevel >= DebugLevel::High)
42 {
44 {
45 Logging::error("U is not upper triangular");
46 }
47 }
48
49 decltype(auto) x = copy<!(!IsReference<XB>() && !IsConst<RB>() &&
51
53 [&U, &x](auto const p, auto const... indices)
54 {
55 Index const k = RB::size(0) - 1 - p;
56 if(x(k, indices...) != zero())
57 {
58 x(k, indices...) /= U(k, k);
59 for(Index i = 0; i < k; ++i)
60 {
61 x(i, indices...) -= x(k, indices...) * U(i, k);
62 }
63 }
64 });
65
66 if constexpr(cDebugLevel >= DebugLevel::High)
67 {
68 if(!isApprox(U * x, b))
69 {
70 Logging::error("Backward substitution failed.");
71 }
72 }
73
74 if constexpr(IsReference<decltype(x)>())
75 {
76 return forward<XB>(b);
77 }
78 else
79 {
80 return x;
81 }
82 }
83 };
84}
85#endif // pRC_ALGORITHMS_SOLVER_BACKWARD_SUBSTITUTION_H
Definition backward_substitution.hpp:21
static void error(Xs &&...args)
Definition log.hpp:14
Restrict
Definition restrict.hpp:11
Hint
Definition hint.hpp:9
Transform
Definition transform.hpp:11
Definition backward_substitution.hpp:19
static constexpr X eval(X &&a)
Definition eval.hpp:11
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::remove_reference_t< T > RemoveReference
Definition type_traits.hpp:56
static constexpr auto zero()
Definition zero.hpp:12
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
std::is_reference< T > IsReference
Definition type_traits.hpp:47
static constexpr auto isUpperTriangular(X &&a, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_upper_triangular.hpp:18
static constexpr auto isApprox(XA &&a, XB &&b, TT const &tolerance=NumericLimits< TT >::tolerance())
Definition is_approx.hpp:24
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
constexpr auto cDebugLevel
Definition config.hpp:46
Definition type_traits.hpp:16