/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/tensors/Tensor_arith_sub.h Copyright (C) 2015 Author: Peter Boyle Author: neo 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 *************************************************************************************/ /* END LEGAL */ #ifndef GRID_MATH_ARITH_SUB_H #define GRID_MATH_ARITH_SUB_H NAMESPACE_BEGIN(Grid); /////////////////////////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////// SUB /////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////////////////////////// // SUB is simple for now; cannot mix types and straightforward template // Scalar +/- Scalar // Vector +/- Vector // Matrix +/- Matrix // Matrix /- scalar template strong_inline void sub(iScalar * __restrict__ ret, const iScalar * __restrict__ lhs, const iScalar * __restrict__ rhs) { sub(&ret->_internal,&lhs->_internal,&rhs->_internal); } template strong_inline void sub(iVector * __restrict__ ret, const iVector * __restrict__ lhs, const iVector * __restrict__ rhs) { for(int c=0;c_internal[c]=lhs->_internal[c]-rhs->_internal[c]; } return; } template strong_inline void sub(iMatrix * __restrict__ ret, const iMatrix * __restrict__ lhs, const iMatrix * __restrict__ rhs){ for(int c2=0;c2_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal[c1][c2]); }} return; } template strong_inline void sub(iMatrix * __restrict__ ret, const iScalar * __restrict__ lhs, const iMatrix * __restrict__ rhs){ for(int c2=0;c2_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]; } }} return; } template strong_inline void sub(iMatrix * __restrict__ ret, const iMatrix * __restrict__ lhs, const iScalar * __restrict__ rhs){ for(int c2=0;c2_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal); else ret->_internal[c1][c2]=lhs->_internal[c1][c2]; }} return; } // - operator for scalar, vector, matrix template strong_inline auto operator - (const iScalar& lhs, const iScalar& rhs) -> iScalar { typedef iScalar ret_t; ret_t ret; sub(&ret,&lhs,&rhs); return ret; } template strong_inline auto operator - (const iVector& lhs,const iVector& rhs) ->iVector { typedef iVector ret_t; ret_t ret; sub(&ret,&lhs,&rhs); return ret; } template strong_inline auto operator - (const iMatrix& lhs,const iMatrix& rhs) ->iMatrix { typedef iMatrix ret_t; ret_t ret; sub(&ret,&lhs,&rhs); return ret; } template strong_inline auto operator - (const iScalar& lhs,const iMatrix& rhs)->iMatrix { typedef iMatrix ret_t; ret_t ret; sub(&ret,&lhs,&rhs); return ret; } template strong_inline auto operator - (const iMatrix& lhs,const iScalar& rhs)->iMatrix { typedef iMatrix ret_t; ret_t ret; sub(&ret,&lhs,&rhs); return ret; } NAMESPACE_END(Grid); #endif