1 #ifndef _FMATVEC_AST_H_ 2 #define _FMATVEC_AST_H_ 6 #include <boost/uuid/uuid.hpp> 7 #include <boost/uuid/uuid_generators.hpp> 8 #include <fmatvec/stream.h> 14 class IndependentVariable;
24 template<
class... Arg>
class Eval;
33 template<
class... Arg>
friend class Eval;
34 friend class EvalHelper;
44 static const struct ConstructSymbol{} constructSymbol;
65 static void garbageCollect();
92 #if !defined(NDEBUG) && !defined(SWIG) 93 static signed long evalOperationsCount;
191 static constexpr
size_t N { 2 };
198 std::function<void(
double*,
const std::array<double*,N>&)> func;
201 std::array<double ,N> argsValue;
202 std::array<double*,N> argsPtr;
215 inline virtual bool isConstantInt()
const;
223 virtual std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
224 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const=0;
234 virtual bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const=0;
245 class FMATVEC_EXPORT
Constant :
public Vertex,
public std::enable_shared_from_this<Constant<T>> {
251 inline bool isConstantInt()
const override;
253 inline const T& getValue()
const;
254 std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
255 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const override;
263 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
265 static std::map<CacheKey, std::weak_ptr<const Constant>> cache;
286 class FMATVEC_EXPORT
Symbol :
public Vertex,
public std::enable_shared_from_this<Symbol> {
291 static IndependentVariable create(
const boost::uuids::uuid& uuid_=boost::uuids::random_generator()());
295 inline void setValue(
double x_)
const;
297 std::string getUUIDStr()
const;
299 std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
300 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const override;
306 Symbol(
const boost::uuids::uuid& uuid_);
307 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
308 mutable double x = 0.0;
309 boost::uuids::uuid uuid;
310 typedef boost::uuids::uuid CacheKey;
311 static std::map<CacheKey, std::weak_ptr<const Symbol>> cache;
321 class FMATVEC_EXPORT
Operation :
public Vertex,
public std::enable_shared_from_this<Operation> {
326 fmatvec::getBoostSpiritQiRule<SymbolicExpression>();
328 fmatvec::getBoostSpiritKarmaRule<SymbolicExpression>();
332 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 };
333 static SymbolicExpression create(Operator op_,
const std::vector<SymbolicExpression> &child_);
336 Operator getOp()
const {
return op; }
337 const std::vector<SymbolicExpression>& getChilds()
const {
return child; }
338 std::vector<ByteCode>::iterator dumpByteCode(std::vector<ByteCode> &byteCode,
339 std::map<
const Vertex*, std::vector<AST::ByteCode>::iterator> &existingVertex)
const override;
345 Operation(Operator op_,
const std::vector<SymbolicExpression> &child_);
346 bool equal(
const SymbolicExpression &b, std::map<IndependentVariable, SymbolicExpression> &m)
const override;
348 std::vector<SymbolicExpression> child;
349 typedef std::pair<Operator, std::vector<std::weak_ptr<const Vertex>>> CacheKey;
351 bool operator()(
const CacheKey& l,
const CacheKey& r)
const;
353 std::owner_less<std::weak_ptr<const Vertex>> ol;
355 static std::map<CacheKey, std::weak_ptr<const Operation>,
CacheKeyComp> cache;
358 std::string funcName;
360 std::function<void(
double*,
const std::array<double*,ByteCode::N>&)> func;
363 static const std::map<Operator, OpMap> opMap;
370 static_cast<const AST::Symbol*>(get())->setValue(x);
377 template<> boost::spirit::karma::rule<std::ostream_iterator<char>,
IndependentVariable()>& getBoostSpiritKarmaRule<IndependentVariable>();
378 template<> boost::spirit::karma::rule<std::ostream_iterator<char>,
SymbolicExpression()>& getBoostSpiritKarmaRule<SymbolicExpression>();
void setValue(double x_) const
Definition: ast.h:314
IndependentVariable & operator^=(double x)
Set the double value of the independent value.
Definition: ast.h:369
A vertex of the AST representing a constant (int or double)
Definition: ast.h:20
const T & getValue() const
Get the constant value of the vertex.
Definition: ast.h:279
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:237
A vertex of the AST representing a independent variable.
Definition: ast.h:286
A vertex of the AST representing an operation.
Definition: ast.h:321
Namespace fmatvec.
Definition: _memory.cc:28
A abstract class for a Vertex of the AST (abstract syntax tree).
Definition: ast.h:208
Operator
Defined operations.
Definition: ast.h:332
ostream & operator<<(ostream &s, const SymbolicExpression &se)
Write a SymbolicExpression to a stream using serialization.
Definition: ast.cc:167
SymbolicExpression parDer(const SymbolicExpression &dep, const IndependentVariable &indep)
Definition: ast.cc:264