DUNE-ACFEM (unstable)

eye.hh
1 #ifndef __DUNE_ACFEM_TENSORS_MODULES_EYE_HH__
2 #define __DUNE_ACFEM_TENSORS_MODULES_EYE_HH__
3 
4 #include "../tensorbase.hh"
5 #include "../expressiontraits.hh"
6 #include "constant.hh"
7 
8 #ifndef SPECIALIZEDTRAITS_DEFINED
9 # error
10 #endif
11 
12 namespace Dune {
13 
14  namespace ACFem {
15 
16  namespace Tensor {
17 
26  template<class Seq, class Field = IntFraction<1> >
27  class Eye;
28 
29  template<class Seq, class Field>
30  struct HasSpecializedExpressionTraits<Eye<Seq, Field> >
31  : TrueType
32  {};
33 
34  template<class, class = void>
35  struct IsEye
36  : FalseType
37  {};
38 
39  template<class T>
40  struct IsEye<T, std::enable_if_t<!IsDecay<T>::value> >
41  : IsEye<std::decay_t<T> >
42  {};
43 
44  template<class Seq, class Field>
45  struct IsEye<Eye<Seq, Field> >
46  : TrueType
47  {};
48 
52  template<class Field, std::size_t... Dimensions>
53  class Eye<Seq<Dimensions...>, Field>
54  : public TensorBase<FloatingPointClosure<Field>, Seq<Dimensions...>, Eye<Seq<Dimensions...>, Field> >
55  , public Expressions::SelfExpression<Eye<Seq<Dimensions...>, Field> >
56  , public MPL::UniqueTags<ConstantExpression,
57  ConditionalType<IsTypedValue<Field>::value, TypedValueExpression, void> >
58  {
59  using BaseType = TensorBase<FloatingPointClosure<Field>, Seq<Dimensions...>, Eye<Seq<Dimensions...>, Field> >;
60  public:
61  using BaseType::rank;
62  using typename BaseType::Signature;
63  using typename BaseType::FieldType;
64  using ValueType = Field;
65 
66  static_assert(IsTypedValue<ValueType>::value,
67  "Eye tensors must not be build with dynamic values.");
68 
72  template<class... Dims,
73  std::enable_if_t<(sizeof...(Dims) == rank
74  &&
76  , int> = 0>
77  FieldType operator()(Dims... indices) const
78  {
79  if (!isConstant(std::forward_as_tuple(indices...))) {
80  return FieldType(0);
81  } else if (IsTypedValue<ValueType>::value) {
82  return FieldType(ValueType{});
83  } else {
84  return FieldType(1);
85  }
86  }
87 
88  template<std::size_t... Indices, class Pos = MakeIndexSequence<sizeof...(Indices)> >
89  static bool constexpr isZero(Seq<Indices...> = Seq<Indices...>{}, Pos = Pos{})
90  {
91  return !(isConstant(Seq<Indices...>{}));
92  }
93 
94  template<std::size_t... Indices, std::enable_if_t<sizeof...(Indices) == rank, int> = 0>
95  decltype(auto) operator()(Seq<Indices...> arg) const
96  {
97  using ReturnType = ConditionalType<isZero<Indices...>(),
98  IntFraction<0>,
99  ConditionalType<IsTypedValue<ValueType>::value,
100  ValueType,
101  IntFraction<1> > >;
102  return ReturnType{};
103  }
104 
105  static constexpr ValueType value()
106  {
107  return ValueType{};
108  }
109 
110  std::string name() const
111  {
112  if (!ExpressionTraits<ValueType>::isOne) {
113  return "eye<"+toString(ValueType{}) + "@" + toString(Signature{})+">";
114  } else {
115  return "eye<"+toString(Signature{})+">";
116  }
117  }
118 
119  };
120 
121  template<std::size_t... Dimensions, class F = Expressions::Closure>
122  auto eye(Seq<Dimensions...>, F closure = F{})
123  {
124  if constexpr (sizeof...(Dimensions) > 1) {
125  return closure(Eye<Seq<Dimensions...>, IntFraction<1> >{});
126  } else {
127  return constantTensor(1_f, Seq<Dimensions...>{}, F{});
128  }
129  }
130 
131  template<std::size_t... Dimensions, class F = Expressions::Closure,
132  std::enable_if_t<!IsTensorOperand<F>::value, int> = 0>
133  auto eye(F closure = F{})
134  {
135  if constexpr (sizeof...(Dimensions) > 1) {
136  return closure(Eye<Seq<Dimensions...>, IntFraction<1> >{});
137  } else {
138  return constantTensor(1_f, Seq<Dimensions...>{}, F{});
139  }
140  }
141 
142  template<class T, class F = Expressions::Closure,
143  std::enable_if_t<IsTensorOperand<T>::value, int> = 0>
144  auto eye(T&&, F = F{})
145  {
146  return eye(typename TensorTraits<T>::Signature{}, F{});
147  }
148 
150 
152 
153  } // NS Tensor
154 
155  template<class Seq, class Field>
156  struct ExpressionTraits<Tensor::Eye<Seq, Field> >
157  : ExpressionTraits<Field>
158  {
159  private:
160  using BaseType = ExpressionTraits<Field>;
161  public:
162  using ExpressionType = Tensor::Eye<Seq, Field>;
163  static constexpr bool isOne = BaseType::isOne && ExpressionType::rank == 0;
164  static constexpr bool isMinusOne = BaseType::isMinusOne && ExpressionType::rank == 0;
165  static constexpr bool isNonZero = BaseType::isNonZero && ExpressionType::rank == 0;
166  static constexpr bool isPositive = BaseType::isPositive && ExpressionType::rank == 0;
167  static constexpr bool isNegative = BaseType::isNegative && ExpressionType::rank == 0;
168  using Sign = ExpressionSign<isNonZero, BaseType::isSemiPositive, BaseType::isSemiNegative>;
169  };
170 
171  } // NS ACFem
172 
173  template<class Field, class Signature>
174  struct FieldTraits<ACFem::Tensor::Eye<Signature, Field> >
175  : FieldTraits<std::decay_t<Field> >
176  {};
177 
178 } // NS Dune
179 
180 #endif // __DUNE_ACFEM_TENSORS_MODULES_EYE_HH__
FieldType operator()(Dims... indices) const
Insert the current view-indices at their proper positions and foward to the underlying "host" tensor.
Definition: eye.hh:77
decltype(operate(std::declval< OptOrF >(), std::declval< Rest >()...)) ExpressionType
Generate the type of an expression by calling operate().
Definition: optimizationbase.hh:256
BoolConstant< ExpressionTraits< T >::isTypedValue > IsTypedValue
Compile-time true if T is a "typed value", e.g. a std::integral_constant.
Definition: expressiontraits.hh:90
MakeSequence< std::size_t, N, Offset, Stride, Repeat > MakeIndexSequence
Make a sequence of std::size_t elements.
Definition: generators.hh:34
constexpr bool isConstant(Sequence< T, T0, Ts... >)
Definition: compare.hh:285
decltype(isIntegralPack(std::declval< T >()...)) IsIntegralPack
Decide whether the given parameter pack contains only integral types.
Definition: compare.hh:377
BoolConstant< false > FalseType
Alias for std::false_type.
Definition: types.hh:110
BoolConstant< true > TrueType
Alias for std::true_type.
Definition: types.hh:107
constexpr bool isZero
Shortcut identifying a zero model.
Definition: modeltraits.hh:642
Terminals may derive from this class to express that they are expressions.
Definition: terminal.hh:25
Base class for all tensors.
Definition: tensorbase.hh:144
Creative Commons License   |  Legal Statements / Impressum  |  Hosted by TU Dresden  |  generated with Hugo v0.80.0 (May 15, 22:30, 2024)