Grid 0.7.0
Tensor_arith_add.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_add.h
6
7 Copyright (C) 2015
8
9Author: Peter Boyle <paboyle@ph.ed.ac.uk>
10Author: neo <cossu@post.kek.jp>
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_ADD_H
30#define GRID_MATH_ARITH_ADD_H
31
33
37
38// ADD is simple for now; cannot mix types and straightforward template
39// Scalar +/- Scalar
40// Vector +/- Vector
41// Matrix +/- Matrix
42template<class vtype,class ltype,class rtype> accelerator_inline void add(iScalar<vtype> * __restrict__ ret,
43 const iScalar<ltype> * __restrict__ lhs,
44 const iScalar<rtype> * __restrict__ rhs)
45{
46 add(&ret->_internal,&lhs->_internal,&rhs->_internal);
47}
48template<class vtype,class ltype,class rtype,int N> accelerator_inline void add(iVector<vtype,N> * __restrict__ ret,
49 const iVector<ltype,N> * __restrict__ lhs,
50 const iVector<rtype,N> * __restrict__ rhs)
51{
52 for(int c=0;c<N;c++){
53 ret->_internal[c]=lhs->_internal[c]+rhs->_internal[c];
54 }
55 return;
56}
57
58template<class vtype,class ltype,class rtype, int N> accelerator_inline void add(iMatrix<vtype,N> * __restrict__ ret,
59 const iMatrix<ltype,N> * __restrict__ lhs,
60 const iMatrix<rtype,N> * __restrict__ rhs)
61{
62 for(int c2=0;c2<N;c2++){
63 for(int c1=0;c1<N;c1++){
64 add(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal[c1][c2]);
65 }}
66 return;
67}
68template<class vtype,class ltype,class rtype, int N> accelerator_inline void add(iMatrix<vtype,N> * __restrict__ ret,
69 const iScalar<ltype> * __restrict__ lhs,
70 const iMatrix<rtype,N> * __restrict__ rhs)
71{
72 for(int c2=0;c2<N;c2++){
73 for(int c1=0;c1<N;c1++){
74 if ( c1==c2)
75 add(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
76 else
77 ret->_internal[c1][c2]=lhs->_internal[c1][c2];
78 }}
79 return;
80}
81template<class vtype,class ltype,class rtype, int N> accelerator_inline void add(iMatrix<vtype,N> * __restrict__ ret,
82 const iMatrix<ltype,N> * __restrict__ lhs,
83 const iScalar<rtype> * __restrict__ rhs)
84{
85 for(int c2=0;c2<N;c2++){
86 for(int c1=0;c1<N;c1++){
87 if ( c1==c2)
88 add(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
89 else
90 ret->_internal[c1][c2]=lhs->_internal[c1][c2];
91 }}
92 return;
93}
94
95
96// + operator for scalar, vector, matrix
97template<class ltype,class rtype>
98accelerator_inline auto operator + (const iScalar<ltype>& lhs,const iScalar<rtype>& rhs) -> iScalar<decltype(lhs._internal + rhs._internal)>
99{
100 typedef iScalar<decltype(lhs._internal+rhs._internal)> ret_t;
101 ret_t ret ;
102 add(&ret,&lhs,&rhs);
103 return ret;
104}
105template<class ltype,class rtype,int N>
106accelerator_inline auto operator + (const iVector<ltype,N>& lhs,const iVector<rtype,N>& rhs) ->iVector<decltype(lhs._internal[0]+rhs._internal[0]),N>
107{
108 typedef iVector<decltype(lhs._internal[0]+rhs._internal[0]),N> ret_t;
109 ret_t ret ;
110 add(&ret,&lhs,&rhs);
111 return ret;
112}
113template<class ltype,class rtype,int N>
114accelerator_inline auto operator + (const iMatrix<ltype,N>& lhs,const iMatrix<rtype,N>& rhs) ->iMatrix<decltype(lhs._internal[0][0]+rhs._internal[0][0]),N>
115{
116 typedef iMatrix<decltype(lhs._internal[0][0]+rhs._internal[0][0]),N> ret_t;
117 ret_t ret ;
118 add(&ret,&lhs,&rhs);
119 return ret;
120}
121template<class ltype,class rtype,int N>
122accelerator_inline auto operator + (const iScalar<ltype>& lhs,const iMatrix<rtype,N>& rhs)->iMatrix<decltype(lhs._internal+rhs._internal[0][0]),N>
123{
124 typedef iMatrix<decltype(lhs._internal+rhs._internal[0][0]),N> ret_t;
125 ret_t ret ;
126 add(&ret,&lhs,&rhs);
127 return ret;
128}
129
130template<class ltype,class rtype,int N>
131accelerator_inline auto operator + (const iMatrix<ltype,N>& lhs,const iScalar<rtype>& rhs)->iMatrix<decltype(lhs._internal[0][0]+rhs._internal),N>
132{
133 typedef iMatrix<decltype(lhs._internal[0][0]+rhs._internal),N> ret_t;
134 ret_t ret ;
135 add(&ret,&lhs,&rhs);
136 return ret;
137}
138
140
141
142#endif
#define accelerator_inline
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
accelerator_inline void add(iScalar< vtype > *__restrict__ ret, const iScalar< ltype > *__restrict__ lhs, const iScalar< rtype > *__restrict__ rhs)
accelerator_inline auto operator+(const iScalar< ltype > &lhs, const iScalar< rtype > &rhs) -> iScalar< decltype(lhs._internal+rhs._internal)>
vtype _internal[N][N]
vtype _internal
vtype _internal[N]