1 #ifndef SYMENGINE_SUBS_H
2 #define SYMENGINE_SUBS_H
11 RCP<const Basic>
xreplace(
const RCP<const Basic> &x,
12 const map_basic_basic &subs_dict,
bool cache =
true);
15 RCP<const Basic> subs(
const RCP<const Basic> &x,
16 const map_basic_basic &subs_dict,
bool cache =
true);
19 RCP<const Basic>
msubs(
const RCP<const Basic> &x,
20 const map_basic_basic &subs_dict,
bool cache =
true);
22 RCP<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());
113 imulnum(outArg(coef), rcp_static_cast<const Number>(
factor));
114 }
else if (is_a<Mul>(*
factor)) {
115 RCP<const Mul> tmp = rcp_static_cast<const Mul>(
factor);
116 imulnum(outArg(coef), tmp->get_coef());
117 for (
const auto &q : tmp->get_dict()) {
118 Mul::dict_add_term_new(outArg(coef), d, q.second, q.first);
121 RCP<const Basic>
exp, t;
123 Mul::dict_add_term_new(outArg(coef), d,
exp, t);
128 void bvisit(
const Pow &x)
130 RCP<const Basic> base_new = apply(x.
get_base());
131 RCP<const Basic> exp_new = apply(x.
get_exp());
135 result_ = pow(base_new, exp_new);
145 result_ = x.
create(result_);
152 RCP<const Basic> a = apply(x.
get_arg1());
153 RCP<const Basic> b = apply(x.
get_arg2());
155 result_ = x.rcp_from_this();
163 for (
auto &elem : v) {
172 for (
auto &elem : v) {
180 RCP<const Basic> a = apply(x.get_expr());
181 auto c = apply(x.get_set());
182 if (not is_a_Set(*c))
183 throw SymEngineException(
"expected an object of type Set");
184 RCP<const Set> b = rcp_static_cast<const Set>(c);
185 if (a == x.get_expr() and b == x.get_set())
188 result_ = x.create(a, b);
191 void bvisit(
const And &x)
194 for (
const auto &elem : x.get_container()) {
195 auto a = apply(elem);
196 if (not is_a_Boolean(*a))
197 throw SymEngineException(
"expected an object of type Boolean");
198 v.
insert(rcp_static_cast<const Boolean>(a));
200 result_ = logical_and(v);
203 void bvisit(
const Or &x)
206 for (
const auto &elem : x.get_container()) {
207 auto a = apply(elem);
208 if (not is_a_Boolean(*a))
209 throw SymEngineException(
"expected an object of type Boolean");
210 v.
insert(rcp_static_cast<const Boolean>(a));
212 result_ = logical_or(v);
215 void bvisit(
const Not &x)
217 RCP<const Basic> a = apply(x.get_arg());
218 if (not is_a_Boolean(*a))
219 throw SymEngineException(
"expected an object of type Boolean");
220 result_ = logical_not(rcp_static_cast<const Boolean>(a));
223 void bvisit(
const Xor &x)
226 for (
const auto &elem : x.get_container()) {
227 auto a = apply(elem);
228 if (not is_a_Boolean(*a))
229 throw SymEngineException(
"expected an object of type Boolean");
230 v.
push_back(rcp_static_cast<const Boolean>(a));
232 result_ = logical_xor(v);
238 for (
const auto &elem : x.get_container()) {
241 result_ = x.create(v);
246 RCP<const Basic> s = apply(x.get_symbol());
247 RCP<const Basic> expr = apply(x.get_expr());
248 auto bs_ = apply(x.get_baseset());
249 if (not is_a_Set(*bs_))
250 throw SymEngineException(
"expected an object of type Set");
251 RCP<const Set> bs = rcp_static_cast<const Set>(bs_);
252 if (s == x.get_symbol() and expr == x.get_expr()
253 and bs == x.get_baseset()) {
256 result_ = x.create(s, expr, bs);
260 void bvisit(
const Union &x)
263 for (
const auto &elem : x.get_container()) {
264 auto a = apply(elem);
265 if (not is_a_Set(*a))
266 throw SymEngineException(
"expected an object of type Set");
267 v.
insert(rcp_static_cast<const Set>(a));
269 result_ = x.create(v);
276 for (
const auto &expr_pred : pw.get_vec()) {
277 const auto expr = apply(*expr_pred.first);
278 const auto pred = apply(*expr_pred.second);
287 auto expr = apply(x.get_arg());
288 for (
const auto &sym : x.get_symbols()) {
290 if (not is_a<Symbol>(*s)) {
291 throw SymEngineException(
"expected an object of type Symbol");
293 expr = expr->diff(rcp_static_cast<const Symbol>(s));
298 void bvisit(
const Subs &x)
300 auto expr = apply(x.get_arg());
302 for (
const auto &sym : x.get_dict()) {
303 insert(new_subs_dict, apply(sym.first), apply(sym.second));
305 result_ = subs(expr, new_subs_dict);
310 auto it = subs_dict_.
find(I);
311 if (it != subs_dict_.
end()) {
312 result_ =
add(apply(x.real_part()),
313 mul(apply(x.imaginary_part()), it->second));
319 RCP<const Basic> apply(
const Basic &x)
324 RCP<const Basic> apply(
const RCP<const Basic> &x)
327 auto it = visited.
find(x);
328 if (it != visited.
end()) {
329 result_ = it->second;
332 insert(visited, x, result_);
335 auto it = subs_dict_.
find(x);
336 if (it != subs_dict_.
end()) {
337 result_ = it->second;
347 inline RCP<const Basic>
xreplace(
const RCP<const Basic> &x,
357 using XReplaceVisitor::bvisit;
364 void bvisit(
const Pow &x)
366 RCP<const Basic> base_new = apply(x.
get_base());
367 RCP<const Basic> exp_new = apply(x.
get_exp());
368 if (subs_dict_.
size() == 1 and is_a<Pow>(*((*subs_dict_.
begin()).first))
370 *down_cast<const Pow &>(*(*subs_dict_.
begin()).first)
373 = down_cast<const Pow &>(*(*subs_dict_.
begin()).first);
374 if (
eq(*subs_first.get_base(), *base_new)) {
375 auto newexpo =
div(exp_new, subs_first.get_exp());
376 if (
is_a_Number(*newexpo) or is_a<Constant>(*newexpo)) {
377 result_ = pow((*subs_dict_.
begin()).second, newexpo);
385 result_ = pow(base_new, exp_new);
395 for (
const auto &p : subs_dict_) {
398 if (
eq(*x.get_arg(), *p.first)) {
399 RCP<const Basic> t = p.second;
400 for (
auto &sym : x.get_symbols()) {
401 if (not is_a<Symbol>(*sym)) {
402 throw SymEngineException(
"Error, expected a Symbol.");
404 t = t->diff(rcp_static_cast<const Symbol>(sym));
410 for (
const auto &p : subs_dict_) {
412 if (
eq(*x.get_arg()->subs({{p.first, p.second}}), *x.get_arg()))
417 if (is_a<Symbol>(*p.first) and is_a<Symbol>(*p.second)
419 *x.get_arg()->diff(rcp_static_cast<const Symbol>(p.second)),
421 insert(n, p.first, p.second);
424 for (
const auto &d : x.get_symbols()) {
425 if (is_a<Symbol>(*d)) {
426 s = rcp_static_cast<const Symbol>(d);
429 if (
neq(*zero, *(p.first->diff(s)))
430 ||
neq(*zero, *(p.second->diff(s)))) {
441 insert(n, p.first, p.second);
443 insert(m, p.first, p.second);
446 auto t = x.get_arg()->
subs(n);
447 for (
auto &p : x.get_symbols()) {
448 auto t2 = p->subs(n);
449 if (not is_a<Symbol>(*t2)) {
450 throw SymEngineException(
"Error, expected a Symbol.");
452 t = t->diff(rcp_static_cast<const Symbol>(t2));
457 result_ = make_rcp<const Subs>(t, m);
461 void bvisit(
const Subs &x)
464 for (
const auto &p : subs_dict_) {
466 for (
const auto &s : x.get_dict()) {
467 if (
neq(*(s.first->subs({{p.first, p.second}})), *(s.first))) {
475 insert(n, p.first, p.second);
478 for (
const auto &s : x.get_dict()) {
479 insert(m, s.first, apply(s.second));
481 RCP<const Basic> presub = x.get_arg()->subs(n);
482 if (is_a<Subs>(*presub)) {
483 for (
auto &q : down_cast<const Subs &>(*presub).get_dict()) {
484 insert(m, q.first, q.second);
486 result_ = down_cast<const Subs &>(*presub).get_arg()->subs(m);
488 result_ = presub->subs(m);
496 using XReplaceVisitor::bvisit;
508 void bvisit(
const Subs &x)
511 for (
const auto &p : subs_dict_) {
512 m[p.first] = p.second;
514 result_ =
msubs(x.get_arg(), m);
521 using XReplaceVisitor::bvisit;
533 for (
auto &p : x.get_symbols()) {
537 result_ = Derivative::create(t, m);
540 void bvisit(
const Subs &x)
543 for (
const auto &p : subs_dict_) {
544 m[p.first] = p.second;
546 result_ =
ssubs(x.get_arg(), m);
551 inline RCP<const Basic>
msubs(
const RCP<const Basic> &x,
559 inline RCP<const Basic>
ssubs(
const RCP<const Basic> &x,
566 inline RCP<const Basic> subs(
const RCP<const Basic> &x,
567 const map_basic_basic &subs_dict,
bool cache)
569 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.
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.
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.
virtual RCP< const Basic > create(const RCP< const Basic > &arg) const =0
Method to construct classes with canonicalization.
RCP< const Basic > get_arg() const
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
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.