cMHN 1.1
C++ library for learning MHNs with pRC
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSD-2-Clause
2
3#ifndef pRC_TENSOR_TRAIN_OPERATOR_VIEWS_VIEW_H
4#define pRC_TENSOR_TRAIN_OPERATOR_VIEWS_VIEW_H
5
17
19{
20 template<class T, Size... Ms, Size... Ns, Size... Rs, class F>
21 class View<T, Sizes<Ms...>, Sizes<Ns...>, Sizes<Rs...>, F> : public CRTP<F>
22 {
23 static_assert(sizeof...(Ms) == sizeof...(Ns));
24 static_assert(sizeof...(Ns) - 1 == sizeof...(Rs));
25 static_assert(IsValue<T>() || IsComplex<T>(),
26 "Operator<T, Sizes<Ns...>>: T has to be of type Value or Complex.");
27
28 public:
29 using M = pRC::Sizes<Ms...>;
30 using N = pRC::Sizes<Ns...>;
31 using L = pRC::Sizes<(Ms * Ns)...>;
32 using Sizes = pRC::Sizes<Ms..., Ns...>;
33
38
39 using Dimension = typename N::Dimension;
40
41 using Ranks = pRC::Sizes<Rs...>;
42 template<class S,
45
46 template<Index C>
48 M::size(C), N::size(C), pRC::Sizes<1, Rs..., 1>::size(C + 1)>;
49
50 using Type = T;
51 template<class C>
53
54 using Value = typename T::Value;
55 template<class V, If<IsValue<V>> = 0>
58
59 using Signed = typename T::Signed;
60 template<Bool R>
63
64 using Width = typename T::Width;
65 template<Size Q>
68
72
73 template<class E = typename M::IsLinearizable, If<E> = 0>
74 static constexpr auto m()
75 {
76 return M::size();
77 }
78
79 static constexpr auto m(Index const dimension)
80 {
81 return M::size(dimension);
82 }
83
84 template<class E = typename N::IsLinearizable, If<E> = 0>
85 static constexpr auto n()
86 {
87 return N::size();
88 }
89
90 static constexpr auto n(Index const dimension)
91 {
92 return N::size(dimension);
93 }
94
95 template<class E = typename Sizes::IsLinearizable, If<E> = 0>
96 static constexpr auto size()
97 {
98 return Sizes::size();
99 }
100
101 static constexpr auto size(Index const dimension)
102 {
103 return Sizes::size(dimension);
104 }
105
106 template<class X, class... Is, If<IsConstructible<T, X>> = 0,
107 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
108 static inline constexpr auto Single(X &&value, Is const... indices)
109 {
110 return Operator<T, M, N, Ranks>::Single(forward<X>(value),
111 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 Operator<T, M, N, Ranks>::Single(forward<X>(value),
119 subscripts);
120 }
121
122 template<class X, If<IsConstructible<T, X>> = 0>
123 static inline constexpr auto Single(X &&value, SubscriptsM const &is,
124 SubscriptsN const &js)
125 {
126 return Operator<T, M, N, Ranks>::Single(forward<X>(value), is, js);
127 }
128
129 public:
130 template<Index C>
131 constexpr decltype(auto) core()
132 {
133 return this->self().template core<C>();
134 }
135
136 template<Index C>
137 constexpr decltype(auto) core() const
138 {
139 return this->self().template core<C>();
140 }
141
142 template<class... Is,
143 If<IsSatisfied<(sizeof...(Is) == 2 * Dimension())>> = 0>
144 constexpr decltype(auto) operator()(Is const... indices)
145 {
146 return (*this)(Subscripts(indices...));
147 }
148
149 template<class... Is,
150 If<IsSatisfied<(sizeof...(Is) == 2 * Dimension())>> = 0>
151 constexpr decltype(auto) operator()(Is const... indices) const
152 {
153 return (*this)(Subscripts(indices...));
154 }
155
156 constexpr decltype(auto) operator()(Subscripts const &subscripts)
157 {
158 return expand(makeSeries<Index, Dimension{}>(),
159 [this, &subscripts](auto const... seq) -> decltype(auto)
160 {
161 return (*this)(SubscriptsM(subscripts[seq]...),
163 });
164 }
165
166 constexpr decltype(auto) operator()(Subscripts const &subscripts) const
167 {
168 return expand(makeSeries<Index, Dimension{}>(),
169 [this, &subscripts](auto const... seq) -> decltype(auto)
170 {
171 return (*this)(SubscriptsM(subscripts[seq]...),
173 });
174 }
175
176 constexpr auto operator()(SubscriptsM const &is, SubscriptsN const &js)
177 {
178 return expand(makeSeries<Index, Dimension{}>(),
179 [this, &is, &js](auto const... seq)
180 {
182 this->template core<seq>(), is[seq], js[seq])...)(0, 0);
183 });
184 }
185
186 constexpr auto operator()(SubscriptsM const &is,
187 SubscriptsN const &js) const
188 {
189 return expand(makeSeries<Index, Dimension{}>(),
190 [this, &is, &js](auto const... seq)
191 {
193 this->template core<seq>(), is[seq], js[seq])...)(0, 0);
194 });
195 }
196
197 template<class E = IsSatisfied<((Ms * ... * 1) * (Ns * ... * 1)) <=
199 typename Sizes::IsLinearizable()>,
200 If<E> = 0>
201 explicit constexpr operator pRC::Tensor<T, Ms..., Ns...>()
202 {
203 pRC::Tensor<T, Ms..., Ns...> full;
204
206 [this, &full](auto const... indices)
207 {
208 full(indices...) = (*this)(indices...);
209 });
210
211 return full;
212 }
213
214 template<class E = IsSatisfied<((Ms * ... * 1) * (Ns * ... * 1)) <=
216 typename Sizes::IsLinearizable()>,
217 If<E> = 0>
218 explicit constexpr operator pRC::Tensor<T, Ms..., Ns...>() const
219 {
220 pRC::Tensor<T, Ns...> full;
221
223 [this, &full](auto const... indices)
224 {
225 full(indices...) = (*this)(indices...);
226 });
227
228 return full;
229 }
230
231 protected:
232 ~View() = default;
233 constexpr View(View const &) = default;
234 constexpr View(View &&) = default;
235 constexpr View &operator=(View const &) = delete;
236 constexpr View &operator=(View &&) = delete;
237 constexpr View()
238 {
239 range<Context::CompileTime, Dimension{}>(
240 [](auto const i)
241 {
242 static_assert(
243 True<decltype(declval<F>().template core<i>())>());
244 static_assert(
246 Cores<i>>());
247 });
248 static_assert(IsBaseOf<View, F>());
249 }
250 };
251}
252
253namespace pRC
254{
255 template<class T, Size... Ms, Size... Ns, class R, class F>
257 F> const &) -> Tensor<T, Ms..., Ns...>;
258}
259#endif // pRC_TENSOR_TRAIN_OPERATOR_VIEWS_VIEW_H
Definition crtp.hpp:12
pRC::Constant< Size, W > Width
Definition float.hpp:39
Float< W > Value
Definition float.hpp:31
False<> IsComplexified
Definition float.hpp:43
True<> Signed
Definition float.hpp:35
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 subscripts.hpp:20
static constexpr auto m(Index const dimension)
Definition view.hpp:79
static constexpr auto n(Index const dimension)
Definition view.hpp:90
static constexpr auto size(Index const dimension)
Definition view.hpp:101
static constexpr auto Single(X &&value, Subscripts const &subscripts)
Definition view.hpp:115
static constexpr auto Single(X &&value, SubscriptsM const &is, SubscriptsN const &js)
Definition view.hpp:123
constexpr auto operator()(SubscriptsM const &is, SubscriptsN const &js) const
Definition view.hpp:186
static constexpr auto Single(X &&value, Is const ... indices)
Definition view.hpp:108
constexpr decltype(auto) operator()(Is const ... indices)
Definition view.hpp:144
constexpr decltype(auto) operator()(Is const ... indices) const
Definition view.hpp:151
constexpr auto operator()(SubscriptsM const &is, SubscriptsN const &js)
Definition view.hpp:176
Definition type_traits.hpp:37
Definition type_traits.hpp:17
Definition tensor.hpp:28
static constexpr auto size()
Definition tensor.hpp:65
TN::Subscripts S
Definition externs_nonTT.hpp:9
pRC::Float<> T
Definition externs_nonTT.hpp:1
Definition type_traits.hpp:35
Definition cholesky.hpp:18
static constexpr auto makeConstantSequence()
Definition sequence.hpp:402
Size Index
Definition type_traits.hpp:21
std::size_t Size
Definition type_traits.hpp:20
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:16
Tensor(TensorViews::View< T, Sizes< Ns... >, F > const &) -> Tensor< T, Ns... >
static constexpr auto makeSeries()
Definition sequence.hpp:351
static constexpr decltype(auto) expand(Sequence< T, Seq... > const, F &&f, Xs &&...args)
Definition sequence.hpp:344
Definition type_traits.hpp:15
Definition limits.hpp:13