/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/tensors/Tensor_arith_scalar.h Copyright (C) 2015 Author: Azusa Yamaguchi Author: Peter Boyle 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_SCALAR_H #define GRID_MATH_ARITH_SCALAR_H namespace Grid { ////////////////////////////////////////////////////////////////////////////////////////// // Must support native C++ types Integer, Complex, Real ////////////////////////////////////////////////////////////////////////////////////////// // multiplication by fundamental scalar type template strong_inline iScalar operator * (const iScalar& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs; srhs=rhs; return lhs*srhs; } template strong_inline iScalar operator * (const typename iScalar::scalar_type lhs,const iScalar& rhs) { return rhs*lhs; } template strong_inline iVector operator * (const iVector& lhs,const typename iScalar::scalar_type rhs) { typename iVector::tensor_reduced srhs; srhs=rhs; return lhs*srhs; } template strong_inline iVector operator * (const typename iScalar::scalar_type lhs,const iVector& rhs) { return rhs*lhs; } template strong_inline iMatrix operator * (const iMatrix& lhs,const typename iScalar::scalar_type &rhs) { typename iMatrix::tensor_reduced srhs; srhs=rhs; return lhs*srhs; } template strong_inline iMatrix operator * (const typename iScalar::scalar_type & lhs,const iMatrix& rhs) { return rhs*lhs; } //////////////////////////////////////////////////////////////////// // Double support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template strong_inline iScalar operator * (const iScalar& lhs,double rhs) { typename iScalar::scalar_type t; t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iScalar operator * (double lhs,const iScalar& rhs) { return rhs*lhs; } template strong_inline iVector operator * (const iVector& lhs,double rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iVector operator * (double lhs,const iVector& rhs) { return rhs*lhs; } template strong_inline iMatrix operator * (const iMatrix& lhs,double rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iMatrix operator * (double lhs,const iMatrix& rhs) { return rhs*lhs; } //////////////////////////////////////////////////////////////////// // Complex support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template strong_inline iScalar operator * (const iScalar& lhs,ComplexD rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iScalar operator * (ComplexD lhs,const iScalar& rhs) { return rhs*lhs; } template strong_inline iVector operator * (const iVector& lhs,ComplexD rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iVector operator * (ComplexD lhs,const iVector& rhs) { return rhs*lhs; } template strong_inline iMatrix operator * (const iMatrix& lhs,ComplexD rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iMatrix operator * (ComplexD lhs,const iMatrix& rhs) { return rhs*lhs; } //////////////////////////////////////////////////////////////////// // Integer support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template strong_inline iScalar operator * (const iScalar& lhs,Integer rhs) { typename iScalar::scalar_type t; t=rhs; typename iScalar::tensor_reduced srhs; srhs=t; return lhs*srhs; } template strong_inline iScalar operator * (Integer lhs,const iScalar& rhs) { return rhs*lhs; } template strong_inline iVector operator * (const iVector& lhs,Integer rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iVector operator * (Integer lhs,const iVector& rhs) { return rhs*lhs; } template strong_inline iMatrix operator * (const iMatrix& lhs,Integer rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs*srhs; } template strong_inline iMatrix operator * (Integer lhs,const iMatrix& rhs) { return rhs*lhs; } /////////////////////////////////////////////////////////////////////////////////////////////// // addition by fundamental scalar type applies to matrix(down diag) and scalar /////////////////////////////////////////////////////////////////////////////////////////////// template strong_inline iScalar operator + (const iScalar& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs; srhs=rhs; return lhs+srhs; } template strong_inline iScalar operator + (const typename iScalar::scalar_type lhs,const iScalar& rhs) { return rhs+lhs; } template strong_inline iMatrix operator + (const iMatrix& lhs,const typename iScalar::scalar_type rhs) { typename iMatrix::tensor_reduced srhs; srhs=rhs; return lhs+srhs; } template strong_inline iMatrix operator + (const typename iScalar::scalar_type lhs,const iMatrix& rhs) { return rhs+lhs; } //////////////////////////////////////////////////////////////////// // Double support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template strong_inline iScalar operator + (const iScalar& lhs,double rhs) { typename iScalar::scalar_type t; t=rhs; typename iScalar::tensor_reduced srhs; srhs=t; return lhs+srhs; } template strong_inline iScalar operator + (double lhs,const iScalar& rhs) { return rhs+lhs; } template strong_inline iMatrix operator + (const iMatrix& lhs,double rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs+srhs; } template strong_inline iMatrix operator + (double lhs,const iMatrix& rhs) { return rhs+lhs; } // Integer support cast to scalar type through constructor template strong_inline iScalar operator + (const iScalar& lhs,Integer rhs) { typename iScalar::scalar_type t; t=rhs; typename iScalar::tensor_reduced srhs; srhs=t; return lhs+srhs; } template strong_inline iScalar operator + (Integer lhs,const iScalar& rhs) { return rhs+lhs; } template strong_inline iMatrix operator + (const iMatrix& lhs,Integer rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs+srhs; } template strong_inline iMatrix operator + (Integer lhs,const iMatrix& rhs) { return rhs+lhs; } /////////////////////////////////////////////////////////////////////////////////////////////// // subtraction of fundamental scalar type applies to matrix(down diag) and scalar /////////////////////////////////////////////////////////////////////////////////////////////// template strong_inline iScalar operator - (const iScalar& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs; srhs=rhs; return lhs-srhs; } template strong_inline iScalar operator - (const typename iScalar::scalar_type lhs,const iScalar& rhs) { typename iScalar::tensor_reduced slhs;slhs=lhs; return slhs-rhs; } template strong_inline iMatrix operator - (const iMatrix& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs; srhs=rhs; return lhs-srhs; } template strong_inline iMatrix operator - (const typename iScalar::scalar_type lhs,const iMatrix& rhs) { typename iScalar::tensor_reduced slhs;slhs=lhs; return slhs-rhs; } //////////////////////////////////////////////////////////////////// // Double support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template strong_inline iScalar operator - (const iScalar& lhs,double rhs) { typename iScalar::scalar_type t; t=rhs; typename iScalar::tensor_reduced srhs; srhs=t; return lhs-srhs; } template strong_inline iScalar operator - (double lhs,const iScalar& rhs) { typename iScalar::scalar_type t(lhs); typename iScalar::tensor_reduced slhs;slhs=t; return slhs-rhs; } template strong_inline iMatrix operator - (const iMatrix& lhs,double rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs-srhs; } template strong_inline iMatrix operator - (double lhs,const iMatrix& rhs) { typename iScalar::scalar_type t(lhs); typename iScalar::tensor_reduced slhs;slhs=t; return slhs-rhs; } //////////////////////////////////////////////////////////////////// // Integer support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template strong_inline iScalar operator - (const iScalar& lhs,Integer rhs) { typename iScalar::scalar_type t; t=rhs; typename iScalar::tensor_reduced srhs; srhs=t; return lhs-srhs; } template strong_inline iScalar operator - (Integer lhs,const iScalar& rhs) { typename iScalar::scalar_type t;t=lhs; typename iScalar::tensor_reduced slhs;slhs=t; return slhs-rhs; } template strong_inline iMatrix operator - (const iMatrix& lhs,Integer rhs) { typename iScalar::scalar_type t;t=rhs; typename iScalar::tensor_reduced srhs;srhs=t; return lhs-srhs; } template strong_inline iMatrix operator - (Integer lhs,const iMatrix& rhs) { typename iScalar::scalar_type t;t=lhs; typename iScalar::tensor_reduced slhs;slhs=t; return slhs-rhs; } } #endif