1#ifndef AMPGEN_EXPRESSION_H
2#define AMPGEN_EXPRESSION_H
43#define ADD_DEBUG(X, Y) \
44 if(Y != 0) Y->push_back(DebugSymbol(std::string(#X), X));
48#define DEFINE_CAST(X) \
49 X::operator Expression() const { return Expression(std::make_shared<X>(*this)); }
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)); }
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) + ")"; }
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()); }
69#define DECLARE_UNARY_OPERATOR(X) \
70 class X : public IUnaryExpression { \
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; \
81#define DECLARE_BINARY_OPERATOR(X) \
82 class X : public IBinaryExpression { \
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; \
153 std::shared_ptr<IExpression> m_expression;
160 template <typename T1, typename T2, typename = std::enable_if_t<std::is_constructible<complex_t, T1, T2>::value>>
187 std::string
name()
const {
return m_name; }
194 double m_defaultValue;
260 Function(
const std::string &name,
const std::vector<Expression> &args);
279 const std::vector<Expression> &
expressions()
const {
return m_expressions; }
282 std::vector<Expression> m_expressions;
292 if(as_pack !=
nullptr) {
293 auto expr = as_pack->expressions();
294 if(expr.size() != 2)
FATAL(
"Wrong number of inputs");
298 FATAL(
"wrong number of inputs");
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()); }
ACos(const Expression &other)
(Internal) class to aide in the resolution of the dependencies of expression trees.
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).
complex_t operator()() const override
Evaluate the expression using the tree, will generally be very slow but ocassionally useful for debug...
Constant(const complex_t &value)
Constant(const T1 &real, const T2 &imag=0)
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...
void resolve(ASTResolver &resolver) const
IExpression * get() const
Expression operator-=(const Expression &other)
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.
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)
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
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,...
IBinaryExpression(const Expression &pack)
IBinaryExpression(const Expression &l, const Expression &r)
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.
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)
virtual Expression d() const =0
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)
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
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...
void resolve(ASTResolver &resolver) const override
Resolve the dependencies of a tree using an ASTResolver, which keeps track of parameters,...
Free parameter for expression.
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...
const double & defaultValue() const
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,...
#define DECLARE_BINARY_OPERATOR(X)
Macro to declare a binary operator, see IBinaryExpression.
#define DECLARE_UNARY_OPERATOR(X)
Macro to declare a unary operator, see IUnaryExpression.
#define FATAL(X)
Used for printing fatal errors messages, and will always be printed and will terminate the process af...
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)
Expression operator||(const Expression &A, const Expression &B)
Complex< real_t > operator/(const Complex< real_t > &lhs, const R2_t &rhs)
Complex< real_t > operator*(const Complex< real_t > &lhs, const R2_t &rhs)
std::complex< real_t > complex_t
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)
bool is(const Expression &expression)
Expression operator<=(const Expression &A, const Expression &B)
real_t real(const Complex< real_t > &arg)
std::string type_string()
Utility classes for compile-time metaprogramming, such as identifying the types of arguments for gene...
Expression operator==(const Expression &A, const Expression &B)
std::vector< DebugSymbol > DebugSymbols
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)
real_t imag(const Complex< real_t > &arg)
T cast(const Expression &expression)
Expression operator>(const Expression &A, const Expression &B)
std::pair< std::string, Expression > DebugSymbol
Function(const std::string &name, const std::vector< Expression > &args)
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...
std::vector< Expression > m_args
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.
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)
Expression expression() const