AmpGen 2.1
Loading...
Searching...
No Matches
Tensor.h
Go to the documentation of this file.
1#ifndef AMPGEN_TENSOR_H
2#define AMPGEN_TENSOR_H
3#include <memory.h>
4#include <stddef.h>
5#include <algorithm>
6#include <complex>
7#include <initializer_list>
8#include <iostream>
9#include <string>
10#include <vector>
11#include <utility>
12
13#include "AmpGen/Expression.h"
14#include "AmpGen/MsgService.h"
15#include "AmpGen/Types.h"
16
17#define ADD_DEBUG_TENSOR( X, Y ) \
18 if ( Y != nullptr ) for( unsigned i = 0 ; i < Tensor(X).size(); ++i ) \
19 Y->emplace_back( std::string(#X) + Tensor::coordinates_to_string( Tensor(X).coords(i) ) , Tensor(X)[i] );
20
21#define ADD_DEBUG_TENSOR_NAMED( X, Y, Z ) \
22 if ( Y != nullptr ) for( unsigned i = 0 ; i < X.size(); ++i ) \
23 Y->emplace_back( Z + Tensor::coordinates_to_string( X.coords(i) ) , X[i] );
24
25namespace AmpGen
26{
27 class ASTResolver;
28 class TensorProxy;
29
30 class Tensor
31 {
32 public:
33 class Index {
34 private:
35 std::shared_ptr<int> m_ptr;
36 bool m_isUpper;
37 public:
38 bool operator==( const Tensor::Index& other ) const { return m_ptr.get() == other.m_ptr.get(); }
39 bool operator!=( const Tensor::Index& other ) const { return m_ptr.get() != other.m_ptr.get(); }
40 bool isUpper() const { return m_isUpper; };
41 explicit Index( bool isUpper = false ) : m_ptr( std::make_shared<int>() ), m_isUpper( isUpper ) {}
42 Index( const std::shared_ptr<int>& index, bool isUpper = false ) : m_ptr( index ), m_isUpper( isUpper ) {}
43 Index operator-() const { return Index( m_ptr, !m_isUpper ); }
44 friend std::ostream& operator <<( std::ostream& out, const Index& index );
45 uint64_t ptr() const { return uint64_t(m_ptr.get()); }
46 };
47 struct Dim : public std::vector<unsigned> {
48 Dim( unsigned a ) : std::vector<unsigned>({a}) {}
49 Dim( unsigned a , unsigned b) : std::vector<unsigned>({a,b}) {}
50 Dim( unsigned a , unsigned b, unsigned c) : std::vector<unsigned>({a,b,c}) {}
51 Dim( unsigned a , unsigned b, unsigned c, unsigned d ) : std::vector<unsigned>({a,b,c,d}) {}
52
53 };
54
56 explicit Tensor(const std::vector<Expression>& elements);
57 explicit Tensor(const std::vector<unsigned>& dim);
58 explicit Tensor(const Tensor::Dim& dim) : Tensor( std::vector<unsigned>(dim) ) {};
59
60 template <class TYPE> Tensor(const std::initializer_list<TYPE>& elements,
61 const std::vector<unsigned>& dim) : m_dim(dim)
62 {
64 for ( auto& x : elements ) append( x );
65 }
66
67 template <class TYPE> Tensor(const std::vector<TYPE>& elements,
68 const std::vector<unsigned>& dim) : m_dim(dim)
69 {
71 for ( auto& x : elements ) append( x );
72 }
73
75 Expression& operator[]( const unsigned& i );
76 Expression& operator[]( const std::vector<unsigned>& co );
77 const Expression& operator[]( const unsigned& i ) const;
78 const Expression& operator[]( const std::vector<unsigned>& co ) const;
79
80 Expression get( const unsigned& co );
81 Expression get( const unsigned& co ) const;
82 Expression get( const std::vector<unsigned>& _co ) const;
83
86
87 Expression& operator()( const unsigned& a ) { return Tensor::operator[]( {a} ) ; }
88 Expression& operator()( const unsigned& a, const unsigned& b) { return Tensor::operator[]( {a,b} ) ; }
89 const Expression operator()( const unsigned& a ) const { return Tensor::operator[]( {a} ) ; }
90 const Expression& operator()( const unsigned& a, const unsigned& b) const { return Tensor::operator[]( {a,b} ) ; }
91
94 TensorProxy operator()( const Tensor::Index& a, const Tensor::Index& b, const Tensor::Index& c ) const;
96 const Tensor::Index& d ) const;
97 TensorProxy operator()( const std::vector<Tensor::Index>& indices ) const;
98
99 Tensor operator-() const;
100
101 void st(const bool simplify=false);
102 bool rankMatches( const Tensor& other );
103
104 void imposeSymmetry( unsigned indexA, unsigned indexB);
105 void imposeSymmetry( std::vector<unsigned> indices );
106
107 Tensor Invert() const;
108 std::string to_string(const ASTResolver* resolver=nullptr) const ;
109
110 int metricSgn( const std::vector<unsigned>& coordinates ) const;
111 int metricSgn( const unsigned& index ) const;
112 void append( const Expression& expression );
113 void append( const real_t& value );
114 void append( const complex_t& value );
115 void append( const std::string& value );
117
118 unsigned nDim() const;
119 unsigned rank() const;
120 unsigned size() const;
121 unsigned index( const std::vector<unsigned>& _co ) const;
122 unsigned symmetrisedIndex( const std::vector<unsigned>& _co ) const;
123 unsigned nElements() const;
124
125 const std::vector<unsigned> coords( const unsigned& index ) const;
126 const std::vector<unsigned>& dims() const { return m_dim; }
127 const std::string dimString() const ;
128
129 void print(const bool& eval = false) const;
130
131 const std::vector<unsigned>& uniqueElements() const { return m_uniqueElements; }
132 void operator+=( const Tensor& rhs );
133 void operator-=( const Tensor& rhs );
135
136 static std::vector<unsigned> index_to_coordinates( const unsigned& index, const std::vector<unsigned>& dim );
137 static unsigned coordinates_to_index( const std::vector<unsigned>& coords, const std::vector<unsigned>& dim );
138 static std::string coordinates_to_string( const std::vector<unsigned >& coordinates );
139 template <class... ARGS>
140 static std::vector<unsigned> dim( const ARGS&... args ){
141 std::vector<unsigned> rt;
142 auto up = std::tuple<ARGS...>(args...);
143 for_each(up, [&rt]( const unsigned& f ) { rt.emplace_back(f); } );
144 return rt;
145 }
146 private:
147 std::vector<unsigned> m_dim;
148 std::vector<unsigned> m_symmetrisedCoordinates;
149 std::vector<unsigned> m_uniqueElements;
150 std::vector<Expression> m_elements;
151 };
152
161 {
162 public:
163 TensorProxy( const Tensor& tensor, const std::vector<Tensor::Index>& indices ) ;
164 std::vector<Tensor::Index> indices() const;
165 const Tensor& tensor() const;
167 TensorProxy reorder( const std::vector<Tensor::Index>& indices );
168 operator Expression() const;
169 operator Tensor() const;
170
171 private:
172 Tensor m_tensor;
173 std::vector<Tensor::Index> m_indices;
174 };
175
177 {
178 public:
180 std::string to_string(const ASTResolver* resolver) const override;
181 void resolve( ASTResolver& resolver ) const override;
182 complex_t operator()() const override;
183 operator Expression() const;
184 Tensor tensor() const { return m_tensor;}
185 unsigned size() const { return m_tensor.size(); }
186 private:
187 Tensor m_tensor;
188 };
189
190 Tensor operator+(const Tensor&, const Tensor&);
191 Tensor operator-(const Tensor&, const Tensor&);
195
196 Tensor operator/(const Tensor&, const double&);
197 Tensor operator*(const double&, const Tensor&);
198 Tensor operator*(const Tensor&, const double&);
199
203
207
208 TensorProxy operator/(const TensorProxy&, const double&);
209 TensorProxy operator*(const double& , const TensorProxy&);
210 TensorProxy operator*(const TensorProxy&, const double&);
211
212 Tensor Identity( const unsigned& rank = 4 );
213
214 const Tensor LeviCivita( const unsigned& rank = 4);
215 Expression dot( const Tensor& A, const Tensor& B );
216
217 std::ostream& operator <<( std::ostream& out, const Tensor::Index& index );
218} // namespace AmpGen
219
220#endif
(Internal) class to aide in the resolution of the dependencies of expression trees.
Definition ASTResolver.h:29
Wrapper class for shared_ptrs to virtual expressions for use in conjunction with operators to build e...
Definition Expression.h:135
Virtual base class for other expression tree components.
Definition Expression.h:116
Index(bool isUpper=false)
Definition Tensor.h:41
bool operator!=(const Tensor::Index &other) const
Definition Tensor.h:39
uint64_t ptr() const
Definition Tensor.h:45
bool operator==(const Tensor::Index &other) const
Definition Tensor.h:38
bool isUpper() const
Definition Tensor.h:40
Index(const std::shared_ptr< int > &index, bool isUpper=false)
Definition Tensor.h:42
Index operator-() const
Definition Tensor.h:43
friend std::ostream & operator<<(std::ostream &out, const Index &index)
Tensor tensor() const
Definition Tensor.h:184
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
unsigned size() const
Definition Tensor.h:185
std::string to_string(const ASTResolver *resolver) const override
Called to convert the Expression tree into source code.
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
TensorExpression(const Tensor &tensor)
Tensor(const Tensor::Dim &dim)
Definition Tensor.h:58
unsigned nElements() const
Expression get(const unsigned &co) const
const Expression & operator()(const unsigned &a, const unsigned &b) const
Definition Tensor.h:90
const std::vector< unsigned > coords(const unsigned &index) const
int metricSgn(const unsigned &index) const
void append(const real_t &value)
void append(const std::string &value)
std::string to_string(const ASTResolver *resolver=nullptr) const
unsigned rank() const
Tensor(const std::vector< TYPE > &elements, const std::vector< unsigned > &dim)
Definition Tensor.h:67
unsigned size() const
void append(const Expression &expression)
unsigned symmetrisedIndex(const std::vector< unsigned > &_co) const
const Expression operator()(const unsigned &a) const
Definition Tensor.h:89
void append(const complex_t &value)
const std::vector< unsigned > & uniqueElements() const
Definition Tensor.h:131
Tensor(const std::vector< Expression > &elements)
Expression & operator()(const unsigned &a)
TensorProxy access to class members High level access is done via these commands, i....
Definition Tensor.h:87
void st(const bool simplify=false)
const Expression & operator[](const unsigned &i) const
Expression & operator[](const unsigned &i)
Low level access of elements, either by coordinates or by index ///.
void operator+=(const Tensor &rhs)
static std::vector< unsigned > index_to_coordinates(const unsigned &index, const std::vector< unsigned > &dim)
Expression & operator[](const std::vector< unsigned > &co)
Expression get(const std::vector< unsigned > &_co) const
const Expression & operator[](const std::vector< unsigned > &co) const
Tensor(const std::vector< unsigned > &dim)
TensorProxy operator()(const Tensor::Index &a, const Tensor::Index &b) const
TensorProxy operator()(const Tensor::Index &a, const Tensor::Index &b, const Tensor::Index &c) const
Tensor conjugate() const
Tensor operator-() const
void setupCoordinates()
unsigned index(const std::vector< unsigned > &_co) const
Tensor(const std::initializer_list< TYPE > &elements, const std::vector< unsigned > &dim)
Definition Tensor.h:60
static unsigned coordinates_to_index(const std::vector< unsigned > &coords, const std::vector< unsigned > &dim)
void imposeSymmetry(std::vector< unsigned > indices)
void operator-=(const Tensor &rhs)
int metricSgn(const std::vector< unsigned > &coordinates) const
void print(const bool &eval=false) const
bool rankMatches(const Tensor &other)
const std::string dimString() const
TensorProxy operator()(const Tensor::Index &a, const Tensor::Index &b, const Tensor::Index &c, const Tensor::Index &d) const
TensorProxy operator()(const std::vector< Tensor::Index > &indices) const
Expression get(const unsigned &co)
void imposeSymmetry(unsigned indexA, unsigned indexB)
Tensor Invert() const
TensorProxy operator()(const Tensor::Index &a) const
Expression & operator()(const unsigned &a, const unsigned &b)
Definition Tensor.h:88
static std::vector< unsigned > dim(const ARGS &... args)
Definition Tensor.h:140
const std::vector< unsigned > & dims() const
Definition Tensor.h:126
static std::string coordinates_to_string(const std::vector< unsigned > &coordinates)
unsigned nDim() const
Utility class that wraps a tensor and a set of indices such that tensor operations can be performed.
Definition Tensor.h:161
const Tensor & tensor() const
TensorProxy reorder(const std::vector< Tensor::Index > &indices)
TensorProxy(const Tensor &tensor, const std::vector< Tensor::Index > &indices)
Tensor & tensorMutable()
std::vector< Tensor::Index > indices() const
double real_t
Definition Types.h:6
Complex< real_t > operator/(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:53
Complex< real_t > operator*(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:52
std::complex< real_t > complex_t
Definition Types.h:7
Expression dot(const Tensor &A, const Tensor &B)
std::ostream & operator<<(std::ostream &os, const CompiledExpressionBase &expression)
const Tensor LeviCivita(const unsigned &rank=4)
Complex< real_t > operator+(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:50
Complex< real_t > operator-(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:51
Tensor Identity(const unsigned &rank=4)
std::enable_if_t< I==sizeof...(Tp), void > for_each(std::tuple< Tp... > &, FuncT)
Definition MetaUtils.h:39
Dim(unsigned a, unsigned b)
Definition Tensor.h:49
Dim(unsigned a, unsigned b, unsigned c, unsigned d)
Definition Tensor.h:51
Dim(unsigned a)
Definition Tensor.h:48
Dim(unsigned a, unsigned b, unsigned c)
Definition Tensor.h:50