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 // This is here to catch errors in expression types. It should not be
72 // possible to construct this type.
73 template <typename Array>
74 struct ExprTrait<TsrExpr<const Array, false>>; // <----- This should never
75  // happen!
76 
78 
82 template <typename Array, bool Alias>
83 class TsrExpr : public Expr<TsrExpr<Array, Alias>> {
84  public:
87  typedef
89  typedef typename ExprTrait<TsrExpr_>::engine_type
91  using index1_type = TA_1INDEX_TYPE;
92 
93  private:
94  array_type& array_;
95  std::string annotation_;
96 
97  public:
98  // Compiler generated functions
99  TsrExpr() = default;
100  TsrExpr(const TsrExpr_&) = default;
101  TsrExpr(TsrExpr_&&) = default;
102  ~TsrExpr() = default;
103 
105 
108  TsrExpr(array_type& array, const std::string& annotation)
109  : array_(array), annotation_(annotation) {}
110 
112 
115  other.eval_to(*this);
116  return array_;
117  }
118 
120 
123  template <typename D>
124  array_type& operator=(const Expr<D>& other) {
125  static_assert(
127  "no_alias() expressions are not allowed on the right-hand side of "
128  "the assignment operator.");
129  other.derived().eval_to(*this);
130  return array_;
131  }
132 
134 
137  template <typename D>
138  array_type& operator+=(const Expr<D>& other) {
139  static_assert(
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) {
152  static_assert(
154  "no_alias() expressions are not allowed on the right-hand side of "
155  "the assignment operator.");
156  return operator=(SubtExpr<TsrExpr_, D>(*this, other.derived()));
157  }
158 
160 
163  template <typename D>
164  array_type& operator*=(const Expr<D>& other) {
165  static_assert(
167  "no_alias() expressions are not allowed on the right-hand side of "
168  "the assignment operator.");
169  return operator=(MultExpr<TsrExpr_, D>(*this, other.derived()));
170  }
171 
173 
175  array_type& array() const { return array_; }
176 
178 
181  return TsrExpr<Array, false>(array_, annotation_);
182  }
183 
185 
190  template <typename Index1, typename Index2,
191  typename = std::enable_if_t<
192  TiledArray::detail::is_integral_range_v<Index1> &&
193  TiledArray::detail::is_integral_range_v<Index2>>>
194  BlkTsrExpr<const Array, Alias> block(const Index1& lower_bound,
195  const Index2& upper_bound) const {
196  return BlkTsrExpr<const Array, Alias>(array_, annotation_, lower_bound,
197  upper_bound);
198  }
199 
201 
206  template <typename Index1, typename Index2,
207  typename = std::enable_if_t<std::is_integral_v<Index1> &&
208  std::is_integral_v<Index2>>>
210  const std::initializer_list<Index1>& lower_bound,
211  const std::initializer_list<Index2>& upper_bound) const {
212  return BlkTsrExpr<const Array, Alias>(array_, annotation_, lower_bound,
213  upper_bound);
214  }
215 
217 
221  template <typename PairRange,
222  typename = std::enable_if_t<
223  TiledArray::detail::is_gpair_range_v<PairRange>>>
224  BlkTsrExpr<const Array, Alias> block(const PairRange& bounds) const {
225  return BlkTsrExpr<const Array, Alias>(array_, annotation_, bounds);
226  }
227 
229 
232  template <typename Index,
233  typename = std::enable_if_t<std::is_integral_v<Index>>>
235  const std::initializer_list<std::initializer_list<Index>>& bounds) const {
236  return BlkTsrExpr<const Array, Alias>(array_, annotation_, bounds);
237  }
238 
240 
245  template <typename Index1, typename Index2,
246  typename = std::enable_if_t<
247  TiledArray::detail::is_integral_range_v<Index1> &&
248  TiledArray::detail::is_integral_range_v<Index2>>>
249  BlkTsrExpr<Array, Alias> block(const Index1& lower_bound,
250  const Index2& upper_bound) {
251  return BlkTsrExpr<Array, Alias>(array_, annotation_, lower_bound,
252  upper_bound);
253  }
254 
256 
261  template <typename Index1, typename Index2,
262  typename = std::enable_if_t<std::is_integral_v<Index1> &&
263  std::is_integral_v<Index2>>>
265  const std::initializer_list<Index1>& lower_bound,
266  const std::initializer_list<Index2>& upper_bound) {
267  return BlkTsrExpr<Array, Alias>(array_, annotation_, lower_bound,
268  upper_bound);
269  }
270 
272 
276  template <typename PairRange,
277  typename = std::enable_if_t<
278  TiledArray::detail::is_gpair_range_v<PairRange>>>
279  BlkTsrExpr<Array, Alias> block(const PairRange& bounds) {
280  return BlkTsrExpr<Array, Alias>(array_, annotation_, bounds);
281  }
282 
284 
287  template <typename Index,
288  typename = std::enable_if_t<std::is_integral_v<Index>>>
290  const std::initializer_list<std::initializer_list<Index>>& bounds) {
291  return BlkTsrExpr<Array, Alias>(array_, annotation_, bounds);
292  }
293 
295 
298  return ConjTsrExpr<Array>(array_, annotation_, conj_op());
299  }
300 
302 
304  const std::string& annotation() const { return annotation_; }
305 
306 }; // class TsrExpr
307 
309 
311 template <typename Array>
312 class TsrExpr<const Array, true> : public Expr<TsrExpr<const Array, true>> {
313  public:
316  typedef
318  typedef typename ExprTrait<TsrExpr_>::engine_type
320  using index1_type = TA_1INDEX_TYPE;
321 
322  private:
323  const array_type& array_;
324  std::string annotation_;
325 
326  // Not allowed
328 
329  public:
330  // Compiler generated functions
331  TsrExpr(const TsrExpr_&) = default;
332  TsrExpr(TsrExpr_&&) = default;
333  ~TsrExpr() = default;
334  TsrExpr_& operator=(const TsrExpr_&) = delete;
336 
338 
341  TsrExpr(const array_type& array, const std::string& annotation)
342  : Expr_(), array_(array), annotation_(annotation) {}
343 
345 
347  const array_type& array() const { return array_; }
348 
350 
355  template <typename Index1, typename Index2,
356  typename = std::enable_if_t<
357  TiledArray::detail::is_integral_range_v<Index1> &&
358  TiledArray::detail::is_integral_range_v<Index2>>>
359  BlkTsrExpr<const Array, true> block(const Index1& lower_bound,
360  const Index2& upper_bound) const {
361  return BlkTsrExpr<const Array, true>(array_, annotation_, lower_bound,
362  upper_bound);
363  }
364 
366 
372  template <typename Index1, typename Index2,
373  typename = std::enable_if_t<std::is_integral_v<Index1> &&
374  std::is_integral_v<Index2>>>
376  const std::initializer_list<Index1>& lower_bound,
377  const std::initializer_list<Index2>& upper_bound) const {
378  return BlkTsrExpr<const Array, true>(array_, annotation_, lower_bound,
379  upper_bound);
380  }
381 
383 
387  template <typename PairRange,
388  typename = std::enable_if_t<
389  TiledArray::detail::is_gpair_range_v<PairRange>>>
390  BlkTsrExpr<const Array, true> block(const PairRange& bounds) const {
391  return BlkTsrExpr<const Array, true>(array_, annotation_, bounds);
392  }
393 
395 
398  template <typename Index,
399  typename = std::enable_if_t<std::is_integral_v<Index>>>
401  const std::initializer_list<std::initializer_list<Index>>& bounds) const {
402  return BlkTsrExpr<const Array, true>(array_, annotation_, bounds);
403  }
404 
406 
409  return ConjTsrExpr<Array>(array_, annotation_, conj_op());
410  }
411 
413 
415  const std::string& annotation() const { return annotation_; }
416 
417 }; // class TsrExpr<const A>
418 
419 } // namespace expressions
420 } // namespace TiledArray
421 
422 #endif // TILEDARRAY_EXPRESSIONS_TSR_EXPR_H__INCLUDED
TsrExpr< const Array, true > TsrExpr_
This class type.
Definition: tsr_expr.h:314
array_type & operator=(const Expr< D > &other)
Expression assignment operator.
Definition: tsr_expr.h:124
BlkTsrExpr< const Array, true > block(const std::initializer_list< std::initializer_list< Index >> &bounds) const
Block expression.
Definition: tsr_expr.h:400
TsrEngine< Array, typename Array::eval_type, true > engine_type
Expression engine type.
Definition: tsr_expr.h:68
typename TiledArray::detail::numeric_type< T >::type numeric_t
numeric_t<T> is an alias for numeric_type<T>::type
Definition: type_traits.h:730
array_type & array() const
Array accessor.
Definition: tsr_expr.h:175
BlkTsrExpr< const Array, true > block(const Index1 &lower_bound, const Index2 &upper_bound) const
Block expression.
Definition: tsr_expr.h:359
Expression wrapper for array objects.
Definition: tsr_expr.h:83
ExprTrait< TsrExpr_ >::array_type array_type
The array type.
Definition: tsr_expr.h:317
const array_type & array() const
Array accessor.
Definition: tsr_expr.h:347
TiledArray::detail::numeric_t< Array > numeric_type
Array base numeric type.
Definition: tsr_expr.h:64
Subtraction expression.
Definition: subt_expr.h:88
ConjTsrExpr< Array > conj() const
Conjugated-tensor expression factor.
Definition: tsr_expr.h:297
Base class for expression evaluation.
Definition: expr.h:97
BlkTsrExpr< const Array, Alias > block(const PairRange &bounds) const
immutable Block expression factory
Definition: tsr_expr.h:224
BlkTsrExpr< Array, Alias > block(const std::initializer_list< Index1 > &lower_bound, const std::initializer_list< Index2 > &upper_bound)
mutable Block expression factory
Definition: tsr_expr.h:264
TsrExpr(const TsrExpr_ &)=default
BlkTsrExpr< const Array, true > block(const PairRange &bounds) const
Block expression.
Definition: tsr_expr.h:390
TsrExpr(array_type &array, const std::string &annotation)
Constructor.
Definition: tsr_expr.h:108
TsrExpr_ & operator=(const TsrExpr_ &)=delete
BlkTsrExpr< Array, Alias > block(const std::initializer_list< std::initializer_list< Index >> &bounds)
mutable Block expression factory
Definition: tsr_expr.h:289
ExprTrait< TsrExpr_ >::engine_type engine_type
Expression engine type.
Definition: tsr_expr.h:90
array_type & operator=(TsrExpr_ &other)
Expression assignment operator.
Definition: tsr_expr.h:114
typename TiledArray::detail::scalar_type< T >::type scalar_t
scalar_t<T> is an alias for scalar_type<T>::type
Definition: type_traits.h:760
ExprTrait< TsrExpr_ >::array_type array_type
The array type.
Definition: tsr_expr.h:88
Tensor expression engine.
Definition: tsr_engine.h:90
Addition expression.
Definition: add_expr.h:88
BlkTsrExpr< const Array, Alias > block(const std::initializer_list< Index1 > &lower_bound, const std::initializer_list< Index2 > &upper_bound) const
immutable Block expression factory
Definition: tsr_expr.h:209
TsrExpr(TsrExpr_ &&)=default
TsrExpr(const array_type &array, const std::string &annotation)
Constructor.
Definition: tsr_expr.h:341
Forward declarations.
Definition: dist_array.h:57
array_type & operator-=(const Expr< D > &other)
Expression minus-assignment operator.
Definition: tsr_expr.h:151
BlkTsrExpr< Array, Alias > block(const Index1 &lower_bound, const Index2 &upper_bound)
mutable Block expression factory
Definition: tsr_expr.h:249
TsrEngine< Array, typename Array::eval_type, Alias > engine_type
Expression engine type.
Definition: tsr_expr.h:57
ExprTrait< TsrExpr_ >::engine_type engine_type
Expression engine type.
Definition: tsr_expr.h:319
BlkTsrExpr< const Array, Alias > block(const std::initializer_list< std::initializer_list< Index >> &bounds) const
immutable Block expression factory
Definition: tsr_expr.h:234
Expression wrapper for scaled array objects.
Definition: scal_tsr_expr.h:67
array_type & operator+=(const Expr< D > &other)
Expression plus-assignment operator.
Definition: tsr_expr.h:138
BlkTsrExpr< Array, Alias > block(const PairRange &bounds)
mutable Block expression factory
Definition: tsr_expr.h:279
array_type & operator*=(const Expr< D > &other)
Expression multiply-assignment operator.
Definition: tsr_expr.h:164
const std::string & annotation() const
Tensor annotation accessor.
Definition: tsr_expr.h:304
TsrExpr< Array, false > no_alias() const
Flag this tensor expression for a non-aliasing assignment.
Definition: tsr_expr.h:180
ComplexConjugate< S > conj_op(const S factor)
ComplexConjugate operator factory function.
Definition: complex.h:204
Expr< TsrExpr_ > Expr_
Expression base type.
Definition: tsr_expr.h:315
TiledArray::detail::scalar_t< Array > scalar_type
Array base scalar type.
Definition: tsr_expr.h:66
BlkTsrExpr< const Array, Alias > block(const Index1 &lower_bound, const Index2 &upper_bound) const
immutable Block expression factory
Definition: tsr_expr.h:194
TiledArray::detail::numeric_t< Array > numeric_type
Array base numeric type.
Definition: tsr_expr.h:53
Expression wrapper for const array objects.
Definition: tsr_expr.h:312
Multiplication expression.
Definition: mult_expr.h:88
ConjTsrExpr< Array > conj() const
Conjugated-tensor expression factor.
Definition: tsr_expr.h:408
BlkTsrExpr< const Array, true > block(const std::initializer_list< Index1 > &lower_bound, const std::initializer_list< Index2 > &upper_bound) const
Block expression.
Definition: tsr_expr.h:375
const std::string & annotation() const
Tensor annotation accessor.
Definition: tsr_expr.h:415
Expr< TsrExpr_ > Expr_
Base class type.
Definition: tsr_expr.h:86
derived_type & derived()
Cast this object to its derived type.
Definition: expr.h:365
TsrExpr< Array, Alias > TsrExpr_
This class type.
Definition: tsr_expr.h:85
TiledArray::detail::scalar_t< Array > scalar_type
Array base scalar type.
Definition: tsr_expr.h:55