3 * Implementation of GiNaC's symbolic exponentiation (basis^exponent). */
6 * GiNaC Copyright (C) 1999-2004 Johannes Gutenberg University Mainz, Germany
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 #include "expairseq.h"
35 #include "operators.h"
36 #include "inifcns.h" // for log() in power::derivative()
46 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(power, basic,
47 print_func<print_dflt>(&power::do_print_dflt).
48 print_func<print_latex>(&power::do_print_latex).
49 print_func<print_csrc>(&power::do_print_csrc).
50 print_func<print_python>(&power::do_print_python).
51 print_func<print_python_repr>(&power::do_print_python_repr))
53 typedef std::vector<int> intvector;
56 // default constructor
59 power::power() : inherited(TINFO_power) { }
71 power::power(const archive_node &n, lst &sym_lst) : inherited(n, sym_lst)
73 n.find_ex("basis", basis, sym_lst);
74 n.find_ex("exponent", exponent, sym_lst);
77 void power::archive(archive_node &n) const
79 inherited::archive(n);
80 n.add_ex("basis", basis);
81 n.add_ex("exponent", exponent);
84 DEFAULT_UNARCHIVE(power)
87 // functions overriding virtual functions from base classes
92 void power::print_power(const print_context & c, const char *powersymbol, const char *openbrace, const char *closebrace, unsigned level) const
94 // Ordinary output of powers using '^' or '**'
95 if (precedence() <= level)
96 c.s << openbrace << '(';
97 basis.print(c, precedence());
100 exponent.print(c, precedence());
102 if (precedence() <= level)
103 c.s << ')' << closebrace;
106 void power::do_print_dflt(const print_dflt & c, unsigned level) const
108 if (exponent.is_equal(_ex1_2)) {
110 // Square roots are printed in a special way
116 print_power(c, "^", "", "", level);
119 void power::do_print_latex(const print_latex & c, unsigned level) const
121 if (is_exactly_a<numeric>(exponent) && ex_to<numeric>(exponent).is_negative()) {
123 // Powers with negative numeric exponents are printed as fractions
125 power(basis, -exponent).eval().print(c);
128 } else if (exponent.is_equal(_ex1_2)) {
130 // Square roots are printed in a special way
136 print_power(c, "^", "{", "}", level);
139 static void print_sym_pow(const print_context & c, const symbol &x, int exp)
141 // Optimal output of integer powers of symbols to aid compiler CSE.
142 // C.f. ISO/IEC 14882:1998, section 1.9 [intro execution], paragraph 15
143 // to learn why such a parenthesation is really necessary.
146 } else if (exp == 2) {
150 } else if (exp & 1) {
153 print_sym_pow(c, x, exp-1);
156 print_sym_pow(c, x, exp >> 1);
158 print_sym_pow(c, x, exp >> 1);
163 void power::do_print_csrc(const print_csrc & c, unsigned level) const
165 // Integer powers of symbols are printed in a special, optimized way
166 if (exponent.info(info_flags::integer)
167 && (is_a<symbol>(basis) || is_a<constant>(basis))) {
168 int exp = ex_to<numeric>(exponent).to_int();
173 if (is_a<print_csrc_cl_N>(c))
178 print_sym_pow(c, ex_to<symbol>(basis), exp);
181 // <expr>^-1 is printed as "1.0/<expr>" or with the recip() function of CLN
182 } else if (exponent.is_equal(_ex_1)) {
183 if (is_a<print_csrc_cl_N>(c))
190 // Otherwise, use the pow() or expt() (CLN) functions
192 if (is_a<print_csrc_cl_N>(c))
203 void power::do_print_python(const print_python & c, unsigned level) const
205 print_power(c, "**", "", "", level);
208 void power::do_print_python_repr(const print_python_repr & c, unsigned level) const
210 c.s << class_name() << '(';
217 bool power::info(unsigned inf) const
220 case info_flags::polynomial:
221 case info_flags::integer_polynomial:
222 case info_flags::cinteger_polynomial:
223 case info_flags::rational_polynomial:
224 case info_flags::crational_polynomial:
225 return exponent.info(info_flags::nonnegint);
226 case info_flags::rational_function:
227 return exponent.info(info_flags::integer);
228 case info_flags::algebraic:
229 return (!exponent.info(info_flags::integer) ||
232 return inherited::info(inf);
235 size_t power::nops() const
240 ex power::op(size_t i) const
244 return i==0 ? basis : exponent;
247 ex power::map(map_function & f) const
249 return (new power(f(basis), f(exponent)))->setflag(status_flags::dynallocated);
252 int power::degree(const ex & s) const
254 if (is_equal(ex_to<basic>(s)))
256 else if (is_exactly_a<numeric>(exponent) && ex_to<numeric>(exponent).is_integer()) {
257 if (basis.is_equal(s))
258 return ex_to<numeric>(exponent).to_int();
260 return basis.degree(s) * ex_to<numeric>(exponent).to_int();
261 } else if (basis.has(s))
262 throw(std::runtime_error("power::degree(): undefined degree because of non-integer exponent"));
267 int power::ldegree(const ex & s) const
269 if (is_equal(ex_to<basic>(s)))
271 else if (is_exactly_a<numeric>(exponent) && ex_to<numeric>(exponent).is_integer()) {
272 if (basis.is_equal(s))
273 return ex_to<numeric>(exponent).to_int();
275 return basis.ldegree(s) * ex_to<numeric>(exponent).to_int();
276 } else if (basis.has(s))
277 throw(std::runtime_error("power::ldegree(): undefined degree because of non-integer exponent"));
282 ex power::coeff(const ex & s, int n) const
284 if (is_equal(ex_to<basic>(s)))
285 return n==1 ? _ex1 : _ex0;
286 else if (!basis.is_equal(s)) {
287 // basis not equal to s
294 if (is_exactly_a<numeric>(exponent) && ex_to<numeric>(exponent).is_integer()) {
296 int int_exp = ex_to<numeric>(exponent).to_int();
302 // non-integer exponents are treated as zero
311 /** Perform automatic term rewriting rules in this class. In the following
312 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
313 * stand for such expressions that contain a plain number.
314 * - ^(x,0) -> 1 (also handles ^(0,0))
316 * - ^(0,c) -> 0 or exception (depending on the real part of c)
318 * - ^(c1,c2) -> *(c1^n,c1^(c2-n)) (so that 0<(c2-n)<1, try to evaluate roots, possibly in numerator and denominator of c1)
319 * - ^(^(x,c1),c2) -> ^(x,c1*c2) (c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
320 * - ^(*(x,y,z),c) -> *(x^c,y^c,z^c) (if c integer)
321 * - ^(*(x,c1),c2) -> ^(x,c2)*c1^c2 (c1>0)
322 * - ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1<0)
324 * @param level cut-off in recursive evaluation */
325 ex power::eval(int level) const
327 if ((level==1) && (flags & status_flags::evaluated))
329 else if (level == -max_recursion_level)
330 throw(std::runtime_error("max recursion level reached"));
332 const ex & ebasis = level==1 ? basis : basis.eval(level-1);
333 const ex & eexponent = level==1 ? exponent : exponent.eval(level-1);
335 bool basis_is_numerical = false;
336 bool exponent_is_numerical = false;
337 const numeric *num_basis;
338 const numeric *num_exponent;
340 if (is_exactly_a<numeric>(ebasis)) {
341 basis_is_numerical = true;
342 num_basis = &ex_to<numeric>(ebasis);
344 if (is_exactly_a<numeric>(eexponent)) {
345 exponent_is_numerical = true;
346 num_exponent = &ex_to<numeric>(eexponent);
349 // ^(x,0) -> 1 (0^0 also handled here)
350 if (eexponent.is_zero()) {
351 if (ebasis.is_zero())
352 throw (std::domain_error("power::eval(): pow(0,0) is undefined"));
358 if (eexponent.is_equal(_ex1))
361 // ^(0,c1) -> 0 or exception (depending on real value of c1)
362 if (ebasis.is_zero() && exponent_is_numerical) {
363 if ((num_exponent->real()).is_zero())
364 throw (std::domain_error("power::eval(): pow(0,I) is undefined"));
365 else if ((num_exponent->real()).is_negative())
366 throw (pole_error("power::eval(): division by zero",1));
372 if (ebasis.is_equal(_ex1))
375 if (exponent_is_numerical) {
377 // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
378 // except if c1,c2 are rational, but c1^c2 is not)
379 if (basis_is_numerical) {
380 const bool basis_is_crational = num_basis->is_crational();
381 const bool exponent_is_crational = num_exponent->is_crational();
382 if (!basis_is_crational || !exponent_is_crational) {
383 // return a plain float
384 return (new numeric(num_basis->power(*num_exponent)))->setflag(status_flags::dynallocated |
385 status_flags::evaluated |
386 status_flags::expanded);
389 const numeric res = num_basis->power(*num_exponent);
390 if (res.is_crational()) {
393 GINAC_ASSERT(!num_exponent->is_integer()); // has been handled by now
395 // ^(c1,n/m) -> *(c1^q,c1^(n/m-q)), 0<(n/m-q)<1, q integer
396 if (basis_is_crational && exponent_is_crational
397 && num_exponent->is_real()
398 && !num_exponent->is_integer()) {
399 const numeric n = num_exponent->numer();
400 const numeric m = num_exponent->denom();
402 numeric q = iquo(n, m, r);
403 if (r.is_negative()) {
407 if (q.is_zero()) { // the exponent was in the allowed range 0<(n/m)<1
408 if (num_basis->is_rational() && !num_basis->is_integer()) {
409 // try it for numerator and denominator separately, in order to
410 // partially simplify things like (5/8)^(1/3) -> 1/2*5^(1/3)
411 const numeric bnum = num_basis->numer();
412 const numeric bden = num_basis->denom();
413 const numeric res_bnum = bnum.power(*num_exponent);
414 const numeric res_bden = bden.power(*num_exponent);
415 if (res_bnum.is_integer())
416 return (new mul(power(bden,-*num_exponent),res_bnum))->setflag(status_flags::dynallocated | status_flags::evaluated);
417 if (res_bden.is_integer())
418 return (new mul(power(bnum,*num_exponent),res_bden.inverse()))->setflag(status_flags::dynallocated | status_flags::evaluated);
422 // assemble resulting product, but allowing for a re-evaluation,
423 // because otherwise we'll end up with something like
424 // (7/8)^(4/3) -> 7/8*(1/2*7^(1/3))
425 // instead of 7/16*7^(1/3).
426 ex prod = power(*num_basis,r.div(m));
427 return prod*power(*num_basis,q);
432 // ^(^(x,c1),c2) -> ^(x,c1*c2)
433 // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
434 // case c1==1 should not happen, see below!)
435 if (is_exactly_a<power>(ebasis)) {
436 const power & sub_power = ex_to<power>(ebasis);
437 const ex & sub_basis = sub_power.basis;
438 const ex & sub_exponent = sub_power.exponent;
439 if (is_exactly_a<numeric>(sub_exponent)) {
440 const numeric & num_sub_exponent = ex_to<numeric>(sub_exponent);
441 GINAC_ASSERT(num_sub_exponent!=numeric(1));
442 if (num_exponent->is_integer() || (abs(num_sub_exponent) - _num1).is_negative())
443 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
447 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
448 if (num_exponent->is_integer() && is_exactly_a<mul>(ebasis)) {
449 return expand_mul(ex_to<mul>(ebasis), *num_exponent, 0);
452 // ^(*(...,x;c1),c2) -> *(^(*(...,x;1),c2),c1^c2) (c1, c2 numeric(), c1>0)
453 // ^(*(...,x;c1),c2) -> *(^(*(...,x;-1),c2),(-c1)^c2) (c1, c2 numeric(), c1<0)
454 if (is_exactly_a<mul>(ebasis)) {
455 GINAC_ASSERT(!num_exponent->is_integer()); // should have been handled above
456 const mul & mulref = ex_to<mul>(ebasis);
457 if (!mulref.overall_coeff.is_equal(_ex1)) {
458 const numeric & num_coeff = ex_to<numeric>(mulref.overall_coeff);
459 if (num_coeff.is_real()) {
460 if (num_coeff.is_positive()) {
461 mul *mulp = new mul(mulref);
462 mulp->overall_coeff = _ex1;
463 mulp->clearflag(status_flags::evaluated);
464 mulp->clearflag(status_flags::hash_calculated);
465 return (new mul(power(*mulp,exponent),
466 power(num_coeff,*num_exponent)))->setflag(status_flags::dynallocated);
468 GINAC_ASSERT(num_coeff.compare(_num0)<0);
469 if (!num_coeff.is_equal(_num_1)) {
470 mul *mulp = new mul(mulref);
471 mulp->overall_coeff = _ex_1;
472 mulp->clearflag(status_flags::evaluated);
473 mulp->clearflag(status_flags::hash_calculated);
474 return (new mul(power(*mulp,exponent),
475 power(abs(num_coeff),*num_exponent)))->setflag(status_flags::dynallocated);
482 // ^(nc,c1) -> ncmul(nc,nc,...) (c1 positive integer, unless nc is a matrix)
483 if (num_exponent->is_pos_integer() &&
484 ebasis.return_type() != return_types::commutative &&
485 !is_a<matrix>(ebasis)) {
486 return ncmul(exvector(num_exponent->to_int(), ebasis), true);
490 if (are_ex_trivially_equal(ebasis,basis) &&
491 are_ex_trivially_equal(eexponent,exponent)) {
494 return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
495 status_flags::evaluated);
498 ex power::evalf(int level) const
505 eexponent = exponent;
506 } else if (level == -max_recursion_level) {
507 throw(std::runtime_error("max recursion level reached"));
509 ebasis = basis.evalf(level-1);
510 if (!is_exactly_a<numeric>(exponent))
511 eexponent = exponent.evalf(level-1);
513 eexponent = exponent;
516 return power(ebasis,eexponent);
519 ex power::evalm() const
521 const ex ebasis = basis.evalm();
522 const ex eexponent = exponent.evalm();
523 if (is_a<matrix>(ebasis)) {
524 if (is_exactly_a<numeric>(eexponent)) {
525 return (new matrix(ex_to<matrix>(ebasis).pow(eexponent)))->setflag(status_flags::dynallocated);
528 return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated);
532 extern bool tryfactsubs(const ex &, const ex &, int &, lst &);
534 ex power::subs(const exmap & m, unsigned options) const
536 const ex &subsed_basis = basis.subs(m, options);
537 const ex &subsed_exponent = exponent.subs(m, options);
539 if (!are_ex_trivially_equal(basis, subsed_basis)
540 || !are_ex_trivially_equal(exponent, subsed_exponent))
541 return power(subsed_basis, subsed_exponent).subs_one_level(m, options);
543 if (!(options & subs_options::algebraic))
544 return subs_one_level(m, options);
546 for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
547 int nummatches = std::numeric_limits<int>::max();
549 if (tryfactsubs(*this, it->first, nummatches, repls))
550 return (ex_to<basic>((*this) * power(it->second.subs(ex(repls), subs_options::no_pattern) / it->first.subs(ex(repls), subs_options::no_pattern), nummatches))).subs_one_level(m, options);
553 return subs_one_level(m, options);
556 ex power::eval_ncmul(const exvector & v) const
558 return inherited::eval_ncmul(v);
561 ex power::conjugate() const
563 ex newbasis = basis.conjugate();
564 ex newexponent = exponent.conjugate();
565 if (are_ex_trivially_equal(basis, newbasis) && are_ex_trivially_equal(exponent, newexponent)) {
568 return (new power(newbasis, newexponent))->setflag(status_flags::dynallocated);
573 /** Implementation of ex::diff() for a power.
575 ex power::derivative(const symbol & s) const
577 if (exponent.info(info_flags::real)) {
578 // D(b^r) = r * b^(r-1) * D(b) (faster than the formula below)
581 newseq.push_back(expair(basis, exponent - _ex1));
582 newseq.push_back(expair(basis.diff(s), _ex1));
583 return mul(newseq, exponent);
585 // D(b^e) = b^e * (D(e)*ln(b) + e*D(b)/b)
587 add(mul(exponent.diff(s), log(basis)),
588 mul(mul(exponent, basis.diff(s)), power(basis, _ex_1))));
592 int power::compare_same_type(const basic & other) const
594 GINAC_ASSERT(is_exactly_a<power>(other));
595 const power &o = static_cast<const power &>(other);
597 int cmpval = basis.compare(o.basis);
601 return exponent.compare(o.exponent);
604 unsigned power::return_type() const
606 return basis.return_type();
609 unsigned power::return_type_tinfo() const
611 return basis.return_type_tinfo();
614 ex power::expand(unsigned options) const
616 if (options == 0 && (flags & status_flags::expanded))
619 const ex expanded_basis = basis.expand(options);
620 const ex expanded_exponent = exponent.expand(options);
622 // x^(a+b) -> x^a * x^b
623 if (is_exactly_a<add>(expanded_exponent)) {
624 const add &a = ex_to<add>(expanded_exponent);
626 distrseq.reserve(a.seq.size() + 1);
627 epvector::const_iterator last = a.seq.end();
628 epvector::const_iterator cit = a.seq.begin();
630 distrseq.push_back(power(expanded_basis, a.recombine_pair_to_ex(*cit)));
634 // Make sure that e.g. (x+y)^(2+a) expands the (x+y)^2 factor
635 if (ex_to<numeric>(a.overall_coeff).is_integer()) {
636 const numeric &num_exponent = ex_to<numeric>(a.overall_coeff);
637 int int_exponent = num_exponent.to_int();
638 if (int_exponent > 0 && is_exactly_a<add>(expanded_basis))
639 distrseq.push_back(expand_add(ex_to<add>(expanded_basis), int_exponent, options));
641 distrseq.push_back(power(expanded_basis, a.overall_coeff));
643 distrseq.push_back(power(expanded_basis, a.overall_coeff));
645 // Make sure that e.g. (x+y)^(1+a) -> x*(x+y)^a + y*(x+y)^a
646 ex r = (new mul(distrseq))->setflag(status_flags::dynallocated);
647 return r.expand(options);
650 if (!is_exactly_a<numeric>(expanded_exponent) ||
651 !ex_to<numeric>(expanded_exponent).is_integer()) {
652 if (are_ex_trivially_equal(basis,expanded_basis) && are_ex_trivially_equal(exponent,expanded_exponent)) {
655 return (new power(expanded_basis,expanded_exponent))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
659 // integer numeric exponent
660 const numeric & num_exponent = ex_to<numeric>(expanded_exponent);
661 int int_exponent = num_exponent.to_int();
664 if (int_exponent > 0 && is_exactly_a<add>(expanded_basis))
665 return expand_add(ex_to<add>(expanded_basis), int_exponent, options);
667 // (x*y)^n -> x^n * y^n
668 if (is_exactly_a<mul>(expanded_basis))
669 return expand_mul(ex_to<mul>(expanded_basis), num_exponent, options, true);
671 // cannot expand further
672 if (are_ex_trivially_equal(basis,expanded_basis) && are_ex_trivially_equal(exponent,expanded_exponent))
675 return (new power(expanded_basis,expanded_exponent))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
679 // new virtual functions which can be overridden by derived classes
685 // non-virtual functions in this class
688 /** expand a^n where a is an add and n is a positive integer.
689 * @see power::expand */
690 ex power::expand_add(const add & a, int n, unsigned options) const
693 return expand_add_2(a, options);
695 const size_t m = a.nops();
697 // The number of terms will be the number of combinatorial compositions,
698 // i.e. the number of unordered arrangement of m nonnegative integers
699 // which sum up to n. It is frequently written as C_n(m) and directly
700 // related with binomial coefficients:
701 result.reserve(binomial(numeric(n+m-1), numeric(m-1)).to_int());
703 intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
704 intvector upper_limit(m-1);
707 for (size_t l=0; l<m-1; ++l) {
716 for (l=0; l<m-1; ++l) {
717 const ex & b = a.op(l);
718 GINAC_ASSERT(!is_exactly_a<add>(b));
719 GINAC_ASSERT(!is_exactly_a<power>(b) ||
720 !is_exactly_a<numeric>(ex_to<power>(b).exponent) ||
721 !ex_to<numeric>(ex_to<power>(b).exponent).is_pos_integer() ||
722 !is_exactly_a<add>(ex_to<power>(b).basis) ||
723 !is_exactly_a<mul>(ex_to<power>(b).basis) ||
724 !is_exactly_a<power>(ex_to<power>(b).basis));
725 if (is_exactly_a<mul>(b))
726 term.push_back(expand_mul(ex_to<mul>(b), numeric(k[l]), options, true));
728 term.push_back(power(b,k[l]));
731 const ex & b = a.op(l);
732 GINAC_ASSERT(!is_exactly_a<add>(b));
733 GINAC_ASSERT(!is_exactly_a<power>(b) ||
734 !is_exactly_a<numeric>(ex_to<power>(b).exponent) ||
735 !ex_to<numeric>(ex_to<power>(b).exponent).is_pos_integer() ||
736 !is_exactly_a<add>(ex_to<power>(b).basis) ||
737 !is_exactly_a<mul>(ex_to<power>(b).basis) ||
738 !is_exactly_a<power>(ex_to<power>(b).basis));
739 if (is_exactly_a<mul>(b))
740 term.push_back(expand_mul(ex_to<mul>(b), numeric(n-k_cum[m-2]), options, true));
742 term.push_back(power(b,n-k_cum[m-2]));
744 numeric f = binomial(numeric(n),numeric(k[0]));
745 for (l=1; l<m-1; ++l)
746 f *= binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
750 result.push_back(ex((new mul(term))->setflag(status_flags::dynallocated)).expand(options));
754 while ((l>=0) && ((++k[l])>upper_limit[l])) {
760 // recalc k_cum[] and upper_limit[]
761 k_cum[l] = (l==0 ? k[0] : k_cum[l-1]+k[l]);
763 for (size_t i=l+1; i<m-1; ++i)
764 k_cum[i] = k_cum[i-1]+k[i];
766 for (size_t i=l+1; i<m-1; ++i)
767 upper_limit[i] = n-k_cum[i-1];
770 return (new add(result))->setflag(status_flags::dynallocated |
771 status_flags::expanded);
775 /** Special case of power::expand_add. Expands a^2 where a is an add.
776 * @see power::expand_add */
777 ex power::expand_add_2(const add & a, unsigned options) const
780 size_t a_nops = a.nops();
781 sum.reserve((a_nops*(a_nops+1))/2);
782 epvector::const_iterator last = a.seq.end();
784 // power(+(x,...,z;c),2)=power(+(x,...,z;0),2)+2*c*+(x,...,z;0)+c*c
785 // first part: ignore overall_coeff and expand other terms
786 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
787 const ex & r = cit0->rest;
788 const ex & c = cit0->coeff;
790 GINAC_ASSERT(!is_exactly_a<add>(r));
791 GINAC_ASSERT(!is_exactly_a<power>(r) ||
792 !is_exactly_a<numeric>(ex_to<power>(r).exponent) ||
793 !ex_to<numeric>(ex_to<power>(r).exponent).is_pos_integer() ||
794 !is_exactly_a<add>(ex_to<power>(r).basis) ||
795 !is_exactly_a<mul>(ex_to<power>(r).basis) ||
796 !is_exactly_a<power>(ex_to<power>(r).basis));
798 if (c.is_equal(_ex1)) {
799 if (is_exactly_a<mul>(r)) {
800 sum.push_back(expair(expand_mul(ex_to<mul>(r), _num2, options, true),
803 sum.push_back(expair((new power(r,_ex2))->setflag(status_flags::dynallocated),
807 if (is_exactly_a<mul>(r)) {
808 sum.push_back(a.combine_ex_with_coeff_to_pair(expand_mul(ex_to<mul>(r), _num2, options, true),
809 ex_to<numeric>(c).power_dyn(_num2)));
811 sum.push_back(a.combine_ex_with_coeff_to_pair((new power(r,_ex2))->setflag(status_flags::dynallocated),
812 ex_to<numeric>(c).power_dyn(_num2)));
816 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
817 const ex & r1 = cit1->rest;
818 const ex & c1 = cit1->coeff;
819 sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
820 _num2.mul(ex_to<numeric>(c)).mul_dyn(ex_to<numeric>(c1))));
824 GINAC_ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
826 // second part: add terms coming from overall_factor (if != 0)
827 if (!a.overall_coeff.is_zero()) {
828 epvector::const_iterator i = a.seq.begin(), end = a.seq.end();
830 sum.push_back(a.combine_pair_with_coeff_to_pair(*i, ex_to<numeric>(a.overall_coeff).mul_dyn(_num2)));
833 sum.push_back(expair(ex_to<numeric>(a.overall_coeff).power_dyn(_num2),_ex1));
836 GINAC_ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
838 return (new add(sum))->setflag(status_flags::dynallocated | status_flags::expanded);
841 /** Expand factors of m in m^n where m is a mul and n is and integer.
842 * @see power::expand */
843 ex power::expand_mul(const mul & m, const numeric & n, unsigned options, bool from_expand) const
845 GINAC_ASSERT(n.is_integer());
851 distrseq.reserve(m.seq.size());
852 bool need_reexpand = false;
854 epvector::const_iterator last = m.seq.end();
855 epvector::const_iterator cit = m.seq.begin();
857 if (is_exactly_a<numeric>(cit->rest)) {
858 distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit, n));
860 // it is safe not to call mul::combine_pair_with_coeff_to_pair()
861 // since n is an integer
862 numeric new_coeff = ex_to<numeric>(cit->coeff).mul(n);
863 if (from_expand && is_exactly_a<add>(cit->rest) && new_coeff.is_pos_integer()) {
864 // this happens when e.g. (a+b)^(1/2) gets squared and
865 // the resulting product needs to be reexpanded
866 need_reexpand = true;
868 distrseq.push_back(expair(cit->rest, new_coeff));
873 const mul & result = static_cast<const mul &>((new mul(distrseq, ex_to<numeric>(m.overall_coeff).power_dyn(n)))->setflag(status_flags::dynallocated));
875 return ex(result).expand(options);
877 return result.setflag(status_flags::expanded);