cMHN 1.2
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
tensor.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_TENSOR_TRAIN_TENSOR_TENSOR_H
4#define pRC_TENSOR_TRAIN_TENSOR_TENSOR_H
5
12
13namespace pRC::TensorTrain
14{
15 template<class T, Size... Ns, Size... Rs>
16 requires(IsValue<T> || IsComplex<T>) &&
18 class Tensor<T, Sizes<Ns...>, Sizes<Rs...>>
19 {
20 public:
21 using N = pRC::Sizes<Ns...>;
22 using L = N;
23 using Sizes = N;
24
28
29 static constexpr auto Dimension = N::Dimension;
30
31 using Ranks = pRC::Sizes<Rs...>;
32 template<class S>
33 requires(S::Dimension == Ranks::Dimension)
35
36 template<Index C>
38 N::size(C), pRC::Sizes<1, Rs..., 1>::size(C + 1)>;
39
40 using Type = T;
41 template<class C>
43
44 static constexpr auto n()
45 requires requires { N::size(); }
46 {
47 return N::size();
48 }
49
50 static constexpr auto n(Index const dimension)
51 {
52 return N::size(dimension);
53 }
54
55 static constexpr auto size()
56 requires requires { Sizes::size(); }
57 {
58 return Sizes::size();
59 }
60
61 static constexpr auto size(Index const dimension)
62 {
63 return Sizes::size(dimension);
64 }
65
66 template<class X, IsConvertible<Index>... Is>
67 requires IsConstructible<T, X> && (sizeof...(Is) == Dimension)
68 static constexpr auto Single(X &&value, Is const... indices)
69 {
70 return Single(forward<X>(value), Subscripts(indices...));
71 }
72
73 template<class X>
75 static constexpr auto Single(X &&value, Subscripts const &subscripts)
76 {
77 auto const f = [subscripts, value = T(forward<X>(value))]<Index C>()
78 {
79 if constexpr(C == 0)
80 {
81 return Cores<C>::Single(value, 0, subscripts[C], 0);
82 }
83 else
84 {
85 return Cores<C>::Single(identity<T>(), 0, subscripts[C], 0);
86 }
87 };
88 using F = RemoveConstReference<decltype(f)>;
90 }
91
92 public:
93 ~Tensor() = default;
94 constexpr Tensor(Tensor const &) = default;
95 constexpr Tensor(Tensor &&) = default;
96 constexpr Tensor &operator=(Tensor const &) & = default;
97 constexpr Tensor &operator=(Tensor &&) & = default;
98 constexpr Tensor() = default;
99
100 template<class X>
102 constexpr Tensor(X &&other)
103 {
104 *this = forward<X>(other);
105 }
106
107 template<class X>
109 constexpr auto &operator=(X &&rhs) &
110 {
111 view(*this) = forward<X>(rhs);
112 return *this;
113 }
114
115 template<Index C>
116 constexpr decltype(auto) core() &&
117 {
118 return get<C>(move(mCores));
119 }
120
121 template<Index C>
122 constexpr decltype(auto) core() const &&
123 {
124 return get<C>(move(mCores));
125 }
126
127 template<Index C>
128 constexpr decltype(auto) core() &
129 {
130 return get<C>(mCores);
131 }
132
133 template<Index C>
134 constexpr decltype(auto) core() const &
135 {
136 return get<C>(mCores);
137 }
138
139 template<IsConvertible<Index>... Is>
140 requires(sizeof...(Is) == Dimension)
141 constexpr decltype(auto) operator()(Is const... indices) const
142 {
143 return view(*this)(indices...);
144 }
145
146 constexpr decltype(auto) operator()(Subscripts const &subscripts) const
147 {
148 return view(*this)(subscripts);
149 }
150
151 template<class X>
153 constexpr auto &operator+=(X &&rhs) &
154 {
155 return *this = *this + forward<X>(rhs);
156 }
157
158 template<class X>
160 constexpr auto &operator-=(X &&rhs) &
161 {
162 return *this = *this - forward<X>(rhs);
163 }
164
165 template<class X>
167 constexpr auto &applyOnTheLeft(X &&lhs) &
168 {
169 view(*this).applyOnTheLeft(forward<X>(lhs));
170 return *this;
171 }
172
173 template<class X>
175 constexpr auto &applyOnTheRight(X &&rhs) &
176 {
177 view(*this).applyOnTheRight(forward<X>(rhs));
178 return *this;
179 }
180
181 template<class X>
183 constexpr auto &operator*=(X &&rhs) &
184 {
185 view(*this) *= forward<X>(rhs);
186 return *this;
187 }
188
189 template<class X>
191 constexpr auto &operator/=(X &&rhs) &
192 {
193 return *this = *this / forward<X>(rhs);
194 }
195
196 template<class X>
197 requires requires { typename pRC::Tensor<T, Ns...>; } &&
198 IsSame<X, pRC::Tensor<T, Ns...>>
199 explicit constexpr operator X() const
200 {
201 return pRC::Tensor(view(*this));
202 }
203
204 private:
205 template<Index... seq>
206 static constexpr auto coreTypes(Sequence<Index, seq...>)
207 {
208 return Tuple<Cores<seq>...>{};
209 }
210
211 using CoreTypes = decltype(coreTypes(makeSeries<Index, Dimension>()));
212
213 private:
214 CoreTypes mCores;
215 };
216
217 template<class T, class N, class Ranks, class F>
219}
220
221namespace pRC
222{
223 template<class T, Size... Ns, class R>
225}
226#endif // pRC_TENSOR_TRAIN_TENSOR_TENSOR_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 enumerate.hpp:20
Definition declarations.hpp:21
constexpr auto & operator*=(X &&rhs) &
Definition tensor.hpp:183
constexpr auto & applyOnTheLeft(X &&lhs) &
Definition tensor.hpp:167
constexpr auto & operator=(X &&rhs) &
Definition tensor.hpp:109
constexpr auto & applyOnTheRight(X &&rhs) &
Definition tensor.hpp:175
static constexpr auto n()
Definition tensor.hpp:44
constexpr Tensor & operator=(Tensor &&) &=default
constexpr decltype(auto) core() &&
Definition tensor.hpp:116
constexpr auto & operator/=(X &&rhs) &
Definition tensor.hpp:191
static constexpr auto size()
Definition tensor.hpp:55
static constexpr auto Single(X &&value, Subscripts const &subscripts)
Definition tensor.hpp:75
constexpr decltype(auto) core() const &&
Definition tensor.hpp:122
constexpr decltype(auto) core() &
Definition tensor.hpp:128
static constexpr auto n(Index const dimension)
Definition tensor.hpp:50
constexpr Tensor(X &&other)
Definition tensor.hpp:102
constexpr auto & operator-=(X &&rhs) &
Definition tensor.hpp:160
constexpr decltype(auto) core() const &
Definition tensor.hpp:134
static constexpr auto size(Index const dimension)
Definition tensor.hpp:61
constexpr auto & operator+=(X &&rhs) &
Definition tensor.hpp:153
constexpr Tensor & operator=(Tensor const &) &=default
Definition declarations.hpp:16
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
Tensor(TensorViews::View< T, N, Ranks, F > const &) -> Tensor< T, N, Ranks >
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... >
static constexpr auto makeSeries()
Definition sequence.hpp:390
RemoveConst< RemoveReference< T > > RemoveConstReference
Definition basics.hpp:47
static constexpr auto identity()
Definition identity.hpp:13