AmpGen 2.1
Loading...
Searching...
No Matches
Expression.h
Go to the documentation of this file.
1#ifndef AMPGEN_EXPRESSION_H
2#define AMPGEN_EXPRESSION_H
3
38
41#define ADD_DEBUG( X, Y ) \
42 if ( Y != 0 ) Y->push_back( DebugSymbol( std::string( #X ), X ) ); \
43
44
46#define DEFINE_CAST( X ) \
47 X::operator Expression() const \
48{ return Expression( std::make_shared<X>( *this ) ); }
49
50#define DEFINE_BINARY_OPERATOR( X ) \
51 X::X( const AmpGen::Expression& l, const AmpGen::Expression& r ) : IBinaryExpression(l,r ) {} \
52 X::X( const AmpGen::Expression& expr) : IBinaryExpression(expr) {} \
53 X::operator Expression() const { return Expression( std::make_shared<X>(*this) ) ; }
54
55#define DEFINE_UNARY_OPERATOR( X, F ) \
56 X::X( const AmpGen::Expression& expression) : IUnaryExpression(expression) {} \
57 X::operator Expression() const { return Expression( std::make_shared<X>(*this) ) ; } \
58 complex_t X::operator()() const { return F( m_expression() ); } \
59 std::string X::to_string(const ASTResolver* resolver) const { return std::string(#F)+"("+ m_expression.to_string(resolver)+")";}
60
61#define DEFINE_UNARY_OPERATOR_NO_RESOLVER( X, F ) \
62 X::X( const AmpGen::Expression& expression) : IUnaryExpression(expression) {} \
63 X::operator Expression() const { return Expression( std::make_shared<X>(*this) ) ; } \
64 complex_t X::operator()() const { return F( m_expression() ); }
65
68#define DECLARE_UNARY_OPERATOR( X ) \
69 class X : public IUnaryExpression { \
70 public: \
71 explicit X( const Expression& other ); \
72 virtual std::string to_string(const ASTResolver* resolver=nullptr) const override; \
73 virtual Expression d() const override; \
74 operator Expression() const; \
75 virtual complex_t operator()() const override; \
76 }
77
80#define DECLARE_BINARY_OPERATOR( X ) \
81 class X : public IBinaryExpression { \
82 public: \
83 X( const Expression& l, const Expression& r ); \
84 X( const Expression& expr); \
85 virtual std::string to_string(const ASTResolver* resolver=nullptr) const override ; \
86 operator Expression() const ; \
87 virtual complex_t operator()() const override; \
88 }
89
90#include <algorithm>
91#include <complex>
92#include <iostream>
93#include <map>
94#include <memory>
95#include <sstream>
96#include <string>
97#include <utility>
98#include <vector>
99#include <functional>
100#include "AmpGen/MsgService.h"
101#include "AmpGen/MetaUtils.h"
102#include "AmpGen/Types.h"
103
104namespace AmpGen
105{
106 class ASTResolver;
107 class Expression;
108 class Parameter;
109
110 typedef std::pair<std::string, Expression> DebugSymbol;
111 typedef std::vector<DebugSymbol> DebugSymbols;
112
117 public:
120 virtual std::string to_string(const ASTResolver* resolver=nullptr) const = 0;
124 virtual void resolve( ASTResolver& resolver ) const = 0;
126 virtual ~IExpression() = default;
129 virtual complex_t operator()() const = 0;
130 };
131
135 class Expression {
136 public:
138 Expression( const real_t& value );
139 Expression( const complex_t& value );
140 Expression( const std::shared_ptr<IExpression>& expression ) ;
141 ~Expression() = default;
142 std::string to_string(const ASTResolver* resolver=nullptr) const;
143 IExpression* get() const;
144 void resolve( ASTResolver& resolver ) const;
151
152 private:
153 std::shared_ptr<IExpression> m_expression;
154 };
155
158 class Constant : public IExpression {
159 public:
160 template <typename T1,
161 typename T2,
162 typename = std::enable_if_t< std::is_constructible< complex_t, T1, T2 >::value > >
163 Constant( const T1& real, const T2& imag=0 ) : m_value(real,imag) {}
164
165 Constant( const complex_t& value ) : m_value(value) {}
166 std::string to_string(const ASTResolver* resolver=nullptr) const override;
167 void resolve( ASTResolver& resolver ) const override;
168 operator Expression() const;
169 complex_t operator()() const override { return m_value; }
170 private:
171 complex_t m_value;
172 };
173
181 class Parameter : public IExpression {
182 public:
183 Parameter(const std::string& name = "",
184 const double& defaultValue = 0 ,
185 const bool& resolved = false);
186 std::string to_string(const ASTResolver* resolver = nullptr) const override;
187 void resolve( ASTResolver& resolver ) const override;
188 virtual operator Expression() const;
189 complex_t operator()() const override { return complex_t( m_defaultValue, 0 ); }
190 std::string name() const { return m_name; }
191 const double& defaultValue() const { return m_defaultValue ; }
192 double& defaultValue() { return m_defaultValue ; }
193 bool isResolved() const { return m_resolved ;}
194 private:
195 std::string m_name;
196 double m_defaultValue;
197 bool m_resolved;
198 };
199
201 public:
203 std::string to_string(const ASTResolver* resolver = nullptr ) const override;
204 void resolve( ASTResolver& resolver ) const override;
205 operator Expression() const ;
206 complex_t operator()() const override;
207 private:
208 Parameter m_real;
209 Parameter m_imag;
210 };
211
215 public:
216 template <typename function_type>
217 LambdaExpression( const function_type& function) : m_function(function), m_name(type_string<function_type>()) {}
218 std::string to_string(const ASTResolver* resolver = nullptr) const override;
219 void resolve( ASTResolver& resolver) const override;
220 operator Expression() const;
221 complex_t operator()() const override;
222 std::function<double(void)> m_function;
223 std::string m_name;
224 };
225
233 class Ternary : public IExpression {
234 public:
235 Ternary( const Expression& cond, const Expression& v1, const Expression& v2 );
236 std::string to_string(const ASTResolver* resolver = nullptr ) const override;
237 void resolve( ASTResolver& resolver ) const override;
238 operator Expression() const ;
239 complex_t operator()() const override { return std::real(m_cond()) ? m_v1() : m_v2(); }
240 private:
241 Expression m_cond;
242 Expression m_v1;
243 Expression m_v2;
244 };
245
247 struct SubTree : public IExpression {
248 SubTree( const Expression& other ) ;
249 std::string to_string(const ASTResolver* resolver = nullptr ) const override ;
250 void resolve( ASTResolver& resolver ) const override;
251 operator Expression() const ;
252 complex_t operator()() const override { return m_expression(); }
253 uint64_t key() const;
254 void setKey( const size_t& new_key );
257 uint64_t m_key;
258 };
259
260 struct Function : public IExpression {
261 Function( const std::string& name, const std::vector<Expression>& args ) ;
262 std::string to_string(const ASTResolver* resolver = nullptr ) const override ;
263 void resolve( ASTResolver& resolver ) const override;
264 operator Expression() const ;
265 complex_t operator()() const override { return 0; }
266 std::string m_name;
267 std::vector<Expression> m_args;
268 };
269
271
273 public:
274 explicit ExpressionPack( const std::vector<Expression>& expressions ): m_expressions(expressions) {}
275 ExpressionPack( const Expression& A, const Expression& B );
276 std::string to_string(const ASTResolver* resolver=nullptr) const override;
277 void resolve( ASTResolver& resolver ) const override;
278 complex_t operator()() const override;
279 operator Expression() const ;
280 const std::vector<Expression>& expressions() const { return m_expressions ; }
281 private:
282 std::vector<Expression> m_expressions;
283 };
284
288 public:
289 IBinaryExpression( const Expression& l, const Expression& r ) : lval( l ), rval( r ){};
291 {
292 auto as_pack = static_cast< const ExpressionPack*>(pack.get() );
293 if( as_pack != nullptr )
294 {
295 auto expr = as_pack->expressions();
296 if( expr.size() != 2 ) FATAL("Wrong number of inputs");
297 lval = expr[0];
298 rval = expr[1];
299 }
300 else {
301 FATAL("wrong number of inputs" );
302 }
303 }
304 void resolve( ASTResolver& resolver ) const override;
305 complex_t operator()() const override = 0;
306 Expression l() const { return lval ; }
307 Expression r() const { return rval ; }
308 protected:
311 };
312
316
320
324
328
332
336
340
344
348
352
356
361
366 public:
367 IUnaryExpression( const Expression& other ) : m_expression( other ){};
368 void resolve( ASTResolver& resolver ) const override;
369 complex_t operator()() const override = 0;
370 virtual Expression d() const = 0;
371 Expression arg() const { return m_expression ;}
372 protected:
374 };
375
378
382
386
390
394
398
402
406
410
414
418
422
429
433
437
442
447
448 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
449 Expression operator+( const Expression& A, const T& B ){ return A + Constant(B); }
450 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
451 Expression operator-( const Expression& A, const T& B ){ return A - Constant(B); }
452 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
453 Expression operator*( const Expression& A, const T& B ){ return A * Constant(B); }
454 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
455 Expression operator/( const Expression& A, const T& B ){ return A / Constant(B); }
456
457 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
458 Expression operator+( const T& A, const Expression& B ){ return Constant(A) + B; }
459 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
460 Expression operator-( const T& A, const Expression& B ){ return Constant(A) - B; }
461 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
462 Expression operator*( const T& A, const Expression& B ){ return Constant(A) * B; }
463 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
464 Expression operator/( const T& A, const Expression& B ){ return Constant(A) / B; }
465
469 Expression operator==( const Expression& A, const double& B );
470 Expression operator==( const double& A, const Expression& B );
471
472 std::ostream& operator<<( std::ostream& os, const Expression& expression ) ;
473 namespace fcn {
474 Expression sqrt( const Expression& expression );
475 Expression safe_sqrt( const Expression& expression );
476 Expression complex_sqrt( const Expression& expression );
477 Expression isqrt( const Expression& expression );
478 Expression cos( const Expression& expression );
479 Expression sin( const Expression& expression );
480 Expression tan( const Expression& expression );
481 Expression abs( const Expression& expression );
482 Expression acos( const Expression& expression );
483 Expression asin( const Expression& expression );
484 Expression atan( const Expression& expression );
485 Expression pow( const Expression& expression, const Expression& co );
486 Expression fpow( const Expression& expression, const int& n );
487 Expression norm( const Expression& expression );
488 Expression conj( const Expression& expression );
489 Expression exp( const Expression& expression );
490 Expression log( const Expression& expression );
491 Expression atan2( const Expression& y, const Expression& x);
492 }
493
494 template < class T > bool is( const Expression& expression ){
495 return dynamic_cast< const T* >( expression.get() ) != nullptr;
496 }
497 template < class T > T cast( const Expression& expression ){
498 return *static_cast< const T*>(expression.get() );
499 }
500
501 Expression make_cse( const Expression& A , bool simplify = false);
502
503} // namespace AmpGen
504
505#endif
ACos(const Expression &other)
(Internal) class to aide in the resolution of the dependencies of expression trees.
Definition ASTResolver.h:29
ASin(const Expression &other)
ATan2(const Expression &l, const Expression &r)
ATan(const Expression &other)
Abs(const Expression &other)
And(const Expression &l, const Expression &r)
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
ComplexParameter(const Parameter &real, const Parameter &imag)
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
Conj(const Expression &other)
Class to contain a constant (which can contain a complex value)
Definition Expression.h:158
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Definition Expression.h:169
Constant(const complex_t &value)
Definition Expression.h:165
Constant(const T1 &real, const T2 &imag=0)
Definition Expression.h:163
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
Cos(const Expression &other)
Divide(const Expression &l, const Expression &r)
Equal(const Expression &l, const Expression &r)
Exp(const Expression &other)
Wrapper class for shared_ptrs to virtual expressions for use in conjunction with operators to build e...
Definition Expression.h:135
void resolve(ASTResolver &resolver) const
IExpression * get() const
Expression operator-=(const Expression &other)
~Expression()=default
complex_t operator()() const
Expression operator-() const
Expression operator*=(const Expression &other)
Expression(const real_t &value)
Expression(const complex_t &value)
Expression operator/=(const Expression &other)
std::string to_string(const ASTResolver *resolver=nullptr) const
Expression operator+=(const Expression &other)
Expression(const std::shared_ptr< IExpression > &expression)
A group of expressions packed into a single expression.
Definition Expression.h:272
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
ExpressionPack(const Expression &A, const Expression &B)
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
ExpressionPack(const std::vector< Expression > &expressions)
Definition Expression.h:274
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
const std::vector< Expression > & expressions() const
Definition Expression.h:280
Fmod(const Expression &l, const Expression &r)
GreaterThanEqualTo(const Expression &l, const Expression &r)
GreaterThan(const Expression &l, const Expression &r)
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
Expression r() const
Definition Expression.h:307
Expression l() const
Definition Expression.h:306
IBinaryExpression(const Expression &pack)
Definition Expression.h:290
IBinaryExpression(const Expression &l, const Expression &r)
Definition Expression.h:289
complex_t operator()() const override=0
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Virtual base class for other expression tree components.
Definition Expression.h:116
virtual void resolve(ASTResolver &resolver) const =0
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
virtual complex_t operator()() const =0
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
virtual std::string to_string(const ASTResolver *resolver=nullptr) const =0
Called to convert the Expression tree into source code.
virtual ~IExpression()=default
virtual descructor
ISqrt(const Expression &other)
IUnaryExpression(const Expression &other)
Definition Expression.h:367
virtual Expression d() const =0
Expression arg() const
Definition Expression.h:371
complex_t operator()() const override=0
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
Imag(const Expression &other)
LGamma(const Expression &other)
LambdaExpression(const function_type &function)
Definition Expression.h:217
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
std::function< double(void)> m_function
Definition Expression.h:222
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
LessThanEqualTo(const Expression &l, const Expression &r)
LessThan(const Expression &l, const Expression &r)
Log(const Expression &other)
Norm(const Expression &other)
Or(const Expression &l, const Expression &r)
Free parameter for expression.
Definition Expression.h:181
bool isResolved() const
Definition Expression.h:193
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Definition Expression.h:189
double & defaultValue()
Definition Expression.h:192
const double & defaultValue() const
Definition Expression.h:191
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
Parameter(const std::string &name="", const double &defaultValue=0, const bool &resolved=false)
std::string name() const
Definition Expression.h:190
Pow(const Expression &l, const Expression &r)
Product(const Expression &l, const Expression &r)
Real(const Expression &other)
Sin(const Expression &other)
Sqrt(const Expression &other)
Sub(const Expression &l, const Expression &r)
Sum(const Expression &l, const Expression &r)
Tan(const Expression &other)
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
Ternary(const Expression &cond, const Expression &v1, const Expression &v2)
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Definition Expression.h:239
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
#define DECLARE_BINARY_OPERATOR(X)
Macro to declare a binary operator, see IBinaryExpression.
Definition Expression.h:80
#define DECLARE_UNARY_OPERATOR(X)
Macro to declare a unary operator, see IUnaryExpression.
Definition Expression.h:68
#define FATAL(X)
Used for printing fatal errors messages, and will always be printed and will terminate the process af...
Definition MsgService.h:87
Expression asin(const Expression &expression)
Expression abs(const Expression &expression)
Expression fpow(const Expression &expression, const int &n)
Expression acos(const Expression &expression)
Expression cos(const Expression &expression)
Expression sqrt(const Expression &expression)
Expression isqrt(const Expression &expression)
Expression tan(const Expression &expression)
Expression log(const Expression &expression)
Expression exp(const Expression &expression)
Expression pow(const Expression &expression, const Expression &co)
Expression sin(const Expression &expression)
Expression complex_sqrt(const Expression &expression)
Expression safe_sqrt(const Expression &expression)
Expression norm(const Expression &expression)
Expression conj(const Expression &expression)
Expression atan(const Expression &expression)
Expression atan2(const Expression &y, const Expression &x)
double real_t
Definition Types.h:6
Expression operator||(const Expression &A, const Expression &B)
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 operator<(const Expression &A, const Expression &B)
std::ostream & operator<<(std::ostream &os, const CompiledExpressionBase &expression)
Expression make_cse(const Expression &A, bool simplify=false)
Complex< real_t > operator+(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:50
bool is(const Expression &expression)
Definition Expression.h:494
Expression operator<=(const Expression &A, const Expression &B)
real_t real(const Complex< real_t > &arg)
Definition Complex.h:42
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:18
Expression operator==(const Expression &A, const Expression &B)
std::vector< DebugSymbol > DebugSymbols
Definition Expression.h:111
Expression operator&&(const Expression &A, const Expression &B)
Expression operator>=(const Expression &A, const Expression &B)
Complex< real_t > operator-(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:51
real_t imag(const Complex< real_t > &arg)
Definition Complex.h:44
T cast(const Expression &expression)
Definition Expression.h:497
Expression operator>(const Expression &A, const Expression &B)
std::pair< std::string, Expression > DebugSymbol
Definition Expression.h:110
Function(const std::string &name, const std::vector< Expression > &args)
std::string m_name
Definition Expression.h:266
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
std::string to_string(const ASTResolver *resolver=nullptr) 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...
Definition Expression.h:265
std::vector< Expression > m_args
Definition Expression.h:267
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Definition Expression.h:252
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
SubTree(const Expression &other)
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
void setKey(const size_t &new_key)
uint64_t key() const
Expression expression() const
Definition Expression.h:255
Expression m_expression
Definition Expression.h:256