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;
122 FMATVEC_OPERATORRESULT2(SymbolicExpression,
double, SymbolicExpression)
126 FMATVEC_OPERATORRESULT2(IndependentVariable,
int, SymbolicExpression)
127 FMATVEC_OPERATORRESULT2(IndependentVariable,
double, SymbolicExpression)
128 FMATVEC_OPERATORRESULT2(IndependentVariable, SymbolicExpression, SymbolicExpression)
133 FMATVEC_EXPORT SymbolicExpression operator+(
double a, const SymbolicExpression &b);
134 FMATVEC_EXPORT SymbolicExpression operator-(
double a, const SymbolicExpression &b);
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+(
int a, const SymbolicExpression &b);
138 FMATVEC_EXPORT SymbolicExpression operator-(
int a, const SymbolicExpression &b);
139 FMATVEC_EXPORT SymbolicExpression operator*(
int a, const SymbolicExpression &b);
140 FMATVEC_EXPORT SymbolicExpression operator/(
int a, const SymbolicExpression &b);
144 FMATVEC_EXPORT SymbolicExpression
parDer(const SymbolicExpression &dep, const IndependentVariable &indep);
150 inline
double eval(const SymbolicExpression &x);
153 FMATVEC_EXPORT std::ostream& operator<<(std::ostream& s, const SymbolicExpression& se);
155 FMATVEC_EXPORT std::istream& operator>>(std::istream& s, SymbolicExpression &se);
157 FMATVEC_EXPORT std::istream& operator>>(std::istream& s, IndependentVariable &v);
159 FMATVEC_EXPORT SymbolicExpression
subst(const SymbolicExpression &se, const IndependentVariable& a, const SymbolicExpression &b);
162 FMATVEC_EXPORT SymbolicExpression pow(const SymbolicExpression &a, const SymbolicExpression &b);
163 FMATVEC_EXPORT SymbolicExpression log(const SymbolicExpression &a);
164 FMATVEC_EXPORT SymbolicExpression sqrt(const SymbolicExpression &a);
165 FMATVEC_EXPORT SymbolicExpression sin(const SymbolicExpression &a);
166 FMATVEC_EXPORT SymbolicExpression cos(const SymbolicExpression &a);
167 FMATVEC_EXPORT SymbolicExpression tan(const SymbolicExpression &a);
168 FMATVEC_EXPORT SymbolicExpression sinh(const SymbolicExpression &a);
169 FMATVEC_EXPORT SymbolicExpression cosh(const SymbolicExpression &a);
170 FMATVEC_EXPORT SymbolicExpression tanh(const SymbolicExpression &a);
171 FMATVEC_EXPORT SymbolicExpression asin(const SymbolicExpression &a);
172 FMATVEC_EXPORT SymbolicExpression acos(const SymbolicExpression &a);
173 FMATVEC_EXPORT SymbolicExpression atan(const SymbolicExpression &a);
174 FMATVEC_EXPORT SymbolicExpression asinh(const SymbolicExpression &a);
175 FMATVEC_EXPORT SymbolicExpression acosh(const SymbolicExpression &a);
176 FMATVEC_EXPORT SymbolicExpression atanh(const SymbolicExpression &a);
177 FMATVEC_EXPORT SymbolicExpression exp(const SymbolicExpression &a);
178 FMATVEC_EXPORT SymbolicExpression sign(const SymbolicExpression &a);
179 FMATVEC_EXPORT SymbolicExpression abs(const SymbolicExpression &a);
194 virtual double eval()
const=0;
196 virtual SymbolicExpression
parDer(
const IndependentVariable &x)
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>> {
229 friend SymbolicExpression;
232 static SymbolicExpression create(
const T& c_);
233 inline double eval()
const override;
234 SymbolicExpression
parDer(
const IndependentVariable &x)
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> {
272 friend SymbolicExpression;
273 friend IndependentVariable;
276 static IndependentVariable create(
const boost::uuids::uuid& uuid_=boost::uuids::random_generator()());
277 inline double eval()
const override;
278 SymbolicExpression
parDer(
const IndependentVariable &x)
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> {
316 friend SymbolicExpression;
317 friend SymbolicExpression
fmatvec::subst(
const SymbolicExpression &se,
const IndependentVariable& a,
const SymbolicExpression &b);
318 friend boost::spirit::qi::rule<boost::spirit::istream_iterator, SymbolicExpression()>&
319 fmatvec::getBoostSpiritQiRule<SymbolicExpression>();
320 friend boost::spirit::karma::rule<std::ostream_iterator<char>, 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;
328 SymbolicExpression
parDer(
const IndependentVariable &x)
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.");
435 double eval(
const SymbolicExpression &x) {
439 template<> boost::spirit::qi::rule<boost::spirit::istream_iterator, IndependentVariable()>& getBoostSpiritQiRule<IndependentVariable>();
440 template<> boost::spirit::qi::rule<boost::spirit::istream_iterator, SymbolicExpression()>& getBoostSpiritQiRule<SymbolicExpression>();
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>();
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:308
A vertex of the AST representing a independent variable.
Definition: ast.h:271
virtual bool isConstantInt() const
Rreturn true if this Vertex is a constant integer.
Definition: ast.h:220
const T & getValue() const
Get the constant value of the vertex.
Definition: ast.h:264
A vertex of the AST representing an operation.
Definition: ast.h:315
double eval() const override
Definition: ast.h:304
void setValue(double x_) const
Definition: ast.h:299
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)
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
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