#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 inline iScalar operator * (const iScalar& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs(rhs); return lhs*srhs; } template inline iScalar operator * (const typename iScalar::scalar_type lhs,const iScalar& rhs) { return rhs*lhs; } template inline iVector operator * (const iVector& lhs,const typename iScalar::scalar_type rhs) { typename iVector::tensor_reduced srhs(rhs); return lhs*srhs; } template inline iVector operator * (const typename iScalar::scalar_type lhs,const iVector& rhs) { return rhs*lhs; } template inline iMatrix operator * (const iMatrix& lhs,const typename iScalar::scalar_type &rhs) { typename iMatrix::tensor_reduced srhs(rhs); return lhs*srhs; } template inline iMatrix operator * (const typename iScalar::scalar_type & lhs,const iMatrix& rhs) { return rhs*lhs; } //////////////////////////////////////////////////////////////////// // Double support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template inline iScalar operator * (const iScalar& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iScalar operator * (double lhs,const iScalar& rhs) { return rhs*lhs; } template inline iVector operator * (const iVector& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iVector operator * (double lhs,const iVector& rhs) { return rhs*lhs; } template inline iMatrix operator * (const iMatrix& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iMatrix operator * (double lhs,const iMatrix& rhs) { return rhs*lhs; } //////////////////////////////////////////////////////////////////// // Complex support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template inline iScalar operator * (const iScalar& lhs,ComplexD rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iScalar operator * (ComplexD lhs,const iScalar& rhs) { return rhs*lhs; } template inline iVector operator * (const iVector& lhs,ComplexD rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iVector operator * (ComplexD lhs,const iVector& rhs) { return rhs*lhs; } template inline iMatrix operator * (const iMatrix& lhs,ComplexD rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iMatrix operator * (ComplexD lhs,const iMatrix& rhs) { return rhs*lhs; } //////////////////////////////////////////////////////////////////// // Integer support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template inline iScalar operator * (const iScalar& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iScalar operator * (Integer lhs,const iScalar& rhs) { return rhs*lhs; } template inline iVector operator * (const iVector& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iVector operator * (Integer lhs,const iVector& rhs) { return rhs*lhs; } template inline iMatrix operator * (const iMatrix& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs*srhs; } template inline iMatrix operator * (Integer lhs,const iMatrix& rhs) { return rhs*lhs; } /////////////////////////////////////////////////////////////////////////////////////////////// // addition by fundamental scalar type applies to matrix(down diag) and scalar /////////////////////////////////////////////////////////////////////////////////////////////// template inline iScalar operator + (const iScalar& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs(rhs); return lhs+srhs; } template inline iScalar operator + (const typename iScalar::scalar_type lhs,const iScalar& rhs) { return rhs+lhs; } template inline iMatrix operator + (const iMatrix& lhs,const typename iScalar::scalar_type rhs) { typename iMatrix::tensor_reduced srhs(rhs); return lhs+srhs; } template inline iMatrix operator + (const typename iScalar::scalar_type lhs,const iMatrix& rhs) { return rhs+lhs; } //////////////////////////////////////////////////////////////////// // Double support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template inline iScalar operator + (const iScalar& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs+srhs; } template inline iScalar operator + (double lhs,const iScalar& rhs) { return rhs+lhs; } template inline iMatrix operator + (const iMatrix& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs+srhs; } template inline iMatrix operator + (double lhs,const iMatrix& rhs) { return rhs+lhs; } // Integer support cast to scalar type through constructor template inline iScalar operator + (const iScalar& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs+srhs; } template inline iScalar operator + (Integer lhs,const iScalar& rhs) { return rhs+lhs; } template inline iMatrix operator + (const iMatrix& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs+srhs; } template inline iMatrix operator + (Integer lhs,const iMatrix& rhs) { return rhs+lhs; } /////////////////////////////////////////////////////////////////////////////////////////////// // subtraction of fundamental scalar type applies to matrix(down diag) and scalar /////////////////////////////////////////////////////////////////////////////////////////////// template inline iScalar operator - (const iScalar& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs(rhs); return lhs-srhs; } template inline iScalar operator - (const typename iScalar::scalar_type lhs,const iScalar& rhs) { typename iScalar::tensor_reduced slhs(lhs); return slhs-rhs; } template inline iMatrix operator - (const iMatrix& lhs,const typename iScalar::scalar_type rhs) { typename iScalar::tensor_reduced srhs(rhs); return lhs-srhs; } template inline iMatrix operator - (const typename iScalar::scalar_type lhs,const iMatrix& rhs) { typename iScalar::tensor_reduced slhs(lhs); return slhs-rhs; } //////////////////////////////////////////////////////////////////// // Double support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template inline iScalar operator - (const iScalar& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs-srhs; } template inline iScalar operator - (double lhs,const iScalar& rhs) { typename iScalar::scalar_type t(lhs); typename iScalar::tensor_reduced slhs(t); return slhs-rhs; } template inline iMatrix operator - (const iMatrix& lhs,double rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs-srhs; } template inline iMatrix operator - (double lhs,const iMatrix& rhs) { typename iScalar::scalar_type t(lhs); typename iScalar::tensor_reduced slhs(t); return slhs-rhs; } //////////////////////////////////////////////////////////////////// // Integer support; cast to "scalar_type" through constructor //////////////////////////////////////////////////////////////////// template inline iScalar operator - (const iScalar& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs-srhs; } template inline iScalar operator - (Integer lhs,const iScalar& rhs) { typename iScalar::scalar_type t(lhs); typename iScalar::tensor_reduced slhs(t); return slhs-rhs; } template inline iMatrix operator - (const iMatrix& lhs,Integer rhs) { typename iScalar::scalar_type t(rhs); typename iScalar::tensor_reduced srhs(t); return lhs-srhs; } template inline iMatrix operator - (Integer lhs,const iMatrix& rhs) { typename iScalar::scalar_type t(lhs); typename iScalar::tensor_reduced slhs(t); return slhs-rhs; } } #endif