cMHN 1.1
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_CORE_TENSOR_TENSOR_H
4#define pRC_CORE_TENSOR_TENSOR_H
5
20
21namespace pRC
22{
23 template<class F, class T, Size... Ns>
25
26 template<class T, Size... Ns>
27 class Tensor
28 {
29 static_assert((Ns * ... * 1) <= NumericLimits<Size>::max() &&
31 static_assert(IsValue<T>() || IsComplex<T>(),
32 "Tensor<T, Ns..>: T has to be of type Value, Complex, or Bool.");
33
34 public:
35 using Type = T;
36 template<class C>
37 using ChangeType = Tensor<C, Ns...>;
38
40
41 using Sizes = pRC::Sizes<Ns...>;
42 template<Size... Ss>
43 using ChangeSizes = Tensor<T, Ss...>;
44
45 using Dimension = typename Sizes::Dimension;
46
47 using Value = typename T::Value;
48 template<class V, If<IsValue<V>> = 0>
50
51 using Signed = typename T::Signed;
52 template<Bool R>
55
56 using Width = typename T::Width;
57 template<Size Q>
59
61 using Complexify = Tensor<typename T::Complexify, Ns...>;
62 using NonComplex = Tensor<typename T::NonComplex, Ns...>;
63
64 template<class E = typename Sizes::IsLinearizable, If<E> = 0>
65 static constexpr auto size()
66 {
67 return Sizes::size();
68 }
69
70 static constexpr auto size(Index const dimension)
71 {
72 return Sizes::size(dimension);
73 }
74
75 template<class X, class... Is, If<IsConstructible<T, X>> = 0,
77 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
78 static constexpr auto Single(X &&value, Is const... indices)
79 {
80 return Single(forward<X>(value), Subscripts(indices...));
81 }
82
83 template<class X, If<IsConstructible<T, X>> = 0>
84 static constexpr auto Single(X &&value, Subscripts const &subscripts)
85 {
86 return TensorViews::Single<T, Sizes>(forward<X>(value), subscripts);
87 }
88
89 public:
90 ~Tensor() = default;
91 constexpr Tensor(Tensor const &) = default;
92 constexpr Tensor(Tensor &&) = default;
93 constexpr Tensor &operator=(Tensor const &) & = default;
94 constexpr Tensor &operator=(Tensor &&) & = default;
95 constexpr Tensor() = default;
96
97 template<class X,
99 constexpr Tensor(X &&other)
100 {
101 *this = forward<X>(other);
102 }
103
104 template<class X,
106 constexpr auto &operator=(X &&rhs) &
107 {
108 view(*this) = forward<X>(rhs);
109 return *this;
110 }
111
112 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
113 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
114 constexpr decltype(auto) operator()(Is const... indices) &&
115 {
116 return move(mData)(indices...);
117 }
118
119 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
120 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
121 constexpr decltype(auto) operator()(Is const... indices) const &&
122 {
123 return move(mData)(indices...);
124 }
125
126 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
127 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
128 constexpr decltype(auto) operator()(Is const... indices) &
129 {
130 return mData(indices...);
131 }
132
133 template<class... Is, If<All<IsConvertible<Is, Index>...>> = 0,
134 If<IsSatisfied<(sizeof...(Is) == Dimension())>> = 0>
135 constexpr decltype(auto) operator()(Is const... indices) const &
136 {
137 return mData(indices...);
138 }
139
140 constexpr decltype(auto) operator()(Subscripts const &subscripts) &&
141 {
142 return move(mData)(subscripts);
143 }
144
145 constexpr decltype(auto) operator()(
146 Subscripts const &subscripts) const &&
147 {
148 return move(mData)(subscripts);
149 }
150
151 constexpr decltype(auto) operator()(Subscripts const &subscripts) &
152 {
153 return mData(subscripts);
154 }
155
156 constexpr decltype(auto) operator()(
157 Subscripts const &subscripts) const &
158 {
159 return mData(subscripts);
160 }
161
162 constexpr decltype(auto) operator[](Index const index) &&
163 {
164 return move(mData)[index];
165 }
166
167 constexpr decltype(auto) operator[](Index const index) const &&
168 {
169 return move(mData)[index];
170 }
171
172 constexpr decltype(auto) operator[](Index const index) &
173 {
174 return mData[index];
175 }
176
177 constexpr decltype(auto) operator[](Index const index) const &
178 {
179 return mData[index];
180 }
181
182 template<class X, If<IsInvocable<Add, Tensor &, X>> = 0>
183 constexpr auto &operator+=(X &&rhs) &
184 {
185 return *this = *this + forward<X>(rhs);
186 }
187
188 template<class X, If<IsInvocable<Sub, Tensor &, X>> = 0>
189 constexpr auto &operator-=(X &&rhs) &
190 {
191 return *this = *this - forward<X>(rhs);
192 }
193
194 template<class X, If<IsInvocable<Mul, X, Tensor &>> = 0>
195 constexpr auto &applyOnTheLeft(X &&lhs) &
196 {
197 view(*this).applyOnTheLeft(forward<X>(lhs));
198 return *this;
199 }
200
201 template<class X, If<IsInvocable<Mul, Tensor &, X>> = 0>
202 constexpr auto &applyOnTheRight(X &&rhs) &
203 {
204 view(*this).applyOnTheRight(forward<X>(rhs));
205 return *this;
206 }
207
208 template<class X, If<IsInvocable<Mul, Tensor &, X>> = 0>
209 constexpr auto &operator*=(X &&rhs) &
210 {
211 view(*this) *= forward<X>(rhs);
212 return *this;
213 }
214
215 template<class X, If<IsInvocable<Div, Tensor &, X>> = 0>
216 constexpr auto &operator/=(X &&rhs) &
217 {
218 return *this = *this / forward<X>(rhs);
219 }
220
221 template<class E = IsSatisfied<(Dimension() == 0)>, If<E> = 0>
222 explicit constexpr operator T() const
223 {
224 return operator()();
225 }
226
227 private:
228 BEGIN_IGNORE_DIAGNOSTIC_GCC("-Wpedantic")
229 Array<T, Ns...> mData;
231 };
232}
233#endif // pRC_CORE_TENSOR_TENSOR_H
Float< W > NonComplex
Definition float.hpp:45
pRC::Constant< Size, W > Width
Definition float.hpp:39
Float< W > Value
Definition float.hpp:31
Complex< Float< W > > Complexify
Definition float.hpp:44
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
Definition single.hpp:13
Definition type_traits.hpp:32
Definition tensor.hpp:28
constexpr decltype(auto) operator()(Is const ... indices) &&
Definition tensor.hpp:114
constexpr decltype(auto) operator()(Is const ... indices) const &&
Definition tensor.hpp:121
static constexpr auto Single(X &&value, Is const ... indices)
Definition tensor.hpp:78
pRC::Subscripts< Ns... > Subscripts
Definition tensor.hpp:39
constexpr auto & applyOnTheLeft(X &&lhs) &
Definition tensor.hpp:195
constexpr auto & operator/=(X &&rhs) &
Definition tensor.hpp:216
constexpr Tensor(Tensor const &)=default
constexpr auto & operator*=(X &&rhs) &
Definition tensor.hpp:209
typename T::IsComplexified IsComplexified
Definition tensor.hpp:60
constexpr decltype(auto) operator()(Is const ... indices) &
Definition tensor.hpp:128
constexpr auto & applyOnTheRight(X &&rhs) &
Definition tensor.hpp:202
constexpr decltype(auto) operator()(Is const ... indices) const &
Definition tensor.hpp:135
typename T::Width Width
Definition tensor.hpp:56
constexpr Tensor(X &&other)
Definition tensor.hpp:99
constexpr auto & operator=(X &&rhs) &
Definition tensor.hpp:106
constexpr Tensor & operator=(Tensor const &) &=default
~Tensor()=default
static constexpr auto size(Index const dimension)
Definition tensor.hpp:70
typename Sizes::Dimension Dimension
Definition tensor.hpp:45
constexpr auto & operator+=(X &&rhs) &
Definition tensor.hpp:183
constexpr Tensor(Tensor &&)=default
typename T::Value Value
Definition tensor.hpp:47
constexpr Tensor()=default
static constexpr auto size()
Definition tensor.hpp:65
typename T::Signed Signed
Definition tensor.hpp:51
constexpr Tensor & operator=(Tensor &&) &=default
constexpr auto & operator-=(X &&rhs) &
Definition tensor.hpp:189
static constexpr auto Single(X &&value, Subscripts const &subscripts)
Definition tensor.hpp:84
pRC::Float<> T
Definition externs_nonTT.hpp:1
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
static constexpr X view(X &&a)
Definition view.hpp:12
std::enable_if_t< B{}, int > If
Definition type_traits.hpp:68
Constant< Bool, B > IsSatisfied
Definition type_traits.hpp:71
Tensor(TensorViews::View< T, Sizes< Ns... >, F > const &) -> Tensor< T, Ns... >
Conditional< IsSatisfied<((Ns *... *1) *sizeof(T) > cHugepageSizeByte)>, HeapArray< T, Ns... >, StackArray< T, Ns... > > Array
Definition type_traits.hpp:58
#define BEGIN_IGNORE_DIAGNOSTIC_GCC(warning)
Definition pragma.hpp:42
#define END_IGNORE_DIAGNOSTIC_GCC
Definition pragma.hpp:43
Definition type_traits.hpp:15
Definition limits.hpp:13