1 #ifndef _FMATVEC_AST_H_
2 #define _FMATVEC_AST_H_
7 #include <boost/uuid/uuid.hpp>
10 #define BOOST_PENDING_INTEGER_LOG2_HPP
11 #include <boost/integer/integer_log2.hpp>
13 #include <boost/uuid/uuid_generators.hpp>
14 #include <boost/math/special_functions/sign.hpp>
15 #include <fmatvec/types.h>
16 #include <fmatvec/stream.h>
22 class IndependentVariable;
46 static const struct ConstructSymbol{} constructSymbol;
51 using std::shared_ptr<const AST::Vertex>::get;
52 using std::shared_ptr<const AST::Vertex>::operator*;
53 using std::shared_ptr<const AST::Vertex>::operator->;
67 static void garbageCollect();
94 #if !defined(NDEBUG) && !defined(SWIG)
95 static unsigned long evalOperationsCount;
124 FMATVEC_OPERATORRESULT2(SymbolicExpression,
double, SymbolicExpression)
128 FMATVEC_OPERATORRESULT2(IndependentVariable,
int, SymbolicExpression)
129 FMATVEC_OPERATORRESULT2(IndependentVariable,
double, SymbolicExpression)
130 FMATVEC_OPERATORRESULT2(IndependentVariable, SymbolicExpression, SymbolicExpression)
135 FMATVEC_EXPORT SymbolicExpression operator+(
double a, const SymbolicExpression &b);
136 FMATVEC_EXPORT SymbolicExpression operator-(
double a, const SymbolicExpression &b);
137 FMATVEC_EXPORT SymbolicExpression operator*(
double a, const SymbolicExpression &b);
138 FMATVEC_EXPORT SymbolicExpression operator/(
double a, const SymbolicExpression &b);
139 FMATVEC_EXPORT SymbolicExpression operator+(
int a, const SymbolicExpression &b);
140 FMATVEC_EXPORT SymbolicExpression operator-(
int a, const SymbolicExpression &b);
141 FMATVEC_EXPORT SymbolicExpression operator*(
int a, const SymbolicExpression &b);
142 FMATVEC_EXPORT SymbolicExpression operator/(
int a, const SymbolicExpression &b);
146 FMATVEC_EXPORT SymbolicExpression
parDer(const SymbolicExpression &dep, const IndependentVariable &indep);
152 inline
double eval(const SymbolicExpression &x);
155 FMATVEC_EXPORT std::ostream& operator<<(std::ostream& s, const SymbolicExpression& se);
157 FMATVEC_EXPORT std::istream& operator>>(std::istream& s, SymbolicExpression &se);
159 FMATVEC_EXPORT std::istream& operator>>(std::istream& s, IndependentVariable &v);
161 FMATVEC_EXPORT SymbolicExpression
subst(const SymbolicExpression &se, const IndependentVariable& a, const SymbolicExpression &b);
164 FMATVEC_EXPORT SymbolicExpression pow(const SymbolicExpression &a, const SymbolicExpression &b);
165 FMATVEC_EXPORT SymbolicExpression log(const SymbolicExpression &a);
166 FMATVEC_EXPORT SymbolicExpression sqrt(const SymbolicExpression &a);
167 FMATVEC_EXPORT SymbolicExpression sin(const SymbolicExpression &a);
168 FMATVEC_EXPORT SymbolicExpression cos(const SymbolicExpression &a);
169 FMATVEC_EXPORT SymbolicExpression tan(const SymbolicExpression &a);
170 FMATVEC_EXPORT SymbolicExpression sinh(const SymbolicExpression &a);
171 FMATVEC_EXPORT SymbolicExpression cosh(const SymbolicExpression &a);
172 FMATVEC_EXPORT SymbolicExpression tanh(const SymbolicExpression &a);
173 FMATVEC_EXPORT SymbolicExpression asin(const SymbolicExpression &a);
174 FMATVEC_EXPORT SymbolicExpression acos(const SymbolicExpression &a);
175 FMATVEC_EXPORT SymbolicExpression atan(const SymbolicExpression &a);
176 FMATVEC_EXPORT SymbolicExpression asinh(const SymbolicExpression &a);
177 FMATVEC_EXPORT SymbolicExpression acosh(const SymbolicExpression &a);
178 FMATVEC_EXPORT SymbolicExpression atanh(const SymbolicExpression &a);
179 FMATVEC_EXPORT SymbolicExpression exp(const SymbolicExpression &a);
180 FMATVEC_EXPORT SymbolicExpression sign(const SymbolicExpression &a);
181 FMATVEC_EXPORT SymbolicExpression abs(const SymbolicExpression &a);
196 virtual double eval()
const=0;
198 virtual SymbolicExpression
parDer(
const IndependentVariable &x)
const=0;
200 inline virtual bool isConstantInt()
const;
208 const std::map<std::shared_ptr<const Symbol>,
unsigned long>& getDependsOn()
const;
216 virtual bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const=0;
219 mutable std::map<std::shared_ptr<const Symbol>,
unsigned long> dependsOn;
230 class FMATVEC_EXPORT
Constant :
public Vertex,
public std::enable_shared_from_this<Constant<T>> {
231 friend SymbolicExpression;
234 static SymbolicExpression create(
const T& c_);
235 inline double eval()
const override;
236 SymbolicExpression
parDer(
const IndependentVariable &x)
const override;
237 inline bool isConstantInt()
const override;
239 inline const T& getValue()
const;
245 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
247 static std::map<CacheKey, std::weak_ptr<const Constant>> cache;
273 class FMATVEC_EXPORT
Symbol :
public Vertex,
public std::enable_shared_from_this<Symbol> {
274 friend SymbolicExpression;
275 friend IndependentVariable;
278 static IndependentVariable create(
const boost::uuids::uuid& uuid_=boost::uuids::random_generator()());
279 inline double eval()
const override;
280 SymbolicExpression
parDer(
const IndependentVariable &x)
const override;
283 inline void setValue(
double x_)
const;
286 inline unsigned long getVersion()
const;
288 std::string getUUIDStr()
const;
292 Symbol(
const boost::uuids::uuid& uuid_);
293 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
294 mutable double x = 0.0;
295 mutable unsigned long version;
296 boost::uuids::uuid uuid;
297 typedef boost::uuids::uuid CacheKey;
298 static std::map<CacheKey, std::weak_ptr<const Symbol>> cache;
317 class FMATVEC_EXPORT
Operation :
public Vertex,
public std::enable_shared_from_this<Operation> {
318 friend SymbolicExpression;
319 friend SymbolicExpression
fmatvec::subst(
const SymbolicExpression &se,
const IndependentVariable& a,
const SymbolicExpression &b);
320 friend boost::spirit::qi::rule<boost::spirit::istream_iterator, SymbolicExpression()>&
321 fmatvec::getBoostSpiritQiRule<SymbolicExpression>();
322 friend boost::spirit::karma::rule<std::ostream_iterator<char>, SymbolicExpression()>&
323 fmatvec::getBoostSpiritKarmaRule<SymbolicExpression>();
327 enum Operator { Plus, Minus, Mult, Div, Pow, Log, Sqrt, Neg, Sin, Cos, Tan, Sinh, Cosh, Tanh, ASin, ACos, ATan, ASinh, ACosh, ATanh, Exp, Sign, Abs };
328 static SymbolicExpression create(Operator op_,
const std::vector<SymbolicExpression> &child_);
329 inline double eval()
const override;
330 SymbolicExpression
parDer(
const IndependentVariable &x)
const override;
332 Operator getOp()
const {
return op; }
333 const std::vector<SymbolicExpression>& getChilds()
const {
return child; }
337 Operation(Operator op_,
const std::vector<SymbolicExpression> &child_);
338 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
340 std::vector<SymbolicExpression> child;
341 typedef std::pair<Operator, std::vector<std::weak_ptr<const Vertex>>> CacheKey;
343 bool operator()(
const CacheKey& l,
const CacheKey& r)
const;
345 std::owner_less<std::weak_ptr<const Vertex>> ol;
347 static std::map<CacheKey, std::weak_ptr<const Operation>,
CacheKeyComp> cache;
348 mutable double cacheValue;
349 static const std::map<Operator, std::string> opMap;
353 if(!dependsOn.empty()) {
355 bool useCacheValue=
true;
356 for(
auto &d : dependsOn) {
357 if(d.first->getVersion() > d.second)
359 d.second=d.first->getVersion();
364 #if !defined(NDEBUG) && !defined(SWIG)
365 SymbolicExpression::evalOperationsCount++;
370 #define a child[0]->eval() // value of first argument
371 #define b child[1]->eval() // value of second argument
374 return cacheValue = a + b;
376 return cacheValue = a - b;
378 return cacheValue = a * b;
380 return cacheValue = a / b;
383 return cacheValue = std::pow(a,
static_cast<const Constant<int>*
>(child[1].
get())->getValue());
385 return cacheValue = std::pow(a, b);
387 return cacheValue = std::log(a);
389 return cacheValue = std::sqrt(a);
391 return cacheValue = - a;
393 return cacheValue = std::sin(a);
395 return cacheValue = std::cos(a);
397 return cacheValue = std::tan(a);
399 return cacheValue = std::sinh(a);
401 return cacheValue = std::cosh(a);
403 return cacheValue = std::tanh(a);
405 return cacheValue = std::asin(a);
407 return cacheValue = std::acos(a);
409 return cacheValue = std::atan(a);
411 return cacheValue = std::asinh(a);
413 return cacheValue = std::acosh(a);
415 return cacheValue = std::atanh(a);
417 return cacheValue = std::exp(a);
419 return cacheValue = boost::math::sign(a);
421 return cacheValue = std::abs(a);
425 throw std::runtime_error(
"Unknown operation.");
436 double eval(
const SymbolicExpression &x) {
440 template<> boost::spirit::qi::rule<boost::spirit::istream_iterator, IndependentVariable()>& getBoostSpiritQiRule<IndependentVariable>();
441 template<> boost::spirit::qi::rule<boost::spirit::istream_iterator, SymbolicExpression()>& getBoostSpiritQiRule<SymbolicExpression>();
443 template<> boost::spirit::karma::rule<std::ostream_iterator<char>, IndependentVariable()>& getBoostSpiritKarmaRule<IndependentVariable>();
444 template<> boost::spirit::karma::rule<std::ostream_iterator<char>, SymbolicExpression()>& getBoostSpiritKarmaRule<SymbolicExpression>();
IndependentVariable & operator^=(double x)
Set the double value of the independent value.
Definition: ast.h:431
A vertex of the AST representing a constant (int or double)
Definition: ast.h:28
bool isConstantInt() const override
Rreturn true if this Vertex is a constant integer.
unsigned long getVersion() const
Definition: ast.h:310
A vertex of the AST representing a independent variable.
Definition: ast.h:273
virtual bool isConstantInt() const
Rreturn true if this Vertex is a constant integer.
Definition: ast.h:222
const T & getValue() const
Get the constant value of the vertex.
Definition: ast.h:266
A vertex of the AST representing an operation.
Definition: ast.h:317
double eval() const override
Definition: ast.h:306
void setValue(double x_) const
Definition: ast.h:301
A abstract class for a Vertex of the AST (abstract syntax tree).
Definition: ast.h:189
friend FMATVEC_EXPORT double eval(const SymbolicExpression &x)
SymbolicExpression subst(const SymbolicExpression &se, const IndependentVariable &a, const SymbolicExpression &b)
Substitutes in se the independent variable a with the expression b and returns the new expression...
Definition: ast.cc:234
Operator
Defined operations.
Definition: ast.h:327
double eval() const override
Definition: ast.h:261
double eval(const SymbolicExpression &x)
Definition: ast.h:436
SymbolicExpression parDer(const SymbolicExpression &dep, const IndependentVariable &indep)
Definition: ast.cc:282
double eval() const override
Definition: ast.h:352