cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
mhn_operator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef cMHN_NONTT_MHN_OPERATOR_H
4#define cMHN_NONTT_MHN_OPERATOR_H
5
6#include <prc.hpp>
7
8namespace cMHN::nonTT
9{
22 template<class T, pRC::Size D>
24 {
25 public:
26 using Type = T;
27
28 public:
30 : mBigTheta(exp(smallTheta))
31 {
32 }
33
34 constexpr auto &bigTheta(pRC::Index const i, pRC::Index const j) const
35 {
36 return mBigTheta(i, j);
37 }
38
39 constexpr auto &bigTheta() const
40 {
41 return mBigTheta;
42 }
43
44 auto &bigTheta()
45 {
46 return mBigTheta;
47 }
48
49 constexpr auto smallTheta(pRC::Index const i, pRC::Index const j) const
50 {
51 return log(mBigTheta(i, j));
52 }
53
54 constexpr auto smallTheta() const
55 {
56 return eval(log(mBigTheta));
57 }
58
59 private:
60 pRC::Tensor<T, D, D> mBigTheta;
61 };
62
75 // apply dQ/dtheta_ii to a vector x
79 pRC::Size D, class T2, pRC::Size... Ns>
80 requires(OR == pRC::Operator::Restrict::None) &&
82 static inline constexpr auto applyDerivative(MHNOperator<T1, D> const &op,
83 pRC::Tensor<T2, Ns...> const &x, pRC::Index const &i)
84
85 {
86 auto r = x;
88 [&](auto const j)
89 {
90 if(i == j)
91 {
92 pRC::chip<j>(r, 0) *= -op.bigTheta(i, i);
93 pRC::chip<j>(r, 1) = -pRC::chip<j>(r, 0);
94 }
95 else
96 {
97 pRC::chip<j>(r, 1) *= op.bigTheta(i, j);
98 }
99 });
100 return r;
101 }
102
118 pRC::Size D, class T2, pRC::Size... Ns>
119 requires(OR == pRC::Operator::Restrict::None)
120 static inline constexpr auto apply_diag(MHNOperator<T1, D> const &op,
122 {
123 auto b = x;
124 for(pRC::Index i = 0; i < D; ++i)
125 {
126 auto v = x;
128 [&](auto const j)
129 {
130 if(i == j)
131 {
132 pRC::chip<j>(v, 0) *= -op.bigTheta(i, i);
133 pRC::chip<j>(v, 1) = pRC::zero();
134 }
135 else
136 {
137 pRC::chip<j>(v, 1) *= op.bigTheta(i, j);
138 }
139 });
140 b -= v;
141 }
142 return b;
143 }
144
161 pRC::Size D, class T2, pRC::Size... Ns>
162 requires(OR == pRC::Operator::Restrict::None)
163 static inline constexpr auto apply_offdiag(MHNOperator<T1, D> const &op,
165 {
166 pRC::Tensor r = x;
167 r = pRC::zero();
168 for(pRC::Index i = 0; i < D; ++i)
169 {
170 auto v = x;
172 [&](auto const j)
173 {
174 if(i == j)
175 {
176 if constexpr(OT == pRC::Operator::Transform::Transpose)
177 {
178 pRC::chip<j>(v, 0) =
179 op.bigTheta(i, i) * pRC::chip<j>(v, 1);
180 pRC::chip<j>(v, 1) = pRC::zero();
181 }
182 else
183 {
184 pRC::chip<j>(v, 1) =
185 op.bigTheta(i, i) * pRC::chip<j>(v, 0);
186 pRC::chip<j>(v, 0) = pRC::zero();
187 }
188 }
189 else
190 {
191 pRC::chip<j>(v, 1) *= op.bigTheta(i, j);
192 }
193 });
194 r += v;
195 }
196 return r;
197 }
198
212 // apply (1-Q) or the transposed to a vector x
216 pRC::Size D, class T2, pRC::Size... Ns>
217 requires(OR == pRC::Operator::Restrict::None)
218 static inline constexpr auto apply(MHNOperator<T1, D> const &op,
220
221 {
222 auto b = x;
223 for(pRC::Index i = 0; i < D; ++i)
224 {
225 auto v = x;
227 [&](auto const j)
228 {
229 if(i == j)
230 {
231 if constexpr(OT == pRC::Operator::Transform::Transpose)
232 {
233 pRC::chip<j>(v, 0) =
234 -op.bigTheta(i, i) * pRC::chip<j>(v, 0) +
235 op.bigTheta(i, i) * pRC::chip<j>(v, 1);
236 pRC::chip<j>(v, 1) = pRC::zero();
237 }
238 else
239 {
240 pRC::chip<j>(v, 0) *= -op.bigTheta(i, i);
241 pRC::chip<j>(v, 1) = -pRC::chip<j>(v, 0);
242 }
243 }
244 else
245 {
246 pRC::chip<j>(v, 1) *= op.bigTheta(i, j);
247 }
248 });
249 b -= v;
250 }
251 return b;
252 }
253
254}
255
256#endif // cMHN_NONTT_MHN_OPERATOR_H
pRC::Size const D
Definition CalculatePThetaTests.cpp:9
Class storing an MHN operator represented by a theta matrix (for non TT calculations)
Definition mhn_operator.hpp:24
constexpr auto smallTheta(pRC::Index const i, pRC::Index const j) const
Definition mhn_operator.hpp:49
auto & bigTheta()
Definition mhn_operator.hpp:44
constexpr auto & bigTheta(pRC::Index const i, pRC::Index const j) const
Definition mhn_operator.hpp:34
constexpr MHNOperator(pRC::Tensor< T, D, D > const &smallTheta)
Definition mhn_operator.hpp:29
constexpr auto smallTheta() const
Definition mhn_operator.hpp:54
constexpr auto & bigTheta() const
Definition mhn_operator.hpp:39
Definition value.hpp:12
Definition tensor.hpp:25
pRC::Float<> T
Definition externs_nonTT.hpp:1
int i
Definition gmock-matchers-comparisons_test.cc:603
int x
Definition gmock-matchers-containers_test.cc:376
Definition jacobi.hpp:11
static constexpr auto applyDerivative(MHNOperator< T1, D > const &op, pRC::Tensor< T2, Ns... > const &x, pRC::Index const &i)
apply the derivative of an MHN Q wrt to theta_ii to a vector x
Definition mhn_operator.hpp:82
static constexpr auto apply(MHNOperator< T1, D > const &op, pRC::Tensor< T2, Ns... > const &x)
apply (1-Q) or its transposed to a vector x, given an MHN Q
Definition mhn_operator.hpp:218
static constexpr auto apply_diag(MHNOperator< T1, D > const &op, pRC::Tensor< T2, Ns... > const &x)
apply the diagonal part of (1-Q) to a vector x, given an MHN Q
Definition mhn_operator.hpp:120
static constexpr auto apply_offdiag(MHNOperator< T1, D > const &op, pRC::Tensor< T2, Ns... > const &x)
apply the off-diagonal part of Q or its transposed to a vector x, given an MHN Q
Definition mhn_operator.hpp:163
Restrict
Definition restrict.hpp:9
Hint
Definition hint.hpp:9
Transform
Definition transform.hpp:9
Definition cholesky.hpp:10
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
static constexpr auto chip(Sequence< T, Is... > const)
Definition sequence.hpp:584
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:18
static constexpr auto zero()
Definition zero.hpp:12