3 * Implementation of GiNaC's symbolic exponentiation (basis^exponent). */
6 * GiNaC Copyright (C) 1999 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
28 #include "expairseq.h"
32 #include "relational.h"
38 typedef vector<int> intvector;
41 // default constructor, destructor, copy constructor assignment operator and helpers
46 power::power() : basic(TINFO_power)
48 debugmsg("power default constructor",LOGLEVEL_CONSTRUCT);
53 debugmsg("power destructor",LOGLEVEL_DESTRUCT);
57 power::power(power const & other)
59 debugmsg("power copy constructor",LOGLEVEL_CONSTRUCT);
63 power const & power::operator=(power const & other)
65 debugmsg("power operator=",LOGLEVEL_ASSIGNMENT);
75 void power::copy(power const & other)
79 exponent=other.exponent;
82 void power::destroy(bool call_parent)
84 if (call_parent) basic::destroy(call_parent);
93 power::power(ex const & lh, ex const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
95 debugmsg("power constructor from ex,ex",LOGLEVEL_CONSTRUCT);
96 GINAC_ASSERT(basis.return_type()==return_types::commutative);
99 power::power(ex const & lh, numeric const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
101 debugmsg("power constructor from ex,numeric",LOGLEVEL_CONSTRUCT);
102 GINAC_ASSERT(basis.return_type()==return_types::commutative);
106 // functions overriding virtual functions from bases classes
111 basic * power::duplicate() const
113 debugmsg("power duplicate",LOGLEVEL_DUPLICATE);
114 return new power(*this);
117 bool power::info(unsigned inf) const
119 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial) {
120 return exponent.info(info_flags::nonnegint);
121 } else if (inf==info_flags::rational_function) {
122 return exponent.info(info_flags::integer);
124 return basic::info(inf);
128 int power::nops() const
133 ex & power::let_op(int const i)
138 return i==0 ? basis : exponent;
141 int power::degree(symbol const & s) const
143 if (is_exactly_of_type(*exponent.bp,numeric)) {
144 if ((*basis.bp).compare(s)==0)
145 return ex_to_numeric(exponent).to_int();
147 return basis.degree(s) * ex_to_numeric(exponent).to_int();
152 int power::ldegree(symbol const & s) const
154 if (is_exactly_of_type(*exponent.bp,numeric)) {
155 if ((*basis.bp).compare(s)==0)
156 return ex_to_numeric(exponent).to_int();
158 return basis.ldegree(s) * ex_to_numeric(exponent).to_int();
163 ex power::coeff(symbol const & s, int const n) const
165 if ((*basis.bp).compare(s)!=0) {
166 // basis not equal to s
172 } else if (is_exactly_of_type(*exponent.bp,numeric)&&
173 (static_cast<numeric const &>(*exponent.bp).compare(numeric(n))==0)) {
180 ex power::eval(int level) const
182 // simplifications: ^(x,0) -> 1 (0^0 handled here)
184 // ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
186 // ^(c1,c2) -> *(c1^n,c1^(c2-n)) (c1, c2 numeric(), 0<(c2-n)<1 except if c1,c2 are rational, but c1^c2 is not)
187 // ^(^(x,c1),c2) -> ^(x,c1*c2) (c1, c2 numeric(), c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
188 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
189 // ^(*(x,c1),c2) -> ^(x,c2)*c1^c2 (c1, c2 numeric(), c1>0)
190 // ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1, c2 numeric(), c1<0)
192 debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
194 if ((level==1)&&(flags & status_flags::evaluated)) {
196 } else if (level == -max_recursion_level) {
197 throw(std::runtime_error("max recursion level reached"));
200 ex const & ebasis = level==1 ? basis : basis.eval(level-1);
201 ex const & eexponent = level==1 ? exponent : exponent.eval(level-1);
203 bool basis_is_numerical=0;
204 bool exponent_is_numerical=0;
206 numeric * num_exponent;
208 if (is_exactly_of_type(*ebasis.bp,numeric)) {
209 basis_is_numerical=1;
210 num_basis=static_cast<numeric *>(ebasis.bp);
212 if (is_exactly_of_type(*eexponent.bp,numeric)) {
213 exponent_is_numerical=1;
214 num_exponent=static_cast<numeric *>(eexponent.bp);
217 // ^(x,0) -> 1 (0^0 also handled here)
218 if (eexponent.is_zero())
222 if (eexponent.is_equal(exONE()))
225 // ^(0,x) -> 0 (except if x is real and negative)
226 if (ebasis.is_zero()) {
227 if (exponent_is_numerical && num_exponent->is_negative()) {
228 throw(std::overflow_error("power::eval(): division by zero"));
234 if (ebasis.is_equal(exONE()))
237 if (basis_is_numerical && exponent_is_numerical) {
238 // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
239 // except if c1,c2 are rational, but c1^c2 is not)
240 bool basis_is_rational = num_basis->is_rational();
241 bool exponent_is_rational = num_exponent->is_rational();
242 numeric res = (*num_basis).power(*num_exponent);
244 if ((!basis_is_rational || !exponent_is_rational)
245 || res.is_rational()) {
248 GINAC_ASSERT(!num_exponent->is_integer()); // has been handled by now
249 // ^(c1,n/m) -> *(c1^q,c1^(n/m-q)), 0<(n/m-h)<1, q integer
250 if (basis_is_rational && exponent_is_rational
251 && num_exponent->is_real()
252 && !num_exponent->is_integer()) {
254 n = num_exponent->numer();
255 m = num_exponent->denom();
257 if (r.is_negative()) {
261 if (q.is_zero()) // the exponent was in the allowed range 0<(n/m)<1
265 res.push_back(expair(ebasis,r.div(m)));
266 res.push_back(expair(ex(num_basis->power(q)),exONE()));
267 return (new mul(res))->setflag(status_flags::dynallocated | status_flags::evaluated);
268 /*return mul(num_basis->power(q),
269 power(ex(*num_basis),ex(r.div(m)))).hold();
271 /* return (new mul(num_basis->power(q),
272 power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
278 // ^(^(x,c1),c2) -> ^(x,c1*c2)
279 // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
280 // case c1=1 should not happen, see below!)
281 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,power)) {
282 power const & sub_power=ex_to_power(ebasis);
283 ex const & sub_basis=sub_power.basis;
284 ex const & sub_exponent=sub_power.exponent;
285 if (is_ex_exactly_of_type(sub_exponent,numeric)) {
286 numeric const & num_sub_exponent=ex_to_numeric(sub_exponent);
287 GINAC_ASSERT(num_sub_exponent!=numeric(1));
288 if (num_exponent->is_integer() || abs(num_sub_exponent)<1) {
289 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
294 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
295 if (exponent_is_numerical && num_exponent->is_integer() &&
296 is_ex_exactly_of_type(ebasis,mul)) {
297 return expand_mul(ex_to_mul(ebasis), *num_exponent);
300 // ^(*(...,x;c1),c2) -> ^(*(...,x;1),c2)*c1^c2 (c1, c2 numeric(), c1>0)
301 // ^(*(...,x,c1),c2) -> ^(*(...,x;-1),c2)*(-c1)^c2 (c1, c2 numeric(), c1<0)
302 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,mul)) {
303 GINAC_ASSERT(!num_exponent->is_integer()); // should have been handled above
304 mul const & mulref=ex_to_mul(ebasis);
305 if (!mulref.overall_coeff.is_equal(exONE())) {
306 numeric const & num_coeff=ex_to_numeric(mulref.overall_coeff);
307 if (num_coeff.is_real()) {
308 if (num_coeff.is_positive()>0) {
309 mul * mulp=new mul(mulref);
310 mulp->overall_coeff=exONE();
311 mulp->clearflag(status_flags::evaluated);
312 mulp->clearflag(status_flags::hash_calculated);
313 return (new mul(power(*mulp,exponent),
314 power(num_coeff,*num_exponent)))->
315 setflag(status_flags::dynallocated);
317 GINAC_ASSERT(num_coeff.compare(numZERO())<0);
318 if (num_coeff.compare(numMINUSONE())!=0) {
319 mul * mulp=new mul(mulref);
320 mulp->overall_coeff=exMINUSONE();
321 mulp->clearflag(status_flags::evaluated);
322 mulp->clearflag(status_flags::hash_calculated);
323 return (new mul(power(*mulp,exponent),
324 power(abs(num_coeff),*num_exponent)))->
325 setflag(status_flags::dynallocated);
332 if (are_ex_trivially_equal(ebasis,basis) &&
333 are_ex_trivially_equal(eexponent,exponent)) {
336 return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
337 status_flags::evaluated);
340 ex power::evalf(int level) const
342 debugmsg("power evalf",LOGLEVEL_MEMBER_FUNCTION);
350 } else if (level == -max_recursion_level) {
351 throw(std::runtime_error("max recursion level reached"));
353 ebasis=basis.evalf(level-1);
354 eexponent=exponent.evalf(level-1);
357 return power(ebasis,eexponent);
360 ex power::subs(lst const & ls, lst const & lr) const
362 ex const & subsed_basis=basis.subs(ls,lr);
363 ex const & subsed_exponent=exponent.subs(ls,lr);
365 if (are_ex_trivially_equal(basis,subsed_basis)&&
366 are_ex_trivially_equal(exponent,subsed_exponent)) {
370 return power(subsed_basis, subsed_exponent);
373 ex power::simplify_ncmul(exvector const & v) const
375 return basic::simplify_ncmul(v);
380 int power::compare_same_type(basic const & other) const
382 GINAC_ASSERT(is_exactly_of_type(other, power));
383 power const & o=static_cast<power const &>(const_cast<basic &>(other));
386 cmpval=basis.compare(o.basis);
388 return exponent.compare(o.exponent);
393 unsigned power::return_type(void) const
395 return basis.return_type();
398 unsigned power::return_type_tinfo(void) const
400 return basis.return_type_tinfo();
403 ex power::expand(unsigned options) const
405 ex expanded_basis=basis.expand(options);
407 if (!is_ex_exactly_of_type(exponent,numeric)||
408 !ex_to_numeric(exponent).is_integer()) {
409 if (are_ex_trivially_equal(basis,expanded_basis)) {
412 return (new power(expanded_basis,exponent))->
413 setflag(status_flags::dynallocated);
417 // integer numeric exponent
418 numeric const & num_exponent=ex_to_numeric(exponent);
419 int int_exponent = num_exponent.to_int();
421 if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
422 return expand_add(ex_to_add(expanded_basis), int_exponent);
425 if (is_ex_exactly_of_type(expanded_basis,mul)) {
426 return expand_mul(ex_to_mul(expanded_basis), num_exponent);
429 // cannot expand further
430 if (are_ex_trivially_equal(basis,expanded_basis)) {
433 return (new power(expanded_basis,exponent))->
434 setflag(status_flags::dynallocated);
439 // new virtual functions which can be overridden by derived classes
445 // non-virtual functions in this class
448 ex power::expand_add(add const & a, int const n) const
450 // expand a^n where a is an add and n is an integer
453 return expand_add_2(a);
458 sum.reserve((n+1)*(m-1));
460 intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
461 intvector upper_limit(m-1);
464 for (int l=0; l<m-1; l++) {
473 for (l=0; l<m-1; l++) {
474 ex const & b=a.op(l);
475 GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
476 GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
477 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
478 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
479 if (is_ex_exactly_of_type(b,mul)) {
480 term.push_back(expand_mul(ex_to_mul(b),numeric(k[l])));
482 term.push_back(power(b,k[l]));
486 ex const & b=a.op(l);
487 GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
488 GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
489 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
490 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
491 if (is_ex_exactly_of_type(b,mul)) {
492 term.push_back(expand_mul(ex_to_mul(b),numeric(n-k_cum[m-2])));
494 term.push_back(power(b,n-k_cum[m-2]));
497 numeric f=binomial(numeric(n),numeric(k[0]));
498 for (l=1; l<m-1; l++) {
499 f=f*binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
504 cout << "begin term" << endl;
505 for (int i=0; i<m-1; i++) {
506 cout << "k[" << i << "]=" << k[i] << endl;
507 cout << "k_cum[" << i << "]=" << k_cum[i] << endl;
508 cout << "upper_limit[" << i << "]=" << upper_limit[i] << endl;
510 for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
511 cout << *cit << endl;
513 cout << "end term" << endl;
516 // TODO: optimize this
517 sum.push_back((new mul(term))->setflag(status_flags::dynallocated));
521 while ((l>=0)&&((++k[l])>upper_limit[l])) {
527 // recalc k_cum[] and upper_limit[]
531 k_cum[l]=k_cum[l-1]+k[l];
533 for (int i=l+1; i<m-1; i++) {
534 k_cum[i]=k_cum[i-1]+k[i];
537 for (int i=l+1; i<m-1; i++) {
538 upper_limit[i]=n-k_cum[i-1];
541 return (new add(sum))->setflag(status_flags::dynallocated);
545 ex power::expand_add_2(add const & a) const
547 // special case: expand a^2 where a is an add
550 sum.reserve((a.seq.size()*(a.seq.size()+1))/2);
551 epvector::const_iterator last=a.seq.end();
553 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
554 ex const & b=a.recombine_pair_to_ex(*cit0);
555 GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
556 GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
557 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
558 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
559 if (is_ex_exactly_of_type(b,mul)) {
560 sum.push_back(a.split_ex_to_pair(expand_mul(ex_to_mul(b),numTWO())));
562 sum.push_back(a.split_ex_to_pair((new power(b,exTWO()))->
563 setflag(status_flags::dynallocated)));
565 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
566 sum.push_back(a.split_ex_to_pair((new mul(a.recombine_pair_to_ex(*cit0),
567 a.recombine_pair_to_ex(*cit1)))->
568 setflag(status_flags::dynallocated),
573 GINAC_ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
575 return (new add(sum))->setflag(status_flags::dynallocated);
579 ex power::expand_add_2(add const & a) const
581 // special case: expand a^2 where a is an add
584 unsigned a_nops=a.nops();
585 sum.reserve((a_nops*(a_nops+1))/2);
586 epvector::const_iterator last=a.seq.end();
588 // power(+(x,...,z;c),2)=power(+(x,...,z;0),2)+2*c*+(x,...,z;0)+c*c
589 // first part: ignore overall_coeff and expand other terms
590 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
591 ex const & r=(*cit0).rest;
592 ex const & c=(*cit0).coeff;
594 GINAC_ASSERT(!is_ex_exactly_of_type(r,add));
595 GINAC_ASSERT(!is_ex_exactly_of_type(r,power)||
596 !is_ex_exactly_of_type(ex_to_power(r).exponent,numeric)||
597 !ex_to_numeric(ex_to_power(r).exponent).is_pos_integer()||
598 !is_ex_exactly_of_type(ex_to_power(r).basis,add)||
599 !is_ex_exactly_of_type(ex_to_power(r).basis,mul)||
600 !is_ex_exactly_of_type(ex_to_power(r).basis,power));
602 if (are_ex_trivially_equal(c,exONE())) {
603 if (is_ex_exactly_of_type(r,mul)) {
604 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),exONE()));
606 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
610 if (is_ex_exactly_of_type(r,mul)) {
611 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),
612 ex_to_numeric(c).power_dyn(numTWO())));
614 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
615 ex_to_numeric(c).power_dyn(numTWO())));
619 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
620 ex const & r1=(*cit1).rest;
621 ex const & c1=(*cit1).coeff;
622 sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
623 numTWO().mul(ex_to_numeric(c)).mul_dyn(ex_to_numeric(c1))));
627 GINAC_ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
629 // second part: add terms coming from overall_factor (if != 0)
630 if (!a.overall_coeff.is_equal(exZERO())) {
631 for (epvector::const_iterator cit=a.seq.begin(); cit!=a.seq.end(); ++cit) {
632 sum.push_back(a.combine_pair_with_coeff_to_pair(*cit,ex_to_numeric(a.overall_coeff).mul_dyn(numTWO())));
634 sum.push_back(expair(ex_to_numeric(a.overall_coeff).power_dyn(numTWO()),exONE()));
637 GINAC_ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
639 return (new add(sum))->setflag(status_flags::dynallocated);
642 ex power::expand_mul(mul const & m, numeric const & n) const
644 // expand m^n where m is a mul and n is and integer
646 if (n.is_equal(numZERO())) {
651 distrseq.reserve(m.seq.size());
652 epvector::const_iterator last=m.seq.end();
653 epvector::const_iterator cit=m.seq.begin();
655 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
656 distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
658 // it is safe not to call mul::combine_pair_with_coeff_to_pair()
659 // since n is an integer
660 distrseq.push_back(expair((*cit).rest,
661 ex_to_numeric((*cit).coeff).mul(n)));
665 return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
666 ->setflag(status_flags::dynallocated);
670 ex power::expand_commutative_3(ex const & basis, numeric const & exponent,
671 unsigned options) const
678 add const & addref=static_cast<add const &>(*basis.bp);
682 ex first_operands=add(splitseq);
683 ex last_operand=addref.recombine_pair_to_ex(*(addref.seq.end()-1));
685 int n=exponent.to_int();
686 for (int k=0; k<=n; k++) {
687 distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))*
688 power(last_operand,numeric(n-k)));
690 return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
691 status_flags::expanded |
692 status_flags::dynallocated )).
698 ex power::expand_noncommutative(ex const & basis, numeric const & exponent,
699 unsigned options) const
701 ex rest_power=ex(power(basis,exponent.add(numMINUSONE()))).
702 expand(options | expand_options::internal_do_not_expand_power_operands);
704 return ex(mul(rest_power,basis),0).
705 expand(options | expand_options::internal_do_not_expand_mul_operands);
710 // static member variables
715 unsigned power::precedence=60;
721 const power some_power;
722 type_info const & typeid_power=typeid(some_power);