1
0
mirror of https://github.com/paboyle/Grid.git synced 2024-11-10 15:55:37 +00:00
Grid/lib/tensors/Tensor_arith_sub.h

143 lines
5.3 KiB
C
Raw Normal View History

2018-01-13 00:31:02 +00:00
/*************************************************************************************
Grid physics library, www.github.com/paboyle/Grid
Source file: ./lib/tensors/Tensor_arith_sub.h
Copyright (C) 2015
Author: Peter Boyle <paboyle@ph.ed.ac.uk>
Author: neo <cossu@post.kek.jp>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
See the full license in the file "LICENSE" in the top level distribution directory
2018-01-13 00:31:02 +00:00
*************************************************************************************/
/* END LEGAL */
2015-04-18 18:54:30 +01:00
#ifndef GRID_MATH_ARITH_SUB_H
#define GRID_MATH_ARITH_SUB_H
2018-01-13 00:31:02 +00:00
NAMESPACE_BEGIN(Grid);
2015-04-18 18:54:30 +01:00
2018-01-13 00:31:02 +00:00
///////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////// SUB ///////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
2015-04-18 18:54:30 +01:00
// SUB is simple for now; cannot mix types and straightforward template
// Scalar +/- Scalar
// Vector +/- Vector
// Matrix +/- Matrix
// Matrix /- scalar
template<class vtype,class ltype,class rtype> accelerator_inline void sub(iScalar<vtype> * __restrict__ ret,
2018-01-13 00:31:02 +00:00
const iScalar<ltype> * __restrict__ lhs,
const iScalar<rtype> * __restrict__ rhs)
2015-04-18 18:54:30 +01:00
{
2018-01-13 00:31:02 +00:00
sub(&ret->_internal,&lhs->_internal,&rhs->_internal);
2015-04-18 18:54:30 +01:00
}
template<class vtype,class ltype,class rtype,int N> accelerator_inline void sub(iVector<vtype,N> * __restrict__ ret,
2018-01-13 00:31:02 +00:00
const iVector<ltype,N> * __restrict__ lhs,
const iVector<rtype,N> * __restrict__ rhs)
2015-04-18 18:54:30 +01:00
{
2018-01-13 00:31:02 +00:00
for(int c=0;c<N;c++){
ret->_internal[c]=lhs->_internal[c]-rhs->_internal[c];
}
return;
2015-04-18 18:54:30 +01:00
}
template<class vtype,class ltype,class rtype, int N> accelerator_inline void sub(iMatrix<vtype,N> * __restrict__ ret,
2018-01-13 00:31:02 +00:00
const iMatrix<ltype,N> * __restrict__ lhs,
const iMatrix<rtype,N> * __restrict__ rhs){
for(int c2=0;c2<N;c2++){
2015-04-18 18:54:30 +01:00
for(int c1=0;c1<N;c1++){
2018-01-13 00:31:02 +00:00
sub(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal[c1][c2]);
2015-04-18 18:54:30 +01:00
}}
2018-01-13 00:31:02 +00:00
return;
2015-04-18 18:54:30 +01:00
}
template<class vtype,class ltype,class rtype, int N> accelerator_inline void sub(iMatrix<vtype,N> * __restrict__ ret,
2018-01-13 00:31:02 +00:00
const iScalar<ltype> * __restrict__ lhs,
const iMatrix<rtype,N> * __restrict__ rhs){
for(int c2=0;c2<N;c2++){
2015-04-18 18:54:30 +01:00
for(int c1=0;c1<N;c1++){
2018-01-13 00:31:02 +00:00
if ( c1==c2) {
sub(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
} else {
// Fails -- need unary minus. Catalogue other unops?
ret->_internal[c1][c2]=zero;
ret->_internal[c1][c2]=ret->_internal[c1][c2]-rhs->_internal[c1][c2];
}
2015-04-18 18:54:30 +01:00
}}
2018-01-13 00:31:02 +00:00
return;
2015-04-18 18:54:30 +01:00
}
template<class vtype,class ltype,class rtype, int N> accelerator_inline void sub(iMatrix<vtype,N> * __restrict__ ret,
2018-01-13 00:31:02 +00:00
const iMatrix<ltype,N> * __restrict__ lhs,
const iScalar<rtype> * __restrict__ rhs){
for(int c2=0;c2<N;c2++){
2015-04-18 18:54:30 +01:00
for(int c1=0;c1<N;c1++){
2018-01-13 00:31:02 +00:00
if ( c1==c2)
sub(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
else
ret->_internal[c1][c2]=lhs->_internal[c1][c2];
2015-04-18 18:54:30 +01:00
}}
2018-01-13 00:31:02 +00:00
return;
2015-04-18 18:54:30 +01:00
}
2018-01-13 00:31:02 +00:00
// - operator for scalar, vector, matrix
template<class ltype,class rtype> accelerator_inline auto
2015-04-18 18:54:30 +01:00
operator - (const iScalar<ltype>& lhs, const iScalar<rtype>& rhs) -> iScalar<decltype(lhs._internal - rhs._internal)>
{
2018-01-13 00:31:02 +00:00
typedef iScalar<decltype(lhs._internal-rhs._internal)> ret_t;
ret_t ret;
sub(&ret,&lhs,&rhs);
return ret;
2015-04-18 18:54:30 +01:00
}
template<class ltype,class rtype,int N>
accelerator_inline auto operator - (const iVector<ltype,N>& lhs,const iVector<rtype,N>& rhs) ->iVector<decltype(lhs._internal[0]-rhs._internal[0]),N>
2015-04-18 18:54:30 +01:00
{
2018-01-13 00:31:02 +00:00
typedef iVector<decltype(lhs._internal[0]-rhs._internal[0]),N> ret_t;
ret_t ret;
sub(&ret,&lhs,&rhs);
return ret;
2015-04-18 18:54:30 +01:00
}
template<class ltype,class rtype,int N>
accelerator_inline auto operator - (const iMatrix<ltype,N>& lhs,const iMatrix<rtype,N>& rhs) ->iMatrix<decltype(lhs._internal[0][0]-rhs._internal[0][0]),N>
2015-04-18 18:54:30 +01:00
{
2018-01-13 00:31:02 +00:00
typedef iMatrix<decltype(lhs._internal[0][0]-rhs._internal[0][0]),N> ret_t;
ret_t ret;
sub(&ret,&lhs,&rhs);
return ret;
2015-04-18 18:54:30 +01:00
}
template<class ltype,class rtype,int N>
accelerator_inline auto operator - (const iScalar<ltype>& lhs,const iMatrix<rtype,N>& rhs)->iMatrix<decltype(lhs._internal-rhs._internal[0][0]),N>
2015-04-18 18:54:30 +01:00
{
2018-01-13 00:31:02 +00:00
typedef iMatrix<decltype(lhs._internal-rhs._internal[0][0]),N> ret_t;
ret_t ret;
sub(&ret,&lhs,&rhs);
return ret;
2015-04-18 18:54:30 +01:00
}
template<class ltype,class rtype,int N>
accelerator_inline auto operator - (const iMatrix<ltype,N>& lhs,const iScalar<rtype>& rhs)->iMatrix<decltype(lhs._internal[0][0]-rhs._internal),N>
2015-04-18 18:54:30 +01:00
{
2018-01-13 00:31:02 +00:00
typedef iMatrix<decltype(lhs._internal[0][0]-rhs._internal),N> ret_t;
ret_t ret;
sub(&ret,&lhs,&rhs);
return ret;
2015-04-18 18:54:30 +01:00
}
2018-01-13 00:31:02 +00:00
NAMESPACE_END(Grid);
2015-04-18 18:54:30 +01:00
#endif