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
40
43#define ADD_DEBUG(X, Y) \
44 if(Y != 0) Y->push_back(DebugSymbol(std::string(#X), X));
45
48#define DEFINE_CAST(X) \
49 X::operator Expression() const { return Expression(std::make_shared<X>(*this)); }
50
51#define DEFINE_BINARY_OPERATOR(X) \
52 X::X(const AmpGen::Expression &l, const AmpGen::Expression &r) : IBinaryExpression(l, r) {} \
53 X::X(const AmpGen::Expression &expr) : IBinaryExpression(expr) {} \
54 X::operator Expression() const { return Expression(std::make_shared<X>(*this)); }
55
56#define DEFINE_UNARY_OPERATOR(X, F) \
57 X::X(const AmpGen::Expression &expression) : IUnaryExpression(expression) {} \
58 X::operator Expression() const { return Expression(std::make_shared<X>(*this)); } \
59 complex_t X::operator()() const { return F(m_expression()); } \
60 std::string X::to_string(const ASTResolver *resolver) const { return std::string(#F) + "(" + m_expression.to_string(resolver) + ")"; }
61
62#define DEFINE_UNARY_OPERATOR_NO_RESOLVER(X, F) \
63 X::X(const AmpGen::Expression &expression) : IUnaryExpression(expression) {} \
64 X::operator Expression() const { return Expression(std::make_shared<X>(*this)); } \
65 complex_t X::operator()() const { return F(m_expression()); }
66
69#define DECLARE_UNARY_OPERATOR(X) \
70 class X : public IUnaryExpression { \
71 public: \
72 explicit X(const Expression &other); \
73 virtual std::string to_string(const ASTResolver *resolver = nullptr) const override; \
74 virtual Expression d() const override; \
75 operator Expression() const; \
76 virtual complex_t operator()() const override; \
77 }
78
81#define DECLARE_BINARY_OPERATOR(X) \
82 class X : public IBinaryExpression { \
83 public: \
84 X(const Expression &l, const Expression &r); \
85 X(const Expression &expr); \
86 virtual std::string to_string(const ASTResolver *resolver = nullptr) const override; \
87 operator Expression() const; \
88 virtual complex_t operator()() const override; \
89 }
90
91#include <algorithm>
92#include <complex>
93#include <iostream>
94#include <map>
95#include <memory>
96#include <sstream>
97#include <string>
98#include <utility>
99#include <vector>
100#include <functional>
101#include "AmpGen/MsgService.h"
102#include "AmpGen/MetaUtils.h"
103#include "AmpGen/Types.h"
104
105namespace AmpGen {
106 class ASTResolver;
107 class Expression;
108 class Variable;
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
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, typename T2, typename = std::enable_if_t<std::is_constructible<complex_t, T1, T2>::value>>
161 Constant(const T1 &real, const T2 &imag = 0) : m_value(real, imag) {}
162
163 Constant(const complex_t &value) : m_value(value) {}
164 std::string to_string(const ASTResolver *resolver = nullptr) const override;
165 void resolve(ASTResolver &resolver) const override;
166 operator Expression() const;
167 complex_t operator()() const override { return m_value; }
168
169 private:
170 complex_t m_value;
171 };
172
180 class Variable : public IExpression {
181 public:
182 Variable(const std::string &name = "", const double &defaultValue = 0, const bool &resolved = false);
183 std::string to_string(const ASTResolver *resolver = nullptr) const override;
184 void resolve(ASTResolver &resolver) const override;
185 virtual operator Expression() const;
186 complex_t operator()() const override { return complex_t(m_defaultValue, 0); }
187 std::string name() const { return m_name; }
188 const double &defaultValue() const { return m_defaultValue; }
189 double &defaultValue() { return m_defaultValue; }
190 bool isResolved() const { return m_resolved; }
191
192 private:
193 std::string m_name;
194 double m_defaultValue;
195 bool m_resolved;
196 };
197
199 public:
201 std::string to_string(const ASTResolver *resolver = nullptr) const override;
202 void resolve(ASTResolver &resolver) const override;
203 operator Expression() const;
204 complex_t operator()() const override;
205
206 private:
207 Variable m_real;
208 Variable m_imag;
209 };
210
214 public:
215 template <typename function_type> LambdaExpression(const function_type &function) : m_function(function), m_name(type_string<function_type>()) {}
216 std::string to_string(const ASTResolver *resolver = nullptr) const override;
217 void resolve(ASTResolver &resolver) const override;
218 operator Expression() const;
219 complex_t operator()() const override;
220 std::function<double(void)> m_function;
221 std::string m_name;
222 };
223
231 class Ternary : public IExpression {
232 public:
233 Ternary(const Expression &cond, const Expression &v1, const Expression &v2);
234 std::string to_string(const ASTResolver *resolver = nullptr) const override;
235 void resolve(ASTResolver &resolver) const override;
236 operator Expression() const;
237 complex_t operator()() const override { return std::real(m_cond()) ? m_v1() : m_v2(); }
238
239 private:
240 Expression m_cond;
241 Expression m_v1;
242 Expression m_v2;
243 };
244
246 struct SubTree : public IExpression {
247 SubTree(const Expression &other);
248 std::string to_string(const ASTResolver *resolver = nullptr) const override;
249 void resolve(ASTResolver &resolver) const override;
250 operator Expression() const;
251 complex_t operator()() const override { return m_expression(); }
252 uint64_t key() const;
253 void setKey(const size_t &new_key);
256 uint64_t m_key;
257 };
258
259 struct Function : public IExpression {
260 Function(const std::string &name, const std::vector<Expression> &args);
261 std::string to_string(const ASTResolver *resolver = nullptr) const override;
262 void resolve(ASTResolver &resolver) const override;
263 operator Expression() const;
264 complex_t operator()() const override { return 0; }
265 std::string m_name;
266 std::vector<Expression> m_args;
267 };
268
270
272 public:
273 explicit ExpressionPack(const std::vector<Expression> &expressions) : m_expressions(expressions) {}
275 std::string to_string(const ASTResolver *resolver = nullptr) const override;
276 void resolve(ASTResolver &resolver) const override;
277 complex_t operator()() const override;
278 operator Expression() const;
279 const std::vector<Expression> &expressions() const { return m_expressions; }
280
281 private:
282 std::vector<Expression> m_expressions;
283 };
284
288 public:
291 auto as_pack = static_cast<const ExpressionPack *>(pack.get());
292 if(as_pack != nullptr) {
293 auto expr = as_pack->expressions();
294 if(expr.size() != 2) FATAL("Wrong number of inputs");
295 lval = expr[0];
296 rval = expr[1];
297 } else {
298 FATAL("wrong number of inputs");
299 }
300 }
301 void resolve(ASTResolver &resolver) const override;
302 complex_t operator()() const override = 0;
303 Expression l() const { return lval; }
304 Expression r() const { return rval; }
305
306 protected:
309 };
310
314
318
322
326
330
334
338
342
346
350
354
359
364 public:
365 IUnaryExpression(const Expression &other) : m_expression(other) {};
366 void resolve(ASTResolver &resolver) const override;
367 complex_t operator()() const override = 0;
368 virtual Expression d() const = 0;
369 Expression arg() const { return m_expression; }
370
371 protected:
373 };
374
377
381
385
389
393
397
401
405
409
413
417
421
428
432
436
441
446
447 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator+(const Expression &A, const T &B) { return A + Constant(B); }
448 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator-(const Expression &A, const T &B) { return A - Constant(B); }
449 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator*(const Expression &A, const T &B) { return A * Constant(B); }
450 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator/(const Expression &A, const T &B) { return A / Constant(B); }
451
452 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator+(const T &A, const Expression &B) { return Constant(A) + B; }
453 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator-(const T &A, const Expression &B) { return Constant(A) - B; }
454 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator*(const T &A, const Expression &B) { return Constant(A) * B; }
455 template <class T, typename std::enable_if_t<hasConstructor<Constant, T>()>> Expression operator/(const T &A, const Expression &B) { return Constant(A) / B; }
456
460 Expression operator==(const Expression &A, const double &B);
461 Expression operator==(const double &A, const Expression &B);
462
463 std::ostream &operator<<(std::ostream &os, const Expression &expression);
464 namespace fcn {
465 Expression sqrt(const Expression &expression);
466 Expression safe_sqrt(const Expression &expression);
468 Expression isqrt(const Expression &expression);
469 Expression cos(const Expression &expression);
470 Expression sin(const Expression &expression);
471 Expression tan(const Expression &expression);
472 Expression abs(const Expression &expression);
473 Expression acos(const Expression &expression);
474 Expression asin(const Expression &expression);
475 Expression atan(const Expression &expression);
476 Expression pow(const Expression &expression, const Expression &co);
477 Expression fpow(const Expression &expression, const int &n);
478 Expression norm(const Expression &expression);
479 Expression conj(const Expression &expression);
480 Expression exp(const Expression &expression);
481 Expression log(const Expression &expression);
483 }
484
485 template <class T> bool is(const Expression &expression) { return dynamic_cast<const T *>(expression.get()) != nullptr; }
486 template <class T> T cast(const Expression &expression) { return *static_cast<const T *>(expression.get()); }
487
488 Expression make_cse(const Expression &A, bool simplify = false);
489
490} // namespace AmpGen
491
492#endif
ACos(const Expression &other)
(Internal) class to aide in the resolution of the dependencies of expression trees.
Definition ASTResolver.h:30
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)
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
ComplexVariable(const Variable &real, const Variable &imag)
std::string to_string(const ASTResolver *resolver=nullptr) const override
Called to convert the Expression tree into source code.
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:167
Constant(const complex_t &value)
Definition Expression.h:163
Constant(const T1 &real, const T2 &imag=0)
Definition Expression.h:161
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:271
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:273
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:279
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:304
Expression l() const
Definition Expression.h:303
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:365
virtual Expression d() const =0
Expression arg() const
Definition Expression.h:369
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:215
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:220
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)
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:237
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
Free parameter for expression.
Definition Expression.h:180
Variable(const std::string &name="", const double &defaultValue=0, const bool &resolved=false)
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Definition Expression.h:186
const double & defaultValue() const
Definition Expression.h:188
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,...
std::string name() const
Definition Expression.h:187
bool isResolved() const
Definition Expression.h:190
double & defaultValue()
Definition Expression.h:189
#define DECLARE_BINARY_OPERATOR(X)
Macro to declare a binary operator, see IBinaryExpression.
Definition Expression.h:81
#define DECLARE_UNARY_OPERATOR(X)
Macro to declare a unary operator, see IUnaryExpression.
Definition Expression.h:69
#define FATAL(X)
Used for printing fatal errors messages, and will always be printed and will terminate the process af...
Definition MsgService.h:92
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:52
Complex< real_t > operator*(const Complex< real_t > &lhs, const R2_t &rhs)
Definition Complex.h:49
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:43
bool is(const Expression &expression)
Definition Expression.h:485
Expression operator<=(const Expression &A, const Expression &B)
real_t real(const Complex< real_t > &arg)
Definition Complex.h:36
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Definition MetaUtils.h:17
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:46
real_t imag(const Complex< real_t > &arg)
Definition Complex.h:38
T cast(const Expression &expression)
Definition Expression.h:486
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:265
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:264
std::vector< Expression > m_args
Definition Expression.h:266
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Definition Expression.h:251
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:254
Expression m_expression
Definition Expression.h:255