Grid 0.7.0
Tensor_arith_scalar.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/tensors/Tensor_arith_scalar.h
6
7 Copyright (C) 2015
8
9Author: Azusa Yamaguchi <ayamaguc@staffmail.ed.ac.uk>
10Author: Peter Boyle <paboyle@ph.ed.ac.uk>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27*************************************************************************************/
28/* END LEGAL */
29#ifndef GRID_MATH_ARITH_SCALAR_H
30#define GRID_MATH_ARITH_SCALAR_H
31
33
35// Must support native C++ types Integer, Complex, Real
37
38// multiplication by fundamental scalar type
39template<class l> accelerator_inline iScalar<l> operator * (const iScalar<l>& lhs,const typename iScalar<l>::scalar_type rhs)
40{
41 typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
42 return lhs*srhs;
43}
44template<class l> accelerator_inline iScalar<l> operator * (const typename iScalar<l>::scalar_type lhs,const iScalar<l>& rhs) { return rhs*lhs; }
45
46template<class l,int N> accelerator_inline iVector<l,N> operator * (const iVector<l,N>& lhs,const typename iScalar<l>::scalar_type rhs)
47{
48 typename iVector<l,N>::tensor_reduced srhs; srhs=rhs;
49 return lhs*srhs;
50}
51template<class l,int N> accelerator_inline iVector<l,N> operator * (const typename iScalar<l>::scalar_type lhs,const iVector<l,N>& rhs) { return rhs*lhs; }
52
53template<class l,int N> accelerator_inline iMatrix<l,N> operator * (const iMatrix<l,N>& lhs,const typename iScalar<l>::scalar_type &rhs)
54{
55 typename iMatrix<l,N>::tensor_reduced srhs; srhs=rhs;
56 return lhs*srhs;
57}
58template<class l,int N> accelerator_inline iMatrix<l,N> operator * (const typename iScalar<l>::scalar_type & lhs,const iMatrix<l,N>& rhs) { return rhs*lhs; }
59
61// Double support; cast to "scalar_type" through constructor
63
64template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
66{
67 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,double>::value,int>::type i=0;
68 typename iScalar<l>::scalar_type t; t=rhs;
69 typename iScalar<l>::tensor_reduced srhs;srhs=t;
70 return lhs*srhs;
71}
72template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
74{
75 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,double>::value,int>::type i=0;
76 return rhs*lhs;
77}
78
79template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
81{
82 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,double>::value,int>::type i=0;
83 typename iScalar<l>::scalar_type t;t=rhs;
84 typename iScalar<l>::tensor_reduced srhs;srhs=t;
85 return lhs*srhs;
86}
87template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
89{
90 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,double>::value,int>::type i=0;
91 return rhs*lhs;
92}
93
94template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
96{
97 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,double>::value,int>::type i=0;
98 typename iScalar<l>::scalar_type t;t=rhs;
99 typename iScalar<l>::tensor_reduced srhs;srhs=t;
100 return lhs*srhs;
101}
102template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
104{
105 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,double>::value,int>::type i=0;
106 return rhs*lhs;
107}
108
110// Complex support; cast to "scalar_type" through constructor
112template<class l,IfNotSame<typename iScalar<l>::scalar_type,ComplexD> = 0>
114{
115 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
116 typename iScalar<l>::scalar_type t;t=rhs;
117 typename iScalar<l>::tensor_reduced srhs;srhs=t;
118
119 return lhs*srhs;
120}
121template<class l,IfNotSame<typename iScalar<l>::scalar_type,ComplexD> = 0>
123{
124 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
125 return rhs*lhs;
126}
127
128template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,ComplexD> = 0>
130{
131 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
132 typename iScalar<l>::scalar_type t;t=rhs;
133 typename iScalar<l>::tensor_reduced srhs;srhs=t;
134 return lhs*srhs;
135}
136template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,ComplexD> = 0>
138{
139 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
140 return rhs*lhs;
141}
142
143template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,ComplexD> = 0>
145{
146 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
147 typename iScalar<l>::scalar_type t;t=rhs;
148 typename iScalar<l>::tensor_reduced srhs;srhs=t;
149 return lhs*srhs;
150}
151template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,ComplexD> = 0>
153{
154 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
155 return rhs*lhs;
156}
157
159// Integer support; cast to "scalar_type" through constructor
161template<class l,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
163{
164 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,ComplexD>::value,int>::type i=0;
165 typename iScalar<l>::scalar_type t; t=rhs;
166 typename iScalar<l>::tensor_reduced srhs; srhs=t;
167 return lhs*srhs;
168}
169template<class l,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
171{
172 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,Integer>::value,int>::type i=0;
173 return rhs*lhs;
174}
175
176template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
178{
179 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,Integer>::value,int>::type i=0;
180 typename iScalar<l>::scalar_type t;t=rhs;
181 typename iScalar<l>::tensor_reduced srhs;srhs=t;
182 return lhs*srhs;
183}
184template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
186{
187 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,Integer>::value,int>::type i=0;
188 return rhs*lhs;
189}
190
191template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
193{
194 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,Integer>::value,int>::type i=0;
195 typename iScalar<l>::scalar_type t;t=rhs;
196 typename iScalar<l>::tensor_reduced srhs;srhs=t;
197 return lhs*srhs;
198}
199template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
201{
202 // typename std::enable_if<!std::is_same<typename iScalar<l>::scalar_type,Integer>::value,int>::type i=0;
203 return rhs*lhs;
204}
205
206
207
209// addition by fundamental scalar type applies to matrix(down diag) and scalar
211template<class l>
213{
214 typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
215 return lhs+srhs;
216}
217template<class l>
218accelerator_inline iScalar<l> operator + (const typename iScalar<l>::scalar_type lhs,const iScalar<l>& rhs) { return rhs+lhs; }
219
220template<class l,int N>
222{
223 typename iMatrix<l,N>::tensor_reduced srhs; srhs=rhs;
224 return lhs+srhs;
225}
226template<class l,int N>
227accelerator_inline iMatrix<l,N> operator + (const typename iScalar<l>::scalar_type lhs,const iMatrix<l,N>& rhs) { return rhs+lhs; }
228
230// Double support; cast to "scalar_type" through constructor
232template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
234{
235 typename iScalar<l>::scalar_type t; t=rhs;
236 typename iScalar<l>::tensor_reduced srhs; srhs=t;
237 return lhs+srhs;
238}
239template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
240accelerator_inline iScalar<l> operator + (double lhs,const iScalar<l>& rhs) { return rhs+lhs; }
241
242template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
244{
245 typename iScalar<l>::scalar_type t;t=rhs;
246 typename iScalar<l>::tensor_reduced srhs;srhs=t;
247 return lhs+srhs;
248}
249template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
250accelerator_inline iMatrix<l,N> operator + (double lhs,const iMatrix<l,N>& rhs) { return rhs+lhs; }
251
252
253// Integer support cast to scalar type through constructor
254template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
256{
257 typename iScalar<l>::scalar_type t; t=rhs;
258 typename iScalar<l>::tensor_reduced srhs; srhs=t;
259 return lhs+srhs;
260}
261
262template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
263accelerator_inline iScalar<l> operator + (Integer lhs,const iScalar<l>& rhs) { return rhs+lhs; }
264
265template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
267{
268 typename iScalar<l>::scalar_type t;t=rhs;
269 typename iScalar<l>::tensor_reduced srhs;srhs=t;
270 return lhs+srhs;
271}
272template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
274
275
277// subtraction of fundamental scalar type applies to matrix(down diag) and scalar
279template<class l>
281{
282 typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
283 return lhs-srhs;
284}
285template<class l>
287{
288 typename iScalar<l>::tensor_reduced slhs;slhs=lhs;
289 return slhs-rhs;
290}
291
292template<class l,int N>
294{
295 typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
296 return lhs-srhs;
297}
298template<class l,int N>
300{
301 typename iScalar<l>::tensor_reduced slhs;slhs=lhs;
302 return slhs-rhs;
303}
304
306// Double support; cast to "scalar_type" through constructor
308template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
310{
311 typename iScalar<l>::scalar_type t; t=rhs;
312 typename iScalar<l>::tensor_reduced srhs; srhs=t;
313 return lhs-srhs;
314}
315template<class l,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
317{
318 typename iScalar<l>::scalar_type t(lhs);
319 typename iScalar<l>::tensor_reduced slhs;slhs=t;
320 return slhs-rhs;
321}
322
323template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
325{
326 typename iScalar<l>::scalar_type t;t=rhs;
327 typename iScalar<l>::tensor_reduced srhs;srhs=t;
328 return lhs-srhs;
329}
330template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,double> = 0>
332{
333 typename iScalar<l>::scalar_type t(lhs);
334 typename iScalar<l>::tensor_reduced slhs;slhs=t;
335 return slhs-rhs;
336}
337
339// Integer support; cast to "scalar_type" through constructor
341template<class l,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
343{
344 typename iScalar<l>::scalar_type t; t=rhs;
345 typename iScalar<l>::tensor_reduced srhs; srhs=t;
346 return lhs-srhs;
347}
348template<class l,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
350{
351 typename iScalar<l>::scalar_type t;t=lhs;
352 typename iScalar<l>::tensor_reduced slhs;slhs=t;
353 return slhs-rhs;
354}
355template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
357{
358 typename iScalar<l>::scalar_type t;t=rhs;
359 typename iScalar<l>::tensor_reduced srhs;srhs=t;
360 return lhs-srhs;
361}
362template<class l,int N,IfNotSame<typename iScalar<l>::scalar_type,Integer> = 0>
364{
365 typename iScalar<l>::scalar_type t;t=lhs;
366 typename iScalar<l>::tensor_reduced slhs;slhs=t;
367 return slhs-rhs;
368}
369
371
372#endif
#define accelerator_inline
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
uint32_t Integer
Definition Simd.h:58
std::complex< RealD > ComplexD
Definition Simd.h:79
accelerator_inline iScalar< l > operator*(const iScalar< l > &lhs, const typename iScalar< l >::scalar_type rhs)
accelerator_inline iScalar< l > operator-(const iScalar< l > &lhs, const typename iScalar< l >::scalar_type rhs)
accelerator_inline iScalar< l > operator+(const iScalar< l > &lhs, const typename iScalar< l >::scalar_type rhs)