scale.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2016 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
19  * Department of Chemistry, Virginia Tech
20  *
21  * scale.h
22  * Jan 11, 2016
23  *
24  */
25 
26 #ifndef TILEDARRAY_SRC_TILEDARRAY_TILE_INTERFACE_SCALE_H__INCLUDED
27 #define TILEDARRAY_SRC_TILEDARRAY_TILE_INTERFACE_SCALE_H__INCLUDED
28 
29 #include "../permutation.h"
30 #include "../type_traits.h"
31 #include "cast.h"
32 
33 namespace TiledArray {
34 
36 
41 template <typename Arg, typename Scalar,
42  std::enable_if_t<TiledArray::detail::is_numeric_v<Scalar> &&
43  !TiledArray::detail::is_array_v<Arg>>* = nullptr>
44 inline auto scale(const Arg& arg, const Scalar factor) {
45  return arg.scale(factor);
46 }
47 
49 
56 template <
57  typename Arg, typename Scalar, typename Perm,
58  std::enable_if_t<TiledArray::detail::is_numeric_v<Scalar> &&
59  TiledArray::detail::is_permutation_v<Perm>>* = nullptr>
60 inline auto scale(const Arg& arg, const Scalar factor, const Perm& perm) {
61  return arg.scale(factor, perm);
62 }
63 
65 
71 template <typename Result, typename Scalar,
72  std::enable_if_t<TiledArray::detail::is_numeric_v<Scalar>>* = nullptr>
73 inline Result& scale_to(Result& result, const Scalar factor) {
74  return result.scale_to(factor);
75 }
76 
77 namespace tile_interface {
78 
79 template <typename... T>
80 using result_of_scale_t = decltype(scale(std::declval<T>()...));
81 
82 template <typename... T>
83 using result_of_scale_to_t = decltype(scale_to(std::declval<T>()...));
84 
85 template <typename Arg, typename Scalar, typename Enabler = void>
86 struct scale_trait {
87  typedef Arg type;
88 };
89 
90 template <typename Arg, typename Scalar>
91 struct scale_trait<Arg, Scalar,
92  typename std::enable_if<TiledArray::detail::is_type<
93  result_of_scale_t<Arg, Scalar>>::value>::type> {
95 };
96 
97 template <typename Result, typename Arg, typename Scalar,
98  typename Enabler = void>
99 class Scale {
100  public:
101  static_assert(TiledArray::detail::is_numeric_v<Scalar>,
102  "Cannot scale tiles by a non-scalar type");
103 
104  typedef Result result_type;
105  typedef Arg argument_type;
106  typedef Scalar scalar_type;
107 
109  const scalar_type factor) const {
110  using TiledArray::scale;
111  return scale(arg, factor);
112  }
113 
114  template <typename Perm, typename = std::enable_if_t<
115  TiledArray::detail::is_permutation_v<Perm>>>
117  const Perm& perm) const {
118  using TiledArray::scale;
119  return scale(arg, factor, perm);
120  }
121 };
122 
123 template <typename Result, typename Arg, typename Scalar>
124 class Scale<Result, Arg, Scalar,
125  typename std::enable_if<!std::is_same<
126  Result, result_of_scale_t<Arg, Scalar>>::value>::type> {
127  public:
128  static_assert(TiledArray::detail::is_numeric_v<Scalar>,
129  "Cannot scale tiles by a non-scalar type");
130 
131  typedef Result result_type;
132  typedef Arg argument_type;
133  typedef Scalar scalar_type;
134 
136  const scalar_type factor) const {
137  using TiledArray::scale;
139  return cast(scale(arg, factor));
140  }
141 
142  template <typename Perm, typename = std::enable_if_t<
143  TiledArray::detail::is_permutation_v<Perm>>>
145  const Perm& perm) const {
146  using TiledArray::scale;
148  return cast(scale(arg, factor, perm));
149  }
150 };
151 
152 template <typename Result, typename Arg, typename Scalar,
153  typename Enabler = void>
154 class ScaleTo {
155  public:
156  static_assert(TiledArray::detail::is_numeric_v<Scalar>,
157  "Cannot scale tiles by a non-scalar type");
158 
159  typedef Result result_type;
160  typedef Arg argument_type;
161  typedef Scalar scalar_type;
162 
163  result_type operator()(argument_type& arg, const scalar_type factor) const {
164  using TiledArray::scale_to;
165  return scale_to(arg, factor);
166  }
167 };
168 
169 template <typename Result, typename Arg, typename Scalar>
170 class ScaleTo<Result, Arg, Scalar,
171  typename std::enable_if<!std::is_same<
172  Result, result_of_scale_t<Arg, Scalar>>::value>::type> {
173  public:
174  static_assert(TiledArray::detail::is_numeric_v<Scalar>,
175  "Cannot scale tiles by a non-scalar type");
176 
177  typedef Result result_type;
178  typedef Arg argument_type;
179  typedef Scalar scalar_type;
180 
182  const scalar_type factor) const {
183  using TiledArray::scale_to;
185  return cast(scale_to(arg, factor));
186  }
187 };
188 
189 } // namespace tile_interface
190 
192 
197 template <typename Arg, typename Scalar>
199  : public TiledArray::tile_interface::scale_trait<Arg, Scalar> {};
200 
202 
207 template <typename Result, typename Arg, typename Scalar>
208 class Scale : public TiledArray::tile_interface::Scale<Result, Arg, Scalar> {};
209 
210 } // namespace TiledArray
211 
212 #endif // TILEDARRAY_SRC_TILEDARRAY_TILE_INTERFACE_SCALE_H__INCLUDED
result_type operator()(const argument_type &arg, const scalar_type factor, const Perm &perm) const
Definition: scale.h:116
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:1204
Scalar scalar_type
Scaling factor type.
Definition: scale.h:161
Arg argument_type
Argument tile type.
Definition: scale.h:160
decltype(scale_to(std::declval< T >()...)) result_of_scale_to_t
Definition: scale.h:83
Scalar scalar_type
Scaling factor type.
Definition: scale.h:106
decltype(scale(std::declval< T >()...)) result_of_scale_t
Definition: scale.h:80
Tile cast operation.
Definition: cast.h:168
Result result_type
Result tile type.
Definition: scale.h:157
decltype(auto) scale(const Tile< Arg > &arg, const Scalar factor)
Scalar the tile argument.
Definition: tile.h:1174
result_type operator()(const argument_type &arg, const scalar_type factor) const
Definition: scale.h:108
result_type operator()(argument_type &arg, const scalar_type factor) const
Definition: scale.h:163
Arg argument_type
Argument tile type.
Definition: scale.h:105
Result result_type
Result tile type.
Definition: scale.h:102
Scale tile.
Definition: scale.h:208
Scale trait.
Definition: scale.h:199