RCP<const Basic> Mul::diff(const RCP<const Symbol> &x) const { RCP<const Basic> r=zero; for (auto &p: dict_) { RCP<const Number> coef = 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 = 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); } // TODO: speed this up: r = add(r, Mul::from_dict(coef, std::move(d))); } return r; }
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> Mul::subs(const map_basic_basic &subs_dict) const { RCP<const Mul> self = rcp_const_cast<Mul>(rcp(this)); auto it = subs_dict.find(self); if (it != subs_dict.end()) return it->second; RCP<const Number> coef = coef_; map_basic_basic d; for (auto &p: dict_) { RCP<const Basic> factor_old = pow(p.first, p.second); RCP<const Basic> factor = factor_old->subs(subs_dict); if (factor == factor_old) { Mul::dict_add_term_new(outArg(coef), d, p.second, p.first); } else if (is_a<Integer>(*factor) && rcp_static_cast<const Integer>(factor)->is_zero()) { return zero; } else 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); } } return Mul::from_dict(coef, std::move(d)); }
RCP<const Basic> mul(const RCP<const Basic> &a, const RCP<const Basic> &b) { SymEngine::map_basic_basic d; RCP<const Number> coef = one; if (SymEngine::is_a<Mul>(*a) && SymEngine::is_a<Mul>(*b)) { RCP<const Mul> A = rcp_static_cast<const Mul>(a); RCP<const Mul> B = rcp_static_cast<const Mul>(b); // This is important optimization, as coef=1 if Mul is inside an Add. // To further speed this up, the upper level code could tell us that we // are inside an Add, then we don't even have can simply skip the // following two lines. if (!(A->coef_->is_one()) || !(B->coef_->is_one())) coef = mulnum(A->coef_, B->coef_); d = A->dict_; for (auto &p: B->dict_) Mul::dict_add_term_new(outArg(coef), d, p.second, p.first); } else if (SymEngine::is_a<Mul>(*a)) { RCP<const Basic> exp; RCP<const Basic> t; coef = (rcp_static_cast<const Mul>(a))->coef_; d = (rcp_static_cast<const Mul>(a))->dict_; if (is_a_Number(*b)) { imulnum(outArg(coef), rcp_static_cast<const Number>(b)); } else { Mul::as_base_exp(b, outArg(exp), outArg(t)); Mul::dict_add_term_new(outArg(coef), d, exp, t); } } else if (SymEngine::is_a<Mul>(*b)) { RCP<const Basic> exp; RCP<const Basic> t; coef = (rcp_static_cast<const Mul>(b))->coef_; d = (rcp_static_cast<const Mul>(b))->dict_; if (is_a_Number(*a)) { imulnum(outArg(coef), rcp_static_cast<const Number>(a)); } else { Mul::as_base_exp(a, outArg(exp), outArg(t)); Mul::dict_add_term_new(outArg(coef), d, exp, t); } } else { RCP<const Basic> exp; RCP<const Basic> t; if (is_a_Number(*a)) { imulnum(outArg(coef), rcp_static_cast<const Number>(a)); } else { Mul::as_base_exp(a, outArg(exp), outArg(t)); Mul::dict_add_term_new(outArg(coef), d, exp, t); } if (is_a_Number(*b)) { imulnum(outArg(coef), rcp_static_cast<const Number>(b)); } else { Mul::as_base_exp(b, outArg(exp), outArg(t)); Mul::dict_add_term_new(outArg(coef), d, exp, t); } } return Mul::from_dict(coef, std::move(d)); }
// 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); } } } } }
RCP<const Basic> Mul::power_all_terms(const RCP<const Basic> &exp) const { CSymPy::map_basic_basic d; RCP<const Basic> new_coef = pow(coef_, exp); RCP<const Number> coef_num = one; RCP<const Basic> new_exp; for (auto &p: dict_) { new_exp = mul(p.second, exp); if (is_a_Number(*new_exp)) { // No need for additional dict checks here. // The dict should be of standard form before this is // called. if (rcp_static_cast<const Number>(new_exp)->is_zero()) { continue; } else { Mul::dict_add_term_new(outArg(coef_num), d, new_exp, p.first); } } else { Mul::dict_add_term_new(outArg(coef_num), d, new_exp, p.first); } } if (is_a_Number(*new_coef)) { imulnum(outArg(coef_num), rcp_static_cast<const Number>(new_coef)); return Mul::from_dict(coef_num, std::move(d)); } else { // TODO: this can be made faster probably: return mul(mul(new_coef, coef_num), Mul::from_dict(one, std::move(d))); } }
RCP<const Basic> Mul::power_all_terms(const RCP<const Basic> &exp) const { SymEngine::map_basic_basic d; RCP<const Basic> new_coef = pow(coef_, exp); RCP<const Number> coef_num = one; RCP<const Basic> new_exp; for (auto &p: dict_) { new_exp = mul(p.second, exp); if (is_a_Number(*new_exp)) { // No need for additional dict checks here. // The dict should be of standard form before this is // called. if (rcp_static_cast<const Number>(new_exp)->is_zero()) { continue; } else { Mul::dict_add_term_new(outArg(coef_num), d, new_exp, p.first); } } else { Mul::dict_add_term_new(outArg(coef_num), d, new_exp, p.first); } } if (is_a_Number(*new_coef)) { imulnum(outArg(coef_num), rcp_static_cast<const Number>(new_coef)); } else if (is_a<Mul>(*new_coef)) { RCP<const Mul> tmp = rcp_static_cast<const Mul>(new_coef); imulnum(outArg(coef_num), tmp->coef_); for (auto &q: tmp->dict_) { Mul::dict_add_term_new(outArg(coef_num), d, q.second, q.first); } } else { RCP<const Basic> _exp, t; Mul::as_base_exp(new_coef, outArg(_exp), outArg(t)); Mul::dict_add_term_new(outArg(coef_num), d, _exp, t); } return Mul::from_dict(coef_num, std::move(d)); }
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; }
// 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)))); } } } } }