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;
196 virtual double eval()
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>> {
235 inline double eval()
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> {
278 static IndependentVariable create(
const boost::uuids::uuid& uuid_=boost::uuids::random_generator()());
279 inline double eval()
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> {
321 fmatvec::getBoostSpiritQiRule<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;
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.");
432 static_cast<const AST::Symbol*>(get())->setValue(x);
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>();
void setValue(double x_) const
Definition: ast.h:301
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
const T & getValue() const
Get the constant value of the vertex.
Definition: ast.h:266
bool isConstantInt() const override
Rreturn true if this Vertex is a constant integer.
virtual bool isConstantInt() const
Rreturn true if this Vertex is a constant integer.
Definition: ast.h:222
A vertex of the AST representing a independent variable.
Definition: ast.h:273
unsigned long getVersion() const
Definition: ast.h:310
A vertex of the AST representing an operation.
Definition: ast.h:317
Namespace fmatvec.
Definition: _memory.cc:28
double eval() const override
Definition: ast.h:306
A abstract class for a Vertex of the AST (abstract syntax tree).
Definition: ast.h:189
friend FMATVEC_EXPORT double eval(const SymbolicExpression &x)
Definition: ast.h:436
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
ostream & operator<<(ostream &s, const SymbolicExpression &se)
Write a SymbolicExpression to a stream using serialization.
Definition: ast.cc:169
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