DUNE-ACFEM (unstable)

expressionoperations.hh
1#ifndef __DUNE_ACFEM_TENSORS_EXPRESSIONOPERATIONS_HH__
2#define __DUNE_ACFEM_TENSORS_EXPRESSIONOPERATIONS_HH__
3
6#include "../mpl/compare.hh"
7#include "../expressions/storage.hh"
8#include "../expressions/expressionoperations.hh"
9#include "operations/einsum.hh"
10#include "operations/product.hh"
11#include "operations/reshape.hh"
12#include "operations/restriction.hh"
13#include "operations/transpose.hh"
14
15namespace Dune {
16
17 namespace ACFem {
18
34 template<class T>
35 struct IsTensorOperation
36 : FalseType
37 {};
38
40
51 template<class Dims, class Pivot>
53 {};
54
55 template<class Dims, class Pivot>
56 struct IsTensorOperation<RestrictionOperation<Dims, Pivot> >
57 : TrueType
58 {};
59
60 template<class T>
61 struct IsRestrictionOperation
62 : FalseType
63 {};
64
65 template<class Dims, class Pivot>
66 struct IsRestrictionOperation<RestrictionOperation<Dims, Pivot> >
67 : TrueType
68 {};
69
70 template<class T, class SFINAE = void>
71 struct IsRestrictionExpression
72 : FalseType
73 {};
74
75 template<class T>
76 struct IsRestrictionExpression<
77 T,
78 std::enable_if_t<(IsExpressionOfArity<1, T>::value
79 && IsRestrictionOperation<Expressions::Operation<T> >::value
80 )> >
81 : TrueType
82 {};
83
84 template<class T, class SFINAE = void>
85 struct RestrictionTraits
86 {};
87
88 template<class Dims, class Pivot>
89 struct RestrictionTraits<RestrictionOperation<Dims, Pivot> >
90 {
91 using DefectPositions = Dims;
92 using PivotSequence = Pivot;
93 };
94
95 template<class Dims, class Pivot>
96 struct RestrictionTraits<OperationTraits<RestrictionOperation<Dims, Pivot> > >
97 : RestrictionTraits<RestrictionOperation<Dims, Pivot> >
98 {};
99
100 template<class T>
101 struct RestrictionTraits<T, std::enable_if_t<IsRestrictionExpression<T>::value> >
102 : RestrictionTraits<Expressions::Operation<T> >
103 {};
104
105 template<class T, class SFINAE = void>
106 struct IsConstRestrictionOperation
107 : FalseType
108 {};
109
110 template<class T>
111 struct IsConstRestrictionOperation<
112 T,
113 std::enable_if_t<(IsRestrictionOperation<T>::value
114 && (RestrictionTraits<T>::DefectPositions::size()
115 ==
116 RestrictionTraits<T>::PivotSequence::size())
117 )> >
118 : TrueType
119 {};
120
121 template<class T, class SFINAE = void>
122 struct IsConstRestrictionExpression
123 : FalseType
124 {};
125
126 template<class T>
127 struct IsConstRestrictionExpression<
128 T,
129 std::enable_if_t<(IsRestrictionExpression<T>::value
130 && IsConstRestrictionOperation<Expressions::Operation<T> >::value
131 )> >
132 : TrueType
133 {};
134
135 template<class T, class SFINAE = void>
136 struct IsDynamicRestrictionExpression
137 : FalseType
138 {};
139
140 template<class T>
141 struct IsDynamicRestrictionExpression<
142 T,
143 std::enable_if_t<(IsRestrictionExpression<T>::value
144 && !IsConstRestrictionOperation<Expressions::Operation<T> >::value
145 )> >
146 : TrueType
147 {};
148
149 template<class T>
150 struct IsDynamicOperation<
151 T,
152 std::enable_if_t<(IsRestrictionOperation<T>::value
153 && !IsConstRestrictionOperation<T>::value
154 && (RestrictionTraits<T>::DefectPositions::size() > 0)
155 )> >
156 : TrueType
157 {};
158
160
165 template<class Perm>
167 {
168 static_assert(isPermutation(Perm{}),
169 "Given sequence is not a permutation of 0, 1, ...");
170 };
171
172 template<class Perm>
173 struct IsTensorOperation<TransposeOperation<Perm> >
174 : TrueType
175 {};
176
177 template<class T>
178 struct IsTransposeOperation
179 : FalseType
180 {};
181
182 template<class Perm>
183 struct IsTransposeOperation<TransposeOperation<Perm> >
184 : TrueType
185 {};
186
187 template<class T, class SFINAE = void>
188 struct IsTransposeExpression
189 : FalseType
190 {};
191
192 template<class T>
193 struct IsTransposeExpression<
194 T,
195 std::enable_if_t<(IsExpression<T>::value
196 && Expressions::Arity<T>::value == 1
197 && IsTransposeOperation<Expressions::Operation<T> >::value
198 )> >
199 : TrueType
200 {};
201
202 template<class T, class SFINAE = void>
203 struct TransposeTraits
204 {
205 // using Permutation = void;
206 };
207
208 template<class Perm>
209 struct TransposeTraits<TransposeOperation<Perm> >
210 {
211 using Permutation = Perm;
212 };
213
214 template<class Perm>
215 struct TransposeTraits<OperationTraits<TransposeOperation<Perm> > >
216 : TransposeTraits<TransposeOperation<Perm> >
217 {};
218
219 template<class T>
220 struct TransposeTraits<T, std::enable_if_t<IsTransposeExpression<T>::value> >
221 : TransposeTraits<Expressions::Operation<T> >
222 {};
223
225
230 template<class Signature>
232 {};
233
234 template<class Signature>
235 struct IsTensorOperation<ReshapeOperation<Signature> >
236 : TrueType
237 {};
238
239 template<class T>
240 struct IsReshapeOperation
241 : FalseType
242 {};
243
244 template<class Sig>
245 struct IsReshapeOperation<ReshapeOperation<Sig> >
246 : TrueType
247 {};
248
249 template<class T, class SFINAE = void>
250 struct IsReshapeExpression
251 : FalseType
252 {};
253
254 template<class T>
255 struct IsReshapeExpression<T, std::enable_if_t<!IsDecay<T>::value> >
256 : IsReshapeExpression<std::decay_t<T> >
257 {};
258
259 template<class T>
260 struct IsReshapeExpression<
261 T,
262 std::enable_if_t<(IsDecay<T>::value
263 && IsExpression<T>::value
264 && Expressions::Arity<T>::value == 1
265 && IsReshapeOperation<Expressions::Operation<T> >::value
266 )> >
267 : TrueType
268 {};
269
270 template<class T, class SFINAE = void>
271 struct ReshapeTraits
272 {
273 // using Signature = void;
274 };
275
276 template<class Sig>
277 struct ReshapeTraits<ReshapeOperation<Sig> >
278 {
279 using Signature = Sig;
280 };
281
282 template<class Sig>
283 struct ReshapeTraits<OperationTraits<ReshapeOperation<Sig> > >
284 : ReshapeTraits<ReshapeOperation<Sig> >
285 {};
286
287 template<class T>
288 struct ReshapeTraits<T, std::enable_if_t<IsReshapeExpression<T>::value> >
289 : ReshapeTraits<Expressions::Operation<T> >
290 {};
291
293
306 template<class Pos1, class Pos2, class ContractDims>
308 {
309 static_assert(Pos1::size() == Pos2::size() && Pos2::size() == ContractDims::size(),
310 "Number of contraction indices and dimensions differs.");
311 };
312
313 template<class Pos1, class Pos2, class ContractDims>
314 struct IsTensorOperation<EinsumOperation<Pos1, Pos2, ContractDims> >
315 : TrueType
316 {};
317
318 template<class T>
319 struct IsEinsumOperation
320 : FalseType
321 {};
322
323 template<class Pos1, class Pos2, class ContractDims>
324 struct IsEinsumOperation<EinsumOperation<Pos1, Pos2, ContractDims> >
325 : TrueType
326 {};
327
328 template<class T>
329 constexpr inline bool HasEinsumFunctorV = IsEinsumOperation<typename T::FunctorType::OperationType>::value;
330
331 template<class T, class SFINAE = void>
332 struct IsEinsumExpression
333 : FalseType
334 {};
335
336 template<class T>
337 struct IsEinsumExpression<
338 T,
339 std::enable_if_t<(IsExpression<T>::value
340 && Expressions::Arity<T>::value == 2
341 && IsEinsumOperation<Expressions::Operation<T> >::value
342 )> >
343 : TrueType
344 {};
345
346 template<class T, class SFINAE = void>
347 struct EinsumTraits
348 {};
349
350 template<class Pos1, class Pos2, class Dims>
351 struct EinsumTraits<EinsumOperation<Pos1, Pos2, Dims> >
352 {
353 static constexpr std::size_t defectRank_ = Dims::size();
354 using LeftIndexPositions = Pos1;
355 using RightIndexPositions = Pos2;
356 using Dimensions = Dims;
357 };
358
359 template<class Pos1, class Pos2, class Dims>
360 struct EinsumTraits<OperationTraits<EinsumOperation<Pos1, Pos2, Dims> > >
361 : EinsumTraits<EinsumOperation<Pos1, Pos2, Dims> >
362 {};
363
364 template<class T>
365 struct EinsumTraits<T, std::enable_if_t<IsEinsumExpression<T>::value> >
366 : EinsumTraits<Expressions::Operation<T> >
367 {};
368
369 template<class Pos1, class Pos2, class Dims>
370 struct IsProductOperation<EinsumOperation<Pos1, Pos2, Dims> >
371 : TrueType
372 {};
373
375
387 template<class Pos1, class Pos2, class ProductDims>
389 {};
390
391 template<class Pos1, class Pos2, class ProductDims>
392 struct IsTensorOperation<TensorProductOperation<Pos1, Pos2, ProductDims> >
393 : TrueType
394 {};
395
396 template<class T>
397 struct IsTensorProductOperation
398 : FalseType
399 {};
400
401 template<class Pos1, class Pos2, class ContractDims>
402 struct IsTensorProductOperation<TensorProductOperation<Pos1, Pos2, ContractDims> >
403 : TrueType
404 {};
405
406 template<class T, class SFINAE = void>
407 struct IsTensorProductExpression
408 : FalseType
409 {};
410
411 template<class T>
412 struct IsTensorProductExpression<
413 T,
414 std::enable_if_t<(IsExpression<T>::value
415 && Expressions::Arity<T>::value == 2
416 && IsTensorProductOperation<Expressions::Operation<T> >::value
417 )> >
418 : TrueType
419 {};
420
421 template<class T>
422 struct TensorProductTraits
423 {};
424
425 template<class Pos1, class Pos2, class Dims>
426 struct TensorProductTraits<TensorProductOperation<Pos1, Pos2, Dims> >
427 {
428 static constexpr std::size_t defectRank_ = Dims::size();
429 using LeftIndexPositions = Pos1;
430 using RightIndexPositions = Pos2;
431 using Dimensions = Dims;
432 };
433
434 template<class Pos1, class Pos2, class Dims>
435 struct TensorProductTraits<OperationTraits<TensorProductOperation<Pos1, Pos2, Dims> > >
436 : TensorProductTraits<TensorProductOperation<Pos1, Pos2, Dims> >
437 {};
438
439 template<class Pos1, class Pos2, class Dims>
440 struct IsProductOperation<TensorProductOperation<Pos1, Pos2, Dims> >
441 : TrueType
442 {};
443
445
446 namespace Tensor {
447
448 template<class T, class SFINAE = void>
449 struct IsComponentWiseOperation
450 : TrueType
451 {};
452
456 template<class T>
457 struct IsComponentWiseOperation<
458 T,
459 std::enable_if_t<(IsReshapeOperation<T>::value
460 || IsTransposeOperation<T>::value
461 || IsRestrictionOperation<T>::value
462 || IsProductOperation<T>::value
463 )> >
464 : FalseType
465 {};
466
467 template<class T, class SFINAE = void>
468 struct IsProductWithScalarExpression
469 : FalseType
470 {};
471
472 template<class T>
473 struct IsProductWithScalarExpression<
474 T,
475 std::enable_if_t<(IsProductExpression<T>::value
476 && (TensorTraits<Operand<0, T> >::rank == 0
477 ||
478 TensorTraits<Operand<1, T> >::rank == 0)
479 )> >
480 : TrueType
481 {};
482
483 template<class T, class SFINAE = void>
484 struct IsComponentWiseExpression
485 : IsComponentWiseOperation<Expressions::Operation<T> >
486 {};
487
488 template<class T>
489 struct IsComponentWiseExpression<T, std::enable_if_t<IsProductWithScalarExpression<T>::value> >
490 : TrueType
491 {};
492
493 }
494
495 template<class Operation>
496 struct IsDistributiveOperation<
497 Operation,
498 std::enable_if_t<(IsTransposeOperation<Operation>::value
499 || IsRestrictionOperation<Operation>::value
500 )> >
501 : TrueType
502 {};
503
505
507
508 } // NS ACFem
509
510} // NS Dune
511
512#endif // __DUNE_ACFEM_TENSORS_EXPRESSIONOPERATIONS_HH__
constexpr std::size_t size()
Gives the number of elements in tuple-likes and std::integer_sequence.
Definition: size.hh:73
constexpr bool isPermutation(IndexSequence< Ind... >=IndexSequence< Ind... >{})
Definition: compare.hh:362
BoolConstant< false > FalseType
Alias for std::false_type.
Definition: types.hh:110
BoolConstant< true > TrueType
Alias for std::true_type.
Definition: types.hh:107
STL namespace.
Einstein summation, i.e.
Definition: expressionoperations.hh:308
Signature of index positions of tensors.
Definition: expressionoperations.hh:232
AutoDiff operation.
Definition: expressionoperations.hh:53
Component-wise product over given index-set.
Definition: expressionoperations.hh:389
Permutation of index positions of tensors.
Definition: expressionoperations.hh:167
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.111.3 (Jul 15, 22:36, 2024)