6 #ifndef SYMENGINE_VISITOR_H
7 #define SYMENGINE_VISITOR_H
11 #include <symengine/polys/uexprpoly.h>
12 #include <symengine/polys/msymenginepoly.h>
14 #include <symengine/complex_mpc.h>
16 #include <symengine/series_piranha.h>
17 #include <symengine/series_flint.h>
19 #include <symengine/series_piranha.h>
25 #include <symengine/matrix.h>
26 #include <symengine/ntheory_funcs.h>
27 #include <symengine/symengine_casts.h>
28 #include <symengine/tuple.h>
29 #include <symengine/matrix_expressions.h>
38 #define SYMENGINE_ENUM(TypeID, Class) virtual void visit(const Class &) = 0;
39 #include "symengine/type_codes.inc"
46 template <
class Derived,
class Base = Visitor>
51 #if (defined(__GNUC__) && __GNUC__ == 4 && __GNUC_MINOR__ < 8 \
52 || defined(__SUNPRO_CC))
56 template <
typename... Args,
59 BaseVisitor(Args &&...args) : Base(std::forward<Args>(args)...)
68 #define SYMENGINE_ENUM(TypeID, Class) \
69 virtual void visit(const Class &x) \
71 down_cast<Derived *>(this)->bvisit(x); \
73 #include "symengine/type_codes.inc"
102 void bvisit(
const Symbol &x)
118 void bvisit(
const Basic &x){};
120 bool apply(
const Basic &b)
124 preorder_traversal_stop(b, *
this);
129 bool has_symbol(
const Basic &b,
const Basic &x);
136 RCP<const Basic> coeff_;
139 CoeffVisitor(Ptr<const Basic> x, Ptr<const Basic> n) : x_(x), n_(n) {}
141 void bvisit(
const Add &x)
144 RCP<const Number> coef = zero;
145 for (
auto &p : x.get_dict()) {
146 p.first->accept(*
this);
147 if (
neq(*coeff_, *zero)) {
151 if (
eq(*zero, *n_)) {
152 iaddnum(outArg(coef), x.
get_coef());
157 void bvisit(
const Mul &x)
159 for (
auto &p : x.get_dict()) {
160 if (
eq(*p.first, *x_) and
eq(*p.second, *n_)) {
167 if (
eq(*zero, *n_) and not has_symbol(x, *x_)) {
174 void bvisit(
const Pow &x)
185 void bvisit(
const Symbol &x)
187 if (
eq(x, *x_) and
eq(*one, *n_)) {
189 }
else if (
neq(x, *x_) and
eq(*zero, *n_)) {
198 if (
eq(x, *x_) and
eq(*one, *n_)) {
200 }
else if (
neq(x, *x_) and
eq(*zero, *n_)) {
207 void bvisit(
const Basic &x)
209 if (
neq(*zero, *n_)) {
213 if (has_symbol(x, *x_)) {
220 RCP<const Basic> apply(
const Basic &b)
228 RCP<const Basic> coeff(
const Basic &b,
const Basic &x,
const Basic &n);
239 RCP<const Basic> result_;
244 virtual RCP<const Basic> apply(
const RCP<const Basic> &x);
246 void bvisit(
const Basic &x);
247 void bvisit(
const Add &x);
248 void bvisit(
const Mul &x);
249 void bvisit(
const Pow &x);
256 auto newarg1 = apply(farg1), newarg2 = apply(farg2);
257 if (farg1 != newarg1 or farg2 != newarg2) {
258 result_ = x.
create(newarg1, newarg2);
260 result_ = x.rcp_from_this();
268 template <
typename Derived,
typename First,
typename... Rest>
274 template <
typename Derived,
typename First>
279 template <
typename... Args>
286 template <
typename T,
288 void bvisit(
const T &x)
290 s.
insert(x.rcp_from_this());
291 visited.
insert(x.rcp_from_this());
292 bvisit((
const Basic &)x);
295 void bvisit(
const Basic &x)
297 for (
const auto &p : x.
get_args()) {
298 auto iter = visited.
insert(p->rcp_from_this());
312 template <
typename... Args>
316 return visitor.apply(b);
327 void apply(
const Basic &b);
328 void bvisit(
const Mul &x);
329 void bvisit(
const Add &x);
330 void bvisit(
const Pow &x);
331 void bvisit(
const Number &x);
333 void bvisit(
const Symbol &x);
335 void bvisit(
const Basic &x);
The base class for representing addition in symbolic expressions.
static RCP< const Basic > from_dict(const RCP< const Number > &coef, umap_basic_num &&d)
Create an appropriate instance from dictionary quickly.
static void coef_dict_add_term(const Ptr< RCP< const Number >> &coef, umap_basic_num &d, const RCP< const Number > &c, const RCP< const Basic > &term)
Updates the numerical coefficient and the dictionary.
const RCP< const Number > & get_coef() const
The lowest unit of symbolic representation.
virtual vec_basic get_args() const =0
Returns the list of arguments.
ComplexBase Class for deriving all complex classes.
RCP< T > rcp_from_this()
Get RCP<T> pointer to self (it will cast the pointer to T)
static RCP< const Basic > from_dict(const RCP< const Number > &coef, map_basic_basic &&d)
Create a Mul from a dict.
RCP< const Basic > get_exp() const
RCP< const Basic > get_base() const
virtual RCP< const Basic > create(const RCP< const Basic > &a, const RCP< const Basic > &b) const =0
Method to construct classes with canonicalization.
RCP< const Basic > get_arg1() const
RCP< const Basic > get_arg2() const
Main namespace for SymEngine package.
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b