pRC
multi-purpose Tensor Train library for C++
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_TENSOR_VIEWS_VIEW_H
4#define pRC_TENSOR_TRAIN_TENSOR_VIEWS_VIEW_H
5
17
19{
20 template<class T, Size... Ns, Size... Rs, class F>
21 class View<T, Sizes<Ns...>, Sizes<Rs...>, F> : public CRTP<F>
22 {
23 static_assert(sizeof...(Ns) - 1 == sizeof...(Rs));
24 static_assert(IsValue<T>() || IsComplex<T>(),
25 "Tensor<T, Sizes<Ns...>>: T has to be of type Value or Complex.");
26
27 public:
28 using N = pRC::Sizes<Ns...>;
29 using L = N;
30 using Sizes = N;
31
35
36 using Dimension = typename N::Dimension;
37
38 using Ranks = pRC::Sizes<Rs...>;
39 template<class S,
42
43 template<Index C>
45 N::size(C), pRC::Sizes<1, Rs..., 1>::size(C + 1)>;
46
47 using Type = T;
48 template<class C>
50
51 using Value = typename T::Value;
52 template<class V, If<IsValue<V>> = 0>
55
56 using Signed = typename T::Signed;
57 template<Bool R>
60
61 using Width = typename T::Width;
62 template<Size Q>
65
66 using IsComplexified = typename T::IsComplexified;
69
70 template<class E = typename N::IsLinearizable, If<E> = 0>
71 static constexpr auto n()
72 {
73 return N::size();
74 }
75
76 static constexpr auto n(Index const dimension)
77 {
78 return N::size(dimension);
79 }
80
81 template<class E = typename Sizes::IsLinearizable, If<E> = 0>
82 static constexpr auto size()
83 {
84 return Sizes::size();
85 }
86
87 static constexpr auto size(Index const dimension)
88 {
89 return Sizes::size(dimension);
90 }
91
92 template<class X, class... Is, If<IsConstructible<T, X>> = 0,
93 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
94 static inline constexpr auto Single(X &&value, Is const... indices)
95 {
97 }
98
99 template<class X, If<IsConstructible<T, X>> = 0>
100 static inline constexpr auto Single(X &&value,
101 Subscripts const &subscripts)
102 {
104 }
105
106 public:
107 template<Index C>
108 constexpr decltype(auto) core()
109 {
110 return this->self().template core<C>();
111 }
112
113 template<Index C>
114 constexpr decltype(auto) core() const
115 {
116 return this->self().template core<C>();
117 }
118
119 template<class... Is,
120 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
121 constexpr auto operator()(Is const... indices)
122 {
123 return expand(makeSeries<Index, Dimension{}>(),
124 [this, &indices...](auto const... seq)
125 {
127 chip<1>(this->template core<seq>(), indices)...)(0, 0);
128 });
129 }
130
131 template<class... Is,
132 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
133 constexpr auto operator()(Is const... indices) const
134 {
135 return expand(makeSeries<Index, Dimension{}>(),
136 [this, &indices...](auto const... seq)
137 {
139 chip<1>(this->template core<seq>(), indices)...)(0, 0);
140 });
141 }
142
143 constexpr decltype(auto) operator()(Subscripts const &subscripts)
144 {
145 return expand(makeSeries<Index, Dimension{}>(),
146 [this, &subscripts](auto const... seq) -> decltype(auto)
147 {
148 return (*this)(subscripts[seq]...);
149 });
150 }
151
152 constexpr decltype(auto) operator()(Subscripts const &subscripts) const
153 {
154 return expand(makeSeries<Index, Dimension{}>(),
155 [this, &subscripts](auto const... seq) -> decltype(auto)
156 {
157 return (*this)(subscripts[seq]...);
158 });
159 }
160
161 template<class E =
162 IsSatisfied<(Ns * ... * 1) <= NumericLimits<Size>::max() &&
163 typename Sizes::IsLinearizable()>,
164 If<E> = 0>
165 explicit constexpr operator pRC::Tensor<T, Ns...>()
166 {
167 pRC::Tensor<T, Ns...> full;
168
170 [this, &full](auto const... indices)
171 {
172 full(indices...) = (*this)(indices...);
173 });
174
175 return full;
176 }
177
178 template<class E =
179 IsSatisfied<(Ns * ... * 1) <= NumericLimits<Size>::max() &&
180 typename Sizes::IsLinearizable()>,
181 If<E> = 0>
182 explicit constexpr operator pRC::Tensor<T, Ns...>() const
183 {
184 pRC::Tensor<T, Ns...> full;
185
187 [this, &full](auto const... indices)
188 {
189 full(indices...) = (*this)(indices...);
190 });
191
192 return full;
193 }
194
195 protected:
196 ~View() = default;
197 constexpr View(View const &) = default;
198 constexpr View(View &&) = default;
199 constexpr View &operator=(View const &) = delete;
200 constexpr View &operator=(View &&) = delete;
201 constexpr View()
202 {
203 range<Context::CompileTime, Dimension{}>(
204 [](auto const i)
205 {
206 static_assert(
207 True<decltype(declval<F>().template core<i>())>());
208 static_assert(
210 Cores<i>>());
211 });
212 static_assert(IsBaseOf<View, F>());
213 }
214 };
215}
216
217namespace pRC
218{
219 template<class T, Size... Ns, class R, class F>
221 -> Tensor<T, Ns...>;
222}
223#endif // pRC_TENSOR_TRAIN_TENSOR_VIEWS_VIEW_H
Definition crtp.hpp:12
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 Single(X &&value, Is const ... indices)
Definition view.hpp:94
static constexpr auto size(Index const dimension)
Definition view.hpp:87
static constexpr auto n(Index const dimension)
Definition view.hpp:76
static constexpr auto Single(X &&value, Subscripts const &subscripts)
Definition view.hpp:100
constexpr decltype(auto) core()
Definition view.hpp:108
typename T::IsComplexified IsComplexified
Definition view.hpp:66
constexpr decltype(auto) core() const
Definition view.hpp:114
Definition type_traits.hpp:37
Definition type_traits.hpp:17
Class storing tensors.
Definition tensor.hpp:44
static constexpr auto Single(X &&value, Is const ... indices)
Returns a Tensor of the Tensor class with a single non-zero entry.
Definition tensor.hpp:120
static constexpr auto size()
Returns the number of entries of the Tensor class.
Definition tensor.hpp:84
Definition type_traits.hpp:35
Definition cholesky.hpp:18
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
std::size_t Size
Definition type_traits.hpp:20
static constexpr auto range(F &&f, Xs &&...args)
Definition range.hpp:16
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
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