#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