TiledArray  0.7.0
scal_expr.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2014 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  * scal_expr.h
22  * Mar 16, 2014
23  *
24  */
25 
26 #ifndef TILEDARRAY_EXPRESSIONS_SCAL_EXPR_H__INCLUDED
27 #define TILEDARRAY_EXPRESSIONS_SCAL_EXPR_H__INCLUDED
28 
31 
32 namespace TiledArray {
33  namespace expressions {
34 
38 
39  template <typename Arg, typename Scalar>
40  struct ExprTrait<ScalExpr<Arg, Scalar> > {
41  typedef Arg argument_type;
42  typedef Scalar scalar_type;
48  typedef numeric_t<typename EngineTrait<engine_type>::eval_type>
50  };
51 
53 
55  template <typename Arg, typename Scalar>
56  class ScalExpr : public UnaryExpr<ScalExpr<Arg, Scalar> > {
57  public:
63 
64  private:
65 
66  scalar_type factor_;
67 
68  public:
69 
70  // Compiler generated functions
71  ScalExpr(const ScalExpr_&) = default;
72  ScalExpr(ScalExpr_&&) = default;
73  ~ScalExpr() = default;
74  ScalExpr_& operator=(const ScalExpr_&) = delete;
75  ScalExpr_& operator=(ScalExpr_&&) = delete;
76 
78 
82  UnaryExpr_(arg), factor_(factor)
83  { }
84 
86 
89  ScalExpr(const ScalExpr_& other, const scalar_type factor) :
90  UnaryExpr_(other), factor_(other.factor_ * factor)
91  { }
92 
93 
95 
97  scalar_type factor() const { return factor_; }
98 
99  }; // class ScalExpr
100 
101 
103 
105 
111  template <typename Arg, typename Scalar,
112  typename std::enable_if<
114  >::type* = nullptr>
115  inline ScalExpr<Arg, Scalar>
116  operator*(const Expr<Arg>& expr, const Scalar& factor) {
118  "no_alias() expressions are not allowed on the right-hand side of "
119  "the assignment operator.");
120  return ScalExpr<Arg, Scalar>(expr.derived(), factor);
121  }
122 
124 
130  template <typename Arg, typename Scalar,
131  typename std::enable_if<
133  >::type* = nullptr>
134  inline ScalExpr<Arg, Scalar>
135  operator*(const Scalar& factor, const Expr<Arg>& expr) {
137  "no_alias() expressions are not allowed on the right-hand side of "
138  "the assignment operator.");
139  return ScalExpr<Arg, Scalar>(expr.derived(), factor);
140  }
141 
143 
149  template <typename Arg, typename Scalar1, typename Scalar2,
150  typename std::enable_if<
152  >::type* = nullptr>
153  inline ScalExpr<Arg, mult_t<Scalar1, Scalar2> >
154  operator*(const ScalExpr<Arg, Scalar1>& expr, const Scalar2& factor) {
155  return ScalExpr<Arg, mult_t<Scalar1, Scalar2> >(expr, factor);
156  }
157 
159 
165  template <typename Arg, typename Scalar1, typename Scalar2,
166  typename std::enable_if<
168  >::type* = nullptr>
169  inline ScalExpr<Arg, mult_t<Scalar2, Scalar1> >
170  operator*(const Scalar1& factor, const ScalExpr<Arg, Scalar2>& expr) {
171  return ScalExpr<Arg, mult_t<Scalar2, Scalar1> >(expr, factor);
172  }
173 
175 
179  template <typename Arg>
181  operator-(const Expr<Arg>& expr) {
183  "no_alias() expressions are not allowed on the right-hand side of "
184  "the assignment operator.");
186  }
187 
189 
193  template <typename Arg, typename Scalar>
195  return ScalExpr<Arg, Scalar>(expr, -1);
196  }
197 
198  } // namespace expressions
199 } // namespace TiledArray
200 
201 #endif // TILEDARRAY_EXPRESSIONS_SCAL_EXPR_H__INCLUDED
ScalEngine< typename ExprTrait< Arg >::engine_type, Scalar, result_type > engine_type
Expression engine type.
Definition: scal_expr.h:47
ScalExpr_ & operator=(const ScalExpr_ &)=delete
ScalExpr< Arg, Scalar > ScalExpr_
This class type.
Definition: scal_expr.h:58
Scalar scalar_type
Addition result scalar type.
Definition: scal_expr.h:42
scalar_type factor() const
Scaling factor accessor.
Definition: scal_expr.h:97
Scaling expression engine.
Definition: scal_engine.h:38
ExprTrait< ScalExpr_ >::engine_type engine_type
Expression engine type.
Definition: scal_expr.h:61
std::enable_if< TiledArray::detail::is_numeric< Scalar >::value, ScalAddExpr< Left, Right, Scalar > >::type operator*(const AddExpr< Left, Right > &expr, const Scalar &factor)
Scaled-addition expression factor.
Definition: add_expr.h:198
decltype(std::declval< Scalar1 >() *std::declval< Scalar2 >()) mult_t
Definition: type_traits.h:748
decltype(scale(std::declval< T >()...)) result_of_scale_t
Definition: scale.h:76
typename TiledArray::detail::numeric_type< T >::type numeric_t
Definition: type_traits.h:525
TiledArray::tile_interface::result_of_scale_t< typename EngineTrait< typename ExprTrait< Arg >::engine_type >::eval_type, scalar_type > result_type
Result tile type.
Definition: scal_expr.h:45
numeric_t< typename EngineTrait< engine_type >::eval_type > numeric_type
Addition result numeric type.
Definition: scal_expr.h:49
typename TiledArray::detail::scalar_type< T >::type scalar_t
Definition: type_traits.h:555
ExprTrait< ScalExpr_ >::argument_type argument_type
The argument expression type.
Definition: scal_expr.h:60
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
ScalAddExpr< Left, Right, typename ExprTrait< AddExpr< Left, Right > >::numeric_type > operator-(const AddExpr< Left, Right > &expr)
Negated addition expression factor.
Definition: add_expr.h:266
ScalExpr(const ScalExpr_ &)=default
UnaryExpr< ScalExpr_ > UnaryExpr_
Unary base class type.
Definition: scal_expr.h:59
ExprTrait< ScalExpr_ >::scalar_type scalar_type
Scalar type.
Definition: scal_expr.h:62
ScalExpr(const ScalExpr_ &other, const scalar_type factor)
Rescale expression constructor.
Definition: scal_expr.h:89
const argument_type & arg() const
Argument expression accessor.
Definition: unary_expr.h:63
ScalExpr(const argument_type &arg, const scalar_type factor)
Scaled expression constructor.
Definition: scal_expr.h:81