operators.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2015 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  * operators.h
22  * Jun 16, 2015
23  *
24  */
25 
26 #ifndef TILEDARRAY_TENSOR_OPERATORS_H__INCLUDED
27 #define TILEDARRAY_TENSOR_OPERATORS_H__INCLUDED
28 
30 
31 namespace TiledArray {
32 
33 // Tensor arithmetic operators
34 
36 
43 template <typename T1, typename T2,
44  typename std::enable_if<
45  detail::is_tensor<T1, T2>::value ||
46  detail::is_tensor_of_tensor<T1, T2>::value>::type* = nullptr>
47 inline auto operator+(const T1& left, const T2& right) {
48  return add(left, right);
49 }
50 
52 
59 template <typename T1, typename T2,
60  typename std::enable_if<
61  detail::is_tensor<T1, T2>::value ||
62  detail::is_tensor_of_tensor<T1, T2>::value>::type* = nullptr>
63 inline auto operator-(const T1& left, const T2& right) {
64  return subt(left, right);
65 }
66 
68 
75 template <typename T1, typename T2,
76  typename std::enable_if<
77  detail::is_tensor<T1, T2>::value ||
78  detail::is_tensor_of_tensor<T1, T2>::value>::type* = nullptr>
79 inline auto operator*(const T1& left, const T2& right) {
80  return mult(left, right);
81 }
82 
84 
91 template <typename T, typename N,
92  typename std::enable_if<(detail::is_tensor<T>::value ||
94  detail::is_numeric_v<N>>::type* = nullptr>
95 inline auto operator*(const T& left, N right) {
96  return scale(left, right);
97 }
98 
100 
106 template <typename N, typename T,
107  typename std::enable_if<
108  detail::is_numeric_v<N> &&
110  detail::is_tensor_of_tensor<T>::value)>::type* = nullptr>
111 inline auto operator*(N left, const T& right) {
112  return scale(right, left);
113 }
114 
116 
120 template <typename T, typename std::enable_if<detail::is_tensor<T>::value ||
121  detail::is_tensor_of_tensor<
122  T>::value>::type* = nullptr>
123 inline auto operator-(const T& arg) -> decltype(arg.neg()) {
124  return neg(arg);
125 }
126 
128 
132 template <typename T, typename std::enable_if<detail::is_tensor<T>::value ||
133  detail::is_tensor_of_tensor<
134  T>::value>::type* = nullptr>
135 inline auto operator*(const Permutation& perm, const T& arg) {
136  return permute(arg, perm);
137 }
138 
140 
147 template <typename T1, typename T2,
148  typename std::enable_if<
149  detail::is_tensor<T1, T2>::value ||
150  detail::is_tensor_of_tensor<T1, T2>::value>::type* = nullptr>
151 inline auto operator+=(T1& left, const T2& right) {
152  return add_to(left, right);
153 }
154 
156 
163 template <typename T1, typename T2,
164  typename std::enable_if<
165  detail::is_tensor<T1, T2>::value ||
166  detail::is_tensor_of_tensor<T1, T2>::value>::type* = nullptr>
167 inline auto operator-=(T1& left, const T2& right) {
168  return sub_to(left, right);
169 }
170 
172 
179 template <typename T1, typename T2,
180  typename std::enable_if<
181  detail::is_tensor<T1, T2>::value ||
182  detail::is_tensor_of_tensor<T1, T2>::value>::type* = nullptr>
183 inline auto operator*=(T1& left, const T2& right) {
184  return mult_to(left, right);
185 }
186 
188 
195 template <typename T, typename N,
196  typename std::enable_if<(detail::is_tensor<T>::value ||
198  detail::is_numeric_v<N>>::type* = nullptr>
199 inline auto operator+=(T& left, N right) {
200  return add_to(left, right);
201 }
202 
204 
211 template <typename T, typename N,
212  typename std::enable_if<(detail::is_tensor<T>::value ||
214  detail::is_numeric_v<N>>::type* = nullptr>
215 inline auto operator-=(T& left, N right) {
216  return subt_to(left, right);
217 }
218 
220 
227 template <typename T, typename N,
228  typename std::enable_if<(detail::is_tensor<T>::value ||
230  detail::is_numeric_v<N>>::type* = nullptr>
231 inline auto operator*=(T& left, N right) {
232  return scale_to(left, right);
233 }
234 
235 } // namespace TiledArray
236 
237 #endif // TILEDARRAY_TENSOR_OPERATORS_H__INCLUDED
decltype(auto) subt(const Tile< Left > &left, const Tile< Right > &right)
Subtract tile arguments.
Definition: tile.h:879
std::enable_if<!TiledArray::detail::is_permutation_v< Perm >, TiledArray::Range >::type permute(const TiledArray::Range &r, const Perm &p)
Definition: btas.h:803
auto operator+(const T1 &left, const T2 &right)
Tensor plus operator.
Definition: operators.h:47
Tile< Result > & scale_to(Tile< Result > &result, const Scalar factor)
Scale to the result tile.
Definition: tile.h:1204
Permutation of a sequence of objects indexed by base-0 indices.
Definition: permutation.h:130
Tile< Result > & mult_to(Tile< Result > &result, const Tile< Arg > &arg)
Multiply to the result tile.
Definition: tile.h:1081
decltype(auto) add(const Tile< Left > &left, const Tile< Right > &right)
Add tile arguments.
Definition: tile.h:734
std::array< T, N > operator*(const Permutation &, const std::array< T, N > &)
Permute a std::array.
Definition: permutation.h:492
decltype(auto) neg(const Tile< Arg > &arg)
Negate the tile argument.
Definition: tile.h:1218
auto operator-=(T1 &left, const T2 &right)
Tensor minus operator.
Definition: operators.h:167
Tile< Result > & add_to(Tile< Result > &result, const Tile< Arg > &arg)
Add to the result tile.
Definition: tile.h:831
auto operator+=(T1 &left, const T2 &right)
Tensor plus operator.
Definition: operators.h:151
decltype(auto) mult(const Tile< Left > &left, const Tile< Right > &right)
Multiplication tile arguments.
Definition: tile.h:1018
Permutation operator-(const Permutation &perm)
Inverse permutation operator.
Definition: permutation.h:452
decltype(auto) scale(const Tile< Arg > &arg, const Scalar factor)
Scalar the tile argument.
Definition: tile.h:1174
std::array< T, N > & operator*=(std::array< T, N > &, const Permutation &)
In-place permute a std::array.
Definition: permutation.h:510
Tile< Result > & subt_to(Tile< Result > &result, const Tile< Arg > &arg)
Subtract from the result tile.
Definition: tile.h:972
static constexpr bool value
Definition: type_traits.h:122