RCP<const Basic> Add::subs(const map_basic_basic &subs_dict) const { RCP<const Add> self = rcp_const_cast<Add>(rcp(this)); auto it = subs_dict.find(self); if (it != subs_dict.end()) return it->second; CSymPy::umap_basic_num d; RCP<const Number> coef=coef_, coef2; RCP<const Basic> t; for (auto &p: dict_) { RCP<const Basic> term = p.first->subs(subs_dict); if (term == p.first) { Add::dict_add_term(d, p.second, p.first); } else if (is_a<Integer>(*term) && rcp_static_cast<const Integer>(term)->is_zero()) { continue; } else if (is_a_Number(*term)) { iaddnum(outArg(coef), mulnum(p.second, rcp_static_cast<const Number>(term))); } else if (is_a<Add>(*term)) { for (auto &q: (rcp_static_cast<const Add>(term))->dict_) Add::dict_add_term(d, q.second, q.first); iaddnum(outArg(coef), rcp_static_cast<const Add>(term)->coef_); } else { Add::as_coef_term(mul(p.second, term), outArg(coef2), outArg(t)); Add::dict_add_term(d, coef2, t); } } return Add::from_dict(coef, std::move(d)); }
// Mul (t^exp) to the dict "d" void Mul::dict_add_term(map_basic_basic &d, const RCP<const Basic> &exp, const RCP<const Basic> &t) { auto it = d.find(t); if (it == d.end()) { insert(d, t, exp); } else { // Very common case, needs to be fast: if (is_a_Number(*it->second) && is_a_Number(*exp)) { RCP<const Number> tmp = rcp_static_cast<const Number>(it->second); iaddnum(outArg(tmp), rcp_static_cast<const Number>(exp)); if (tmp->is_zero()) { d.erase(it); } else { it->second = tmp; } } else { // General case: it->second = add(it->second, exp); if (is_a<Integer>(*it->second) && rcp_static_cast<const Integer>(it->second)->is_zero()) { d.erase(it); } } } }
static RCP<const Basic> diff(const Mul &self, const RCP<const Symbol> &x) { RCP<const Number> overall_coef = zero; umap_basic_num add_dict; for (auto &p: self.dict_) { RCP<const Number> coef = self.coef_; RCP<const Basic> factor = pow(p.first, p.second)->diff(x); if (is_a<Integer>(*factor) && rcp_static_cast<const Integer>(factor)->is_zero()) continue; map_basic_basic d = self.dict_; d.erase(p.first); if (is_a_Number(*factor)) { imulnum(outArg(coef), rcp_static_cast<const Number>(factor)); } else if (is_a<Mul>(*factor)) { RCP<const Mul> tmp = rcp_static_cast<const Mul>(factor); imulnum(outArg(coef), tmp->coef_); for (auto &q: tmp->dict_) { Mul::dict_add_term_new(outArg(coef), d, q.second, q.first); } } else { RCP<const Basic> exp, t; Mul::as_base_exp(factor, outArg(exp), outArg(t)); Mul::dict_add_term_new(outArg(coef), d, exp, t); } if (d.size() == 0) { iaddnum(outArg(overall_coef), coef); } else { RCP<const Basic> mul = Mul::from_dict(one, std::move(d)); Add::dict_add_term(add_dict, coef, mul); } } return Add::from_dict(overall_coef, std::move(add_dict)); }
RCP<const Basic> add(const RCP<const Basic> &a, const RCP<const Basic> &b) { CSymPy::umap_basic_num d; RCP<const Number> coef; RCP<const Basic> t; if (CSymPy::is_a<Add>(*a) && CSymPy::is_a<Add>(*b)) { coef = (rcp_static_cast<const Add>(a))->coef_; d = (rcp_static_cast<const Add>(a))->dict_; for (auto &p: (rcp_static_cast<const Add>(b))->dict_) Add::dict_add_term(d, p.second, p.first); iaddnum(outArg(coef), rcp_static_cast<const Add>(b)->coef_); } else if (CSymPy::is_a<Add>(*a)) { coef = (rcp_static_cast<const Add>(a))->coef_; d = (rcp_static_cast<const Add>(a))->dict_; if (is_a_Number(*b)) { iaddnum(outArg(coef), rcp_static_cast<const Number>(b)); } else { RCP<const Number> coef2; Add::as_coef_term(b, outArg(coef2), outArg(t)); Add::dict_add_term(d, coef2, t); } } else if (CSymPy::is_a<Add>(*b)) { coef = (rcp_static_cast<const Add>(b))->coef_; d = (rcp_static_cast<const Add>(b))->dict_; if (is_a_Number(*a)) { iaddnum(outArg(coef), rcp_static_cast<const Number>(a)); } else { RCP<const Number> coef2; Add::as_coef_term(a, outArg(coef2), outArg(t)); Add::dict_add_term(d, coef2, t); } } else { Add::as_coef_term(a, outArg(coef), outArg(t)); Add::dict_add_term(d, coef, t); Add::as_coef_term(b, outArg(coef), outArg(t)); Add::dict_add_term(d, coef, t); auto it = d.find(one); if (it == d.end()) { coef = zero; } else { coef = it->second; d.erase(it); } return Add::from_dict(coef, std::move(d)); } return Add::from_dict(coef, std::move(d)); }
void Add::coef_dict_add_term(const Ptr<RCP<const Number>> &coef, umap_basic_num &d, const RCP<const Number> &c, const RCP<const Basic> &term) { if (is_a_Number(*term)) { iaddnum(coef, mulnum(c, rcp_static_cast<const Number>(term))); } else if (is_a<Add>(*term)) { if (c->is_one()) { for (const auto &q: (rcp_static_cast<const Add>(term))->dict_) Add::dict_add_term(d, q.second, q.first); iaddnum(coef, rcp_static_cast<const Add>(term)->coef_); } else { Add::dict_add_term(d, c, term); } } else { RCP<const Number> coef2; RCP<const Basic> t; Add::as_coef_term(term, outArg(coef2), outArg(t)); Add::dict_add_term(d, mulnum(c, coef2), t); } }
// Mul (t^exp) to the dict "d" void Mul::dict_add_term_new(const Ptr<RCP<const Number>> &coef, map_basic_basic &d, const RCP<const Basic> &exp, const RCP<const Basic> &t) { auto it = d.find(t); if (it == d.end()) { // Don't check for `exp = 0` here // `pow` for Complex is not expanded by default if (is_a<Integer>(*exp) && (is_a<Integer>(*t) || is_a<Rational>(*t))) { imulnum(outArg(*coef), pownum(rcp_static_cast<const Number>(t), rcp_static_cast<const Number>(exp))); } else if (is_a<Integer>(*exp) && is_a<Complex>(*t)) { if (rcp_static_cast<const Integer>(exp)->is_one()) { imulnum(outArg(*coef), rcp_static_cast<const Number>(t)); } else if (rcp_static_cast<const Integer>(exp)->is_minus_one()) { idivnum(outArg(*coef), rcp_static_cast<const Number>(t)); } else { insert(d, t, exp); } } else { insert(d, t, exp); } } else { // Very common case, needs to be fast: if (is_a_Number(*exp) && is_a_Number(*it->second)) { RCP<const Number> tmp = rcp_static_cast<const Number>(it->second); iaddnum(outArg(tmp), rcp_static_cast<const Number>(exp)); it->second = tmp; } else it->second = add(it->second, exp); if (is_a<Integer>(*it->second)) { // `pow` for Complex is not expanded by default if (is_a<Integer>(*t) || is_a<Rational>(*t)) { if (!rcp_static_cast<const Integer>(it->second)->is_zero()) { imulnum(outArg(*coef), pownum(rcp_static_cast<const Number>(t), rcp_static_cast<const Number>(it->second))); } d.erase(it); } else if (rcp_static_cast<const Integer>(it->second)->is_zero()) { d.erase(it); } else if (is_a<Complex>(*t)) { if (rcp_static_cast<const Integer>(it->second)->is_one()) { imulnum(outArg(*coef), rcp_static_cast<const Number>(t)); d.erase(it); } else if (rcp_static_cast<const Integer>(it->second)->is_minus_one()) { idivnum(outArg(*coef), rcp_static_cast<const Number>(t)); d.erase(it); } } } } }
// Adds (coef*t) to the dict "d" // Assumption: "t" does not have any numerical coefficients, those are in "coef" void Add::dict_add_term(umap_basic_int &d, const RCP<Number> &coef, const RCP<Basic> &t) { auto it = d.find(t); if (it == d.end()) { // Not found, add it in if it is nonzero: if (!(coef->is_zero())) insert(d, t, coef); } else { iaddnum(outArg(it->second), coef); if (it->second->is_zero()) d.erase(it); } }
RCP<const Basic> Add::diff(const RCP<const Symbol> &x) const { CSymPy::umap_basic_num d; RCP<const Number> coef=zero, coef2; RCP<const Basic> t; for (auto &p: dict_) { RCP<const Basic> term = p.first->diff(x); if (is_a<Integer>(*term) && rcp_static_cast<const Integer>(term)->is_zero()) { continue; } else if (is_a_Number(*term)) { iaddnum(outArg(coef), mulnum(p.second, rcp_static_cast<const Number>(term))); } else if (is_a<Add>(*term)) { for (auto &q: (rcp_static_cast<const Add>(term))->dict_) Add::dict_add_term(d, q.second, q.first); iaddnum(outArg(coef), rcp_static_cast<const Add>(term)->coef_); } else { Add::as_coef_term(mul(p.second, term), outArg(coef2), outArg(t)); Add::dict_add_term(d, coef2, t); } } return Add::from_dict(coef, std::move(d)); }
RCP<const Basic> add_expand(const RCP<const Add> &self) { umap_basic_num d; RCP<const Number> coef_overall = self->coef_; RCP<const Number> coef; RCP<const Basic> tmp, tmp2; for (auto &p: self->dict_) { tmp = expand(p.first); if (is_a<Add>(*tmp)) { for (auto &q: (rcp_static_cast<const Add>(tmp))->dict_) { Add::as_coef_term(q.first, outArg(coef), outArg(tmp2)); Add::dict_add_term(d, mulnum(mulnum(p.second, q.second), coef), tmp2); } iaddnum(outArg(coef_overall), mulnum(p.second, rcp_static_cast<const Add>(tmp)->coef_)); } else { Add::as_coef_term(tmp, outArg(coef), outArg(tmp)); Add::dict_add_term(d, mulnum(p.second, coef), tmp); } } return Add::from_dict(coef_overall, std::move(d)); }
// Very quickly (!) creates the appropriate instance (i.e. Add, Symbol, // Integer, Mul) depending on the size of the dictionary 'd'. // If d.size() > 1 then it just returns Add. This means that the dictionary // must be in canonical form already. For d.size == 1, it returns Mul, Pow, // Symbol or Integer, depending on the expression. RCP<const Basic> Add::from_dict(const RCP<const Number> &coef, umap_basic_num &&d) { if (d.size() == 0) { return coef; } else if (d.size() == 1 && coef->is_zero()) { auto p = d.begin(); if (is_a<Integer>(*(p->second))) { if (rcp_static_cast<const Integer>(p->second)->is_zero()) { return zero; } if (rcp_static_cast<const Integer>(p->second)->is_one()) { return p->first; } if (is_a<Mul>(*(p->first))) { #if !defined(WITH_CSYMPY_THREAD_SAFE) && defined(WITH_CSYMPY_RCP) if (rcp_static_cast<const Mul>(p->first)->refcount_ == 1) { // We can steal the dictionary: // Cast away const'ness, so that we can move 'dict_', since // 'p->first' will be destroyed when 'd' is at the end of // this function, so we "steal" its dict_ to avoid an // unnecessary copy. We know the refcount_ is one, so // nobody else is using the Mul except us. const map_basic_basic &d2 = rcp_static_cast<const Mul>(p->first)->dict_; map_basic_basic &d3 = const_cast<map_basic_basic &>(d2); return Mul::from_dict(p->second, std::move(d3)); } else { #else { #endif // We need to copy the dictionary: map_basic_basic d2 = rcp_static_cast<const Mul>(p->first)->dict_; return Mul::from_dict(p->second, std::move(d2)); } } map_basic_basic m; if (is_a<Pow>(*(p->first))) { insert(m, rcp_static_cast<const Pow>(p->first)->base_, rcp_static_cast<const Pow>(p->first)->exp_); } else { insert(m, p->first, one); } return rcp(new Mul(p->second, std::move(m))); } map_basic_basic m; if (is_a_Number(*p->second)) { if (is_a<Mul>(*(p->first))) { #if !defined(WITH_CSYMPY_THREAD_SAFE) && defined(WITH_CSYMPY_RCP) if (rcp_static_cast<const Mul>(p->first)->refcount_ == 1) { // We can steal the dictionary: // Cast away const'ness, so that we can move 'dict_', since // 'p->first' will be destroyed when 'd' is at the end of // this function, so we "steal" its dict_ to avoid an // unnecessary copy. We know the refcount_ is one, so // nobody else is using the Mul except us. const map_basic_basic &d2 = rcp_static_cast<const Mul>(p->first)->dict_; map_basic_basic &d3 = const_cast<map_basic_basic &>(d2); return Mul::from_dict(p->second, std::move(d3)); } else { #else { #endif // We need to copy the dictionary: map_basic_basic d2 = rcp_static_cast<const Mul>(p->first)->dict_; return Mul::from_dict(p->second, std::move(d2)); } } if (is_a<Pow>(*p->first)) { insert(m, rcp_static_cast<const Pow>(p->first)->base_, rcp_static_cast<const Pow>(p->first)->exp_); } else { insert(m, p->first, one); } return rcp(new Mul(p->second, std::move(m))); } else { insert(m, p->first, one); insert(m, p->second, one); return rcp(new Mul(one, std::move(m))); } } else { return rcp(new Add(coef, std::move(d))); } } // Adds (coef*t) to the dict "d" // Assumption: "t" does not have any numerical coefficients, those are in "coef" void Add::dict_add_term(umap_basic_num &d, const RCP<const Number> &coef, const RCP<const Basic> &t) { auto it = d.find(t); if (it == d.end()) { // Not found, add it in if it is nonzero: if (!(coef->is_zero())) insert(d, t, coef); } else { iaddnum(outArg(it->second), coef); if (it->second->is_zero()) d.erase(it); } }
RCP<const Basic> pow_expand(const RCP<const Pow> &self) { RCP<const Basic> _base = expand(self->base_); bool negative_pow = false; if (! is_a<Integer>(*self->exp_) || ! is_a<Add>(*_base)) { if (neq(_base, self->base_)) { return pow(_base, self->exp_); } else { return self; } } map_vec_mpz r; int n = rcp_static_cast<const Integer>(self->exp_)->as_int(); if (n < 0) { n = -n; negative_pow = true; } RCP<const Add> base = rcp_static_cast<const Add>(_base); umap_basic_num base_dict = base->dict_; if (! (base->coef_->is_zero())) { // Add the numerical coefficient into the dictionary. This // allows a little bit easier treatment below. insert(base_dict, base->coef_, one); } int m = base_dict.size(); multinomial_coefficients_mpz(m, n, r); umap_basic_num rd; // This speeds up overall expansion. For example for the benchmark // (y + x + z + w)^60 it improves the timing from 135ms to 124ms. rd.reserve(2*r.size()); RCP<const Number> add_overall_coeff=zero; for (auto &p: r) { auto power = p.first.begin(); auto i2 = base_dict.begin(); map_basic_basic d; RCP<const Number> overall_coeff=one; for (; power != p.first.end(); ++power, ++i2) { if (*power > 0) { RCP<const Integer> exp = rcp(new Integer(*power)); RCP<const Basic> base = i2->first; if (is_a<Integer>(*base)) { imulnum(outArg(overall_coeff), rcp_static_cast<const Number>( rcp_static_cast<const Integer>(base)->powint(*exp))); } else if (is_a<Symbol>(*base)) { Mul::dict_add_term(d, exp, base); } else { RCP<const Basic> exp2, t, tmp; tmp = pow(base, exp); if (is_a<Mul>(*tmp)) { for (auto &p: (rcp_static_cast<const Mul>(tmp))->dict_) { Mul::dict_add_term_new(outArg(overall_coeff), d, p.second, p.first); } imulnum(outArg(overall_coeff), (rcp_static_cast<const Mul>(tmp))->coef_); } else { Mul::as_base_exp(tmp, outArg(exp2), outArg(t)); Mul::dict_add_term_new(outArg(overall_coeff), d, exp2, t); } } if (!(i2->second->is_one())) { if (is_a<Integer>(*(i2->second)) || is_a<Rational>(*(i2->second))) { imulnum(outArg(overall_coeff), pownum(i2->second, rcp_static_cast<const Number>(exp))); } else if (is_a<Complex>(*(i2->second))) { RCP<const Number> tmp = rcp_static_cast<const Complex>(i2->second)->pow(*exp); imulnum(outArg(overall_coeff), tmp); } } } } RCP<const Basic> term = Mul::from_dict(overall_coeff, std::move(d)); RCP<const Number> coef2 = rcp(new Integer(p.second)); if (is_a_Number(*term)) { iaddnum(outArg(add_overall_coeff), mulnum(rcp_static_cast<const Number>(term), coef2)); } else { if (is_a<Mul>(*term) && !(rcp_static_cast<const Mul>(term)->coef_->is_one())) { // Tidy up things like {2x: 3} -> {x: 6} imulnum(outArg(coef2), rcp_static_cast<const Mul>(term)->coef_); // We make a copy of the dict_: map_basic_basic d2 = rcp_static_cast<const Mul>(term)->dict_; term = Mul::from_dict(one, std::move(d2)); } Add::dict_add_term(rd, coef2, term); } } RCP<const Basic> result = Add::from_dict(add_overall_coeff, std::move(rd)); if (negative_pow) result = pow(result, minus_one); return result; }
RCP<const Basic> mul_expand_two(const RCP<const Basic> &a, const RCP<const Basic> &b) { // Both a and b are assumed to be expanded if (is_a<Add>(*a) && is_a<Add>(*b)) { RCP<const Number> coef = mulnum(rcp_static_cast<const Add>(a)->coef_, rcp_static_cast<const Add>(b)->coef_); umap_basic_num d; // Improves (x+1)^3(x+2)^3...(x+350)^3 expansion from 0.97s to 0.93s: d.reserve((rcp_static_cast<const Add>(a))->dict_.size()* (rcp_static_cast<const Add>(b))->dict_.size()); // Expand dicts first: for (auto &p: (rcp_static_cast<const Add>(a))->dict_) { for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { // The main bottleneck here is the mul(p.first, q.first) command RCP<const Basic> term = mul(p.first, q.first); if (is_a_Number(*term)) { iaddnum(outArg(coef), rcp_static_cast<const Number>(term)); } else { Add::dict_add_term(d, mulnum(p.second, q.second), term); } } Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(b)->coef_, p.second), p.first); } // Handle the coefficient of "a": for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(a)->coef_, q.second), q.first); } return Add::from_dict(coef, std::move(d)); } else if (is_a<Add>(*a)) { return mul_expand_two(b, a); } else if (is_a<Add>(*b)) { RCP<const Number> a_coef; RCP<const Basic> a_term; Add::as_coef_term(a, outArg(a_coef), outArg(a_term)); RCP<const Number> coef = zero; umap_basic_num d; d.reserve((rcp_static_cast<const Add>(b))->dict_.size()); for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { RCP<const Basic> term = mul(a_term, q.first); if (is_a_Number(*term)) { iaddnum(outArg(coef), rcp_static_cast<const Number>(term)); } else { Add::dict_add_term(d, mulnum(a_coef, q.second), term); } } if (eq(a_term, one)) { iaddnum(outArg(coef), mulnum(rcp_static_cast<const Add>(b)->coef_, a_coef)); } else { Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(b)->coef_, a_coef), a_term); } return Add::from_dict(coef, std::move(d)); } return mul(a, b); }
RCP<const Basic> mul_expand_two(const RCP<const Basic> &a, const RCP<const Basic> &b) { // Both a and b are assumed to be expanded if (is_a<Add>(*a) && is_a<Add>(*b)) { RCP<const Number> coef = mulnum(rcp_static_cast<const Add>(a)->coef_, rcp_static_cast<const Add>(b)->coef_); umap_basic_num d; // Improves (x+1)**3*(x+2)**3*...(x+350)**3 expansion from 0.97s to 0.93s: d.reserve((rcp_static_cast<const Add>(a))->dict_.size()* (rcp_static_cast<const Add>(b))->dict_.size()); // Expand dicts first: for (auto &p: (rcp_static_cast<const Add>(a))->dict_) { for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { // The main bottleneck here is the mul(p.first, q.first) command RCP<const Basic> term = mul(p.first, q.first); if (is_a_Number(*term)) { iaddnum(outArg(coef), mulnum(mulnum(p.second, q.second), rcp_static_cast<const Number>(term))); } else { if (is_a<Mul>(*term) && !(rcp_static_cast<const Mul>(term)->coef_->is_one())) { // Tidy up things like {2x: 3} -> {x: 6} RCP<const Number> coef2 = rcp_static_cast<const Mul>(term)->coef_; // We make a copy of the dict_: map_basic_basic d2 = rcp_static_cast<const Mul>(term)->dict_; term = Mul::from_dict(one, std::move(d2)); Add::dict_add_term(d, mulnum(mulnum(p.second, q.second), coef2), term); } else { Add::dict_add_term(d, mulnum(p.second, q.second), term); } } } Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(b)->coef_, p.second), p.first); } // Handle the coefficient of "a": for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(a)->coef_, q.second), q.first); } return Add::from_dict(coef, std::move(d)); } else if (is_a<Add>(*a)) { return mul_expand_two(b, a); } else if (is_a<Add>(*b)) { RCP<const Number> a_coef; RCP<const Basic> a_term; Add::as_coef_term(a, outArg(a_coef), outArg(a_term)); RCP<const Number> coef = zero; umap_basic_num d; d.reserve((rcp_static_cast<const Add>(b))->dict_.size()); for (auto &q: (rcp_static_cast<const Add>(b))->dict_) { RCP<const Basic> term = mul(a_term, q.first); if (is_a_Number(*term)) { iaddnum(outArg(coef), mulnum(mulnum(q.second, a_coef), rcp_static_cast<const Number>(term))); } else { if (is_a<Mul>(*term) && !(rcp_static_cast<const Mul>(term)->coef_->is_one())) { // Tidy up things like {2x: 3} -> {x: 6} RCP<const Number> coef2 = rcp_static_cast<const Mul>(term)->coef_; // We make a copy of the dict_: map_basic_basic d2 = rcp_static_cast<const Mul>(term)->dict_; term = Mul::from_dict(one, std::move(d2)); Add::dict_add_term(d, mulnum(mulnum(q.second, a_coef), coef2), term); } else { Add::dict_add_term(d, mulnum(a_coef, q.second), term); } } } if (eq(*a_term, *one)) { iaddnum(outArg(coef), mulnum(rcp_static_cast<const Add>(b)->coef_, a_coef)); } else { Add::dict_add_term(d, mulnum(rcp_static_cast<const Add>(b)->coef_, a_coef), a_term); } return Add::from_dict(coef, std::move(d)); } return mul(a, b); }
// Mul (t**exp) to the dict "d" void Mul::dict_add_term_new(const Ptr<RCP<const Number>> &coef, map_basic_basic &d, const RCP<const Basic> &exp, const RCP<const Basic> &t) { auto it = d.find(t); if (it == d.end()) { // Don't check for `exp = 0` here // `pow` for Complex is not expanded by default if (is_a<Integer>(*t) || is_a<Rational>(*t)) { if (is_a<Integer>(*exp)) { imulnum(outArg(*coef), pownum(rcp_static_cast<const Number>(t), rcp_static_cast<const Number>(exp))); } else if (is_a<Rational>(*exp)) { // Here we make the exponent postive and a fraction between // 0 and 1. mpz_class q, r, num, den; num = rcp_static_cast<const Rational>(exp)->i.get_num(); den = rcp_static_cast<const Rational>(exp)->i.get_den(); mpz_fdiv_qr(q.get_mpz_t(), r.get_mpz_t(), num.get_mpz_t(), den.get_mpz_t()); insert(d, t, Rational::from_mpq(mpq_class(r, den))); imulnum(outArg(*coef), pownum(rcp_static_cast<const Number>(t), rcp_static_cast<const Number>(integer(q)))); } else { insert(d, t, exp); } } else if (is_a<Integer>(*exp) && is_a<Complex>(*t)) { if (rcp_static_cast<const Integer>(exp)->is_one()) { imulnum(outArg(*coef), rcp_static_cast<const Number>(t)); } else if (rcp_static_cast<const Integer>(exp)->is_minus_one()) { idivnum(outArg(*coef), rcp_static_cast<const Number>(t)); } else { insert(d, t, exp); } } else { insert(d, t, exp); } } else { // Very common case, needs to be fast: if (is_a_Number(*exp) && is_a_Number(*it->second)) { RCP<const Number> tmp = rcp_static_cast<const Number>(it->second); iaddnum(outArg(tmp), rcp_static_cast<const Number>(exp)); it->second = tmp; } else it->second = add(it->second, exp); if (is_a<Integer>(*it->second)) { // `pow` for Complex is not expanded by default if (is_a<Integer>(*t) || is_a<Rational>(*t)) { if (!rcp_static_cast<const Integer>(it->second)->is_zero()) { imulnum(outArg(*coef), pownum(rcp_static_cast<const Number>(t), rcp_static_cast<const Number>(it->second))); } d.erase(it); } else if (rcp_static_cast<const Integer>(it->second)->is_zero()) { d.erase(it); } else if (is_a<Complex>(*t)) { if (rcp_static_cast<const Integer>(it->second)->is_one()) { imulnum(outArg(*coef), rcp_static_cast<const Number>(t)); d.erase(it); } else if (rcp_static_cast<const Integer>(it->second)->is_minus_one()) { idivnum(outArg(*coef), rcp_static_cast<const Number>(t)); d.erase(it); } } } else if (is_a<Rational>(*it->second)) { if (is_a_Number(*t)) { mpz_class q, r, num, den; num = rcp_static_cast<const Rational>(it->second)->i.get_num(); den = rcp_static_cast<const Rational>(it->second)->i.get_den(); // Here we make the exponent postive and a fraction between // 0 and 1. if (num > den || num < 0) { mpz_fdiv_qr(q.get_mpz_t(), r.get_mpz_t(), num.get_mpz_t(), den.get_mpz_t()); it->second = Rational::from_mpq(mpq_class(r, den)); imulnum(outArg(*coef), pownum(rcp_static_cast<const Number>(t), rcp_static_cast<const Number>(integer(q)))); } } } } }