/************************************************************************************* Grid physics library, www.github.com/paboyle/Grid Source file: ./lib/tensors/Tensor_index.h Copyright (C) 2015 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_TENSOR_INDEX_H #define GRID_TENSOR_INDEX_H //////////////////////////////////////////////////////////////////////////////////////// // Recursion for trace, transpose, peek, poke a specific index //////////////////////////////////////////////////////////////////////////////////////// // Allow trace to recurse if vector, but never terminate on a vector // trace of a different index can distribute across the vector index in a replicated way // but we do not trace a vector index. NAMESPACE_BEGIN(Grid); /* Needed? template accelerator_inline ComplexF traceIndex(const ComplexF arg) { return arg;} template accelerator_inline ComplexD traceIndex(const ComplexD arg) { return arg;} template accelerator_inline RealF traceIndex(const RealF arg) { return arg;} template accelerator_inline RealD traceIndex(const RealD arg) { return arg;} */ template class TensorIndexRecursion { public: //////////////////////////////////////////////////// // Type Queries //////////////////////////////////////////////////// template static accelerator_inline int indexRank(const iScalar tmp) { return TensorIndexRecursion::indexRank(tmp._internal); } template static accelerator_inline int indexRank(const iVector tmp){ return TensorIndexRecursion::indexRank(tmp._internal[0]); } template static accelerator_inline int indexRank(const iMatrix tmp){ return TensorIndexRecursion::indexRank(tmp._internal[0][0]); } template static accelerator_inline int isScalar(const iScalar tmp) { return TensorIndexRecursion::isScalar(tmp._internal); } template static accelerator_inline int isScalar(const iVector tmp){ return TensorIndexRecursion::isScalar(tmp._internal[0]); } template static accelerator_inline int isScalar(const iMatrix tmp){ return TensorIndexRecursion::isScalar(tmp._internal[0][0]); } template static accelerator_inline int isVector(const iScalar tmp) { return TensorIndexRecursion::isVector(tmp._internal); } template static accelerator_inline int isVector(const iVector tmp){ return TensorIndexRecursion::isVector(tmp._internal[0]); } template static accelerator_inline int isVector(const iMatrix tmp){ return TensorIndexRecursion::isVector(tmp._internal[0][0]); } template static accelerator_inline int isMatrix(const iScalar tmp) { return TensorIndexRecursion::isMatrix(tmp._internal); } template static accelerator_inline int isMatrix(const iVector tmp){ return TensorIndexRecursion::isMatrix(tmp._internal[0]); } template static accelerator_inline int isMatrix(const iMatrix tmp){ return TensorIndexRecursion::isMatrix(tmp._internal[0][0]); } //////////////////////////////////////////////////// // Trace //////////////////////////////////////////////////// template static accelerator_inline auto traceIndex(const iScalar arg) -> iScalar::traceIndex(arg._internal))> { iScalar::traceIndex(arg._internal))> ret; ret._internal = TensorIndexRecursion::traceIndex(arg._internal); return ret; } template static accelerator_inline auto traceIndex(const iVector arg) -> iVector::traceIndex(arg._internal[0])),N> { iVector::traceIndex(arg._internal[0])),N> ret; for(int i=0;i::traceIndex(arg._internal[i]); } return ret; } template static accelerator_inline auto traceIndex(const iMatrix arg) -> iMatrix::traceIndex(arg._internal[0][0])),N> { iMatrix::traceIndex(arg._internal[0][0])),N> ret; for(int i=0;i::traceIndex(arg._internal[i][j]); }} return ret; } //////////////////////////////////////////// // Recursion for peeking a specific index //////////////////////////////////////////// template static accelerator_inline auto peekIndex(const iScalar arg,int i) -> iScalar::peekIndex(arg._internal,0))> { iScalar::peekIndex(arg._internal,0))> ret; ret._internal = TensorIndexRecursion::peekIndex(arg._internal,i); return ret; } template static accelerator_inline auto peekIndex(const iScalar arg,int i,int j) -> iScalar::peekIndex(arg._internal,0,0))> { iScalar::peekIndex(arg._internal,0,0))> ret; ret._internal = TensorIndexRecursion::peekIndex(arg._internal,i,j); return ret; } template static accelerator_inline auto peekIndex(const iVector arg,int ii) -> iVector::peekIndex(arg._internal[0],0)),N> { iVector::peekIndex(arg._internal[0],0)),N> ret; for(int i=0;i::peekIndex(arg._internal[i],ii); } return ret; } template static accelerator_inline auto peekIndex(const iVector arg,int ii,int jj) -> iVector::peekIndex(arg._internal[0],0,0)),N> { iVector::peekIndex(arg._internal[0],0,0)),N> ret; for(int i=0;i::peekIndex(arg._internal[i],ii,jj); } return ret; } template static accelerator_inline auto peekIndex(const iMatrix arg,int ii) -> iMatrix::peekIndex(arg._internal[0][0],0)),N> { iMatrix::peekIndex(arg._internal[0][0],0)),N> ret; for(int i=0;i::peekIndex(arg._internal[i][j],ii); }} return ret; } template static accelerator_inline auto peekIndex(const iMatrix arg,int ii,int jj) -> iMatrix::peekIndex(arg._internal[0][0],0,0)),N> { iMatrix::peekIndex(arg._internal[0][0],0,0)),N> ret; for(int i=0;i::peekIndex(arg._internal[i][j],ii,jj); }} return ret; } //////////////////////////////////////////// // Recursion for poking a specific index //////////////////////////////////////////// template accelerator_inline static void pokeIndex(iScalar &ret, const iScalar::peekIndex(ret._internal,0))> &arg, int i) { TensorIndexRecursion::pokeIndex(ret._internal,arg._internal,i); } template accelerator_inline static void pokeIndex(iScalar &ret, const iScalar::peekIndex(ret._internal,0,0))> &arg, int i,int j) { TensorIndexRecursion::pokeIndex(ret._internal,arg._internal,i,j); } template accelerator_inline static void pokeIndex(iVector &ret, const iVector::peekIndex(ret._internal[0],0)),N> &arg, int i) { for(int ii=0;ii::pokeIndex(ret._internal[ii],arg._internal[ii],i); } } template accelerator_inline static void pokeIndex(iVector &ret, const iVector::peekIndex(ret._internal[0],0,0)),N> &arg, int i,int j) { for(int ii=0;ii::pokeIndex(ret._internal[ii],arg._internal[ii],i,j); } } template accelerator_inline static void pokeIndex(iMatrix &ret, const iMatrix::peekIndex(ret._internal[0][0],0)),N> &arg, int i) { for(int ii=0;ii::pokeIndex(ret._internal[ii][jj],arg._internal[ii][jj],i); }} } template accelerator_inline static void pokeIndex(iMatrix &ret, const iMatrix::peekIndex(ret._internal[0][0],0,0)),N> &arg, int i,int j) { for(int ii=0;ii::pokeIndex(ret._internal[ii][jj],arg._internal[ii][jj],i,j); }} } //////////////////////////////////////////// // Recursion for transposing a specific index //////////////////////////////////////////// template accelerator_inline static auto transposeIndex(const iScalar arg) -> iScalar { iScalar ret; ret._internal = TensorIndexRecursion::transposeIndex(arg._internal); return ret; } template accelerator_inline static auto transposeIndex(const iVector arg) -> iVector { iVector ret; for(int i=0;i::transposeIndex(arg._internal[i]); } return ret; } template accelerator_inline static auto transposeIndex(const iMatrix arg) -> iMatrix { iMatrix ret; for(int i=0;i::transposeIndex(arg._internal[i][j]); }} return ret; } }; //////////////////////////// // strip const & ref quali's //////////////////////////// #define RemoveCRV(a) typename std::remove_const::type>::type template<> class TensorIndexRecursion<0> { public: //////////////////////////////////////////////////// // Type Queries //////////////////////////////////////////////////// template static accelerator_inline int indexRank(const iScalar tmp) { return 1; } template static accelerator_inline int indexRank(const iVector tmp){ return N; } template static accelerator_inline int indexRank(const iMatrix tmp){ return N; } template static accelerator_inline int isScalar(const iScalar tmp) { return true;} template static accelerator_inline int isScalar(const iVector tmp){ return false;} template static accelerator_inline int isScalar(const iMatrix tmp){ return false;} template static accelerator_inline int isVector(const iScalar tmp) { return false;} template static accelerator_inline int isVector(const iVector tmp){ return true;} template static accelerator_inline int isVector(const iMatrix tmp){ return false;} template static accelerator_inline int isMatrix(const iScalar tmp) { return false;} template static accelerator_inline int isMatrix(const iVector tmp){ return false;} template static accelerator_inline int isMatrix(const iMatrix tmp){ return true;} ///////////////////////////////////////// // Ends recursion for trace (scalar/vector/matrix) ///////////////////////////////////////// template accelerator_inline static auto traceIndex(const iScalar arg) -> iScalar { iScalar ret; ret._internal = arg._internal; return ret; } template accelerator_inline static auto traceIndex(const iVector arg) -> iScalar { iScalar ret; ret._internal=zero; for(int i=0;i accelerator_inline static auto traceIndex(const iMatrix arg) -> iScalar { iScalar ret; zeroit(ret); for(int i=0;i accelerator_inline static auto transposeIndex(const iScalar arg) -> iScalar { iScalar ret; ret._internal = arg._internal; return ret; } template accelerator_inline static auto transposeIndex(const iMatrix arg) -> iMatrix { iMatrix ret; ret=zero; for(int i=0;i accelerator_inline static auto peekIndex(const iVector arg,int ii) -> iScalar { iScalar ret; ret._internal = arg._internal[ii]; return ret; } template accelerator_inline static auto peekIndex(const iMatrix arg,int ii,int jj) -> iScalar { iScalar ret; ret._internal = arg._internal[ii][jj]; return ret; } // Vector poke, one index template accelerator_inline static void pokeIndex(iVector &ret, const iScalar &arg,int i) { ret._internal[i] = arg._internal; } // Matrix poke two indices template accelerator_inline static void pokeIndex(iMatrix &ret, const iScalar &arg,int i,int j) { ret._internal[i][j] = arg._internal; } }; //////////////////////////////////////////////////////////////////////////////////////////////////////// // External wrappers //////////////////////////////////////////////////////////////////////////////////////////////////////// template accelerator_inline int indexRank(void) { vtype tmp; return TensorIndexRecursion::indexRank(tmp); } template accelerator_inline int isScalar(void) { vtype tmp; return TensorIndexRecursion::isScalar(tmp); } template accelerator_inline int isVector(void) { vtype tmp; return TensorIndexRecursion::isVector(tmp); } template accelerator_inline int isMatrix(void) { vtype tmp; return TensorIndexRecursion::isMatrix(tmp); } template accelerator_inline auto traceIndex (const vtype &arg) -> RemoveCRV(TensorIndexRecursion::traceIndex(arg)) { RemoveCRV(TensorIndexRecursion::traceIndex(arg)) ret; ret=TensorIndexRecursion::traceIndex(arg); return ret; } template accelerator_inline auto transposeIndex (const vtype &arg) -> RemoveCRV(TensorIndexRecursion::transposeIndex(arg)) { RemoveCRV(TensorIndexRecursion::transposeIndex(arg)) ret; ret=TensorIndexRecursion::transposeIndex(arg); return ret; } template accelerator_inline auto peekIndex (const vtype &arg,int i) -> RemoveCRV(TensorIndexRecursion::peekIndex(arg,0)) { RemoveCRV(TensorIndexRecursion::peekIndex(arg,0)) ret; ret=TensorIndexRecursion::peekIndex(arg,i); return ret; } template accelerator_inline auto peekIndex (const vtype &arg,int i,int j) -> RemoveCRV(TensorIndexRecursion::peekIndex(arg,0,0)) { RemoveCRV(TensorIndexRecursion::peekIndex(arg,0,0)) ret; ret=TensorIndexRecursion::peekIndex(arg,i,j); return ret; } template accelerator_inline void pokeIndex (vtype &ret,const decltype(TensorIndexRecursion::peekIndex(ret,0)) &arg,int i) { TensorIndexRecursion::pokeIndex(ret,arg,i); } template accelerator_inline void pokeIndex (vtype &ret,const decltype(TensorIndexRecursion::peekIndex(ret,0,0)) &arg,int i,int j) { TensorIndexRecursion::pokeIndex(ret,arg,i,j); } #undef RemoveCRV NAMESPACE_END(Grid); #endif