v0.14.0
Tensor2_Expr.hpp
Go to the documentation of this file.
1 /* Declares a wrapper class for rank 2 Tensor expressions. I
2  specialize it for when I wrap a simple Tensor2 or Tensor2_ptr so
3  that it has a reference to the Tensor2(_ptr) and not a copy.
4  Otherwise assignment wouldn't work. */
5 
6 #pragma once
7 
12 #include "Tensor2_or_Tensor2.hpp"
13 #include "Tensor2_plus_Tensor2.hpp"
17 #include "Tensor2_transform.hpp"
18 #include "conj_Tensor2.hpp"
19 #include "minus_Tensor2.hpp"
20 
21 #include "../permute.hpp"
22 
23 namespace FTensor
24 {
25  template <class A, class T, int Dim0, int Dim1, char i, char j>
27  {
29 
30  public:
31  Tensor2_Expr(const A &a) : iter(a) {}
32  T operator()(const int N1, const int N2) const { return iter(N1, N2); }
33  };
34 
35  template <class A, class T, int Tensor_Dim0, int Tensor_Dim1, int Dim0,
36  int Dim1, char i, char j>
37  class Tensor2_Expr<Tensor2<A, Tensor_Dim0, Tensor_Dim1>, T, Dim0, Dim1, i, j>
38  {
40 
41  public:
43  T &operator()(const int N1, const int N2) { return iter(N1, N2); }
44  T operator()(const int N1, const int N2) const { return iter(N1, N2); }
45  T* ptr(const int N1, const int N2) const { return iter.ptr(N1, N2); }
46 
47  /* Various assignment operators. I have to explicitly declare the
48  second operator= because otherwise the compiler will generate its
49  own and not use the template code. */
50 
51  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
53  {
54  for(int ii = 0; ii < Dim0; ++ii)
55  for(int jj = 0; jj < Dim1; ++jj)
56  {
57  iter(ii, jj) = permute(*this, rhs, ii, jj);
58  }
59  return *this;
60  }
61 
62  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
63  auto &
65  {
66  return equals(rhs);
67  }
68 
70  Dim0, Dim1, i, j> &rhs)
71  {
72  return equals(rhs);
73  }
74 
75  template <class B, class U, int Dim, char i_1, char j_1>
77  for (int ii = 0; ii != Dim; ++ii)
78  for (int jj = 0; jj != Dim; ++jj) {
79  iter(ii, jj) = rhs(ii, jj);
80  }
81  return *this;
82  }
83 
84  template <class B, class U, int Dim, char i_1, char j_1>
86  return equals(rhs);
87  }
88 
89  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
91  {
92  for(int ii = 0; ii < Dim0; ++ii)
93  for(int jj = 0; jj < Dim1; ++jj)
94  {
95  iter(ii, jj) += permute(*this, rhs, ii, jj);
96  }
97  return *this;
98  }
99 
100  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
102  {
103  for(int ii = 0; ii < Dim0; ++ii)
104  for(int jj = 0; jj < Dim1; ++jj)
105  {
106  iter(ii, jj) -= permute(*this, rhs, ii, jj);
107  }
108  return *this;
109  }
110 
111  template <class B, class U, int Dim, char i_1, char j_1>
113  for (int ii = 0; ii != Dim; ++ii)
114  for (int jj = 0; jj != Dim; ++jj) {
115  iter(ii, jj) += rhs(ii, jj);
116  }
117  return *this;
118  }
119 
120  template <class B, class U, int Dim, char i_1, char j_1>
122  for (int ii = 0; ii != Dim; ++ii)
123  for (int jj = 0; jj != Dim; ++jj) {
124  iter(ii, jj) -= rhs(ii, jj);
125  }
126  return *this;
127  }
128 
129  /* This is for int's, double's, etc. */
130 
131  template <class U> auto &operator=(const U &u)
132  {
133  for(int ii = 0; ii < Dim0; ++ii)
134  for(int jj = 0; jj < Dim1; ++jj)
135  {
136  iter(ii, jj) = u;
137  }
138  return *this;
139  }
140 
141  template <class U> auto &operator+=(const U &u)
142  {
143  for(int ii = 0; ii < Dim0; ++ii)
144  for(int jj = 0; jj < Dim1; ++jj)
145  {
146  iter(ii, jj) += u;
147  }
148  return *this;
149  }
150 
151  template <class U> auto &operator-=(const U &u)
152  {
153  for(int ii = 0; ii < Dim0; ++ii)
154  for(int jj = 0; jj < Dim1; ++jj)
155  {
156  iter(ii, jj) -= u;
157  }
158  return *this;
159  }
160 
161  template <class U> auto &operator*=(const U &u)
162  {
163  for(int ii = 0; ii < Dim0; ++ii)
164  for(int jj = 0; jj < Dim1; ++jj)
165  {
166  iter(ii, jj) *= u;
167  }
168  return *this;
169  }
170 
171  template <class U> auto &operator/=(const U &u)
172  {
173  for(int ii = 0; ii < Dim0; ++ii)
174  for(int jj = 0; jj < Dim1; ++jj)
175  {
176  iter(ii, jj) /= u;
177  }
178  return *this;
179  }
180 
181  /* ADOL-C */
182 
183  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
185  {
186  for(int ii = 0; ii < Dim0; ++ii)
187  for(int jj = 0; jj < Dim1; ++jj)
188  {
189  iter(ii, jj) <<= permute(*this, rhs, ii, jj);
190  }
191  return *this;
192  }
193 
194  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
196  {
197  for(int ii = 0; ii < Dim0; ++ii)
198  for(int jj = 0; jj < Dim1; ++jj)
199  {
200  iter(ii, jj) >>= permute_ref(*this, rhs, ii, jj);
201  }
202  return *this;
203  }
204 
205  };
206 
207  /* Specialized for Dg_number_rhs_0 (Dg with the
208  first or second index explicitly given). */
209 
210  template <class A, class T, int Dim0, int Dim1, char i, char j, int N>
211  class Tensor2_Expr<Dg_number_rhs_0<A, T, N>, T, Dim0, Dim1, i, j>
212  {
213  A &iter;
214 
215  public:
217  T &operator()(const int N1, const int N2) { return iter(N, N1, N2); }
218  T operator()(const int N1, const int N2) const { return iter(N, N1, N2); }
219 
220  /* Various assignment operators. I have to explicitly declare the
221  second operator= because otherwise the compiler will generate its
222  own and not use the template code. */
223 
224  template <class B, class U, int Dim1_0, int Dim1_1, char i_1, char j_1>
226  {
227  for(int ii = 0; ii < Dim0; ++ii)
228  for(int jj = 0; jj < Dim1; ++jj)
229  {
230  iter(ii, jj) = permute(*this, rhs, ii, jj);
231  }
232  return *this;
233  }
234 
235  auto &operator=(
236  const Tensor2_Expr<Dg_number_rhs_0<A, T, N>, T, Dim0, Dim1, i, j> &result)
237  {
238  return operator=<Dg_number_rhs_0<A, T, N>, T>(result);
239  }
240 
241  };
242 }
FTensor::Tensor2_Expr::iter
A iter
Definition: Tensor2_Expr.hpp:28
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator=
auto & operator=(const Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j > &rhs)
Definition: Tensor2_Expr.hpp:69
FTensor
JSON compatible output.
Definition: Christof_constructor.hpp:6
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator=
auto & operator=(const FTensor::Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:64
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator*=
auto & operator*=(const U &u)
Definition: Tensor2_Expr.hpp:161
FTensor::permute_ref
U & permute_ref(const Tensor2_Expr< A, T, Dim0_0, Dim0_1, i0, j0 > &, const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i1, j1 > &rhs, const int N0, const int N1)
Definition: permute.hpp:66
FTensor::Tensor2_symmetric_Expr
Definition: Tensor2_symmetric_Expr.hpp:36
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator-=
auto & operator-=(const U &u)
Definition: Tensor2_Expr.hpp:151
FTensor::Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j >::iter
A & iter
Definition: Tensor2_Expr.hpp:213
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::ptr
T * ptr(const int N1, const int N2) const
Definition: Tensor2_Expr.hpp:45
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::iter
Tensor2< A, Tensor_Dim0, Tensor_Dim1 > & iter
Definition: Tensor2_Expr.hpp:39
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator-=
auto & operator-=(const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:101
Tensor2_times_Tensor2.hpp
FTensor::Tensor2_Expr
Definition: Tensor2_Expr.hpp:26
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::equals
auto & equals(const Tensor2_symmetric_Expr< B, U, Dim, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:76
FTensor::Tensor2_Expr::Tensor2_Expr
Tensor2_Expr(const A &a)
Definition: Tensor2_Expr.hpp:31
A
constexpr AssemblyType A
Definition: operators_tests.cpp:30
Tensor2_carat_Tensor2.hpp
FTensor::permute
U permute(const Tensor2_Expr< A, T, Dim0_0, Dim0_1, i0, j0 > &, const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i1, j1 > &rhs, const int N0, const int N1)
Definition: permute.hpp:11
FTensor::Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j >::operator()
T & operator()(const int N1, const int N2)
Definition: Tensor2_Expr.hpp:217
FTensor::Tensor2
Definition: Tensor2_value.hpp:16
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator()
T operator()(const int N1, const int N2) const
Definition: Tensor2_Expr.hpp:44
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator+=
auto & operator+=(const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:90
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator<<=
auto & operator<<=(const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:184
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator+=
auto & operator+=(const U &u)
Definition: Tensor2_Expr.hpp:141
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator=
auto & operator=(const Tensor2_symmetric_Expr< B, U, Dim, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:85
Tensor2_or_Tensor2.hpp
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator>>=
auto & operator>>=(const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:195
a
constexpr double a
Definition: approx_sphere.cpp:30
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator/=
auto & operator/=(const U &u)
Definition: Tensor2_Expr.hpp:171
FTensor::Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j >::Tensor2_Expr
Tensor2_Expr(A &a)
Definition: Tensor2_Expr.hpp:216
Tensor2_and_Tensor1.hpp
Tensor2_times_Tensor1.hpp
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::equals
auto & equals(const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:52
minus_Tensor2.hpp
i
FTensor::Index< 'i', SPACE_DIM > i
Definition: hcurl_divergence_operator_2d.cpp:27
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator-=
auto & operator-=(const Tensor2_symmetric_Expr< B, U, Dim, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:121
N
const int N
Definition: speed_test.cpp:3
Tensor2_transform.hpp
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator+=
auto & operator+=(const Tensor2_symmetric_Expr< B, U, Dim, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:112
FTensor::Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j >::operator=
auto & operator=(const Tensor2_Expr< B, U, Dim1_0, Dim1_1, i_1, j_1 > &rhs)
Definition: Tensor2_Expr.hpp:225
Tensor2_plus_Tensor2.hpp
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator()
T & operator()(const int N1, const int N2)
Definition: Tensor2_Expr.hpp:43
conj_Tensor2.hpp
Tensor2_times_generic.hpp
Tensor2_divide_generic.hpp
FTensor::Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j >::operator()
T operator()(const int N1, const int N2) const
Definition: Tensor2_Expr.hpp:218
j
FTensor::Index< 'j', 3 > j
Definition: matrix_function.cpp:19
FTensor::Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j >::operator=
auto & operator=(const Tensor2_Expr< Dg_number_rhs_0< A, T, N >, T, Dim0, Dim1, i, j > &result)
Definition: Tensor2_Expr.hpp:235
FTensor::Tensor2_Expr::operator()
T operator()(const int N1, const int N2) const
Definition: Tensor2_Expr.hpp:32
FTensor::Dg_number_rhs_0
Definition: Dg_number.hpp:20
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::Tensor2_Expr
Tensor2_Expr(Tensor2< A, Tensor_Dim0, Tensor_Dim1 > &a)
Definition: Tensor2_Expr.hpp:42
EshelbianPlasticity::U
@ U
Definition: EshelbianContact.cpp:197
Tensor2_minus_Tensor2.hpp
FTensor::Tensor2_Expr< Tensor2< A, Tensor_Dim0, Tensor_Dim1 >, T, Dim0, Dim1, i, j >::operator=
auto & operator=(const U &u)
Definition: Tensor2_Expr.hpp:131