1 #ifndef _FMATVEC_AST_H_ 2 #define _FMATVEC_AST_H_ 7 #include <boost/uuid/uuid.hpp> 8 #include <boost/uuid/uuid_generators.hpp> 9 #include <fmatvec/stream.h> 15 class IndependentVariable;
25 template<
class... Arg>
class Eval;
34 template<
class... Arg>
friend class Eval;
35 friend class EvalHelper;
45 static const struct ConstructSymbol{} constructSymbol;
66 static void garbageCollect();
93 #if !defined(NDEBUG) && !defined(SWIG) 94 static signed long evalOperationsCount;
197 static constexpr
size_t N { 3 };
204 std::function<void(
double*,
const std::array<double*,N>&)> func;
207 std::array<double ,N> argsValue;
208 std::array<double*,N> argsPtr;
221 inline virtual bool isConstantInt()
const;
229 virtual std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
230 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const=0;
240 virtual bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const=0;
251 class FMATVEC_EXPORT
Constant :
public Vertex,
public std::enable_shared_from_this<Constant<T>> {
257 inline bool isConstantInt()
const override;
259 inline const T& getValue()
const;
260 std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
261 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const override;
269 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
271 static std::map<CacheKey, std::weak_ptr<const Constant>> cache;
292 class FMATVEC_EXPORT
Symbol :
public Vertex,
public std::enable_shared_from_this<Symbol> {
297 static IndependentVariable create(
const boost::uuids::uuid& uuid_=boost::uuids::random_generator()());
301 inline void setValue(
double x_)
const;
303 std::string getUUIDStr()
const;
305 std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
306 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const override;
312 Symbol(
const boost::uuids::uuid& uuid_);
313 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
314 mutable double x = 0.0;
315 boost::uuids::uuid uuid;
316 using CacheKey = boost::uuids::uuid;
317 static std::map<CacheKey, std::weak_ptr<const Symbol>> cache;
327 class FMATVEC_EXPORT
Operation :
public Vertex,
public std::enable_shared_from_this<Operation> {
332 fmatvec::getBoostSpiritQiRule<SymbolicExpression>();
334 fmatvec::getBoostSpiritKarmaRule<SymbolicExpression>();
338 enum Operator { Plus, Minus, Mult, Div, Pow, Log, Sqrt, Neg, Sin, Cos, Tan, Sinh, Cosh, Tanh, ASin, ACos, ATan, ATan2, ASinh, ACosh, ATanh, Exp, Sign, Heaviside, Abs, Min, Max, Condition };
339 static SymbolicExpression create(Operator op_,
const std::vector<SymbolicExpression> &child_);
342 Operator getOp()
const {
return op; }
343 const std::vector<SymbolicExpression>& getChilds()
const {
return child; }
344 std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
345 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const override;
351 Operation(Operator op_,
const std::vector<SymbolicExpression> &child_);
352 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
354 std::vector<SymbolicExpression> child;
355 using CacheKey = std::pair<Operator, std::vector<std::weak_ptr<const Vertex>>>;
357 bool operator()(
const CacheKey& l,
const CacheKey& r)
const;
359 std::owner_less<std::weak_ptr<const Vertex>> ol;
361 static std::map<CacheKey, std::weak_ptr<const Operation>,
CacheKeyComp> cache;
364 std::string funcName;
366 std::function<void(
double*,
const std::array<double*,ByteCode::N>&)> func;
369 static const std::map<Operator, OpMap> opMap;
376 static_cast<const AST::Symbol*>(get())->setValue(x);
383 template<> boost::spirit::karma::rule<std::ostream_iterator<char>,
IndependentVariable()>& getBoostSpiritKarmaRule<IndependentVariable>();
384 template<> boost::spirit::karma::rule<std::ostream_iterator<char>,
SymbolicExpression()>& getBoostSpiritKarmaRule<SymbolicExpression>();
void setValue(double x_) const
Definition: ast.h:320
IndependentVariable & operator^=(double x)
Set the double value of the independent value.
Definition: ast.h:375
A vertex of the AST representing a constant (int or double)
Definition: ast.h:21
const T & getValue() const
Get the constant value of the vertex.
Definition: ast.h:285
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:243
A vertex of the AST representing a independent variable.
Definition: ast.h:292
A vertex of the AST representing an operation.
Definition: ast.h:327
Namespace fmatvec.
Definition: _memory.cc:28
A abstract class for a Vertex of the AST (abstract syntax tree).
Definition: ast.h:214
Operator
Defined operations.
Definition: ast.h:338
ostream & operator<<(ostream &s, const SymbolicExpression &se)
Write a SymbolicExpression to a stream using serialization.
Definition: ast.cc:189
SymbolicExpression parDer(const SymbolicExpression &dep, const IndependentVariable &indep)
Definition: ast.cc:286