1#ifndef SYMENGINE_SUBS_H
2#define SYMENGINE_SUBS_H
11RCP<const Basic>
xreplace(
const RCP<const Basic> &x,
12 const map_basic_basic &subs_dict,
bool cache =
true);
15RCP<const Basic> subs(
const RCP<const Basic> &x,
16 const map_basic_basic &subs_dict,
bool cache =
true);
19RCP<const Basic>
msubs(
const RCP<const Basic> &x,
20 const map_basic_basic &subs_dict,
bool cache =
true);
22RCP<const Basic>
ssubs(
const RCP<const Basic> &x,
23 const map_basic_basic &subs_dict,
bool cache =
true);
29 RCP<const Basic> result_;
36 : subs_dict_(subs_dict), cache(cache)
43 void bvisit(
const Basic &x)
48 void bvisit(
const Add &x)
51 RCP<const Number> coef;
54 if (it != subs_dict_.
end()) {
61 for (
const auto &p : x.get_dict()) {
64 if (it != subs_dict_.
end()) {
67 it = subs_dict_.
find(p.second);
68 if (it != subs_dict_.
end()) {
70 mul(it->second, apply(p.first)));
80 void bvisit(
const Mul &x)
82 RCP<const Number> coef = one;
84 for (
const auto &p : x.get_dict()) {
85 RCP<const Basic> factor_old;
86 if (
eq(*p.second, *one)) {
89 factor_old = make_rcp<Pow>(p.first, p.second);
91 RCP<const Basic>
factor = apply(factor_old);
92 if (
factor == factor_old) {
94 Mul::dict_add_term_new(outArg(coef), d, p.second, p.first);
96 imulnum(outArg(coef), rcp_static_cast<const Number>(
factor));
97 }
else if (is_a<Mul>(*
factor)) {
98 RCP<const Mul> tmp = rcp_static_cast<const Mul>(
factor);
99 imulnum(outArg(coef), tmp->get_coef());
100 for (
const auto &q : tmp->get_dict()) {
101 Mul::dict_add_term_new(outArg(coef), d, q.second, q.first);
104 RCP<const Basic>
exp, t;
106 Mul::dict_add_term_new(outArg(coef), d,
exp, t);
111 RCP<const Basic>
factor = apply(x.get_coef());
112 RCP<const Basic>
exp, t;
114 Mul::dict_add_term_new(outArg(coef), d,
exp, t);
119 void bvisit(
const Pow &x)
121 RCP<const Basic> base_new = apply(x.
get_base());
122 RCP<const Basic> exp_new = apply(x.
get_exp());
126 result_ = pow(base_new, exp_new);
136 result_ = x.
create(result_);
143 RCP<const Basic> a = apply(x.
get_arg1());
144 RCP<const Basic> b = apply(x.
get_arg2());
146 result_ = x.rcp_from_this();
154 for (
auto &elem : v) {
163 for (
auto &elem : v) {
171 RCP<const Basic> a = apply(x.get_expr());
172 auto c = apply(x.get_set());
173 if (not is_a_Set(*c))
174 throw SymEngineException(
"expected an object of type Set");
175 RCP<const Set> b = rcp_static_cast<const Set>(c);
176 if (a == x.get_expr() and b == x.get_set())
179 result_ = x.create(a, b);
182 void bvisit(
const And &x)
185 for (
const auto &elem : x.get_container()) {
186 auto a = apply(elem);
187 if (not is_a_Boolean(*a))
188 throw SymEngineException(
"expected an object of type Boolean");
189 v.
insert(rcp_static_cast<const Boolean>(a));
191 result_ = logical_and(v);
194 void bvisit(
const Or &x)
197 for (
const auto &elem : x.get_container()) {
198 auto a = apply(elem);
199 if (not is_a_Boolean(*a))
200 throw SymEngineException(
"expected an object of type Boolean");
201 v.
insert(rcp_static_cast<const Boolean>(a));
203 result_ = logical_or(v);
206 void bvisit(
const Not &x)
208 RCP<const Basic> a = apply(x.get_arg());
209 if (not is_a_Boolean(*a))
210 throw SymEngineException(
"expected an object of type Boolean");
211 result_ = logical_not(rcp_static_cast<const Boolean>(a));
214 void bvisit(
const Xor &x)
217 for (
const auto &elem : x.get_container()) {
218 auto a = apply(elem);
219 if (not is_a_Boolean(*a))
220 throw SymEngineException(
"expected an object of type Boolean");
221 v.
push_back(rcp_static_cast<const Boolean>(a));
223 result_ = logical_xor(v);
229 for (
const auto &elem : x.get_container()) {
232 result_ = x.create(v);
237 RCP<const Basic> s = apply(x.get_symbol());
238 RCP<const Basic> expr = apply(x.get_expr());
239 auto bs_ = apply(x.get_baseset());
240 if (not is_a_Set(*bs_))
241 throw SymEngineException(
"expected an object of type Set");
242 RCP<const Set> bs = rcp_static_cast<const Set>(bs_);
243 if (s == x.get_symbol() and expr == x.get_expr()
244 and bs == x.get_baseset()) {
247 result_ = x.create(s, expr, bs);
251 void bvisit(
const Union &x)
254 for (
const auto &elem : x.get_container()) {
255 auto a = apply(elem);
256 if (not is_a_Set(*a))
257 throw SymEngineException(
"expected an object of type Set");
258 v.
insert(rcp_static_cast<const Set>(a));
260 result_ = x.create(v);
267 for (
const auto &expr_pred : pw.get_vec()) {
268 const auto expr = apply(*expr_pred.first);
269 const auto pred = apply(*expr_pred.second);
278 auto expr = apply(x.get_arg());
279 for (
const auto &sym : x.get_symbols()) {
281 if (not is_a<Symbol>(*s)) {
282 throw SymEngineException(
"expected an object of type Symbol");
284 expr = expr->diff(rcp_static_cast<const Symbol>(s));
289 void bvisit(
const Subs &x)
291 auto expr = apply(x.get_arg());
293 for (
const auto &sym : x.get_dict()) {
294 insert(new_subs_dict, apply(sym.first), apply(sym.second));
296 result_ = subs(expr, new_subs_dict);
301 auto it = subs_dict_.
find(I);
302 if (it != subs_dict_.
end()) {
303 result_ =
add(apply(x.real_part()),
304 mul(apply(x.imaginary_part()), it->second));
310 RCP<const Basic> apply(
const Basic &x)
315 RCP<const Basic> apply(
const RCP<const Basic> &x)
318 auto it = visited.
find(x);
319 if (it != visited.
end()) {
320 result_ = it->second;
323 insert(visited, x, result_);
326 auto it = subs_dict_.
find(x);
327 if (it != subs_dict_.
end()) {
328 result_ = it->second;
338inline RCP<const Basic>
xreplace(
const RCP<const Basic> &x,
348 using XReplaceVisitor::bvisit;
355 void bvisit(
const Pow &x)
357 RCP<const Basic> base_new = apply(x.
get_base());
358 RCP<const Basic> exp_new = apply(x.
get_exp());
359 if (subs_dict_.
size() == 1 and is_a<Pow>(*((*subs_dict_.
begin()).first))
361 *down_cast<const Pow &>(*(*subs_dict_.
begin()).first)
364 = down_cast<const Pow &>(*(*subs_dict_.
begin()).first);
365 if (
eq(*subs_first.get_base(), *base_new)) {
366 auto newexpo =
div(exp_new, subs_first.get_exp());
367 if (
is_a_Number(*newexpo) or is_a<Constant>(*newexpo)) {
368 result_ = pow((*subs_dict_.
begin()).second, newexpo);
376 result_ = pow(base_new, exp_new);
386 for (
const auto &p : subs_dict_) {
389 if (
eq(*x.get_arg(), *p.first)) {
390 RCP<const Basic> t = p.second;
391 for (
auto &sym : x.get_symbols()) {
392 if (not is_a<Symbol>(*sym)) {
393 throw SymEngineException(
"Error, expected a Symbol.");
395 t = t->diff(rcp_static_cast<const Symbol>(sym));
401 for (
const auto &p : subs_dict_) {
403 if (
eq(*x.get_arg()->subs({{p.first, p.second}}), *x.get_arg()))
408 if (is_a<Symbol>(*p.first) and is_a<Symbol>(*p.second)
410 *x.get_arg()->diff(rcp_static_cast<const Symbol>(p.second)),
412 insert(n, p.first, p.second);
415 for (
const auto &d : x.get_symbols()) {
416 if (is_a<Symbol>(*d)) {
417 s = rcp_static_cast<const Symbol>(d);
420 if (
neq(*zero, *(p.first->diff(s)))
421 ||
neq(*zero, *(p.second->diff(s)))) {
432 insert(n, p.first, p.second);
434 insert(m, p.first, p.second);
437 auto t = x.get_arg()->
subs(n);
438 for (
auto &p : x.get_symbols()) {
439 auto t2 = p->subs(n);
440 if (not is_a<Symbol>(*t2)) {
441 throw SymEngineException(
"Error, expected a Symbol.");
443 t = t->diff(rcp_static_cast<const Symbol>(t2));
448 result_ = make_rcp<const Subs>(t, m);
452 void bvisit(
const Subs &x)
455 for (
const auto &p : subs_dict_) {
457 for (
const auto &s : x.get_dict()) {
458 if (
neq(*(s.first->subs({{p.first, p.second}})), *(s.first))) {
466 insert(n, p.first, p.second);
469 for (
const auto &s : x.get_dict()) {
470 insert(m, s.first, apply(s.second));
472 RCP<const Basic> presub = x.get_arg()->subs(n);
473 if (is_a<Subs>(*presub)) {
474 for (
auto &q : down_cast<const Subs &>(*presub).get_dict()) {
475 insert(m, q.first, q.second);
477 result_ = down_cast<const Subs &>(*presub).get_arg()->subs(m);
479 result_ = presub->subs(m);
487 using XReplaceVisitor::bvisit;
499 void bvisit(
const Subs &x)
502 for (
const auto &p : subs_dict_) {
503 m[p.first] = p.second;
505 result_ =
msubs(x.get_arg(), m);
512 using XReplaceVisitor::bvisit;
524 for (
auto &p : x.get_symbols()) {
528 result_ = Derivative::create(t, m);
531 void bvisit(
const Subs &x)
534 for (
const auto &p : subs_dict_) {
535 m[p.first] = p.second;
537 result_ =
ssubs(x.get_arg(), m);
542inline RCP<const Basic>
msubs(
const RCP<const Basic> &x,
550inline RCP<const Basic>
ssubs(
const RCP<const Basic> &x,
557inline RCP<const Basic> subs(
const RCP<const Basic> &x,
558 const map_basic_basic &subs_dict,
bool cache)
560 SubsVisitor b(subs_dict, cache);
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.
const RCP< const Number > & get_coef() const
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.
The lowest unit of symbolic representation.
RCP< const Basic > subs(const map_basic_basic &subs_dict) const
Substitutes 'subs_dict' into 'self'.
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)
RCP< const Basic > create(const vec_basic &x) const override
Method to construct classes with canonicalization.
static void as_base_exp(const RCP< const Basic > &self, const Ptr< RCP< const Basic > > &exp, const Ptr< RCP< const Basic > > &base)
Convert to a base and exponent form.
static RCP< const Basic > from_dict(const RCP< const Number > &coef, map_basic_basic &&d)
Create a Mul from a dict.
virtual RCP< const Basic > create(const vec_basic &v) const =0
Method to construct classes with canonicalization.
vec_basic get_args() const override
Returns the list of arguments.
RCP< const Basic > get_arg() const
virtual RCP< const Basic > create(const RCP< const Basic > &arg) const =0
Method to construct classes with canonicalization.
RCP< const Basic > get_base() const
RCP< const Basic > get_exp() const
RCP< const Basic > get_arg2() const
RCP< const Basic > get_arg1() const
virtual RCP< const Basic > create(const RCP< const Basic > &a, const RCP< const Basic > &b) const =0
Method to construct classes with canonicalization.
T emplace_back(T... args)
Main namespace for SymEngine package.
bool is_a_Number(const Basic &b)
RCP< const Basic > div(const RCP< const Basic > &a, const RCP< const Basic > &b)
Division.
bool eq(const Basic &a, const Basic &b)
Checks equality for a and b
RCP< const Basic > msubs(const RCP< const Basic > &x, const map_basic_basic &subs_dict, bool cache=true)
Subs which treat f(t) and Derivative(f(t), t) as separate variables.
RCP< const Basic > exp(const RCP< const Basic > &x)
Returns the natural exponential function E**x = pow(E, x)
RCP< const Basic > mul(const RCP< const Basic > &a, const RCP< const Basic > &b)
Multiplication.
void insert(T1 &m, const T2 &first, const T3 &second)
int factor(const Ptr< RCP< const Integer > > &f, const Integer &n, double B1)
RCP< const Basic > add(const RCP< const Basic > &a, const RCP< const Basic > &b)
Adds two objects (safely).
RCP< const Basic > xreplace(const RCP< const Basic > &x, const map_basic_basic &subs_dict, bool cache=true)
Mappings in the subs_dict are applied to the expression tree of x
bool neq(const Basic &a, const Basic &b)
Checks inequality for a and b
RCP< const Basic > ssubs(const RCP< const Basic > &x, const map_basic_basic &subs_dict, bool cache=true)
SymPy compatible subs.