pRC
multi-purpose Tensor Train library for C++
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
16
17namespace pRC::TensorTrain
18{
19 template<class T, class M, class N, class Ranks, class F>
22
23 template<class T, Size... Ms, Size... Ns, Size... Rs>
24 class Operator<T, Sizes<Ms...>, Sizes<Ns...>, Sizes<Rs...>,
25 If<All<IsSatisfied<(sizeof...(Ms) == sizeof...(Ns))>,
26 IsSatisfied<(sizeof...(Ns) - 1 == sizeof...(Rs))>,
28 {
29 public:
30 using M = pRC::Sizes<Ms...>;
31 using N = pRC::Sizes<Ns...>;
32 using L = pRC::Sizes<(Ms * Ns)...>;
33 using Sizes = pRC::Sizes<Ms..., Ns...>;
34
39
40 using Dimension = typename N::Dimension;
41
42 using Ranks = pRC::Sizes<Rs...>;
43 template<class S,
46
47 template<Index C>
49 M::size(C), N::size(C), pRC::Sizes<1, Rs..., 1>::size(C + 1)>;
50
51 using Type = T;
52 template<class C>
54
55 using Value = typename T::Value;
56 template<class V, If<IsValue<V>> = 0>
59
60 using Signed = typename T::Signed;
61 template<Bool R>
64
65 using Width = typename T::Width;
66 template<Size Q>
69
70 using IsComplexified = typename T::IsComplexified;
73
74 template<class E = typename M::IsLinearizable, If<E> = 0>
75 static constexpr auto m()
76 {
77 return M::size();
78 }
79
80 static constexpr auto m(Index const dimension)
81 {
82 return M::size(dimension);
83 }
84
85 template<class E = typename N::IsLinearizable, If<E> = 0>
86 static constexpr auto n()
87 {
88 return N::size();
89 }
90
91 static constexpr auto n(Index const dimension)
92 {
93 return N::size(dimension);
94 }
95
96 template<class E = typename Sizes::IsLinearizable, If<E> = 0>
97 static constexpr auto size()
98 {
99 return Sizes::size();
100 }
101
102 static constexpr auto size(Index const dimension)
103 {
104 return Sizes::size(dimension);
105 }
106
107 template<class X, class... Is, If<IsConstructible<T, X>> = 0,
108 If<IsSatisfied<(sizeof...(Is) == 2 * Dimension())>> = 0>
109 static inline constexpr auto Single(X &&value, Is const... indices)
110 {
111 return Single(forward<X>(value), Subscripts(indices...));
112 }
113
114 template<class X, If<IsConstructible<T, X>> = 0>
115 static inline constexpr auto Single(X &&value,
116 Subscripts const &subscripts)
117 {
118 return expand(makeSeries<Index, Dimension{}>(),
119 [&value, &subscripts](auto const... seq)
120 {
121 return Single(forward<X>(value),
124 });
125 }
126
127 template<class X, If<IsConstructible<T, X>> = 0>
128 static inline constexpr auto Single(X &&value, SubscriptsM const &is,
129 SubscriptsN const &js)
130 {
131 auto const f = [is, js, value = T(forward<X>(value))]<Index C>()
132 {
133 if constexpr(C == 0)
134 {
135 return Cores<C>::Single(value, 0, is[C], js[C], 0);
136 }
137 else
138 {
139 return Cores<C>::Single(identity<T>(), 0, is[C], js[C], 0);
140 }
141 };
142 using F = RemoveConstReference<decltype(f)>;
144 }
145
146 public:
147 ~Operator() = default;
148 constexpr Operator(Operator const &) = default;
149 constexpr Operator(Operator &&) = default;
150 constexpr Operator &operator=(Operator const &) & = default;
151 constexpr Operator &operator=(Operator &&) & = default;
152 constexpr Operator() = default;
153
154 template<class X,
156 constexpr Operator(X &&other)
157 {
158 *this = forward<X>(other);
159 }
160
161 template<class X,
163 constexpr auto &operator=(X &&rhs) &
164 {
165 view(*this) = forward<X>(rhs);
166 return *this;
167 }
168
169 template<Index C>
170 constexpr decltype(auto) core() &&
171 {
172 return get<C>(move(mCores));
173 }
174
175 template<Index C>
176 constexpr decltype(auto) core() const &&
177 {
178 return get<C>(move(mCores));
179 }
180
181 template<Index C>
182 constexpr decltype(auto) core() &
183 {
184 return get<C>(mCores);
185 }
186
187 template<Index C>
188 constexpr decltype(auto) core() const &
189 {
190 return get<C>(mCores);
191 }
192
193 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
194 If<IsSatisfied<(sizeof...(Is) == 2 * Dimension())>> = 0>
195 constexpr decltype(auto) operator()(Is const... indices) const
196 {
197 return view(*this)(indices...);
198 }
199
200 constexpr decltype(auto) operator()(Subscripts const &subscripts) const
201 {
202 return view(*this)(subscripts);
203 }
204
205 constexpr decltype(auto) operator()(SubscriptsM const &is,
206 SubscriptsN const &js) const
207 {
208 return view(*this)(is, js);
209 }
210
211 template<class X, If<IsInvocable<Add, Operator &, X>> = 0>
212 constexpr auto &operator+=(X &&rhs) &
213 {
214 return *this = *this + forward<X>(rhs);
215 }
216
217 template<class X, If<IsInvocable<Sub, Operator &, X>> = 0>
218 constexpr auto &operator-=(X &&rhs) &
219 {
220 return *this = *this - forward<X>(rhs);
221 }
222
223 template<class X, If<IsInvocable<Mul, X, Operator &>> = 0>
224 constexpr auto &applyOnTheLeft(X &&lhs) &
225 {
226 view(*this).applyOnTheLeft(forward<X>(lhs));
227 return *this;
228 }
229
230 template<class X, If<IsInvocable<Mul, Operator &, X>> = 0>
231 constexpr auto &applyOnTheRight(X &&rhs) &
232 {
233 view(*this).applyOnTheRight(forward<X>(rhs));
234 return *this;
235 }
236
237 template<class X, If<IsInvocable<Mul, Operator &, X>> = 0>
238 constexpr auto &operator*=(X &&rhs) &
239 {
240 view(*this) *= forward<X>(rhs);
241 return *this;
242 }
243
244 template<class X, If<IsInvocable<Div, Operator &, X>> = 0>
245 constexpr auto &operator/=(X &&rhs) &
246 {
247 return *this = *this / forward<X>(rhs);
248 }
249
250 template<class E = IsSatisfied<((Ms * ... * 1) * (Ns * ... * 1)) <=
252 typename Sizes::IsLinearizable()>,
253 If<E> = 0>
254 explicit constexpr operator pRC::Tensor<T, Ms..., Ns...>() const
255 {
256 return pRC::Tensor(view(*this));
257 }
258
259 private:
260 template<Index... seq>
261 static constexpr auto coreTypes(Sequence<Index, seq...>)
262 {
263 return tuple<Cores<seq>...>{};
264 }
265
266 using CoreTypes = decltype(coreTypes(makeSeries<Index, Dimension{}>()));
267
268 private:
270 };
271}
272
273namespace pRC
274{
275 template<class T, Size... Ms, Size... Ns, class R>
277 -> Tensor<T, Ms..., Ns...>;
278}
279#endif // pRC_TENSOR_TRAIN_OPERATOR_OPERATOR_H
Definition sequence.hpp:56
static constexpr auto size()
Definition sequence.hpp:88
Constant< Size, sizeof...(Ns)> Dimension
Definition sequence.hpp:74
Constant< Bool, linearizable()> IsLinearizable
Definition sequence.hpp:75
Definition sequence.hpp:34
Definition subscripts.hpp:20
Definition type_traits.hpp:37
Definition type_traits.hpp:17
Class storing tensors.
Definition tensor.hpp:44
static constexpr auto size()
Returns the number of entries of the Tensor class.
Definition tensor.hpp:84
Definition from_cores.hpp:11
Definition cholesky.hpp:18
std::conjunction< Bs... > All
Definition type_traits.hpp:77
static constexpr X view(X &&a)
Returns a TensorView obtained from a TensorView.
Definition view.hpp:22
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
std::size_t Size
Definition type_traits.hpp:20
static constexpr auto makeSeries()
Definition sequence.hpp:361
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr Conditional< IsSatisfied< C >, RemoveConstReference< X >, X > copy(X &&a)
Definition copy.hpp:13
std::disjunction< Bs... > Any
Definition type_traits.hpp:80
RemoveConst< RemoveReference< T > > RemoveConstReference
Definition type_traits.hpp:62
Any< IsFloat< T >, IsInteger< T > > IsValue
Definition type_traits.hpp:72
static constexpr decltype(auto) expand(Sequence< T, Seq... > const, F &&f, Xs &&...args)
forwards the values in a pRC::Sequence to a function as parameters
Definition sequence.hpp:354
Size Index
Definition type_traits.hpp:21
Definition type_traits.hpp:15
Definition limits.hpp:13