TiledArray  0.7.0
add.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 Calvin
19  * Department of Chemistry, Virginia Tech
20  *
21  * add.h
22  * Jan 11, 2016
23  *
24  */
25 
26 #ifndef TILEDARRAY_TILE_INTERFACE_ADD_H__INCLUDED
27 #define TILEDARRAY_TILE_INTERFACE_ADD_H__INCLUDED
28 
29 #include "../type_traits.h"
30 #include "cast.h"
31 
32 namespace TiledArray {
33 
35 
41  template <typename Left, typename Right>
42  inline auto add(const Left& left, const Right& right) ->
43  decltype(left.add(right))
44  { return left.add(right); }
45 
47 
55  template <typename Left, typename Right, typename Scalar,
56  typename std::enable_if<detail::is_numeric<Scalar>::value>::type* = nullptr>
57  inline auto add(const Left& left, const Right& right, const Scalar factor)
58  { return left.add(right, factor); }
59 
61 
68  template <typename Left, typename Right>
69  inline auto add(const Left& left, const Right& right, const Permutation& perm)
70  { return left.add(right, perm); }
71 
73 
82  template <typename Left, typename Right, typename Scalar,
83  typename std::enable_if<detail::is_numeric<Scalar>::value>::type* = nullptr>
84  inline auto add(const Left& left, const Right& right, const Scalar factor,
85  const Permutation& perm)
86  { return left.add(right, factor, perm); }
87 
89 
95  template <typename Result, typename Arg>
96  inline Result& add_to(Result& result, const Arg& arg)
97  { return result.add_to(arg); }
98 
100 
108  template <typename Result, typename Arg, typename Scalar,
109  typename std::enable_if<detail::is_numeric<Scalar>::value>::type* = nullptr>
110  inline Result& add_to(Result& result, const Arg& arg, const Scalar factor)
111  { return result.add_to(arg, factor); }
112 
113 
114  namespace tile_interface {
115 
116  using TiledArray::add;
117  using TiledArray::add_to;
118 
119  template <typename... T>
120  using result_of_add_t = decltype(add(std::declval<T>()...));
121 
122  template <typename... T>
123  using result_of_add_to_t = decltype(add_to(std::declval<T>()...));
124 
125 // template <typename Left, typename Right, typename Enabler = void>
126 // struct add_trait {
127 // typedef Left type;
128 // };
129 //
130 // template <typename Left, typename Right>
131 // struct add_trait<Left, Right,
132 // typename std::enable_if<
133 // TiledArray::detail::is_type<result_of_add_t<Left, Right> >::value
134 // >::type>
135 // {
136 // typedef result_of_add_t<Left, Right> type;
137 // };
138 //
139 // template <typename Left, typename Right, typename Enabler = void>
140 // struct scal_add_trait :
141 // public scal_trait<typename add_trait<Left, Right>::type, Scalar>
142 // { };
143 //
144 // template <typename Left, typename Right, typename Scalar>
145 // struct scal_add_trait<Left, Right,
146 // typename std::enable_if<
147 // TiledArray::detail::is_type<result_of_add_t<Left, Right, Scalar> >::value
148 // >::type>
149 // {
150 // typedef result_of_add_t<Left, Right, Scalar> type;
151 // };
152 
153  template <typename Result, typename Left, typename Right,
154  typename Enabler = void>
155  class Add {
156  public:
157 
158  typedef Result result_type;
159  typedef Left left_type;
160  typedef Right right_type;
161 
163  operator()(const left_type& left, const right_type& right) const {
164  using TiledArray::add;
165  return add(left, right);
166  }
167 
168  result_type operator()(const left_type& left, const right_type& right,
169  const Permutation& perm) const
170  {
171  using TiledArray::add;
172  return add(left, right, perm);
173  }
174 
175  };
176 
177 
178  template <typename Result, typename Left, typename Right>
179  class Add<Result, Left, Right,
180  typename std::enable_if<
181  ! (std::is_same<Result, result_of_add_t<Left, Right> >::value &&
182  std::is_same<Result, result_of_add_t<Left, Right, Permutation> >::value)
183  >::type>
184  {
185  public:
186 
187  typedef Result result_type;
188  typedef Left left_type;
189  typedef Right right_type;
190 
192  const right_type& right) const
193  {
194  using TiledArray::add;
196  return cast(add(left, right));
197  }
198 
199  result_type operator()(const left_type& left, const right_type& right,
200  const Permutation& perm) const
201  {
202  using TiledArray::add;
204  return cast(add(left, right, perm));
205  }
206 
207  };
208 
209  template <typename Result, typename Left, typename Right, typename Scalar,
210  typename Enabler = void>
211  class ScalAdd {
212  public:
214  "Cannot scale tiles by a non-scalar type");
215 
216  typedef Result result_type;
217  typedef Left left_type;
218  typedef Right right_type;
219  typedef Scalar scalar_type;
220 
221  result_type operator()(const left_type& left, const right_type& right,
222  const scalar_type factor) const
223  {
224  using TiledArray::add;
225  return add(left, right, factor);
226  }
227 
228  result_type operator()(const left_type& left, const right_type& right,
229  const scalar_type factor, const Permutation& perm) const
230  {
231  using TiledArray::add;
232  return add(left, right, factor, perm);
233  }
234 
235  };
236 
237 
238  template <typename Result, typename Left, typename Right, typename Scalar>
239  class ScalAdd<Result, Left, Right, Scalar,
240  typename std::enable_if<
241  ! (std::is_same<Result, result_of_add_t<Left, Right, Scalar> >::value &&
242  std::is_same<Result, result_of_add_t<Left, Right, Scalar, Permutation> >::value)
243  >::type>
244  {
245  public:
247  "Cannot scale tiles by a non-scalar type");
248 
249  typedef Result result_type;
250  typedef Left left_type;
251  typedef Right right_type;
252  typedef Scalar scalar_type;
253 
254  result_type operator()(const left_type& left, const right_type& right,
255  const scalar_type factor) const
256  {
257  using TiledArray::add;
259  return cast(add(left, right, factor));
260  }
261 
262  result_type operator()(const left_type& left, const right_type& right,
263  const scalar_type factor, const Permutation& perm) const
264  {
265  using TiledArray::add;
267  return cast(add(left, right, factor, perm));
268  }
269  };
270 
271 
272  template <typename Result, typename Left, typename Right,
273  typename Enabler = void>
274  class AddTo {
275  public:
276 
277  typedef Result result_type;
278  typedef Left left_type;
279  typedef Right right_type;
280 
281  result_type& operator()(left_type& left, const right_type& right) const {
282  using TiledArray::add_to;
283  return add_to(left, right);
284  }
285 
286  };
287 
288 
289  template <typename Result, typename Left, typename Right>
290  class AddTo<Result, Left, Right,
291  typename std::enable_if<
292  ! std::is_same<Result, result_of_add_to_t<Left, Right> >::value
293  >::type>
294  {
295  public:
296 
297  typedef Result result_type;
298  typedef Left left_type;
299  typedef Right right_type;
300 
301  result_type operator()(left_type& left, const right_type& right) const {
302  using TiledArray::add_to;
304  add_to(left, right);
305  return cast(left);
306  }
307 
308  };
309 
310 
311 
312  template <typename Result, typename Left, typename Right, typename Scalar,
313  typename Enabler = void>
314  class ScalAddTo {
315  public:
317  "Cannot scale tiles by a non-scalar type");
318 
319  typedef Result result_type;
320  typedef Left left_type;
321  typedef Right right_type;
322  typedef Scalar scalar_type;
323 
324  result_type& operator()(left_type& left, const right_type& right) const {
325  using TiledArray::add_to;
326  return add_to(left, right);
327  }
328 
329  };
330 
331  template <typename Result, typename Left, typename Right, typename Scalar>
332  class ScalAddTo<Result, Left, Right, Scalar,
333  typename std::enable_if<
334  ! std::is_same<Result, result_of_add_to_t<Left, Right, Scalar> >::value
335  >::type>
336  {
337  public:
339  "Cannot scale tiles by a non-scalar type");
340 
341  typedef Result result_type;
342  typedef Left left_type;
343  typedef Right right_type;
344  typedef Scalar scalar_type;
345 
347  const scalar_type factor) const
348  {
349  using TiledArray::add_to;
351  add_to(left, right);
352  return cast(left);
353  }
354 
355  };
356 
357  } // namespace tile_interface
358 
359 
361 
367  template <typename Result, typename Left, typename Right>
368  class Add :
369  public TiledArray::tile_interface::Add<Result, Left, Right>
370  { };
371 
373 
380  template <typename Result, typename Left, typename Right, typename Scalar>
381  class ScalAdd :
382  public TiledArray::tile_interface::ScalAdd<Result, Left, Right, Scalar>
383  { };
384 
386 
391  template <typename Result, typename Left, typename Right>
392  class AddTo :
393  public TiledArray::tile_interface::AddTo<Result, Left, Right>
394  { };
395 
397 
403  template <typename Result, typename Left, typename Right, typename Scalar>
404  class ScalAddTo :
405  public TiledArray::tile_interface::ScalAddTo<Result, Left, Right, Scalar>
406  { };
407 
408 
409 } // namespace TiledArray
410 
411 #endif // TILEDARRAY_TILE_INTERFACE_ADD_H__INCLUDED
result_type operator()(const left_type &left, const right_type &right, const scalar_type factor, const Permutation &perm) const
Definition: add.h:228
Tile cast operation.
Definition: cast.h:34
Left left_type
Left-hand argument tile type.
Definition: add.h:320
Right right_type
Right-hand argument tile type.
Definition: add.h:321
Right right_type
Right-hand argument tile type.
Definition: add.h:160
Left left_type
Left-hand argument tile type.
Definition: add.h:278
result_type & operator()(left_type &left, const right_type &right) const
Definition: add.h:281
STL namespace.
result_type operator()(const left_type &left, const right_type &right, const scalar_type factor) const
Definition: add.h:221
Tile< Result > & add_to(Tile< Result > &result, const Tile< Arg > &arg)
Add to the result tile.
Definition: tile.h:441
Scalar scalar_type
The scaling factor type.
Definition: add.h:219
Right right_type
Right-hand argument tile type.
Definition: add.h:218
Result result_type
Result tile type.
Definition: add.h:277
decltype(auto) add(const Tile< Left > &left, const Tile< Right > &right)
Add tile arguments.
Definition: tile.h:362
Right right_type
Right-hand argument tile type.
Definition: add.h:279
Result result_type
Result tile type.
Definition: add.h:317
result_type & operator()(left_type &left, const right_type &right) const
Definition: add.h:324
Result result_type
Result tile type.
Definition: add.h:158
Add-to and scale tile operation.
Definition: add.h:404
Scalar scalar_type
The scaling factor type.
Definition: add.h:322
decltype(add_to(std::declval< T >()...)) result_of_add_to_t
Definition: add.h:123
Add-to tile operation.
Definition: add.h:392
result_type operator()(const left_type &left, const right_type &right, const Permutation &perm) const
Definition: add.h:168
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:119
decltype(add(std::declval< T >()...)) result_of_add_t
Definition: add.h:120
result_type operator()(const left_type &left, const right_type &right) const
Definition: add.h:163
Add tile operation.
Definition: add.h:368
Add tile operation.
Definition: add.h:381
Left left_type
Left-hand argument tile type.
Definition: add.h:217
Left left_type
Left-hand argument tile type.
Definition: add.h:159
Result result_type
Result tile type.
Definition: add.h:214