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;
194 virtual double eval()
const=0;
198 inline virtual bool isConstantInt()
const;
206 const std::map<std::weak_ptr<const Symbol>,
unsigned long, std::owner_less<std::weak_ptr<const Symbol>>>& getDependsOn()
const;
214 virtual bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const=0;
217 mutable std::map<std::weak_ptr<const Symbol>,
unsigned long, std::owner_less<std::weak_ptr<const Symbol>>> dependsOn;
228 class FMATVEC_EXPORT
Constant :
public Vertex,
public std::enable_shared_from_this<Constant<T>> {
233 inline double eval()
const override;
235 inline bool isConstantInt()
const override;
237 inline const T& getValue()
const;
243 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
245 static std::map<CacheKey, std::weak_ptr<const Constant>> cache;
271 class FMATVEC_EXPORT
Symbol :
public Vertex,
public std::enable_shared_from_this<Symbol> {
276 static IndependentVariable create(
const boost::uuids::uuid& uuid_=boost::uuids::random_generator()());
277 inline double eval()
const override;
281 inline void setValue(
double x_)
const;
284 inline unsigned long getVersion()
const;
286 std::string getUUIDStr()
const;
290 Symbol(
const boost::uuids::uuid& uuid_);
291 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
292 mutable double x = 0.0;
293 mutable unsigned long version;
294 boost::uuids::uuid uuid;
295 typedef boost::uuids::uuid CacheKey;
296 static std::map<CacheKey, std::weak_ptr<const Symbol>> cache;
315 class FMATVEC_EXPORT
Operation :
public Vertex,
public std::enable_shared_from_this<Operation> {
319 fmatvec::getBoostSpiritQiRule<SymbolicExpression>();
321 fmatvec::getBoostSpiritKarmaRule<SymbolicExpression>();
325 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 };
326 static SymbolicExpression create(Operator op_,
const std::vector<SymbolicExpression> &child_);
327 inline double eval()
const override;
330 Operator getOp()
const {
return op; }
331 const std::vector<SymbolicExpression>& getChilds()
const {
return child; }
335 Operation(Operator op_,
const std::vector<SymbolicExpression> &child_);
336 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
338 std::vector<SymbolicExpression> child;
339 typedef std::pair<Operator, std::vector<std::weak_ptr<const Vertex>>> CacheKey;
341 bool operator()(
const CacheKey& l,
const CacheKey& r)
const;
343 std::owner_less<std::weak_ptr<const Vertex>> ol;
345 static std::map<CacheKey, std::weak_ptr<const Operation>,
CacheKeyComp> cache;
346 mutable double cacheValue;
347 static const std::map<Operator, std::string> opMap;
351 if(!dependsOn.empty()) {
353 bool useCacheValue=
true;
354 for(
auto &d : dependsOn) {
355 auto dFirst=d.first.lock();
356 if(dFirst->getVersion() > d.second)
358 d.second=dFirst->getVersion();
363 #if !defined(NDEBUG) && !defined(SWIG) 364 SymbolicExpression::evalOperationsCount++;
369 #define a child[0]->eval() // value of first argument 370 #define b child[1]->eval() // value of second argument 373 return cacheValue = a + b;
375 return cacheValue = a - b;
377 return cacheValue = a * b;
379 return cacheValue = a / b;
382 return cacheValue = std::pow(a,
static_cast<const Constant<int>*
>(child[1].get())->getValue());
384 return cacheValue = std::pow(a, b);
386 return cacheValue = std::log(a);
388 return cacheValue = std::sqrt(a);
390 return cacheValue = - a;
392 return cacheValue = std::sin(a);
394 return cacheValue = std::cos(a);
396 return cacheValue = std::tan(a);
398 return cacheValue = std::sinh(a);
400 return cacheValue = std::cosh(a);
402 return cacheValue = std::tanh(a);
404 return cacheValue = std::asin(a);
406 return cacheValue = std::acos(a);
408 return cacheValue = std::atan(a);
410 return cacheValue = std::asinh(a);
412 return cacheValue = std::acosh(a);
414 return cacheValue = std::atanh(a);
416 return cacheValue = std::exp(a);
418 return cacheValue = boost::math::sign(a);
420 return cacheValue = std::abs(a);
424 throw std::runtime_error(
"Unknown operation.");
431 static_cast<const AST::Symbol*>(get())->setValue(x);
442 template<> boost::spirit::karma::rule<std::ostream_iterator<char>, IndependentVariable()>& getBoostSpiritKarmaRule<IndependentVariable>();
443 template<> boost::spirit::karma::rule<std::ostream_iterator<char>, SymbolicExpression()>& getBoostSpiritKarmaRule<SymbolicExpression>();
void setValue(double x_) const
Definition: ast.h:299
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:264
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:220
A vertex of the AST representing a independent variable.
Definition: ast.h:271
unsigned long getVersion() const
Definition: ast.h:308
A vertex of the AST representing an operation.
Definition: ast.h:315
Namespace fmatvec.
Definition: _memory.cc:28
double eval() const override
Definition: ast.h:304
A abstract class for a Vertex of the AST (abstract syntax tree).
Definition: ast.h:187
IndependentVariable & operator&=(double x)
Set the double value of the independent value.
Definition: ast.h:430
friend FMATVEC_EXPORT double eval(const SymbolicExpression &x)
Definition: ast.h:435
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:325
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:259
double eval(const SymbolicExpression &x)
Definition: ast.h:435
SymbolicExpression parDer(const SymbolicExpression &dep, const IndependentVariable &indep)
Definition: ast.cc:282
double eval() const override
Definition: ast.h:350