#ifndef GRID_MATH_TENSORS_H #define GRID_MATH_TENSORS_H namespace Grid { /////////////////////////////////////////////////// // Scalar, Vector, Matrix objects. // These can be composed to form tensor products of internal indices. /////////////////////////////////////////////////// template class iScalar { public: vtype _internal; typedef typename GridTypeMapper::scalar_type scalar_type; typedef typename GridTypeMapper::vector_type vector_type; typedef typename GridTypeMapper::tensor_reduced tensor_reduced_v; typedef iScalar tensor_reduced; enum { TensorLevel = GridTypeMapper::TensorLevel + 1}; // Scalar no action // template using tensor_reduce_level = typename iScalar::tensor_reduce_level >; iScalar(){}; iScalar(scalar_type s) : _internal(s) {};// recurse down and hit the constructor for vector_type iScalar(Zero &z){ *this = zero; }; iScalar & operator= (const Zero &hero){ zeroit(*this); return *this; } friend void zeroit(iScalar &that){ zeroit(that._internal); } friend void permute(iScalar &out,const iScalar &in,int permutetype){ permute(out._internal,in._internal,permutetype); } friend void extract(const iScalar &in,std::vector &out){ extract(in._internal,out); // extract advances the pointers in out } friend void merge(iScalar &in,std::vector &out){ merge(in._internal,out); // extract advances the pointers in out } // Unary negation friend inline iScalar operator -(const iScalar &r) { iScalar ret; ret._internal= -r._internal; return ret; } // *=,+=,-= operators inherit from corresponding "*,-,+" behaviour inline iScalar &operator *=(const iScalar &r) { *this = (*this)*r; return *this; } inline iScalar &operator -=(const iScalar &r) { *this = (*this)-r; return *this; } inline iScalar &operator +=(const iScalar &r) { *this = (*this)+r; return *this; } inline vtype & operator ()(void) { return _internal; } operator ComplexD () const { return(TensorRemove(_internal)); }; operator RealD () const { return(real(TensorRemove(_internal))); } }; /////////////////////////////////////////////////////////// // Allows to turn scalar>>> back to double. /////////////////////////////////////////////////////////// template inline typename std::enable_if::notvalue, T>::type TensorRemove(T arg) { return arg;} template inline auto TensorRemove(iScalar arg) -> decltype(TensorRemove(arg._internal)) { return TensorRemove(arg._internal); } template class iVector { public: vtype _internal[N]; typedef typename GridTypeMapper::scalar_type scalar_type; typedef typename GridTypeMapper::vector_type vector_type; typedef typename GridTypeMapper::tensor_reduced tensor_reduced_v; enum { TensorLevel = GridTypeMapper::TensorLevel + 1}; typedef iScalar tensor_reduced; iVector(Zero &z){ *this = zero; }; iVector() {};// Empty constructure iVector & operator= (Zero &hero){ zeroit(*this); return *this; } friend void zeroit(iVector &that){ for(int i=0;i &out,const iVector &in,int permutetype){ for(int i=0;i &in,std::vector &out){ for(int i=0;i &in,std::vector &out){ for(int i=0;i operator -(const iVector &r) { iVector ret; for(int i=0;i &operator *=(const iScalar &r) { *this = (*this)*r; return *this; } inline iVector &operator -=(const iVector &r) { *this = (*this)-r; return *this; } inline iVector &operator +=(const iVector &r) { *this = (*this)+r; return *this; } inline vtype & operator ()(int i) { return _internal[i]; } }; template class iMatrix { public: vtype _internal[N][N]; typedef typename GridTypeMapper::scalar_type scalar_type; typedef typename GridTypeMapper::vector_type vector_type; typedef typename GridTypeMapper::tensor_reduced tensor_reduced_v; enum { TensorLevel = GridTypeMapper::TensorLevel + 1}; typedef iScalar tensor_reduced; iMatrix(Zero &z){ *this = zero; }; iMatrix() {}; iMatrix & operator= (Zero &hero){ zeroit(*this); return *this; } friend void zeroit(iMatrix &that){ for(int i=0;i &out,const iMatrix &in,int permutetype){ for(int i=0;i &in,std::vector &out){ for(int i=0;i &in,std::vector &out){ for(int i=0;i operator -(const iMatrix &r) { iMatrix ret; for(int i=0;i inline iMatrix &operator *=(const T &r) { *this = (*this)*r; return *this; } template inline iMatrix &operator -=(const T &r) { *this = (*this)-r; return *this; } template inline iMatrix &operator +=(const T &r) { *this = (*this)+r; return *this; } inline vtype & operator ()(int i,int j) { return _internal[i][j]; } }; } #endif