TiledArray  0.7.0
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 "../type_traits.h"
30 #include "../permutation.h"
31 #include "cast.h"
32 
33 namespace TiledArray {
34 
36 
42  template <typename Arg, typename Scalar,
43  typename std::enable_if<TiledArray::detail::is_numeric<Scalar>::value>::type* = nullptr>
44  inline auto scale(const Arg& arg, const Scalar factor)
45  { return arg.scale(factor); }
46 
48 
55  template <typename Arg, typename Scalar,
56  typename std::enable_if<TiledArray::detail::is_numeric<Scalar>::value>::type* = nullptr>
57  inline auto scale(const Arg& arg, const Scalar factor, const Permutation& perm)
58  { return arg.scale(factor, perm); }
59 
61 
67  template <typename Result, typename Scalar,
68  typename std::enable_if<TiledArray::detail::is_numeric<Scalar>::value>::type* = nullptr>
69  inline Result& scale_to(Result& result, const Scalar factor)
70  { return result.scale_to(factor); }
71 
72 
73  namespace tile_interface {
74 
75  template <typename... T>
76  using result_of_scale_t = decltype(scale(std::declval<T>()...));
77 
78  template <typename... T>
79  using result_of_scale_to_t = decltype(scale_to(std::declval<T>()...));
80 
81 
82  template <typename Arg, typename Scalar, typename Enabler = void>
83  struct scale_trait {
84  typedef Arg type;
85  };
86 
87  template <typename Arg, typename Scalar>
88  struct scale_trait<Arg, Scalar,
89  typename std::enable_if<
90  TiledArray::detail::is_type<result_of_scale_t<Arg, Scalar> >::value
91  >::type>
92  {
94  };
95 
96  template <typename Result, typename Arg, typename Scalar,
97  typename Enabler = void>
98  class Scale {
99  public:
101  "Cannot scale tiles by a non-scalar type");
102 
103  typedef Result result_type;
104  typedef Arg argument_type;
105  typedef Scalar scalar_type;
106 
108  const scalar_type factor) const
109  {
110  using TiledArray::scale;
111  return scale(arg, factor);
112  }
113 
115  const scalar_type factor, const Permutation& perm) const
116  {
117  using TiledArray::scale;
118  return scale(arg, factor, perm);
119  }
120  };
121 
122  template <typename Result, typename Arg, typename Scalar>
123  class Scale<Result, Arg, Scalar,
124  typename std::enable_if<
125  ! std::is_same<Result, result_of_scale_t<Arg, Scalar> >::value
126  >::type>
127  {
128  public:
130  "Cannot scale tiles by a non-scalar type");
131 
132  typedef Result result_type;
133  typedef Arg argument_type;
134  typedef Scalar scalar_type;
135 
137  const scalar_type factor) const
138  {
139  using TiledArray::scale;
141  return cast(scale(arg, factor));
142  }
143 
144 
146  const scalar_type factor, const Permutation& perm) const
147  {
148  using TiledArray::scale;
150  return cast(scale(arg, factor, perm));
151  }
152 
153  };
154 
155 
156  template <typename Result, typename Arg, typename Scalar,
157  typename Enabler = void>
158  class ScaleTo {
159  public:
161  "Cannot scale tiles by a non-scalar type");
162 
163  typedef Result result_type;
164  typedef Arg argument_type;
165  typedef Scalar scalar_type;
166 
168  const scalar_type factor) const
169  {
170  using TiledArray::scale_to;
171  return scale_to(arg, factor);
172  }
173 
174  };
175 
176  template <typename Result, typename Arg, typename Scalar>
177  class ScaleTo<Result, Arg, Scalar,
178  typename std::enable_if<
179  ! std::is_same<Result, result_of_scale_t<Arg, Scalar> >::value
180  >::type>
181  {
182  public:
184  "Cannot scale tiles by a non-scalar type");
185 
186  typedef Result result_type;
187  typedef Arg argument_type;
188  typedef Scalar scalar_type;
189 
191  const scalar_type factor) const
192  {
193  using TiledArray::scale_to;
195  return cast(scale_to(arg, factor));
196  }
197 
198  };
199 
200  } // namespace tile_interface
201 
202 
204 
209  template <typename Arg, typename Scalar>
210  struct scale_trait :
211  public TiledArray::tile_interface::scale_trait<Arg, Scalar>
212  { };
213 
214 
216 
221  template <typename Result, typename Arg, typename Scalar>
222  class Scale :
223  public TiledArray::tile_interface::Scale<Result, Arg, Scalar>
224  { };
225 
226 
227 } // namespace TiledArray
228 
229 #endif // TILEDARRAY_SRC_TILEDARRAY_TILE_INTERFACE_SCALE_H__INCLUDED
Scale trait.
Definition: scale.h:210
result_type operator()(const argument_type &arg, const scalar_type factor, const Permutation &perm) const
Definition: scale.h:114
Tile cast operation.
Definition: cast.h:34
Scalar scalar_type
Scaling factor type.
Definition: scale.h:165
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:725
decltype(scale_to(std::declval< T >()...)) result_of_scale_to_t
Definition: scale.h:79
void scale(DistArray< Tile, Policy > &a, typename DistArray< Tile, Policy >::element_type scaling_factor)
Definition: utils.h:108
STL namespace.
Scalar scalar_type
Scaling factor type.
Definition: scale.h:105
Result result_type
Result tile type.
Definition: scale.h:161
decltype(scale(std::declval< T >()...)) result_of_scale_t
Definition: scale.h:76
Result result_type
Result tile type.
Definition: scale.h:101
result_type operator()(argument_type &arg, const scalar_type factor) const
Definition: scale.h:167
result_type operator()(const argument_type &arg, const scalar_type factor) const
Definition: scale.h:107
Arg argument_type
Argument tile type.
Definition: scale.h:104
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
Scale tile.
Definition: scale.h:222
Arg argument_type
Argument tile type.
Definition: scale.h:164