mirror of
https://github.com/paboyle/Grid.git
synced 2025-06-10 19:36:56 +01:00
Addedd Ta functionality to the tensor types
Merge remote-tracking branch 'upstream/master' Conflicts: configure
This commit is contained in:
38
lib/tensors/Tensor_Ta.h
Normal file
38
lib/tensors/Tensor_Ta.h
Normal file
@ -0,0 +1,38 @@
|
||||
#ifndef GRID_MATH_TA_H
|
||||
#define GRID_MATH_TA_H
|
||||
namespace Grid {
|
||||
|
||||
///////////////////////////////////////////////
|
||||
// Ta function for scalar, vector, matrix
|
||||
///////////////////////////////////////////////
|
||||
/* inline ComplexF Ta( const ComplexF &arg){ return arg;} */
|
||||
/* inline ComplexD Ta( const ComplexD &arg){ return arg;} */
|
||||
/* inline RealF Ta( const RealF &arg){ return arg;} */
|
||||
/* inline RealD Ta( const RealD &arg){ return arg;} */
|
||||
|
||||
|
||||
template<class vtype> inline iScalar<vtype> Ta(const iScalar<vtype>&r)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
ret._internal = Ta(r._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iVector<vtype,N> Ta(const iVector<vtype,N>&r)
|
||||
{
|
||||
iVector<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
ret._internal[i] = Ta(r._internal[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iMatrix<vtype,N> Ta(const iMatrix<vtype,N> &arg)
|
||||
{
|
||||
iMatrix<vtype,N> ret(arg);
|
||||
vtype factor = (1/(double)N);
|
||||
ret = (ret - adj(arg))*0.5;
|
||||
ret -= trace(ret)*factor;
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
11
lib/tensors/Tensor_arith.h
Normal file
11
lib/tensors/Tensor_arith.h
Normal file
@ -0,0 +1,11 @@
|
||||
#ifndef GRID_MATH_ARITH_H
|
||||
#define GRID_MATH_ARITH_H
|
||||
|
||||
#include <tensors/Tensor_arith_add.h>
|
||||
#include <tensors/Tensor_arith_sub.h>
|
||||
#include <tensors/Tensor_arith_mac.h>
|
||||
#include <tensors/Tensor_arith_mul.h>
|
||||
#include <tensors/Tensor_arith_scalar.h>
|
||||
|
||||
#endif
|
||||
|
118
lib/tensors/Tensor_arith_add.h
Normal file
118
lib/tensors/Tensor_arith_add.h
Normal file
@ -0,0 +1,118 @@
|
||||
#ifndef GRID_MATH_ARITH_ADD_H
|
||||
#define GRID_MATH_ARITH_ADD_H
|
||||
|
||||
namespace Grid {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////// ADD ///////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// ADD is simple for now; cannot mix types and straightforward template
|
||||
// Scalar +/- Scalar
|
||||
// Vector +/- Vector
|
||||
// Matrix +/- Matrix
|
||||
template<class vtype,class ltype,class rtype> strong_inline void add(iScalar<vtype> * __restrict__ ret,
|
||||
const iScalar<ltype> * __restrict__ lhs,
|
||||
const iScalar<rtype> * __restrict__ rhs)
|
||||
{
|
||||
add(&ret->_internal,&lhs->_internal,&rhs->_internal);
|
||||
}
|
||||
template<class vtype,class ltype,class rtype,int N> strong_inline void add(iVector<vtype,N> * __restrict__ ret,
|
||||
const iVector<ltype,N> * __restrict__ lhs,
|
||||
const iVector<rtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c=0;c<N;c++){
|
||||
ret->_internal[c]=lhs->_internal[c]+rhs->_internal[c];
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
template<class vtype,class ltype,class rtype, int N> strong_inline void add(iMatrix<vtype,N> * __restrict__ ret,
|
||||
const iMatrix<ltype,N> * __restrict__ lhs,
|
||||
const iMatrix<rtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
add(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal[c1][c2]);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class vtype,class ltype,class rtype, int N> strong_inline void add(iMatrix<vtype,N> * __restrict__ ret,
|
||||
const iScalar<ltype> * __restrict__ lhs,
|
||||
const iMatrix<rtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
if ( c1==c2)
|
||||
add(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
|
||||
else
|
||||
ret->_internal[c1][c2]=lhs->_internal[c1][c2];
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class vtype,class ltype,class rtype, int N> strong_inline void add(iMatrix<vtype,N> * __restrict__ ret,
|
||||
const iMatrix<ltype,N> * __restrict__ lhs,
|
||||
const iScalar<rtype> * __restrict__ rhs)
|
||||
{
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
if ( c1==c2)
|
||||
add(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
|
||||
else
|
||||
ret->_internal[c1][c2]=lhs->_internal[c1][c2];
|
||||
}}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
// + operator for scalar, vector, matrix
|
||||
template<class ltype,class rtype>
|
||||
//strong_inline auto operator + (iScalar<ltype>& lhs,iScalar<rtype>&& rhs) -> iScalar<decltype(lhs._internal + rhs._internal)>
|
||||
strong_inline auto operator + (const iScalar<ltype>& lhs,const iScalar<rtype>& rhs) -> iScalar<decltype(lhs._internal + rhs._internal)>
|
||||
{
|
||||
typedef iScalar<decltype(lhs._internal+rhs._internal)> ret_t;
|
||||
ret_t ret;
|
||||
add(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator + (const iVector<ltype,N>& lhs,const iVector<rtype,N>& rhs) ->iVector<decltype(lhs._internal[0]+rhs._internal[0]),N>
|
||||
{
|
||||
typedef iVector<decltype(lhs._internal[0]+rhs._internal[0]),N> ret_t;
|
||||
ret_t ret;
|
||||
add(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator + (const iMatrix<ltype,N>& lhs,const iMatrix<rtype,N>& rhs) ->iMatrix<decltype(lhs._internal[0][0]+rhs._internal[0][0]),N>
|
||||
{
|
||||
typedef iMatrix<decltype(lhs._internal[0][0]+rhs._internal[0][0]),N> ret_t;
|
||||
ret_t ret;
|
||||
add(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator + (const iScalar<ltype>& lhs,const iMatrix<rtype,N>& rhs)->iMatrix<decltype(lhs._internal+rhs._internal[0][0]),N>
|
||||
{
|
||||
typedef iMatrix<decltype(lhs._internal+rhs._internal[0][0]),N> ret_t;
|
||||
ret_t ret;
|
||||
add(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator + (const iMatrix<ltype,N>& lhs,const iScalar<rtype>& rhs)->iMatrix<decltype(lhs._internal[0][0]+rhs._internal),N>
|
||||
{
|
||||
typedef iMatrix<decltype(lhs._internal[0][0]+rhs._internal),N> ret_t;
|
||||
ret_t ret;
|
||||
add(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
81
lib/tensors/Tensor_arith_mac.h
Normal file
81
lib/tensors/Tensor_arith_mac.h
Normal file
@ -0,0 +1,81 @@
|
||||
#ifndef GRID_MATH_ARITH_MAC_H
|
||||
#define GRID_MATH_ARITH_MAC_H
|
||||
|
||||
namespace Grid {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////// MAC ///////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////////
|
||||
|
||||
///////////////////////////
|
||||
// Legal multiplication table
|
||||
///////////////////////////
|
||||
// scal x scal = scal
|
||||
// mat x mat = mat
|
||||
// mat x scal = mat
|
||||
// scal x mat = mat
|
||||
// mat x vec = vec
|
||||
// vec x scal = vec
|
||||
// scal x vec = vec
|
||||
///////////////////////////
|
||||
template<class rtype,class vtype,class mtype>
|
||||
strong_inline void mac(iScalar<rtype> * __restrict__ ret,const iScalar<vtype> * __restrict__ lhs,const iScalar<mtype> * __restrict__ rhs)
|
||||
{
|
||||
mac(&ret->_internal,&lhs->_internal,&rhs->_internal);
|
||||
}
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mac(iMatrix<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iMatrix<rtype,N> * __restrict__ rhs){
|
||||
for(int c3=0;c3<N;c3++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mac(&ret->_internal[c1][c2],&lhs->_internal[c1][c3],&rhs->_internal[c3][c2]);
|
||||
}}}
|
||||
return;
|
||||
}
|
||||
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mac(iMatrix<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iScalar<rtype> * __restrict__ rhs){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mac(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mac(iMatrix<rrtype,N> * __restrict__ ret,const iScalar<ltype> * __restrict__ lhs,const iMatrix<rtype,N> * __restrict__ rhs){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mac(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mac(iVector<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iVector<rtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mac(&ret->_internal[c1],&lhs->_internal[c1][c2],&rhs->_internal[c2]);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mac(iVector<rrtype,N> * __restrict__ ret,const iScalar<ltype> * __restrict__ lhs,const iVector<rtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mac(&ret->_internal[c1],&lhs->_internal,&rhs->_internal[c1]);
|
||||
}
|
||||
return;
|
||||
}
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mac(iVector<rrtype,N> * __restrict__ ret,const iVector<ltype,N> * __restrict__ lhs,const iScalar<rtype> * __restrict__ rhs)
|
||||
{
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mac(&ret->_internal[c1],&lhs->_internal[c1],&rhs->_internal);
|
||||
}
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
194
lib/tensors/Tensor_arith_mul.h
Normal file
194
lib/tensors/Tensor_arith_mul.h
Normal file
@ -0,0 +1,194 @@
|
||||
#ifndef GRID_MATH_ARITH_MUL_H
|
||||
#define GRID_MATH_ARITH_MUL_H
|
||||
|
||||
namespace Grid {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////// MUL ///////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class rtype,class vtype,class mtype>
|
||||
strong_inline void mult(iScalar<rtype> * __restrict__ ret,const iScalar<mtype> * __restrict__ lhs,const iScalar<vtype> * __restrict__ rhs){
|
||||
mult(&ret->_internal,&lhs->_internal,&rhs->_internal);
|
||||
}
|
||||
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mult(iMatrix<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iMatrix<rtype,N> * __restrict__ rhs){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mult(&ret->_internal[c1][c2],&lhs->_internal[c1][0],&rhs->_internal[0][c2]);
|
||||
}
|
||||
}
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c3=1;c3<N;c3++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mac(&ret->_internal[c1][c2],&lhs->_internal[c1][c3],&rhs->_internal[c3][c2]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
template<class rrtype,class ltype,class rtype,int N>
|
||||
strong_inline void mult(iMatrix<rrtype,N> * __restrict__ ret,const iMatrix<ltype,N> * __restrict__ lhs,const iScalar<rtype> * __restrict__ rhs){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
|
||||
template<class rrtype,class ltype,class rtype, int N>
|
||||
strong_inline void mult(iMatrix<rrtype,N> * __restrict__ ret,const iScalar<ltype> * __restrict__ lhs,const iMatrix<rtype,N> * __restrict__ rhs){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
// Matrix left multiplies vector
|
||||
template<class rtype,class vtype,class mtype,int N>
|
||||
strong_inline void mult(iVector<rtype,N> * __restrict__ ret,const iMatrix<mtype,N> * __restrict__ lhs,const iVector<vtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret->_internal[c1],&lhs->_internal[c1][0],&rhs->_internal[0]);
|
||||
for(int c2=1;c2<N;c2++){
|
||||
mac(&ret->_internal[c1],&lhs->_internal[c1][c2],&rhs->_internal[c2]);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
template<class rtype,class vtype,class mtype,int N>
|
||||
strong_inline void mult(iVector<rtype,N> * __restrict__ ret,
|
||||
const iScalar<mtype> * __restrict__ lhs,
|
||||
const iVector<vtype,N> * __restrict__ rhs){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret->_internal[c1],&lhs->_internal,&rhs->_internal[c1]);
|
||||
}
|
||||
}
|
||||
template<class rtype,class vtype,class mtype,int N>
|
||||
strong_inline void mult(iVector<rtype,N> * __restrict__ ret,
|
||||
const iVector<vtype,N> * __restrict__ rhs,
|
||||
const iScalar<mtype> * __restrict__ lhs){
|
||||
mult(ret,lhs,rhs);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class rtype,class vtype,class mtype,int N> strong_inline
|
||||
iVector<rtype,N> operator * (const iMatrix<mtype,N>& lhs,const iVector<vtype,N>& rhs)
|
||||
{
|
||||
iVector<rtype,N> ret;
|
||||
mult(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class rtype,class vtype,class mtype,int N> strong_inline
|
||||
iVector<rtype,N> operator * (const iScalar<mtype>& lhs,const iVector<vtype,N>& rhs)
|
||||
{
|
||||
iVector<rtype,N> ret;
|
||||
mult(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class rtype,class vtype,class mtype,int N> strong_inline
|
||||
iVector<rtype,N> operator * (const iVector<mtype,N>& lhs,const iScalar<vtype>& rhs)
|
||||
{
|
||||
iVector<rtype,N> ret;
|
||||
mult(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Glue operators to mult routines. Must resolve return type cleverly from typeof(internal)
|
||||
// since nesting matrix<scalar> x matrix<matrix>-> matrix<matrix>
|
||||
// while matrix<scalar> x matrix<scalar>-> matrix<scalar>
|
||||
// so return type depends on argument types in nasty way.
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// scal x scal = scal
|
||||
// mat x mat = mat
|
||||
// mat x scal = mat
|
||||
// scal x mat = mat
|
||||
// mat x vec = vec
|
||||
// vec x scal = vec
|
||||
// scal x vec = vec
|
||||
//
|
||||
// We can special case scalar_type ??
|
||||
template<class l,class r>
|
||||
strong_inline auto operator * (const iScalar<l>& lhs,const iScalar<r>& rhs) -> iScalar<decltype(lhs._internal * rhs._internal)>
|
||||
{
|
||||
typedef iScalar<decltype(lhs._internal*rhs._internal)> ret_t;
|
||||
ret_t ret;
|
||||
mult(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r,int N> strong_inline
|
||||
auto operator * (const iMatrix<l,N>& lhs,const iMatrix<r,N>& rhs) -> iMatrix<decltype(lhs._internal[0][0]*rhs._internal[0][0]),N>
|
||||
{
|
||||
typedef decltype(lhs._internal[0][0]*rhs._internal[0][0]) ret_t;
|
||||
iMatrix<ret_t,N> ret;
|
||||
mult(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r, int N> strong_inline
|
||||
auto operator * (const iMatrix<r,N>& lhs,const iScalar<l>& rhs) -> iMatrix<decltype(lhs._internal[0][0]*rhs._internal),N>
|
||||
{
|
||||
typedef decltype(lhs._internal[0][0]*rhs._internal) ret_t;
|
||||
|
||||
iMatrix<ret_t,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mult(&ret._internal[c1][c2],&lhs._internal[c1][c2],&rhs._internal);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r,int N> strong_inline
|
||||
auto operator * (const iScalar<l>& lhs,const iMatrix<r,N>& rhs) -> iMatrix<decltype(lhs._internal*rhs._internal[0][0]),N>
|
||||
{
|
||||
typedef decltype(lhs._internal*rhs._internal[0][0]) ret_t;
|
||||
iMatrix<ret_t,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
mult(&ret._internal[c1][c2],&lhs._internal,&rhs._internal[c1][c2]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r,int N> strong_inline
|
||||
auto operator * (const iMatrix<l,N>& lhs,const iVector<r,N>& rhs) -> iVector<decltype(lhs._internal[0][0]*rhs._internal[0]),N>
|
||||
{
|
||||
typedef decltype(lhs._internal[0][0]*rhs._internal[0]) ret_t;
|
||||
iVector<ret_t,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret._internal[c1],&lhs._internal[c1][0],&rhs._internal[0]);
|
||||
for(int c2=1;c2<N;c2++){
|
||||
mac(&ret._internal[c1],&lhs._internal[c1][c2],&rhs._internal[c2]);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r,int N> strong_inline
|
||||
auto operator * (const iScalar<l>& lhs,const iVector<r,N>& rhs) -> iVector<decltype(lhs._internal*rhs._internal[0]),N>
|
||||
{
|
||||
typedef decltype(lhs._internal*rhs._internal[0]) ret_t;
|
||||
iVector<ret_t,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret._internal[c1],&lhs._internal,&rhs._internal[c1]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r,int N> strong_inline
|
||||
auto operator * (const iVector<l,N>& lhs,const iScalar<r>& rhs) -> iVector<decltype(lhs._internal[0]*rhs._internal),N>
|
||||
{
|
||||
typedef decltype(lhs._internal[0]*rhs._internal) ret_t;
|
||||
iVector<ret_t,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
mult(&ret._internal[c1],&lhs._internal[c1],&rhs._internal);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
260
lib/tensors/Tensor_arith_scalar.h
Normal file
260
lib/tensors/Tensor_arith_scalar.h
Normal file
@ -0,0 +1,260 @@
|
||||
#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<class l,int N> strong_inline iScalar<l> operator * (const iScalar<l>& lhs,const typename iScalar<l>::scalar_type rhs)
|
||||
{
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iScalar<l> operator * (const typename iScalar<l>::scalar_type lhs,const iScalar<l>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (const iVector<l,N>& lhs,const typename iScalar<l>::scalar_type rhs)
|
||||
{
|
||||
typename iVector<l,N>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (const typename iScalar<l>::scalar_type lhs,const iVector<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (const iMatrix<l,N>& lhs,const typename iScalar<l>::scalar_type &rhs)
|
||||
{
|
||||
typename iMatrix<l,N>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (const typename iScalar<l>::scalar_type & lhs,const iMatrix<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Double support; cast to "scalar_type" through constructor
|
||||
////////////////////////////////////////////////////////////////////
|
||||
template<class l> strong_inline iScalar<l> operator * (const iScalar<l>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t; t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l> strong_inline iScalar<l> operator * (double lhs,const iScalar<l>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (const iVector<l,N>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (double lhs,const iVector<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (const iMatrix<l,N>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (double lhs,const iMatrix<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Complex support; cast to "scalar_type" through constructor
|
||||
////////////////////////////////////////////////////////////////////
|
||||
template<class l> strong_inline iScalar<l> operator * (const iScalar<l>& lhs,ComplexD rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
|
||||
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l> strong_inline iScalar<l> operator * (ComplexD lhs,const iScalar<l>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (const iVector<l,N>& lhs,ComplexD rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (ComplexD lhs,const iVector<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (const iMatrix<l,N>& lhs,ComplexD rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (ComplexD lhs,const iMatrix<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Integer support; cast to "scalar_type" through constructor
|
||||
////////////////////////////////////////////////////////////////////
|
||||
template<class l> strong_inline iScalar<l> operator * (const iScalar<l>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t; t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l> strong_inline iScalar<l> operator * (Integer lhs,const iScalar<l>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (const iVector<l,N>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iVector<l,N> operator * (Integer lhs,const iVector<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (const iMatrix<l,N>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs*srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator * (Integer lhs,const iMatrix<l,N>& rhs) { return rhs*lhs; }
|
||||
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// addition by fundamental scalar type applies to matrix(down diag) and scalar
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
template<class l,int N> strong_inline iScalar<l> operator + (const iScalar<l>& lhs,const typename iScalar<l>::scalar_type rhs)
|
||||
{
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs+srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iScalar<l> operator + (const typename iScalar<l>::scalar_type lhs,const iScalar<l>& rhs) { return rhs+lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator + (const iMatrix<l,N>& lhs,const typename iScalar<l>::scalar_type rhs)
|
||||
{
|
||||
typename iMatrix<l,N>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs+srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator + (const typename iScalar<l>::scalar_type lhs,const iMatrix<l,N>& rhs) { return rhs+lhs; }
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Double support; cast to "scalar_type" through constructor
|
||||
////////////////////////////////////////////////////////////////////
|
||||
template<class l> strong_inline iScalar<l> operator + (const iScalar<l>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t; t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=t;
|
||||
return lhs+srhs;
|
||||
}
|
||||
template<class l> strong_inline iScalar<l> operator + (double lhs,const iScalar<l>& rhs) { return rhs+lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator + (const iMatrix<l,N>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs+srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator + (double lhs,const iMatrix<l,N>& rhs) { return rhs+lhs; }
|
||||
|
||||
|
||||
// Integer support cast to scalar type through constructor
|
||||
|
||||
|
||||
template<class l> strong_inline iScalar<l> operator + (const iScalar<l>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t; t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=t;
|
||||
return lhs+srhs;
|
||||
}
|
||||
|
||||
template<class l> strong_inline iScalar<l> operator + (Integer lhs,const iScalar<l>& rhs) { return rhs+lhs; }
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator + (const iMatrix<l,N>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs+srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator + (Integer lhs,const iMatrix<l,N>& rhs) { return rhs+lhs; }
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// subtraction of fundamental scalar type applies to matrix(down diag) and scalar
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////
|
||||
template<class l,int N> strong_inline iScalar<l> operator - (const iScalar<l>& lhs,const typename iScalar<l>::scalar_type rhs)
|
||||
{
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs-srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iScalar<l> operator - (const typename iScalar<l>::scalar_type lhs,const iScalar<l>& rhs)
|
||||
{
|
||||
typename iScalar<l>::tensor_reduced slhs;slhs=lhs;
|
||||
return slhs-rhs;
|
||||
}
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator - (const iMatrix<l,N>& lhs,const typename iScalar<l>::scalar_type rhs)
|
||||
{
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=rhs;
|
||||
return lhs-srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator - (const typename iScalar<l>::scalar_type lhs,const iMatrix<l,N>& rhs)
|
||||
{
|
||||
typename iScalar<l>::tensor_reduced slhs;slhs=lhs;
|
||||
return slhs-rhs;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Double support; cast to "scalar_type" through constructor
|
||||
////////////////////////////////////////////////////////////////////
|
||||
template<class l> strong_inline iScalar<l> operator - (const iScalar<l>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t; t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=t;
|
||||
return lhs-srhs;
|
||||
}
|
||||
template<class l> strong_inline iScalar<l> operator - (double lhs,const iScalar<l>& rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t(lhs);
|
||||
typename iScalar<l>::tensor_reduced slhs;slhs=t;
|
||||
return slhs-rhs;
|
||||
}
|
||||
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator - (const iMatrix<l,N>& lhs,double rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs-srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator - (double lhs,const iMatrix<l,N>& rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t(lhs);
|
||||
typename iScalar<l>::tensor_reduced slhs;slhs=t;
|
||||
return slhs-rhs;
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////
|
||||
// Integer support; cast to "scalar_type" through constructor
|
||||
////////////////////////////////////////////////////////////////////
|
||||
template<class l> strong_inline iScalar<l> operator - (const iScalar<l>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t; t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs; srhs=t;
|
||||
return lhs-srhs;
|
||||
}
|
||||
template<class l> strong_inline iScalar<l> operator - (Integer lhs,const iScalar<l>& rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=lhs;
|
||||
typename iScalar<l>::tensor_reduced slhs;slhs=t;
|
||||
return slhs-rhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator - (const iMatrix<l,N>& lhs,Integer rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=rhs;
|
||||
typename iScalar<l>::tensor_reduced srhs;srhs=t;
|
||||
return lhs-srhs;
|
||||
}
|
||||
template<class l,int N> strong_inline iMatrix<l,N> operator - (Integer lhs,const iMatrix<l,N>& rhs)
|
||||
{
|
||||
typename iScalar<l>::scalar_type t;t=lhs;
|
||||
typename iScalar<l>::tensor_reduced slhs;slhs=t;
|
||||
return slhs-rhs;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endif
|
116
lib/tensors/Tensor_arith_sub.h
Normal file
116
lib/tensors/Tensor_arith_sub.h
Normal file
@ -0,0 +1,116 @@
|
||||
#ifndef GRID_MATH_ARITH_SUB_H
|
||||
#define GRID_MATH_ARITH_SUB_H
|
||||
|
||||
namespace Grid {
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
/////////////////////////////////////////// SUB ///////////////////////////////////////////
|
||||
///////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// SUB is simple for now; cannot mix types and straightforward template
|
||||
// Scalar +/- Scalar
|
||||
// Vector +/- Vector
|
||||
// Matrix +/- Matrix
|
||||
// Matrix /- scalar
|
||||
template<class vtype,class ltype,class rtype> strong_inline void sub(iScalar<vtype> * __restrict__ ret,
|
||||
const iScalar<ltype> * __restrict__ lhs,
|
||||
const iScalar<rtype> * __restrict__ rhs)
|
||||
{
|
||||
sub(&ret->_internal,&lhs->_internal,&rhs->_internal);
|
||||
}
|
||||
|
||||
template<class vtype,class ltype,class rtype,int N> strong_inline void sub(iVector<vtype,N> * __restrict__ ret,
|
||||
const iVector<ltype,N> * __restrict__ lhs,
|
||||
const iVector<rtype,N> * __restrict__ rhs)
|
||||
{
|
||||
for(int c=0;c<N;c++){
|
||||
ret->_internal[c]=lhs->_internal[c]-rhs->_internal[c];
|
||||
}
|
||||
return;
|
||||
}
|
||||
template<class vtype,class ltype,class rtype, int N> strong_inline void sub(iMatrix<vtype,N> * __restrict__ ret,
|
||||
const iMatrix<ltype,N> * __restrict__ lhs,
|
||||
const iMatrix<rtype,N> * __restrict__ rhs){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
sub(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal[c1][c2]);
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class vtype,class ltype,class rtype, int N> strong_inline void sub(iMatrix<vtype,N> * __restrict__ ret,
|
||||
const iScalar<ltype> * __restrict__ lhs,
|
||||
const iMatrix<rtype,N> * __restrict__ rhs){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
if ( c1==c2) {
|
||||
sub(&ret->_internal[c1][c2],&lhs->_internal,&rhs->_internal[c1][c2]);
|
||||
} else {
|
||||
// Fails -- need unary minus. Catalogue other unops?
|
||||
ret->_internal[c1][c2]=zero;
|
||||
ret->_internal[c1][c2]=ret->_internal[c1][c2]-rhs->_internal[c1][c2];
|
||||
|
||||
}
|
||||
}}
|
||||
return;
|
||||
}
|
||||
template<class vtype,class ltype,class rtype, int N> strong_inline void sub(iMatrix<vtype,N> * __restrict__ ret,
|
||||
const iMatrix<ltype,N> * __restrict__ lhs,
|
||||
const iScalar<rtype> * __restrict__ rhs){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
for(int c1=0;c1<N;c1++){
|
||||
if ( c1==c2)
|
||||
sub(&ret->_internal[c1][c2],&lhs->_internal[c1][c2],&rhs->_internal);
|
||||
else
|
||||
ret->_internal[c1][c2]=lhs->_internal[c1][c2];
|
||||
}}
|
||||
return;
|
||||
}
|
||||
|
||||
// - operator for scalar, vector, matrix
|
||||
template<class ltype,class rtype> strong_inline auto
|
||||
operator - (const iScalar<ltype>& lhs, const iScalar<rtype>& rhs) -> iScalar<decltype(lhs._internal - rhs._internal)>
|
||||
{
|
||||
typedef iScalar<decltype(lhs._internal-rhs._internal)> ret_t;
|
||||
ret_t ret;
|
||||
sub(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator - (const iVector<ltype,N>& lhs,const iVector<rtype,N>& rhs) ->iVector<decltype(lhs._internal[0]-rhs._internal[0]),N>
|
||||
{
|
||||
typedef iVector<decltype(lhs._internal[0]-rhs._internal[0]),N> ret_t;
|
||||
ret_t ret;
|
||||
sub(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator - (const iMatrix<ltype,N>& lhs,const iMatrix<rtype,N>& rhs) ->iMatrix<decltype(lhs._internal[0][0]-rhs._internal[0][0]),N>
|
||||
{
|
||||
typedef iMatrix<decltype(lhs._internal[0][0]-rhs._internal[0][0]),N> ret_t;
|
||||
ret_t ret;
|
||||
sub(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator - (const iScalar<ltype>& lhs,const iMatrix<rtype,N>& rhs)->iMatrix<decltype(lhs._internal-rhs._internal[0][0]),N>
|
||||
{
|
||||
typedef iMatrix<decltype(lhs._internal-rhs._internal[0][0]),N> ret_t;
|
||||
ret_t ret;
|
||||
sub(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
template<class ltype,class rtype,int N>
|
||||
strong_inline auto operator - (const iMatrix<ltype,N>& lhs,const iScalar<rtype>& rhs)->iMatrix<decltype(lhs._internal[0][0]-rhs._internal),N>
|
||||
{
|
||||
typedef iMatrix<decltype(lhs._internal[0][0]-rhs._internal),N> ret_t;
|
||||
ret_t ret;
|
||||
sub(&ret,&lhs,&rhs);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
344
lib/tensors/Tensor_class.h
Normal file
344
lib/tensors/Tensor_class.h
Normal file
@ -0,0 +1,344 @@
|
||||
#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.
|
||||
///////////////////////////////////////////////////
|
||||
|
||||
// It is useful to NOT have any constructors
|
||||
// so that these classes assert "is_pod<class> == true"
|
||||
// because then the standard C++ valarray container eliminates fill overhead on new allocation and
|
||||
// non-move copying.
|
||||
//
|
||||
// However note that doing this eliminates some syntactical sugar such as
|
||||
// calling the constructor explicitly or implicitly
|
||||
//
|
||||
class GridTensorBase {};
|
||||
|
||||
template<class vtype> class iScalar
|
||||
{
|
||||
public:
|
||||
vtype _internal;
|
||||
|
||||
typedef typename GridTypeMapper<vtype>::scalar_type scalar_type;
|
||||
typedef typename GridTypeMapper<vtype>::vector_type vector_type;
|
||||
typedef typename GridTypeMapper<vtype>::tensor_reduced tensor_reduced_v;
|
||||
typedef iScalar<tensor_reduced_v> tensor_reduced;
|
||||
typedef typename GridTypeMapper<vtype>::scalar_object recurse_scalar_object;
|
||||
typedef iScalar<recurse_scalar_object> scalar_object;
|
||||
|
||||
enum { TensorLevel = GridTypeMapper<vtype>::TensorLevel + 1};
|
||||
|
||||
// Scalar no action
|
||||
// template<int Level> using tensor_reduce_level = typename iScalar<GridTypeMapper<vtype>::tensor_reduce_level<Level> >;
|
||||
iScalar() = default;
|
||||
/*
|
||||
iScalar(const iScalar<vtype> ©me)=default;
|
||||
iScalar(iScalar<vtype> &©me)=default;
|
||||
iScalar<vtype> & operator= (const iScalar<vtype> ©me) = default;
|
||||
iScalar<vtype> & operator= (iScalar<vtype> &©me) = default;
|
||||
*/
|
||||
iScalar(scalar_type s) : _internal(s) {};// recurse down and hit the constructor for vector_type
|
||||
iScalar(const Zero &z){ *this = zero; };
|
||||
|
||||
iScalar<vtype> & operator= (const Zero &hero){
|
||||
zeroit(*this);
|
||||
return *this;
|
||||
}
|
||||
friend strong_inline void vstream(iScalar<vtype> &out,const iScalar<vtype> &in){
|
||||
vstream(out._internal,in._internal);
|
||||
}
|
||||
friend strong_inline void zeroit(iScalar<vtype> &that){
|
||||
zeroit(that._internal);
|
||||
}
|
||||
friend strong_inline void prefetch(iScalar<vtype> &that){
|
||||
prefetch(that._internal);
|
||||
}
|
||||
friend strong_inline void permute(iScalar<vtype> &out,const iScalar<vtype> &in,int permutetype){
|
||||
permute(out._internal,in._internal,permutetype);
|
||||
}
|
||||
|
||||
// Unary negation
|
||||
friend strong_inline iScalar<vtype> operator -(const iScalar<vtype> &r) {
|
||||
iScalar<vtype> ret;
|
||||
ret._internal= -r._internal;
|
||||
return ret;
|
||||
}
|
||||
// *=,+=,-= operators inherit from corresponding "*,-,+" behaviour
|
||||
strong_inline iScalar<vtype> &operator *=(const iScalar<vtype> &r) {
|
||||
*this = (*this)*r;
|
||||
return *this;
|
||||
}
|
||||
strong_inline iScalar<vtype> &operator -=(const iScalar<vtype> &r) {
|
||||
*this = (*this)-r;
|
||||
return *this;
|
||||
}
|
||||
strong_inline iScalar<vtype> &operator +=(const iScalar<vtype> &r) {
|
||||
*this = (*this)+r;
|
||||
return *this;
|
||||
}
|
||||
strong_inline vtype & operator ()(void) {
|
||||
return _internal;
|
||||
}
|
||||
strong_inline const vtype & operator ()(void) const {
|
||||
return _internal;
|
||||
}
|
||||
|
||||
operator ComplexD () const { return(TensorRemove(_internal)); };
|
||||
operator RealD () const { return(real(TensorRemove(_internal))); }
|
||||
|
||||
// convert from a something to a scalar
|
||||
template<class T,typename std::enable_if<!isGridTensor<T>::value, T>::type* = nullptr > strong_inline auto operator = (T arg) -> iScalar<vtype>
|
||||
{
|
||||
_internal = vtype(arg);
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend std::ostream& operator<< (std::ostream& stream, const iScalar<vtype> &o){
|
||||
stream<< "S {"<<o._internal<<"}";
|
||||
return stream;
|
||||
};
|
||||
};
|
||||
///////////////////////////////////////////////////////////
|
||||
// Allows to turn scalar<scalar<scalar<double>>>> back to double.
|
||||
///////////////////////////////////////////////////////////
|
||||
template<class T> strong_inline typename std::enable_if<!isGridTensor<T>::value, T>::type TensorRemove(T arg) { return arg;}
|
||||
template<class vtype> strong_inline auto TensorRemove(iScalar<vtype> arg) -> decltype(TensorRemove(arg._internal))
|
||||
{
|
||||
return TensorRemove(arg._internal);
|
||||
}
|
||||
|
||||
template<class vtype,int N> class iVector
|
||||
{
|
||||
public:
|
||||
vtype _internal[N];
|
||||
|
||||
typedef typename GridTypeMapper<vtype>::scalar_type scalar_type;
|
||||
typedef typename GridTypeMapper<vtype>::vector_type vector_type;
|
||||
typedef typename GridTypeMapper<vtype>::tensor_reduced tensor_reduced_v;
|
||||
typedef typename GridTypeMapper<vtype>::scalar_object recurse_scalar_object;
|
||||
typedef iScalar<tensor_reduced_v> tensor_reduced;
|
||||
typedef iVector<recurse_scalar_object,N> scalar_object;
|
||||
|
||||
|
||||
enum { TensorLevel = GridTypeMapper<vtype>::TensorLevel + 1};
|
||||
iVector(const Zero &z){ *this = zero; };
|
||||
iVector() =default;
|
||||
/*
|
||||
iVector(const iVector<vtype,N> ©me)=default;
|
||||
iVector(iVector<vtype,N> &©me)=default;
|
||||
iVector<vtype,N> & operator= (const iVector<vtype,N> ©me) = default;
|
||||
iVector<vtype,N> & operator= (iVector<vtype,N> &©me) = default;
|
||||
*/
|
||||
|
||||
iVector<vtype,N> & operator= (const Zero &hero){
|
||||
zeroit(*this);
|
||||
return *this;
|
||||
}
|
||||
friend strong_inline void zeroit(iVector<vtype,N> &that){
|
||||
for(int i=0;i<N;i++){
|
||||
zeroit(that._internal[i]);
|
||||
}
|
||||
}
|
||||
friend strong_inline void prefetch(iVector<vtype,N> &that){
|
||||
for(int i=0;i<N;i++) prefetch(that._internal[i]);
|
||||
}
|
||||
friend strong_inline void vstream(iVector<vtype,N> &out,const iVector<vtype,N> &in){
|
||||
for(int i=0;i<N;i++){
|
||||
vstream(out._internal[i],in._internal[i]);
|
||||
}
|
||||
}
|
||||
friend strong_inline void permute(iVector<vtype,N> &out,const iVector<vtype,N> &in,int permutetype){
|
||||
for(int i=0;i<N;i++){
|
||||
permute(out._internal[i],in._internal[i],permutetype);
|
||||
}
|
||||
}
|
||||
// Unary negation
|
||||
friend strong_inline iVector<vtype,N> operator -(const iVector<vtype,N> &r) {
|
||||
iVector<vtype,N> ret;
|
||||
for(int i=0;i<N;i++) ret._internal[i]= -r._internal[i];
|
||||
return ret;
|
||||
}
|
||||
// *=,+=,-= operators inherit from corresponding "*,-,+" behaviour
|
||||
strong_inline iVector<vtype,N> &operator *=(const iScalar<vtype> &r) {
|
||||
*this = (*this)*r;
|
||||
return *this;
|
||||
}
|
||||
strong_inline iVector<vtype,N> &operator -=(const iVector<vtype,N> &r) {
|
||||
*this = (*this)-r;
|
||||
return *this;
|
||||
}
|
||||
strong_inline iVector<vtype,N> &operator +=(const iVector<vtype,N> &r) {
|
||||
*this = (*this)+r;
|
||||
return *this;
|
||||
}
|
||||
strong_inline vtype & operator ()(int i) {
|
||||
return _internal[i];
|
||||
}
|
||||
strong_inline const vtype & operator ()(int i) const {
|
||||
return _internal[i];
|
||||
}
|
||||
friend std::ostream& operator<< (std::ostream& stream, const iVector<vtype,N> &o){
|
||||
stream<< "V<"<<N<<">{";
|
||||
for(int i=0;i<N;i++) {
|
||||
stream<<o._internal[i];
|
||||
if (i<N-1) stream<<",";
|
||||
}
|
||||
stream<<"}";
|
||||
return stream;
|
||||
};
|
||||
// strong_inline vtype && operator ()(int i) {
|
||||
// return _internal[i];
|
||||
// }
|
||||
};
|
||||
|
||||
template<class vtype,int N> class iMatrix
|
||||
{
|
||||
public:
|
||||
vtype _internal[N][N];
|
||||
|
||||
typedef typename GridTypeMapper<vtype>::scalar_type scalar_type;
|
||||
typedef typename GridTypeMapper<vtype>::vector_type vector_type;
|
||||
typedef typename GridTypeMapper<vtype>::tensor_reduced tensor_reduced_v;
|
||||
typedef typename GridTypeMapper<vtype>::scalar_object recurse_scalar_object;
|
||||
typedef iScalar<tensor_reduced_v> tensor_reduced;
|
||||
typedef iMatrix<recurse_scalar_object,N> scalar_object;
|
||||
|
||||
enum { TensorLevel = GridTypeMapper<vtype>::TensorLevel + 1};
|
||||
|
||||
|
||||
iMatrix(const Zero &z){ *this = zero; };
|
||||
iMatrix() =default;
|
||||
|
||||
iMatrix& operator=(const iMatrix& rhs){
|
||||
for(int i=0;i<N;i++)
|
||||
for(int j=0;j<N;j++)
|
||||
vstream(_internal[i][j],rhs._internal[i][j]);
|
||||
return *this;
|
||||
};
|
||||
|
||||
|
||||
|
||||
iMatrix(scalar_type s) { (*this) = s ;};// recurse down and hit the constructor for vector_type
|
||||
|
||||
/*
|
||||
iMatrix(const iMatrix<vtype,N> ©me)=default;
|
||||
iMatrix(iMatrix<vtype,N> &©me)=default;
|
||||
iMatrix<vtype,N> & operator= (const iMatrix<vtype,N> ©me) = default;
|
||||
iMatrix<vtype,N> & operator= (iMatrix<vtype,N> &©me) = default;
|
||||
*/
|
||||
|
||||
|
||||
|
||||
iMatrix<vtype,N> & operator= (const Zero &hero){
|
||||
zeroit(*this);
|
||||
return *this;
|
||||
}
|
||||
template<class T,typename std::enable_if<!isGridTensor<T>::value, T>::type* = nullptr > strong_inline auto operator = (T arg) -> iMatrix<vtype,N>
|
||||
{
|
||||
zeroit(*this);
|
||||
for(int i=0;i<N;i++)
|
||||
_internal[i][i] = arg;
|
||||
return *this;
|
||||
}
|
||||
|
||||
friend strong_inline void zeroit(iMatrix<vtype,N> &that){
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
zeroit(that._internal[i][j]);
|
||||
}}
|
||||
}
|
||||
friend strong_inline void prefetch(iMatrix<vtype,N> &that){
|
||||
for(int i=0;i<N;i++)
|
||||
for(int j=0;j<N;j++)
|
||||
prefetch(that._internal[i][j]);
|
||||
}
|
||||
friend strong_inline void vstream(iMatrix<vtype,N> &out,const iMatrix<vtype,N> &in){
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
vstream(out._internal[i][j],in._internal[i][j]);
|
||||
}}
|
||||
}
|
||||
friend strong_inline void permute(iMatrix<vtype,N> &out,const iMatrix<vtype,N> &in,int permutetype){
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
permute(out._internal[i][j],in._internal[i][j],permutetype);
|
||||
}}
|
||||
}
|
||||
// Unary negation
|
||||
friend strong_inline iMatrix<vtype,N> operator -(const iMatrix<vtype,N> &r) {
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j]= -r._internal[i][j];
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
// *=,+=,-= operators inherit from corresponding "*,-,+" behaviour
|
||||
template<class T>
|
||||
strong_inline iMatrix<vtype,N> &operator *=(const T &r) {
|
||||
*this = (*this)*r;
|
||||
return *this;
|
||||
}
|
||||
template<class T>
|
||||
strong_inline iMatrix<vtype,N> &operator -=(const T &r) {
|
||||
*this = (*this)-r;
|
||||
return *this;
|
||||
}
|
||||
template<class T>
|
||||
strong_inline iMatrix<vtype,N> &operator +=(const T &r) {
|
||||
*this = (*this)+r;
|
||||
return *this;
|
||||
}
|
||||
|
||||
// returns an lvalue reference
|
||||
strong_inline vtype & operator ()(int i,int j) {
|
||||
return _internal[i][j];
|
||||
}
|
||||
strong_inline const vtype & operator ()(int i,int j) const {
|
||||
return _internal[i][j];
|
||||
}
|
||||
friend std::ostream& operator<< (std::ostream& stream, const iMatrix<vtype,N> &o){
|
||||
stream<< "M<"<<N<<">{";
|
||||
for(int i=0;i<N;i++) {
|
||||
stream<< "{";
|
||||
for(int j=0;j<N;j++) {
|
||||
stream<<o._internal[i][j];
|
||||
if (i<N-1) stream<<",";
|
||||
}
|
||||
stream<<"}\n\t\t";
|
||||
}
|
||||
stream<<"}";
|
||||
return stream;
|
||||
};
|
||||
|
||||
// strong_inline vtype && operator ()(int i,int j) {
|
||||
// return _internal[i][j];
|
||||
// }
|
||||
|
||||
};
|
||||
|
||||
template<class v> void vprefetch(const iScalar<v> &vv)
|
||||
{
|
||||
vprefetch(vv._internal);
|
||||
}
|
||||
template<class v,int N> void vprefetch(const iVector<v,N> &vv)
|
||||
{
|
||||
for(int i=0;i<N;i++){
|
||||
vprefetch(vv._internal[i]);
|
||||
}
|
||||
}
|
||||
template<class v,int N> void vprefetch(const iMatrix<v,N> &vv)
|
||||
{
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
vprefetch(vv._internal[i][j]);
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endif
|
193
lib/tensors/Tensor_extract_merge.h
Normal file
193
lib/tensors/Tensor_extract_merge.h
Normal file
@ -0,0 +1,193 @@
|
||||
#ifndef GRID_EXTRACT_H
|
||||
#define GRID_EXTRACT_H
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// Generic extract/merge/permute
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
namespace Grid{
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Extract/merge a fundamental vector type, to pointer array with offset
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class vsimd,class scalar>
|
||||
inline void extract(typename std::enable_if<!isGridTensor<vsimd>::value, const vsimd >::type * y,
|
||||
std::vector<scalar *> &extracted,int offset){
|
||||
// FIXME: bounce off memory is painful
|
||||
int Nextr=extracted.size();
|
||||
int Nsimd=vsimd::Nsimd();
|
||||
int s=Nsimd/Nextr;
|
||||
|
||||
scalar*buf = (scalar *)y;
|
||||
for(int i=0;i<Nextr;i++){
|
||||
extracted[i][offset] = buf[i*s];
|
||||
}
|
||||
};
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Merge simd vector from array of scalars to pointer array with offset
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template<class vsimd,class scalar>
|
||||
inline void merge(typename std::enable_if<!isGridTensor<vsimd>::value, vsimd >::type * y,
|
||||
std::vector<scalar *> &extracted,int offset){
|
||||
int Nextr=extracted.size();
|
||||
int Nsimd=vsimd::Nsimd();
|
||||
int s=Nsimd/Nextr;
|
||||
|
||||
scalar *buf =(scalar *) y;
|
||||
for(int i=0;i<Nextr;i++){
|
||||
for(int ii=0;ii<s;ii++){
|
||||
buf[i*s+ii]=extracted[i][offset];
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Extract a fundamental vector type to scalar array
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
template<class vsimd,class scalar>
|
||||
inline void extract(typename std::enable_if<!isGridTensor<vsimd>::value, const vsimd >::type &y,std::vector<scalar> &extracted){
|
||||
|
||||
int Nextr=extracted.size();
|
||||
int Nsimd=vsimd::Nsimd();
|
||||
int s=Nsimd/Nextr;
|
||||
|
||||
scalar *buf = (scalar *)&y;
|
||||
for(int i=0;i<Nextr;i++){
|
||||
for(int ii=0;ii<s;ii++){
|
||||
extracted[i]=buf[i*s+ii];
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Merge simd vector from array of scalars
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template<class vsimd,class scalar>
|
||||
inline void merge(typename std::enable_if<!isGridTensor<vsimd>::value, vsimd >::type &y,std::vector<scalar> &extracted){
|
||||
int Nextr=extracted.size();
|
||||
int Nsimd=vsimd::Nsimd();
|
||||
int s=Nsimd/Nextr;
|
||||
scalar *buf = (scalar *)&y;
|
||||
|
||||
for(int i=0;i<Nextr;i++){
|
||||
for(int ii=0;ii<s;ii++){
|
||||
buf[i*s+ii]=extracted[i];
|
||||
}
|
||||
}
|
||||
|
||||
};
|
||||
template<class vsimd,class scalar>
|
||||
inline void AmergeA(typename std::enable_if<!isGridTensor<vsimd>::value, vsimd >::type &y,std::vector<scalar> &extracted){
|
||||
int Nextr=extracted.size();
|
||||
int Nsimd=vsimd::Nsimd();
|
||||
int s=Nsimd/Nextr;
|
||||
|
||||
scalar *buf = (scalar *)&y;
|
||||
for(int i=0;i<Nextr;i++){
|
||||
for(int ii=0;ii<s;ii++){
|
||||
buf[i*s+ii]=extracted[i];
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Extract to contiguous array scalar object
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template<class vobj> inline void extract(const vobj &vec,std::vector<typename vobj::scalar_object> &extracted)
|
||||
{
|
||||
typedef typename vobj::scalar_type scalar_type ;
|
||||
typedef typename vobj::vector_type vector_type ;
|
||||
|
||||
const int Nsimd=vobj::vector_type::Nsimd();
|
||||
const int words=sizeof(vobj)/sizeof(vector_type);
|
||||
|
||||
extracted.resize(Nsimd);
|
||||
|
||||
std::vector<scalar_type *> pointers(Nsimd);
|
||||
for(int i=0;i<Nsimd;i++)
|
||||
pointers[i] =(scalar_type *)& extracted[i];
|
||||
|
||||
vector_type *vp = (vector_type *)&vec;
|
||||
for(int w=0;w<words;w++){
|
||||
extract<vector_type,scalar_type>(&vp[w],pointers,w);
|
||||
}
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Extract to a bunch of scalar object pointers, with offset
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template<class vobj> inline
|
||||
void extract(const vobj &vec,std::vector<typename vobj::scalar_object *> &extracted, int offset)
|
||||
{
|
||||
|
||||
typedef typename vobj::scalar_type scalar_type ;
|
||||
typedef typename vobj::vector_type vector_type ;
|
||||
|
||||
const int words=sizeof(vobj)/sizeof(vector_type);
|
||||
const int Nsimd=vobj::vector_type::Nsimd();
|
||||
|
||||
assert(extracted.size()==Nsimd);
|
||||
|
||||
std::vector<scalar_type *> pointers(Nsimd);
|
||||
for(int i=0;i<Nsimd;i++) {
|
||||
pointers[i] =(scalar_type *)& extracted[i][offset];
|
||||
}
|
||||
|
||||
vector_type *vp = (vector_type *)&vec;
|
||||
for(int w=0;w<words;w++){
|
||||
extract<vector_type,scalar_type>(&vp[w],pointers,w);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Merge a contiguous array of scalar objects
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template<class vobj> inline
|
||||
void merge(vobj &vec,std::vector<typename vobj::scalar_object> &extracted)
|
||||
{
|
||||
typedef typename vobj::scalar_type scalar_type ;
|
||||
typedef typename vobj::vector_type vector_type ;
|
||||
|
||||
const int Nsimd=vobj::vector_type::Nsimd();
|
||||
const int words=sizeof(vobj)/sizeof(vector_type);
|
||||
|
||||
assert(extracted.size()==Nsimd);
|
||||
|
||||
std::vector<scalar_type *> pointers(Nsimd);
|
||||
for(int i=0;i<Nsimd;i++)
|
||||
pointers[i] =(scalar_type *)& extracted[i];
|
||||
|
||||
vector_type *vp = (vector_type *)&vec;
|
||||
for(int w=0;w<words;w++){
|
||||
merge<vector_type,scalar_type>(&vp[w],pointers,w);
|
||||
}
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
// Merge a bunch of different scalar object pointers, with offset
|
||||
////////////////////////////////////////////////////////////////////////
|
||||
template<class vobj> inline
|
||||
void merge(vobj &vec,std::vector<typename vobj::scalar_object *> &extracted,int offset)
|
||||
{
|
||||
typedef typename vobj::scalar_type scalar_type ;
|
||||
typedef typename vobj::vector_type vector_type ;
|
||||
|
||||
const int Nsimd=vobj::vector_type::Nsimd();
|
||||
const int words=sizeof(vobj)/sizeof(vector_type);
|
||||
|
||||
assert(extracted.size()==Nsimd);
|
||||
|
||||
std::vector<scalar_type *> pointers(Nsimd);
|
||||
for(int i=0;i<Nsimd;i++)
|
||||
pointers[i] =(scalar_type *)& extracted[i][offset];
|
||||
|
||||
vector_type *vp = (vector_type *)&vec;
|
||||
assert((void *)vp!=NULL);
|
||||
for(int w=0;w<words;w++){
|
||||
merge<vector_type,scalar_type>(&vp[w],pointers,w);
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif
|
50
lib/tensors/Tensor_inner.h
Normal file
50
lib/tensors/Tensor_inner.h
Normal file
@ -0,0 +1,50 @@
|
||||
#ifndef GRID_MATH_INNER_H
|
||||
#define GRID_MATH_INNER_H
|
||||
namespace Grid {
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
// innerProduct Scalar x Scalar -> Scalar
|
||||
// innerProduct Vector x Vector -> Scalar
|
||||
// innerProduct Matrix x Matrix -> Scalar
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
template<class sobj> inline RealD norm2(const sobj &arg){
|
||||
typedef typename sobj::scalar_type scalar;
|
||||
decltype(innerProduct(arg,arg)) nrm;
|
||||
nrm = innerProduct(arg,arg);
|
||||
return real(nrm);
|
||||
}
|
||||
|
||||
template<class l,class r,int N> inline
|
||||
auto innerProduct (const iVector<l,N>& lhs,const iVector<r,N>& rhs) -> iScalar<decltype(innerProduct(lhs._internal[0],rhs._internal[0]))>
|
||||
{
|
||||
typedef decltype(innerProduct(lhs._internal[0],rhs._internal[0])) ret_t;
|
||||
iScalar<ret_t> ret;
|
||||
ret=zero;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
ret._internal += innerProduct(lhs._internal[c1],rhs._internal[c1]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r,int N> inline
|
||||
auto innerProduct (const iMatrix<l,N>& lhs,const iMatrix<r,N>& rhs) -> iScalar<decltype(innerProduct(lhs._internal[0][0],rhs._internal[0][0]))>
|
||||
{
|
||||
typedef decltype(innerProduct(lhs._internal[0][0],rhs._internal[0][0])) ret_t;
|
||||
iScalar<ret_t> ret;
|
||||
iScalar<ret_t> tmp;
|
||||
ret=zero;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
ret._internal+=innerProduct(lhs._internal[c1][c2],rhs._internal[c1][c2]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r> inline
|
||||
auto innerProduct (const iScalar<l>& lhs,const iScalar<r>& rhs) -> iScalar<decltype(innerProduct(lhs._internal,rhs._internal))>
|
||||
{
|
||||
typedef decltype(innerProduct(lhs._internal,rhs._internal)) ret_t;
|
||||
iScalar<ret_t> ret;
|
||||
ret._internal = innerProduct(lhs._internal,rhs._internal);
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
47
lib/tensors/Tensor_outer.h
Normal file
47
lib/tensors/Tensor_outer.h
Normal file
@ -0,0 +1,47 @@
|
||||
#ifndef GRID_MATH_OUTER_H
|
||||
#define GRID_MATH_OUTER_H
|
||||
namespace Grid {
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
// outerProduct Scalar x Scalar -> Scalar
|
||||
// Vector x Vector -> Matrix
|
||||
///////////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template<class l,class r,int N> inline
|
||||
auto outerProduct (const iVector<l,N>& lhs,const iVector<r,N>& rhs) -> iMatrix<decltype(outerProduct(lhs._internal[0],rhs._internal[0])),N>
|
||||
{
|
||||
typedef decltype(outerProduct(lhs._internal[0],rhs._internal[0])) ret_t;
|
||||
iMatrix<ret_t,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
ret._internal[c1][c2] = outerProduct(lhs._internal[c1],rhs._internal[c2]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class l,class r> inline
|
||||
auto outerProduct (const iScalar<l>& lhs,const iScalar<r>& rhs) -> iScalar<decltype(outerProduct(lhs._internal,rhs._internal))>
|
||||
{
|
||||
typedef decltype(outerProduct(lhs._internal,rhs._internal)) ret_t;
|
||||
iScalar<ret_t> ret;
|
||||
ret._internal = outerProduct(lhs._internal,rhs._internal);
|
||||
return ret;
|
||||
}
|
||||
|
||||
inline ComplexF outerProduct(const ComplexF &l, const ComplexF& r)
|
||||
{
|
||||
return l*r;
|
||||
}
|
||||
inline ComplexD outerProduct(const ComplexD &l, const ComplexD& r)
|
||||
{
|
||||
return l*r;
|
||||
}
|
||||
inline RealF outerProduct(const RealF &l, const RealF& r)
|
||||
{
|
||||
return l*r;
|
||||
}
|
||||
inline RealD outerProduct(const RealD &l, const RealD& r)
|
||||
{
|
||||
return l*r;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
125
lib/tensors/Tensor_peek.h
Normal file
125
lib/tensors/Tensor_peek.h
Normal file
@ -0,0 +1,125 @@
|
||||
#ifndef GRID_MATH_PEEK_H
|
||||
#define GRID_MATH_PEEK_H
|
||||
namespace Grid {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Peek on a specific index; returns a scalar in that index, tensor inherits rest
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// If we hit the right index, return scalar with no further recursion
|
||||
|
||||
//template<int Level> inline ComplexF peekIndex(const ComplexF arg) { return arg;}
|
||||
//template<int Level> inline ComplexD peekIndex(const ComplexD arg) { return arg;}
|
||||
//template<int Level> inline RealF peekIndex(const RealF arg) { return arg;}
|
||||
//template<int Level> inline RealD peekIndex(const RealD arg) { return arg;}
|
||||
|
||||
// Scalar peek, no indices
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iScalar<vtype> &arg) -> iScalar<vtype>
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
// Vector peek, one index
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iVector<vtype,N> &arg,int i) -> iScalar<vtype> // Index matches
|
||||
{
|
||||
iScalar<vtype> ret; // return scalar
|
||||
ret._internal = arg._internal[i];
|
||||
return ret;
|
||||
}
|
||||
// Matrix peek, two indices
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iMatrix<vtype,N> &arg,int i,int j) -> iScalar<vtype>
|
||||
{
|
||||
iScalar<vtype> ret; // return scalar
|
||||
ret._internal = arg._internal[i][j];
|
||||
return ret;
|
||||
}
|
||||
|
||||
/////////////
|
||||
// No match peek for scalar,vector,matrix must forward on either 0,1,2 args. Must have 9 routines with notvalue
|
||||
/////////////
|
||||
// scalar
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iScalar<vtype> &arg) -> iScalar<decltype(peekIndex<Level>(arg._internal))>
|
||||
{
|
||||
iScalar<decltype(peekIndex<Level>(arg._internal))> ret;
|
||||
ret._internal= peekIndex<Level>(arg._internal);
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iScalar<vtype> &arg,int i) -> iScalar<decltype(peekIndex<Level>(arg._internal,i))>
|
||||
{
|
||||
iScalar<decltype(peekIndex<Level>(arg._internal,i))> ret;
|
||||
ret._internal=peekIndex<Level>(arg._internal,i);
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iScalar<vtype> &arg,int i,int j) -> iScalar<decltype(peekIndex<Level>(arg._internal,i,j))>
|
||||
{
|
||||
iScalar<decltype(peekIndex<Level>(arg._internal,i,j))> ret;
|
||||
ret._internal=peekIndex<Level>(arg._internal,i,j);
|
||||
return ret;
|
||||
}
|
||||
// vector
|
||||
template<int Level,class vtype,int N, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iVector<vtype,N> &arg) -> iVector<decltype(peekIndex<Level>(arg._internal[0])),N>
|
||||
{
|
||||
iVector<decltype(peekIndex<Level>(arg._internal[0])),N> ret;
|
||||
for(int ii=0;ii<N;ii++){
|
||||
ret._internal[ii]=peekIndex<Level>(arg._internal[ii]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype,int N, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iVector<vtype,N> &arg,int i) -> iVector<decltype(peekIndex<Level>(arg._internal[0],i)),N>
|
||||
{
|
||||
iVector<decltype(peekIndex<Level>(arg._internal[0],i)),N> ret;
|
||||
for(int ii=0;ii<N;ii++){
|
||||
ret._internal[ii]=peekIndex<Level>(arg._internal[ii],i);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype,int N, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iVector<vtype,N> &arg,int i,int j) -> iVector<decltype(peekIndex<Level>(arg._internal[0],i,j)),N>
|
||||
{
|
||||
iVector<decltype(peekIndex<Level>(arg._internal[0],i,j)),N> ret;
|
||||
for(int ii=0;ii<N;ii++){
|
||||
ret._internal[ii]=peekIndex<Level>(arg._internal[ii],i,j);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
// matrix
|
||||
template<int Level,class vtype,int N, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iMatrix<vtype,N> &arg) -> iMatrix<decltype(peekIndex<Level>(arg._internal[0][0])),N>
|
||||
{
|
||||
iMatrix<decltype(peekIndex<Level>(arg._internal[0][0])),N> ret;
|
||||
for(int ii=0;ii<N;ii++){
|
||||
for(int jj=0;jj<N;jj++){
|
||||
ret._internal[ii][jj]=peekIndex<Level>(arg._internal[ii][jj]);// Could avoid this because peeking a scalar is dumb
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype,int N, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iMatrix<vtype,N> &arg,int i) -> iMatrix<decltype(peekIndex<Level>(arg._internal[0][0],i)),N>
|
||||
{
|
||||
iMatrix<decltype(peekIndex<Level>(arg._internal[0][0],i)),N> ret;
|
||||
for(int ii=0;ii<N;ii++){
|
||||
for(int jj=0;jj<N;jj++){
|
||||
ret._internal[ii][jj]=peekIndex<Level>(arg._internal[ii][jj],i);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype,int N, typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto peekIndex(const iMatrix<vtype,N> &arg,int i,int j) -> iMatrix<decltype(peekIndex<Level>(arg._internal[0][0],i,j)),N>
|
||||
{
|
||||
iMatrix<decltype(peekIndex<Level>(arg._internal[0][0],i,j)),N> ret;
|
||||
for(int ii=0;ii<N;ii++){
|
||||
for(int jj=0;jj<N;jj++){
|
||||
ret._internal[ii][jj]=peekIndex<Level>(arg._internal[ii][jj],i,j);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endif
|
101
lib/tensors/Tensor_poke.h
Normal file
101
lib/tensors/Tensor_poke.h
Normal file
@ -0,0 +1,101 @@
|
||||
#ifndef GRID_MATH_POKE_H
|
||||
#define GRID_MATH_POKE_H
|
||||
namespace Grid {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Poke a specific index;
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Scalar poke
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
void pokeIndex(iScalar<vtype> &ret, const iScalar<vtype> &arg)
|
||||
{
|
||||
ret._internal = arg._internal;
|
||||
}
|
||||
// Vector poke, one index
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
void pokeIndex(iVector<vtype,N> &ret, const iScalar<vtype> &arg,int i)
|
||||
{
|
||||
ret._internal[i] = arg._internal;
|
||||
}
|
||||
// Vector poke, two indices
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
void pokeIndex(iMatrix<vtype,N> &ret, const iScalar<vtype> &arg,int i,int j)
|
||||
{
|
||||
ret._internal[i][j] = arg._internal;
|
||||
}
|
||||
|
||||
/////////////
|
||||
// No match poke for scalar,vector,matrix must forward on either 0,1,2 args. Must have 9 routines with notvalue
|
||||
/////////////
|
||||
// scalar
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iScalar<vtype> &ret, const iScalar<decltype(peekIndex<Level>(ret._internal))> &arg)
|
||||
|
||||
{
|
||||
pokeIndex<Level>(ret._internal,arg._internal);
|
||||
}
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iScalar<vtype> &ret, const iScalar<decltype(peekIndex<Level>(ret._internal,0))> &arg, int i)
|
||||
|
||||
{
|
||||
pokeIndex<Level>(ret._internal,arg._internal,i);
|
||||
}
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iScalar<vtype> &ret, const iScalar<decltype(peekIndex<Level>(ret._internal,0,0))> &arg,int i,int j)
|
||||
{
|
||||
pokeIndex<Level>(ret._internal,arg._internal,i,j);
|
||||
}
|
||||
|
||||
// Vector
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iVector<vtype,N> &ret, iVector<decltype(peekIndex<Level>(ret._internal)),N> &arg)
|
||||
{
|
||||
for(int ii=0;ii<N;ii++){
|
||||
pokeIndex<Level>(ret._internal[ii],arg._internal[ii]);
|
||||
}
|
||||
}
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iVector<vtype,N> &ret, const iVector<decltype(peekIndex<Level>(ret._internal,0)),N> &arg,int i)
|
||||
{
|
||||
for(int ii=0;ii<N;ii++){
|
||||
pokeIndex<Level>(ret._internal[ii],arg._internal[ii],i);
|
||||
}
|
||||
}
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iVector<vtype,N> &ret, const iVector<decltype(peekIndex<Level>(ret._internal,0,0)),N> &arg,int i,int j)
|
||||
{
|
||||
for(int ii=0;ii<N;ii++){
|
||||
pokeIndex<Level>(ret._internal[ii],arg._internal[ii],i,j);
|
||||
}
|
||||
}
|
||||
|
||||
// Matrix
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iMatrix<vtype,N> &ret, const iMatrix<decltype(peekIndex<Level>(ret._internal)),N> &arg)
|
||||
{
|
||||
for(int ii=0;ii<N;ii++){
|
||||
for(int jj=0;jj<N;jj++){
|
||||
pokeIndex<Level>(ret._internal[ii][jj],arg._internal[ii][jj]);
|
||||
}}
|
||||
}
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iMatrix<vtype,N> &ret, const iMatrix<decltype(peekIndex<Level>(ret._internal,0)),N> &arg,int i)
|
||||
{
|
||||
for(int ii=0;ii<N;ii++){
|
||||
for(int jj=0;jj<N;jj++){
|
||||
pokeIndex<Level>(ret._internal[ii][jj],arg._internal[ii][jj],i);
|
||||
}}
|
||||
}
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
void pokeIndex(iMatrix<vtype,N> &ret, const iMatrix<decltype(peekIndex<Level>(ret._internal,0,0)),N> &arg, int i,int j)
|
||||
{
|
||||
for(int ii=0;ii<N;ii++){
|
||||
for(int jj=0;jj<N;jj++){
|
||||
pokeIndex<Level>(ret._internal[ii][jj],arg._internal[ii][jj],i,j);
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endif
|
207
lib/tensors/Tensor_reality.h
Normal file
207
lib/tensors/Tensor_reality.h
Normal file
@ -0,0 +1,207 @@
|
||||
#ifndef GRID_MATH_REALITY_H
|
||||
#define GRID_MATH_REALITY_H
|
||||
namespace Grid {
|
||||
|
||||
///////////////////////////////////////////////
|
||||
// multiply by I; make recursive.
|
||||
///////////////////////////////////////////////
|
||||
template<class vtype> inline iScalar<vtype> timesI(const iScalar<vtype>&r)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
timesI(ret._internal,r._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iVector<vtype,N> timesI(const iVector<vtype,N>&r)
|
||||
{
|
||||
iVector<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
timesI(ret._internal[i],r._internal[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iMatrix<vtype,N> timesI(const iMatrix<vtype,N>&r)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
timesI(ret._internal[i][j],r._internal[i][j]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class vtype> inline void timesI(iScalar<vtype> &ret,const iScalar<vtype>&r)
|
||||
{
|
||||
timesI(ret._internal,r._internal);
|
||||
}
|
||||
template<class vtype,int N> inline void timesI(iVector<vtype,N> &ret,const iVector<vtype,N>&r)
|
||||
{
|
||||
for(int i=0;i<N;i++){
|
||||
timesI(ret._internal[i],r._internal[i]);
|
||||
}
|
||||
}
|
||||
template<class vtype,int N> inline void timesI(iMatrix<vtype,N> &ret,const iMatrix<vtype,N>&r)
|
||||
{
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
timesI(ret._internal[i][j],r._internal[i][j]);
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
template<class vtype> inline iScalar<vtype> timesMinusI(const iScalar<vtype>&r)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
timesMinusI(ret._internal,r._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iVector<vtype,N> timesMinusI(const iVector<vtype,N>&r)
|
||||
{
|
||||
iVector<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
timesMinusI(ret._internal[i],r._internal[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iMatrix<vtype,N> timesMinusI(const iMatrix<vtype,N>&r)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
timesMinusI(ret._internal[i][j],r._internal[i][j]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class vtype> inline void timesMinusI(iScalar<vtype> &ret,const iScalar<vtype>&r)
|
||||
{
|
||||
timesMinusI(ret._internal,r._internal);
|
||||
}
|
||||
template<class vtype,int N> inline void timesMinusI(iVector<vtype,N> &ret,const iVector<vtype,N>&r)
|
||||
{
|
||||
for(int i=0;i<N;i++){
|
||||
timesMinusI(ret._internal[i],r._internal[i]);
|
||||
}
|
||||
}
|
||||
template<class vtype,int N> inline void timesMinusI(iMatrix<vtype,N> &ret,const iMatrix<vtype,N>&r)
|
||||
{
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
timesMinusI(ret._internal[i][j],r._internal[i][j]);
|
||||
}}
|
||||
}
|
||||
|
||||
|
||||
///////////////////////////////////////////////
|
||||
// Conj function for scalar, vector, matrix
|
||||
///////////////////////////////////////////////
|
||||
template<class vtype> inline iScalar<vtype> conjugate(const iScalar<vtype>&r)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
ret._internal = conjugate(r._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iVector<vtype,N> conjugate(const iVector<vtype,N>&r)
|
||||
{
|
||||
iVector<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
ret._internal[i] = conjugate(r._internal[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iMatrix<vtype,N> conjugate(const iMatrix<vtype,N>&r)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j] = conjugate(r._internal[i][j]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////
|
||||
// Adj function for scalar, vector, matrix
|
||||
///////////////////////////////////////////////
|
||||
template<class vtype> inline iScalar<vtype> adj(const iScalar<vtype>&r)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
ret._internal = adj(r._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iVector<vtype,N> adj(const iVector<vtype,N>&r)
|
||||
{
|
||||
iVector<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
ret._internal[i] = adj(r._internal[i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N> inline iMatrix<vtype,N> adj(const iMatrix<vtype,N> &arg)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
ret._internal[c1][c2]=adj(arg._internal[c2][c1]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// Can only take the real/imag part of scalar objects, since
|
||||
// lattice objects of different complex nature are non-conformable.
|
||||
/////////////////////////////////////////////////////////////////
|
||||
template<class itype> inline auto real(const iScalar<itype> &z) -> iScalar<decltype(real(z._internal))>
|
||||
{
|
||||
iScalar<decltype(real(z._internal))> ret;
|
||||
ret._internal = real(z._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class itype,int N> inline auto real(const iMatrix<itype,N> &z) -> iMatrix<decltype(real(z._internal[0][0])),N>
|
||||
{
|
||||
iMatrix<decltype(real(z._internal[0][0])),N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
ret._internal[c1][c2] = real(z._internal[c1][c2]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class itype,int N> inline auto real(const iVector<itype,N> &z) -> iVector<decltype(real(z._internal[0])),N>
|
||||
{
|
||||
iVector<decltype(real(z._internal[0])),N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
ret._internal[c1] = real(z._internal[c1]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class itype> inline auto imag(const iScalar<itype> &z) -> iScalar<decltype(imag(z._internal))>
|
||||
{
|
||||
iScalar<decltype(imag(z._internal))> ret;
|
||||
ret._internal = imag(z._internal);
|
||||
return ret;
|
||||
}
|
||||
template<class itype,int N> inline auto imag(const iMatrix<itype,N> &z) -> iMatrix<decltype(imag(z._internal[0][0])),N>
|
||||
{
|
||||
iMatrix<decltype(imag(z._internal[0][0])),N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
for(int c2=0;c2<N;c2++){
|
||||
ret._internal[c1][c2] = imag(z._internal[c1][c2]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class itype,int N> inline auto imag(const iVector<itype,N> &z) -> iVector<decltype(imag(z._internal[0])),N>
|
||||
{
|
||||
iVector<decltype(imag(z._internal[0])),N> ret;
|
||||
for(int c1=0;c1<N;c1++){
|
||||
ret._internal[c1] = imag(z._internal[c1]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
#endif
|
76
lib/tensors/Tensor_trace.h
Normal file
76
lib/tensors/Tensor_trace.h
Normal file
@ -0,0 +1,76 @@
|
||||
#ifndef GRID_MATH_TRACE_H
|
||||
#define GRID_MATH_TRACE_H
|
||||
namespace Grid {
|
||||
//////////////////////////////////////////////////////////////////
|
||||
// Traces: both all indices and a specific index
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
inline ComplexF trace( const ComplexF &arg){ return arg;}
|
||||
inline ComplexD trace( const ComplexD &arg){ return arg;}
|
||||
inline RealF trace( const RealF &arg){ return arg;}
|
||||
inline RealD trace( const RealD &arg){ return arg;}
|
||||
|
||||
template<int Level> inline ComplexF traceIndex(const ComplexF arg) { return arg;}
|
||||
template<int Level> inline ComplexD traceIndex(const ComplexD arg) { return arg;}
|
||||
template<int Level> inline RealF traceIndex(const RealF arg) { return arg;}
|
||||
template<int Level> inline RealD traceIndex(const RealD arg) { return arg;}
|
||||
|
||||
template<class vtype,int N>
|
||||
inline auto trace(const iMatrix<vtype,N> &arg) -> iScalar<decltype(trace(arg._internal[0][0]))>
|
||||
{
|
||||
iScalar<decltype( trace(arg._internal[0][0] )) > ret;
|
||||
zeroit(ret._internal);
|
||||
for(int i=0;i<N;i++){
|
||||
ret._internal=ret._internal+trace(arg._internal[i][i]);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype>
|
||||
inline auto trace(const iScalar<vtype> &arg) -> iScalar<decltype(trace(arg._internal))>
|
||||
{
|
||||
iScalar<decltype(trace(arg._internal))> ret;
|
||||
ret._internal=trace(arg._internal);
|
||||
return ret;
|
||||
}
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Trace Specific indices.
|
||||
////////////////////////////////////////////////////////////////////////////////////////////////////////
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline auto
|
||||
traceIndex (const iScalar<vtype> &arg) -> iScalar<decltype(traceIndex<Level>(arg._internal))>
|
||||
{
|
||||
iScalar<decltype(traceIndex<Level>(arg._internal))> ret;
|
||||
ret._internal=traceIndex<Level>(arg._internal);
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline auto
|
||||
traceIndex (const iScalar<vtype> &arg) -> iScalar<vtype>
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
|
||||
// If we hit the right index, return scalar and trace it with no further recursion
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel == Level >::type * =nullptr> inline
|
||||
auto traceIndex(const iMatrix<vtype,N> &arg) -> iScalar<vtype>
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
zeroit(ret._internal);
|
||||
for(int i=0;i<N;i++){
|
||||
ret._internal = ret._internal + arg._internal[i][i];
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
// not this level, so recurse
|
||||
template<int Level,class vtype,int N,typename std::enable_if< iScalar<vtype>::TensorLevel != Level >::type * =nullptr> inline
|
||||
auto traceIndex(const iMatrix<vtype,N> &arg) -> iMatrix<decltype(traceIndex<Level>(arg._internal[0][0])),N>
|
||||
{
|
||||
iMatrix<decltype(traceIndex<Level>(arg._internal[0][0])),N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j] = traceIndex<Level>(arg._internal[i][j]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
187
lib/tensors/Tensor_traits.h
Normal file
187
lib/tensors/Tensor_traits.h
Normal file
@ -0,0 +1,187 @@
|
||||
#ifndef GRID_MATH_TRAITS_H
|
||||
#define GRID_MATH_TRAITS_H
|
||||
|
||||
#include <type_traits>
|
||||
|
||||
namespace Grid {
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// Want to recurse: GridTypeMapper<Matrix<vComplexD> >::scalar_type == ComplexD.
|
||||
// Use of a helper class like this allows us to template specialise and "dress"
|
||||
// other classes such as RealD == double, ComplexD == std::complex<double> with these
|
||||
// traits.
|
||||
//
|
||||
// It is possible that we could do this more elegantly if I introduced a
|
||||
// queryable trait in iScalar, iMatrix and iVector and used the query on vtype in
|
||||
// place of the type mapper?
|
||||
//
|
||||
// Not sure how to do this, but probably could be done with a research effort
|
||||
// to study C++11's type_traits.h file. (std::enable_if<isGridTensorType<vtype> >)
|
||||
//
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
template <class T> class GridTypeMapper {
|
||||
public:
|
||||
typedef typename T::scalar_type scalar_type;
|
||||
typedef typename T::vector_type vector_type;
|
||||
typedef typename T::tensor_reduced tensor_reduced;
|
||||
typedef typename T::scalar_object scalar_object;
|
||||
enum { TensorLevel = T::TensorLevel };
|
||||
};
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
// Recursion stops with these template specialisations
|
||||
//////////////////////////////////////////////////////////////////////////////////
|
||||
template<> class GridTypeMapper<RealF> {
|
||||
public:
|
||||
typedef RealF scalar_type;
|
||||
typedef RealF vector_type;
|
||||
typedef RealF tensor_reduced ;
|
||||
typedef RealF scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<RealD> {
|
||||
public:
|
||||
typedef RealD scalar_type;
|
||||
typedef RealD vector_type;
|
||||
typedef RealD tensor_reduced;
|
||||
typedef RealD scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<ComplexF> {
|
||||
public:
|
||||
typedef ComplexF scalar_type;
|
||||
typedef ComplexF vector_type;
|
||||
typedef ComplexF tensor_reduced;
|
||||
typedef ComplexF scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<ComplexD> {
|
||||
public:
|
||||
typedef ComplexD scalar_type;
|
||||
typedef ComplexD vector_type;
|
||||
typedef ComplexD tensor_reduced;
|
||||
typedef ComplexD scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<Integer> {
|
||||
public:
|
||||
typedef Integer scalar_type;
|
||||
typedef Integer vector_type;
|
||||
typedef Integer tensor_reduced;
|
||||
typedef Integer scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
|
||||
template<> class GridTypeMapper<vRealF> {
|
||||
public:
|
||||
typedef RealF scalar_type;
|
||||
typedef vRealF vector_type;
|
||||
typedef vRealF tensor_reduced;
|
||||
typedef RealF scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<vRealD> {
|
||||
public:
|
||||
typedef RealD scalar_type;
|
||||
typedef vRealD vector_type;
|
||||
typedef vRealD tensor_reduced;
|
||||
typedef RealD scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<vComplexF> {
|
||||
public:
|
||||
typedef ComplexF scalar_type;
|
||||
typedef vComplexF vector_type;
|
||||
typedef vComplexF tensor_reduced;
|
||||
typedef ComplexF scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<vComplexD> {
|
||||
public:
|
||||
typedef ComplexD scalar_type;
|
||||
typedef vComplexD vector_type;
|
||||
typedef vComplexD tensor_reduced;
|
||||
typedef ComplexD scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
template<> class GridTypeMapper<vInteger> {
|
||||
public:
|
||||
typedef Integer scalar_type;
|
||||
typedef vInteger vector_type;
|
||||
typedef vInteger tensor_reduced;
|
||||
typedef Integer scalar_object;
|
||||
enum { TensorLevel = 0 };
|
||||
};
|
||||
|
||||
// First some of my own traits
|
||||
template<typename T> struct isGridTensor {
|
||||
static const bool value = true;
|
||||
static const bool notvalue = false;
|
||||
};
|
||||
|
||||
template<> struct isGridTensor<int > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<RealD > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<RealF > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<ComplexD > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<ComplexF > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<Integer > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<vRealD > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<vRealF > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<vComplexD > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<vComplexF > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
template<> struct isGridTensor<vInteger > {
|
||||
static const bool value = false;
|
||||
static const bool notvalue = true;
|
||||
};
|
||||
|
||||
// Match the index
|
||||
template<typename T,int Level> struct matchGridTensorIndex {
|
||||
static const bool value = (Level==T::TensorLevel);
|
||||
static const bool notvalue = (Level!=T::TensorLevel);
|
||||
};
|
||||
// What is the vtype
|
||||
template<typename T> struct isComplex {
|
||||
static const bool value = false;
|
||||
};
|
||||
template<> struct isComplex<ComplexF> {
|
||||
static const bool value = true;
|
||||
};
|
||||
template<> struct isComplex<ComplexD> {
|
||||
static const bool value = true;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
101
lib/tensors/Tensor_transpose.h
Normal file
101
lib/tensors/Tensor_transpose.h
Normal file
@ -0,0 +1,101 @@
|
||||
#ifndef GRID_MATH_TRANSPOSE_H
|
||||
#define GRID_MATH_TRANSPOSE_H
|
||||
namespace Grid {
|
||||
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////////////////
|
||||
// Transpose all indices
|
||||
/////////////////////////////////////////////////////////////////
|
||||
|
||||
inline ComplexD transpose(ComplexD &rhs){ return rhs;}
|
||||
inline ComplexF transpose(ComplexF &rhs){ return rhs;}
|
||||
inline RealD transpose(RealD &rhs){ return rhs;}
|
||||
inline RealF transpose(RealF &rhs){ return rhs;}
|
||||
|
||||
template<class vtype,int N>
|
||||
inline typename std::enable_if<isGridTensor<vtype>::value, iMatrix<vtype,N> >::type
|
||||
transpose(iMatrix<vtype,N> arg)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j] = transpose(arg._internal[j][i]); // NB recurses
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<class vtype,int N>
|
||||
inline typename std::enable_if<isGridTensor<vtype>::notvalue, iMatrix<vtype,N> >::type
|
||||
transpose(iMatrix<vtype,N> arg)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j] = arg._internal[j][i]; // Stop recursion if not a tensor type
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class vtype>
|
||||
inline typename std::enable_if<isGridTensor<vtype>::value, iScalar<vtype> >::type
|
||||
transpose(iScalar<vtype> arg)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
ret._internal = transpose(arg._internal); // NB recurses
|
||||
return ret;
|
||||
}
|
||||
|
||||
template<class vtype>
|
||||
inline typename std::enable_if<isGridTensor<vtype>::notvalue, iScalar<vtype> >::type
|
||||
transpose(iScalar<vtype> arg)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
ret._internal = arg._internal; // NB recursion stops
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
// Transpose a specific index; instructive to compare this style of recursion termination
|
||||
// to that of adj; which is easiers?
|
||||
////////////////////////////////////////////////////////////////////////////////////////////
|
||||
template<int Level,class vtype,int N> inline
|
||||
typename std::enable_if<matchGridTensorIndex<iMatrix<vtype,N>,Level>::value, iMatrix<vtype,N> >::type
|
||||
transposeIndex (const iMatrix<vtype,N> &arg)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j] = arg._internal[j][i];
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
// or not
|
||||
template<int Level,class vtype,int N> inline
|
||||
typename std::enable_if<matchGridTensorIndex<iMatrix<vtype,N>,Level>::notvalue, iMatrix<vtype,N> >::type
|
||||
transposeIndex (const iMatrix<vtype,N> &arg)
|
||||
{
|
||||
iMatrix<vtype,N> ret;
|
||||
for(int i=0;i<N;i++){
|
||||
for(int j=0;j<N;j++){
|
||||
ret._internal[i][j] = transposeIndex<Level>(arg._internal[i][j]);
|
||||
}}
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype> inline
|
||||
typename std::enable_if<matchGridTensorIndex<iScalar<vtype>,Level>::notvalue, iScalar<vtype> >::type
|
||||
transposeIndex (const iScalar<vtype> &arg)
|
||||
{
|
||||
iScalar<vtype> ret;
|
||||
ret._internal=transposeIndex<Level>(arg._internal);
|
||||
return ret;
|
||||
}
|
||||
template<int Level,class vtype> inline
|
||||
typename std::enable_if<matchGridTensorIndex<iScalar<vtype>,Level>::value, iScalar<vtype> >::type
|
||||
transposeIndex (const iScalar<vtype> &arg)
|
||||
{
|
||||
return arg;
|
||||
}
|
||||
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user