Grid 0.7.0
Tensor_trace.h
Go to the documentation of this file.
1/*************************************************************************************
2
3 Grid physics library, www.github.com/paboyle/Grid
4
5 Source file: ./lib/tensors/Tensor_trace.h
6
7 Copyright (C) 2015
8
9Author: Peter Boyle <paboyle@ph.ed.ac.uk>
10Author: neo <cossu@post.kek.jp>
11
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21
22 You should have received a copy of the GNU General Public License along
23 with this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25
26 See the full license in the file "LICENSE" in the top level distribution directory
27*************************************************************************************/
28/* END LEGAL */
29#ifndef GRID_MATH_TRACE_H
30#define GRID_MATH_TRACE_H
31
33
35// Traces: both all indices and a specific index. Indices must be
36// either scalar or matrix
38
39accelerator_inline ComplexF trace( const ComplexF &arg){ return arg;}
40accelerator_inline ComplexD trace( const ComplexD &arg){ return arg;}
41accelerator_inline RealF trace( const RealF &arg){ return arg;}
42accelerator_inline RealD trace( const RealD &arg){ return arg;}
43
44template<class vtype,int N>
45accelerator_inline auto trace(const iMatrix<vtype,N> &arg) -> iScalar<decltype(trace(arg._internal[0][0]))>
46{
47 iScalar<decltype( trace(arg._internal[0][0] )) > ret;
48 zeroit(ret._internal);
49 for(int i=0;i<N;i++){
50 ret._internal=ret._internal+trace(arg._internal[i][i]);
51 }
52 return ret;
53}
54
55template<class vtype>
56accelerator_inline auto trace(const iScalar<vtype> &arg) -> iScalar<decltype(trace(arg._internal))>
57{
58 iScalar<decltype(trace(arg._internal))> ret;
59 ret._internal=trace(arg._internal);
60 return ret;
61}
62
63template<class vtype,int N>
64accelerator_inline auto trace(const iVector<vtype,N> &arg) -> iVector<decltype(trace(arg._internal[0])),N>
65{
66 iVector<decltype(trace(arg._internal[0])),N> ret;
67 for(int i=0;i<N;i++){
68 ret._internal[i]=trace(arg._internal[i]);
69 }
70 return ret;
71}
72
73// Fast path traceProduct
75template<class S1 , class S2, IfNotGridTensor<S1> = 0, IfNotGridTensor<S2> = 0>
76accelerator_inline auto traceProduct( const S1 &arg1,const S2 &arg2)
77 -> decltype(arg1*arg2)
78{
79 return arg1*arg2;
80}
81
82template<class vtype,class rtype,int N >
83accelerator_inline auto traceProduct(const iMatrix<vtype,N> &arg1,const iMatrix<rtype,N> &arg2) -> iScalar<decltype(trace(arg1._internal[0][0]*arg2._internal[0][0]))>
84{
85 iScalar<decltype( trace(arg1._internal[0][0]*arg2._internal[0][0] )) > ret;
86 zeroit(ret._internal);
87 for(int i=0;i<N;i++){
88 for(int j=0;j<N;j++){
89 ret._internal=ret._internal+traceProduct(arg1._internal[i][j],arg2._internal[j][i]);
90 }}
91 return ret;
92}
93
94template<class vtype,class rtype >
95accelerator_inline auto traceProduct(const iScalar<vtype> &arg1,const iScalar<rtype> &arg2) -> iScalar<decltype(trace(arg1._internal*arg2._internal))>
96{
97 iScalar<decltype(trace(arg1._internal*arg2._internal))> ret;
98 ret._internal=traceProduct(arg1._internal,arg2._internal);
99 return ret;
100}
101
103
104#endif
#define accelerator_inline
accelerator_inline void zeroit(Grid_simd2< S, V > &z)
#define NAMESPACE_BEGIN(A)
Definition Namespace.h:35
#define NAMESPACE_END(A)
Definition Namespace.h:36
std::complex< RealF > ComplexF
Definition Simd.h:78
float RealF
Definition Simd.h:60
std::complex< RealD > ComplexD
Definition Simd.h:79
double RealD
Definition Simd.h:61
accelerator_inline ComplexF trace(const ComplexF &arg)
accelerator_inline auto traceProduct(const S1 &arg1, const S2 &arg2) -> decltype(arg1 *arg2)
vtype _internal