TiledArray  0.7.0
tsr_expr.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  * Justus Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * tsr_expr.h
22  * Apr 1, 2014
23  *
24  */
25 
26 #ifndef TILEDARRAY_EXPRESSIONS_TSR_EXPR_H__INCLUDED
27 #define TILEDARRAY_EXPRESSIONS_TSR_EXPR_H__INCLUDED
28 
35 
36 namespace TiledArray {
37  namespace expressions {
38 
41 
42  template <typename E>
43  struct is_aliased : public std::true_type { };
44 
45  template <typename Array, bool Alias>
46  struct is_aliased<TsrExpr<Array, Alias> > :
47  public std::integral_constant<bool, Alias> { };
48 
49  template <typename Array, bool Alias>
50  struct ExprTrait<TsrExpr<Array, Alias> > {
51  typedef Array array_type;
58  };
59 
60  template <typename Array>
61  struct ExprTrait<TsrExpr<const Array, true> > {
62  typedef Array array_type;
69  };
70 
71 
72  // This is here to catch errors in expression types. It should not be
73  // possible to construct this type.
74  template <typename Array>
75  struct ExprTrait<TsrExpr<const Array, false> >; // <----- This should never happen!
76 
78 
82  template <typename Array, bool Alias>
83  class TsrExpr : public Expr<TsrExpr<Array, Alias> > {
84  public:
87  typedef typename ExprTrait<TsrExpr_>::array_type
89  typedef typename ExprTrait<TsrExpr_>::engine_type
91 
92  private:
93 
94  array_type& array_;
95  std::string vars_;
96 
97  public:
98 
99  // Compiler generated functions
100  TsrExpr() = default;
101  TsrExpr(const TsrExpr_&) = default;
102  TsrExpr(TsrExpr_&&) = default;
103  ~TsrExpr() = default;
104 
106 
109  TsrExpr(array_type& array, const std::string& vars) :
110  array_(array), vars_(vars)
111  { }
112 
114 
117  other.eval_to(*this);
118  return array_;
119  }
120 
122 
125  template <typename D>
126  array_type& operator=(const Expr<D>& other) {
128  "no_alias() expressions are not allowed on the right-hand side of "
129  "the assignment operator.");
130  other.derived().eval_to(*this);
131  return array_;
132  }
133 
135 
138  template <typename D>
139  array_type& operator+=(const Expr<D>& other) {
141  "no_alias() expressions are not allowed on the right-hand side of "
142  "the assignment operator.");
143  return operator=(AddExpr<TsrExpr_, D>(*this, other.derived()));
144  }
145 
147 
150  template <typename D>
151  array_type& operator-=(const Expr<D>& other) {
153  "no_alias() expressions are not allowed on the right-hand side of "
154  "the assignment operator.");
155  return operator=(SubtExpr<TsrExpr_, D>(*this, other.derived()));
156  }
157 
159 
162  template <typename D>
163  array_type& operator*=(const Expr<D>& other) {
165  "no_alias() expressions are not allowed on the right-hand side of "
166  "the assignment operator.");
167  return operator=(MultExpr<TsrExpr_, D>(*this, other.derived()));
168  }
169 
171 
173  array_type& array() const { return array_; }
174 
176 
179  no_alias() const {
180  return TsrExpr<Array, false>(array_, vars_);
181  }
182 
184 
188  template <typename Index>
190  block(const Index& lower_bound, const Index& upper_bound) const {
191  return BlkTsrExpr<const Array, Alias>(*this, lower_bound,
192  upper_bound);
193  }
194 
196 
200  block(const std::initializer_list<std::size_t>& lower_bound,
201  const std::initializer_list<std::size_t>& upper_bound) const {
202  return BlkTsrExpr<const Array, Alias>(*this, lower_bound,
203  upper_bound);
204  }
205 
207 
211  template <typename Index>
213  block(const Index& lower_bound, const Index& upper_bound) {
214  return BlkTsrExpr<Array, Alias>(array_, vars_, lower_bound,
215  upper_bound);
216  }
217 
219 
223  block(const std::initializer_list<std::size_t>& lower_bound,
224  const std::initializer_list<std::size_t>& upper_bound) {
225  return BlkTsrExpr<Array, Alias>(array_, vars_, lower_bound,
226  upper_bound);
227  }
228 
230 
233  return ConjTsrExpr<Array>(array_, vars_, conj_op());
234  }
235 
237 
239  const std::string& vars() const { return vars_; }
240 
241  }; // class TsrExpr
242 
243 
245 
247  template <typename Array>
248  class TsrExpr<const Array, true> :
249  public Expr<TsrExpr<const Array, true> >
250  {
251  public:
256 
257  private:
258 
259  const array_type& array_;
260  std::string vars_;
261 
262  // Not allowed
264 
265  public:
266 
267  // Compiler generated functions
268  TsrExpr(const TsrExpr_&) = default;
269  TsrExpr(TsrExpr_&&) = default;
270  ~TsrExpr() = default;
271  TsrExpr_& operator=(const TsrExpr_&) = delete;
272  TsrExpr_& operator=(TsrExpr_&&) = delete;
273 
275 
278  TsrExpr(const array_type& array, const std::string& vars) :
279  Expr_(), array_(array), vars_(vars)
280  { }
281 
283 
285  const array_type& array() const { return array_; }
286 
288 
292  template <typename Index>
294  block(const Index& lower_bound, const Index& upper_bound) const {
295  return BlkTsrExpr<const Array, true>(array_, vars_, lower_bound,
296  upper_bound);
297  }
298 
300 
304  template <typename Index>
306  block(const std::initializer_list<Index>& lower_bound,
307  const std::initializer_list<Index>& upper_bound) const {
308  return BlkTsrExpr<const Array, true>(array_, vars_, lower_bound,
309  upper_bound);
310  }
311 
313 
316  return ConjTsrExpr<Array>(array_, vars_, conj_op());
317  }
318 
319 
321 
323  const std::string& vars() const { return vars_; }
324 
325  }; // class TsrExpr<const A>
326 
327  } // namespace expressions
328 } // namespace TiledArray
329 
330 #endif // TILEDARRAY_EXPRESSIONS_TSR_EXPR_H__INCLUDED
TiledArray::detail::numeric_t< Array > numeric_type
Array base numeric type.
Definition: tsr_expr.h:53
ConjTsrExpr< Array > conj() const
Conjugated-tensor expression factor.
Definition: tsr_expr.h:232
TsrExpr< Array, Alias > TsrExpr_
This class type.
Definition: tsr_expr.h:85
array_type & operator=(const Expr< D > &other)
Expression assignment operator.
Definition: tsr_expr.h:126
Expression wrapper for scaled array objects.
const std::string & vars() const
Tensor variable string accessor.
Definition: tsr_expr.h:239
TsrEngine< Array, typename Array::eval_type, Alias > engine_type
Expression engine type.
Definition: tsr_expr.h:57
Addition expression.
Definition: add_engine.h:37
TiledArray::detail::scalar_t< Array > scalar_type
Array base scalar type.
Definition: tsr_expr.h:66
Multiplication expression.
Definition: cont_engine.h:38
typename TiledArray::detail::numeric_type< T >::type numeric_t
Definition: type_traits.h:525
const std::string & vars() const
Tensor variable string accessor.
Definition: tsr_expr.h:323
Expression wrapper for array objects.
Definition: dist_array.h:39
TsrExpr< const Array, true > TsrExpr_
This class type.
Definition: tsr_expr.h:252
array_type & operator-=(const Expr< D > &other)
Expression minus-assignment operator.
Definition: tsr_expr.h:151
ExprTrait< TsrExpr_ >::engine_type engine_type
Expression engine type.
Definition: tsr_expr.h:90
BlkTsrExpr< Array, Alias > block(const Index &lower_bound, const Index &upper_bound)
Block expression.
Definition: tsr_expr.h:213
BlkTsrExpr< const Array, true > block(const Index &lower_bound, const Index &upper_bound) const
Block expression.
Definition: tsr_expr.h:294
const array_type & array() const
Array accessor.
Definition: tsr_expr.h:285
Expr< TsrExpr_ > Expr_
Expression base type.
Definition: tsr_expr.h:253
Forward declarations.
Definition: clone.h:32
BlkTsrExpr< const Array, true > block(const std::initializer_list< Index > &lower_bound, const std::initializer_list< Index > &upper_bound) const
Block expression.
Definition: tsr_expr.h:306
typename TiledArray::detail::scalar_type< T >::type scalar_t
Definition: type_traits.h:555
BlkTsrExpr< Array, Alias > block(const std::initializer_list< std::size_t > &lower_bound, const std::initializer_list< std::size_t > &upper_bound)
Block expression.
Definition: tsr_expr.h:223
ExprTrait< TsrExpr_ >::engine_type engine_type
Expression engine type.
Definition: tsr_expr.h:255
Base class for expression evaluation.
Definition: expr.h:81
derived_type & derived()
Cast this object to it&#39;s derived type.
Definition: expr.h:273
void eval_to(TsrExpr< A, Alias > &tsr) const
Evaluate this object and assign it to tsr.
Definition: expr.h:287
TsrEngine< Array, typename Array::eval_type, true > engine_type
Expression engine type.
Definition: tsr_expr.h:68
TiledArray::detail::numeric_t< Array > numeric_type
Array base numeric type.
Definition: tsr_expr.h:64
ExprTrait< TsrExpr_ >::array_type array_type
The array type.
Definition: tsr_expr.h:254
array_type & operator=(TsrExpr_ &other)
Expression assignment operator.
Definition: tsr_expr.h:116
Expression wrapper for const array objects.
Definition: tsr_expr.h:248
Subtraction expression.
Definition: subt_engine.h:37
ComplexConjugate< S > conj_op(const S factor)
ComplexConjugate operator factory function.
Definition: complex.h:181
array_type & operator*=(const Expr< D > &other)
Expression multiply-assignment operator.
Definition: tsr_expr.h:163
ExprTrait< TsrExpr_ >::array_type array_type
The array type.
Definition: tsr_expr.h:88
ConjTsrExpr< Array > conj() const
Conjugated-tensor expression factor.
Definition: tsr_expr.h:315
array_type & array() const
Array accessor.
Definition: tsr_expr.h:173
Tensor expression engine.
Definition: tsr_engine.h:42
TsrExpr(const array_type &array, const std::string &vars)
Constructor.
Definition: tsr_expr.h:278
TsrExpr(array_type &array, const std::string &vars)
Constructor.
Definition: tsr_expr.h:109
array_type & operator+=(const Expr< D > &other)
Expression plus-assignment operator.
Definition: tsr_expr.h:139
TiledArray::detail::scalar_t< Array > scalar_type
Array base scalar type.
Definition: tsr_expr.h:55
BlkTsrExpr< const Array, Alias > block(const std::initializer_list< std::size_t > &lower_bound, const std::initializer_list< std::size_t > &upper_bound) const
Block expression.
Definition: tsr_expr.h:200
BlkTsrExpr< const Array, Alias > block(const Index &lower_bound, const Index &upper_bound) const
Block expression.
Definition: tsr_expr.h:190
TsrExpr< Array, false > no_alias() const
Flag this tensor expression for a non-aliasing assignment.
Definition: tsr_expr.h:179
Expr< TsrExpr_ > Expr_
Base class type.
Definition: tsr_expr.h:86