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;
50  typedef numeric_t<typename EngineTrait<engine_type>::eval_type>
52 };
53 
55 
57 template <typename Arg, typename Scalar>
58 class ScalExpr : public UnaryExpr<ScalExpr<Arg, Scalar> > {
59  public:
64  typedef typename ExprTrait<ScalExpr_>::engine_type
66  typedef
68 
69  private:
70  scalar_type factor_;
71 
72  public:
73  // Compiler generated functions
74  ScalExpr(const ScalExpr_&) = default;
75  ScalExpr(ScalExpr_&&) = default;
76  ~ScalExpr() = default;
77  ScalExpr_& operator=(const ScalExpr_&) = delete;
79 
81 
85  : UnaryExpr_(arg), factor_(factor) {}
86 
88 
91  ScalExpr(const ScalExpr_& other, const scalar_type factor)
92  : UnaryExpr_(other), factor_(other.factor_ * factor) {}
93 
95 
97  scalar_type factor() const { return factor_; }
98 
99 }; // class ScalExpr
100 
102 
104 
110 template <typename Arg, typename Scalar,
111  typename std::enable_if<
112  TiledArray::detail::is_numeric_v<Scalar> >::type* = nullptr>
114  const Scalar& factor) {
115  static_assert(
117  "no_alias() expressions are not allowed on the right-hand side of "
118  "the assignment operator.");
119  return ScalExpr<Arg, Scalar>(expr.derived(), factor);
120 }
121 
123 
129 template <typename Arg, typename Scalar,
130  typename std::enable_if<
131  TiledArray::detail::is_numeric_v<Scalar> >::type* = nullptr>
132 inline ScalExpr<Arg, Scalar> operator*(const Scalar& factor,
133  const Expr<Arg>& expr) {
134  static_assert(
136  "no_alias() expressions are not allowed on the right-hand side of "
137  "the assignment operator.");
138  return ScalExpr<Arg, Scalar>(expr.derived(), factor);
139 }
140 
142 
148 template <typename Arg, typename Scalar1, typename Scalar2,
149  typename std::enable_if<
150  TiledArray::detail::is_numeric_v<Scalar2> >::type* = nullptr>
152  const ScalExpr<Arg, Scalar1>& expr, const Scalar2& factor) {
153  return ScalExpr<Arg, mult_t<Scalar1, Scalar2> >(expr, factor);
154 }
155 
157 
163 template <typename Arg, typename Scalar1, typename Scalar2,
164  typename std::enable_if<
165  TiledArray::detail::is_numeric_v<Scalar1> >::type* = nullptr>
167  const Scalar1& factor, const ScalExpr<Arg, Scalar2>& expr) {
168  return ScalExpr<Arg, mult_t<Scalar2, Scalar1> >(expr, factor);
169 }
170 
172 
176 template <typename Arg>
178  const Expr<Arg>& expr) {
179  static_assert(
181  "no_alias() expressions are not allowed on the right-hand side of "
182  "the assignment operator.");
184  -1);
185 }
186 
188 
192 template <typename Arg, typename Scalar>
194  return ScalExpr<Arg, Scalar>(expr, -1);
195 }
196 
197 } // namespace expressions
198 } // namespace TiledArray
199 
200 #endif // TILEDARRAY_EXPRESSIONS_SCAL_EXPR_H__INCLUDED
UnaryExpr< ScalExpr_ > UnaryExpr_
Unary base class type.
Definition: scal_expr.h:61
Scaling expression engine.
Definition: scal_engine.h:77
ScalExpr(ScalExpr_ &&)=default
scalar_type factor() const
Scaling factor accessor.
Definition: scal_expr.h:97
ScalAddExpr< Left, Right, typename ExprTrait< AddExpr< Left, Right > >::numeric_type > operator-(const AddExpr< Left, Right > &expr)
Negated addition expression factor.
Definition: add_expr.h:255
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
ExprTrait< ScalExpr_ >::scalar_type scalar_type
Scalar type.
Definition: scal_expr.h:67
decltype(std::declval< Scalar1 >() *std::declval< Scalar2 >()) mult_t
Definition: type_traits.h:1159
ScalExpr(const ScalExpr_ &other, const scalar_type factor)
Rescale expression constructor.
Definition: scal_expr.h:91
ExprTrait< ScalExpr_ >::argument_type argument_type
The argument expression type.
Definition: scal_expr.h:63
ScalExpr_ & operator=(ScalExpr_ &&)=delete
ScalExpr(const argument_type &arg, const scalar_type factor)
Scaled expression constructor.
Definition: scal_expr.h:84
ScalExpr(const ScalExpr_ &)=default
Base class for expression evaluation.
Definition: expr.h:97
std::enable_if< TiledArray::detail::is_numeric_v< Scalar >, ScalAddExpr< Left, Right, Scalar > >::type operator*(const AddExpr< Left, Right > &expr, const Scalar &factor)
Scaled-addition expression factor.
Definition: add_expr.h:191
decltype(scale(std::declval< T >()...)) result_of_scale_t
Definition: scale.h:80
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
ScalExpr< Arg, Scalar > ScalExpr_
This class type.
Definition: scal_expr.h:60
numeric_t< typename EngineTrait< engine_type >::eval_type > numeric_type
Addition result numeric type.
Definition: scal_expr.h:51
ScalExpr_ & operator=(const ScalExpr_ &)=delete
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:46
ExprTrait< ScalExpr_ >::engine_type engine_type
Expression engine type.
Definition: scal_expr.h:65
ScalEngine< typename ExprTrait< Arg >::engine_type, Scalar, result_type > engine_type
Expression engine type.
Definition: scal_expr.h:49
derived_type & derived()
Cast this object to its derived type.
Definition: expr.h:365