cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
operator.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_TENSOR_TRAIN_OPERATOR_OPERATOR_H
4#define pRC_TENSOR_TRAIN_OPERATOR_OPERATOR_H
5
12
13namespace pRC::TensorTrain
14{
15 template<class T, Size... Ms, Size... Ns, Size... Rs>
16 requires(IsValue<T> || IsComplex<T>) &&
18 (Sizes<Ms...>::Dimension - 1 == Sizes<Rs...>::Dimension)
20 {
21 public:
22 using M = pRC::Sizes<Ms...>;
23 using N = pRC::Sizes<Ns...>;
24 using L = pRC::Sizes<(Ms * Ns)...>;
25 using Sizes = pRC::Sizes<Ms..., Ns...>;
26
29 using SubscriptsL = pRC::Subscripts<(Ms * Ns)...>;
30 using Subscripts = pRC::Subscripts<Ms..., Ns...>;
31
32 static constexpr auto Dimension = N::Dimension;
33
34 using Ranks = pRC::Sizes<Rs...>;
35 template<class S>
36 requires(S::Dimension, Ranks::Dimension)
38
39 template<Index C>
41 M::size(C), N::size(C), pRC::Sizes<1, Rs..., 1>::size(C + 1)>;
42
43 using Type = T;
44 template<class C>
46
47 static constexpr auto m()
48 requires requires { M::size(); }
49 {
50 return M::size();
51 }
52
53 static constexpr auto m(Index const dimension)
54 {
55 return M::size(dimension);
56 }
57
58 static constexpr auto n()
59 requires requires { N::size(); }
60 {
61 return N::size();
62 }
63
64 static constexpr auto n(Index const dimension)
65 {
66 return N::size(dimension);
67 }
68
69 static constexpr auto size()
70 requires requires { Sizes::size(); }
71 {
72 return Sizes::size();
73 }
74
75 static constexpr auto size(Index const dimension)
76 {
77 return Sizes::size(dimension);
78 }
79
80 template<class X, IsConvertible<Index>... Is>
81 requires IsConstructible<T, X> && (sizeof...(Is) == 2 * Dimension)
82 static constexpr auto Single(X &&value, Is const... indices)
83 {
84 return Single(forward<X>(value), Subscripts(indices...));
85 }
86
87 template<class X>
89 static constexpr auto Single(X &&value, Subscripts const &subscripts)
90 {
92 [&value, &subscripts](auto const... seq)
93 {
94 return Single(forward<X>(value),
95 SubscriptsM(subscripts[seq]...),
96 SubscriptsN(subscripts[seq + Dimension]...));
97 });
98 }
99
100 template<class X>
101 requires IsConstructible<T, X>
102 static constexpr auto Single(X &&value, SubscriptsM const &is,
103 SubscriptsN const &js)
104 {
105 auto const f = [is, js, value = T(forward<X>(value))]<Index C>()
106 {
107 if constexpr(C == 0)
108 {
109 return Cores<C>::Single(value, 0, is[C], js[C], 0);
110 }
111 else
112 {
113 return Cores<C>::Single(identity<T>(), 0, is[C], js[C], 0);
114 }
115 };
116 using F = RemoveConstReference<decltype(f)>;
118 }
119
120 public:
121 ~Operator() = default;
122 constexpr Operator(Operator const &) = default;
123 constexpr Operator(Operator &&) = default;
124 constexpr Operator &operator=(Operator const &) & = default;
125 constexpr Operator &operator=(Operator &&) & = default;
126 constexpr Operator() = default;
127
128 template<class X>
130 constexpr Operator(X &&other)
131 {
132 *this = forward<X>(other);
133 }
134
135 template<class X>
137 constexpr auto &operator=(X &&rhs) &
138 {
139 view(*this) = forward<X>(rhs);
140 return *this;
141 }
142
143 template<Index C>
144 constexpr decltype(auto) core() &&
145 {
146 return get<C>(move(mCores));
147 }
148
149 template<Index C>
150 constexpr decltype(auto) core() const &&
151 {
152 return get<C>(move(mCores));
153 }
154
155 template<Index C>
156 constexpr decltype(auto) core() &
157 {
158 return get<C>(mCores);
159 }
160
161 template<Index C>
162 constexpr decltype(auto) core() const &
163 {
164 return get<C>(mCores);
165 }
166
167 template<IsConvertible<Index>... Is>
168 requires(sizeof...(Is) == 2 * Dimension)
169 constexpr decltype(auto) operator()(Is const... indices) const
170 {
171 return view(*this)(indices...);
172 }
173
174 constexpr decltype(auto) operator()(Subscripts const &subscripts) const
175 {
176 return view(*this)(subscripts);
177 }
178
179 constexpr decltype(auto) operator()(SubscriptsM const &is,
180 SubscriptsN const &js) const
181 {
182 return view(*this)(is, js);
183 }
184
185 template<class X>
187 constexpr auto &operator+=(X &&rhs) &
188 {
189 return *this = *this + forward<X>(rhs);
190 }
191
192 template<class X>
194 constexpr auto &operator-=(X &&rhs) &
195 {
196 return *this = *this - forward<X>(rhs);
197 }
198
199 template<class X>
201 constexpr auto &applyOnTheLeft(X &&lhs) &
202 {
203 view(*this).applyOnTheLeft(forward<X>(lhs));
204 return *this;
205 }
206
207 template<class X>
209 constexpr auto &applyOnTheRight(X &&rhs) &
210 {
211 view(*this).applyOnTheRight(forward<X>(rhs));
212 return *this;
213 }
214
215 template<class X>
217 constexpr auto &operator*=(X &&rhs) &
218 {
219 view(*this) *= forward<X>(rhs);
220 return *this;
221 }
222
223 template<class X>
225 constexpr auto &operator/=(X &&rhs) &
226 {
227 return *this = *this / forward<X>(rhs);
228 }
229
230 template<class X>
231 requires requires { typename pRC::Tensor<T, Ms..., Ns...>; } &&
232 IsSame<X, pRC::Tensor<T, Ms..., Ns...>>
233 explicit constexpr operator X() const
234 {
235 return pRC::Tensor(view(*this));
236 }
237
238 private:
239 template<Index... seq>
240 static constexpr auto coreTypes(Sequence<Index, seq...>)
241 {
242 return Tuple<Cores<seq>...>{};
243 }
244
245 using CoreTypes = decltype(coreTypes(makeSeries<Index, Dimension>()));
246
247 private:
248 CoreTypes mCores;
249 };
250
251 template<class T, class M, class N, class Ranks, class F>
254}
255
256namespace pRC
257{
258 template<class T, Size... Ms, Size... Ns, class R>
260 -> Tensor<T, Ms..., Ns...>;
261}
262#endif // pRC_TENSOR_TRAIN_OPERATOR_OPERATOR_H
Definition value.hpp:12
Definition sequence.hpp:29
static constexpr Size Dimension
Definition sequence.hpp:47
static constexpr auto size()
Definition sequence.hpp:69
Definition subscripts.hpp:21
Definition declarations.hpp:25
static constexpr auto m(Index const dimension)
Definition operator.hpp:53
constexpr decltype(auto) core() const &&
Definition operator.hpp:150
constexpr auto & operator+=(X &&rhs) &
Definition operator.hpp:187
constexpr auto & applyOnTheRight(X &&rhs) &
Definition operator.hpp:209
constexpr auto & operator=(X &&rhs) &
Definition operator.hpp:137
static constexpr auto size(Index const dimension)
Definition operator.hpp:75
constexpr decltype(auto) core() &
Definition operator.hpp:156
constexpr auto & operator/=(X &&rhs) &
Definition operator.hpp:225
constexpr decltype(auto) core() const &
Definition operator.hpp:162
constexpr auto & operator*=(X &&rhs) &
Definition operator.hpp:217
static constexpr auto n(Index const dimension)
Definition operator.hpp:64
constexpr decltype(auto) core() &&
Definition operator.hpp:144
static constexpr auto Single(X &&value, Subscripts const &subscripts)
Definition operator.hpp:89
static constexpr auto Single(X &&value, SubscriptsM const &is, SubscriptsN const &js)
Definition operator.hpp:102
constexpr auto & applyOnTheLeft(X &&lhs) &
Definition operator.hpp:201
constexpr auto & operator-=(X &&rhs) &
Definition operator.hpp:194
Definition declarations.hpp:20
Definition tensor.hpp:25
static constexpr auto size()
Definition tensor.hpp:39
Definition concepts.hpp:40
Definition concepts.hpp:37
Definition concepts.hpp:31
Definition concepts.hpp:28
pRC::Float<> T
Definition externs_nonTT.hpp:1
int value
Definition gmock-actions_test.cc:1714
Definition from_cores.hpp:11
Definition cholesky.hpp:10
Size Index
Definition basics.hpp:32
std::size_t Size
Definition basics.hpp:31
std::tuple< Ts... > Tuple
Definition basics.hpp:23
static constexpr decltype(auto) view(X &&a)
Definition view.hpp:13
Tensor(TensorViews::View< T, Sizes< Ns... >, F > const &) -> Tensor< T, Ns... >
Sequence< Size, Ns... > Sizes
Definition sequence.hpp:100
static constexpr auto makeSeries()
Definition sequence.hpp:390
RemoveConst< RemoveReference< T > > RemoveConstReference
Definition basics.hpp:47
static constexpr decltype(auto) expand(Sequence< T, Seq... > const, F &&f, Xs &&...args)
Definition sequence.hpp:383
static constexpr auto identity()
Definition identity.hpp:13