1#ifndef AMPGEN_EXPRESSION_H
2#define AMPGEN_EXPRESSION_H
41#define ADD_DEBUG( X, Y ) \
42 if ( Y != 0 ) Y->push_back( DebugSymbol( std::string( #X ), X ) ); \
46#define DEFINE_CAST( X ) \
47 X::operator Expression() const \
48{ return Expression( std::make_shared<X>( *this ) ); }
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) ) ; }
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)+")";}
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() ); }
68#define DECLARE_UNARY_OPERATOR( X ) \
69 class X : public IUnaryExpression { \
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; \
80#define DECLARE_BINARY_OPERATOR( X ) \
81 class X : public IBinaryExpression { \
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; \
140 Expression(
const std::shared_ptr<IExpression>& expression ) ;
153 std::shared_ptr<IExpression> m_expression;
160 template <
typename T1,
162 typename = std::enable_if_t< std::is_constructible< complex_t, T1, T2 >::value > >
185 const bool& resolved =
false);
190 std::string
name()
const {
return m_name; }
196 double m_defaultValue;
216 template <
typename function_type>
261 Function(
const std::string& name,
const std::vector<Expression>& args ) ;
280 const std::vector<Expression>&
expressions()
const {
return m_expressions ; }
282 std::vector<Expression> m_expressions;
293 if( as_pack !=
nullptr )
295 auto expr = as_pack->expressions();
296 if( expr.size() != 2 )
FATAL(
"Wrong number of inputs");
301 FATAL(
"wrong number of inputs" );
448 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
450 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
452 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
454 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
457 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
459 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
461 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
463 template < class T, typename std::enable_if_t< hasConstructor<Constant, T>() > >
495 return dynamic_cast< const T*
>( expression.
get() ) !=
nullptr;
498 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)
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)
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)
Free parameter for expression.
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
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)
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,...
#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