3 * Implementation of GiNaC's symbolic exponentiation (basis^exponent).
5 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "expairseq.h"
31 #include "relational.h"
34 typedef vector<int> intvector;
37 // default constructor, destructor, copy constructor assignment operator and helpers
42 power::power() : basic(TINFO_power)
44 debugmsg("power default constructor",LOGLEVEL_CONSTRUCT);
49 debugmsg("power destructor",LOGLEVEL_DESTRUCT);
53 power::power(power const & other)
55 debugmsg("power copy constructor",LOGLEVEL_CONSTRUCT);
59 power const & power::operator=(power const & other)
61 debugmsg("power operator=",LOGLEVEL_ASSIGNMENT);
71 void power::copy(power const & other)
75 exponent=other.exponent;
78 void power::destroy(bool call_parent)
80 if (call_parent) basic::destroy(call_parent);
89 power::power(ex const & lh, ex const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
91 debugmsg("power constructor from ex,ex",LOGLEVEL_CONSTRUCT);
92 ASSERT(basis.return_type()==return_types::commutative);
95 power::power(ex const & lh, numeric const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
97 debugmsg("power constructor from ex,numeric",LOGLEVEL_CONSTRUCT);
98 ASSERT(basis.return_type()==return_types::commutative);
102 // functions overriding virtual functions from bases classes
107 basic * power::duplicate() const
109 debugmsg("power duplicate",LOGLEVEL_DUPLICATE);
110 return new power(*this);
113 bool power::info(unsigned inf) const
115 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial) {
116 return exponent.info(info_flags::nonnegint);
117 } else if (inf==info_flags::rational_function) {
118 return exponent.info(info_flags::integer);
120 return basic::info(inf);
124 int power::nops() const
129 ex & power::let_op(int const i)
134 return i==0 ? basis : exponent;
137 int power::degree(symbol const & s) const
139 if (is_exactly_of_type(*exponent.bp,numeric)) {
140 if ((*basis.bp).compare(s)==0)
141 return ex_to_numeric(exponent).to_int();
143 return basis.degree(s) * ex_to_numeric(exponent).to_int();
148 int power::ldegree(symbol const & s) const
150 if (is_exactly_of_type(*exponent.bp,numeric)) {
151 if ((*basis.bp).compare(s)==0)
152 return ex_to_numeric(exponent).to_int();
154 return basis.ldegree(s) * ex_to_numeric(exponent).to_int();
159 ex power::coeff(symbol const & s, int const n) const
161 if ((*basis.bp).compare(s)!=0) {
162 // basis not equal to s
168 } else if (is_exactly_of_type(*exponent.bp,numeric)&&
169 (static_cast<numeric const &>(*exponent.bp).compare(numeric(n))==0)) {
176 ex power::eval(int level) const
178 // simplifications: ^(x,0) -> 1 (0^0 handled here)
180 // ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
182 // ^(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)
183 // ^(^(x,c1),c2) -> ^(x,c1*c2) (c1, c2 numeric(), c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
184 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
185 // ^(*(x,c1),c2) -> ^(x,c2)*c1^c2 (c1, c2 numeric(), c1>0)
186 // ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1, c2 numeric(), c1<0)
188 debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
190 if ((level==1)&&(flags & status_flags::evaluated)) {
192 } else if (level == -max_recursion_level) {
193 throw(std::runtime_error("max recursion level reached"));
196 ex const & ebasis = level==1 ? basis : basis.eval(level-1);
197 ex const & eexponent = level==1 ? exponent : exponent.eval(level-1);
199 bool basis_is_numerical=0;
200 bool exponent_is_numerical=0;
202 numeric * num_exponent;
204 if (is_exactly_of_type(*ebasis.bp,numeric)) {
205 basis_is_numerical=1;
206 num_basis=static_cast<numeric *>(ebasis.bp);
208 if (is_exactly_of_type(*eexponent.bp,numeric)) {
209 exponent_is_numerical=1;
210 num_exponent=static_cast<numeric *>(eexponent.bp);
213 // ^(x,0) -> 1 (0^0 also handled here)
214 if (eexponent.is_zero())
218 if (eexponent.is_equal(exONE()))
221 // ^(0,x) -> 0 (except if x is real and negative)
222 if (ebasis.is_zero()) {
223 if (exponent_is_numerical && num_exponent->is_negative()) {
224 throw(std::overflow_error("power::eval(): division by zero"));
230 if (ebasis.is_equal(exONE()))
233 if (basis_is_numerical && exponent_is_numerical) {
234 // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
235 // except if c1,c2 are rational, but c1^c2 is not)
236 bool basis_is_rational = num_basis->is_rational();
237 bool exponent_is_rational = num_exponent->is_rational();
238 numeric res = (*num_basis).power(*num_exponent);
240 if ((!basis_is_rational || !exponent_is_rational)
241 || res.is_rational()) {
244 ASSERT(!num_exponent->is_integer()); // has been handled by now
245 // ^(c1,n/m) -> *(c1^q,c1^(n/m-q)), 0<(n/m-h)<1, q integer
246 if (basis_is_rational && exponent_is_rational
247 && num_exponent->is_real()
248 && !num_exponent->is_integer()) {
250 n = num_exponent->numer();
251 m = num_exponent->denom();
253 if (r.is_negative()) {
257 if (q.is_zero()) // the exponent was in the allowed range 0<(n/m)<1
261 res.push_back(expair(ebasis,r.div(m)));
262 res.push_back(expair(ex(num_basis->power(q)),exONE()));
263 return (new mul(res))->setflag(status_flags::dynallocated | status_flags::evaluated);
264 /*return mul(num_basis->power(q),
265 power(ex(*num_basis),ex(r.div(m)))).hold();
267 /* return (new mul(num_basis->power(q),
268 power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
274 // ^(^(x,c1),c2) -> ^(x,c1*c2)
275 // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
276 // case c1=1 should not happen, see below!)
277 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,power)) {
278 power const & sub_power=ex_to_power(ebasis);
279 ex const & sub_basis=sub_power.basis;
280 ex const & sub_exponent=sub_power.exponent;
281 if (is_ex_exactly_of_type(sub_exponent,numeric)) {
282 numeric const & num_sub_exponent=ex_to_numeric(sub_exponent);
283 ASSERT(num_sub_exponent!=numeric(1));
284 if (num_exponent->is_integer() || abs(num_sub_exponent)<1) {
285 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
290 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
291 if (exponent_is_numerical && num_exponent->is_integer() &&
292 is_ex_exactly_of_type(ebasis,mul)) {
293 return expand_mul(ex_to_mul(ebasis), *num_exponent);
296 // ^(*(...,x;c1),c2) -> ^(*(...,x;1),c2)*c1^c2 (c1, c2 numeric(), c1>0)
297 // ^(*(...,x,c1),c2) -> ^(*(...,x;-1),c2)*(-c1)^c2 (c1, c2 numeric(), c1<0)
298 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,mul)) {
299 ASSERT(!num_exponent->is_integer()); // should have been handled above
300 mul const & mulref=ex_to_mul(ebasis);
301 if (!mulref.overall_coeff.is_equal(exONE())) {
302 numeric const & num_coeff=ex_to_numeric(mulref.overall_coeff);
303 if (num_coeff.is_real()) {
304 if (num_coeff.is_positive()>0) {
305 mul * mulp=new mul(mulref);
306 mulp->overall_coeff=exONE();
307 mulp->clearflag(status_flags::evaluated);
308 mulp->clearflag(status_flags::hash_calculated);
309 return (new mul(power(*mulp,exponent),
310 power(num_coeff,*num_exponent)))->
311 setflag(status_flags::dynallocated);
313 ASSERT(num_coeff.compare(numZERO())<0);
314 if (num_coeff.compare(numMINUSONE())!=0) {
315 mul * mulp=new mul(mulref);
316 mulp->overall_coeff=exMINUSONE();
317 mulp->clearflag(status_flags::evaluated);
318 mulp->clearflag(status_flags::hash_calculated);
319 return (new mul(power(*mulp,exponent),
320 power(abs(num_coeff),*num_exponent)))->
321 setflag(status_flags::dynallocated);
328 if (are_ex_trivially_equal(ebasis,basis) &&
329 are_ex_trivially_equal(eexponent,exponent)) {
332 return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
333 status_flags::evaluated);
336 ex power::evalf(int level) const
338 debugmsg("power evalf",LOGLEVEL_MEMBER_FUNCTION);
346 } else if (level == -max_recursion_level) {
347 throw(std::runtime_error("max recursion level reached"));
349 ebasis=basis.evalf(level-1);
350 eexponent=exponent.evalf(level-1);
353 return power(ebasis,eexponent);
356 ex power::subs(lst const & ls, lst const & lr) const
358 ex const & subsed_basis=basis.subs(ls,lr);
359 ex const & subsed_exponent=exponent.subs(ls,lr);
361 if (are_ex_trivially_equal(basis,subsed_basis)&&
362 are_ex_trivially_equal(exponent,subsed_exponent)) {
366 return power(subsed_basis, subsed_exponent);
369 ex power::simplify_ncmul(exvector const & v) const
371 return basic::simplify_ncmul(v);
376 int power::compare_same_type(basic const & other) const
378 ASSERT(is_exactly_of_type(other, power));
379 power const & o=static_cast<power const &>(const_cast<basic &>(other));
382 cmpval=basis.compare(o.basis);
384 return exponent.compare(o.exponent);
389 unsigned power::return_type(void) const
391 return basis.return_type();
394 unsigned power::return_type_tinfo(void) const
396 return basis.return_type_tinfo();
399 ex power::expand(unsigned options) const
401 ex expanded_basis=basis.expand(options);
403 if (!is_ex_exactly_of_type(exponent,numeric)||
404 !ex_to_numeric(exponent).is_integer()) {
405 if (are_ex_trivially_equal(basis,expanded_basis)) {
408 return (new power(expanded_basis,exponent))->
409 setflag(status_flags::dynallocated);
413 // integer numeric exponent
414 numeric const & num_exponent=ex_to_numeric(exponent);
415 int int_exponent = num_exponent.to_int();
417 if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
418 return expand_add(ex_to_add(expanded_basis), int_exponent);
421 if (is_ex_exactly_of_type(expanded_basis,mul)) {
422 return expand_mul(ex_to_mul(expanded_basis), num_exponent);
425 // cannot expand further
426 if (are_ex_trivially_equal(basis,expanded_basis)) {
429 return (new power(expanded_basis,exponent))->
430 setflag(status_flags::dynallocated);
435 // new virtual functions which can be overridden by derived classes
441 // non-virtual functions in this class
444 ex power::expand_add(add const & a, int const n) const
446 // expand a^n where a is an add and n is an integer
449 return expand_add_2(a);
454 sum.reserve((n+1)*(m-1));
456 intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
457 intvector upper_limit(m-1);
460 for (int l=0; l<m-1; l++) {
469 for (l=0; l<m-1; l++) {
470 ex const & b=a.op(l);
471 ASSERT(!is_ex_exactly_of_type(b,add));
472 ASSERT(!is_ex_exactly_of_type(b,power)||
473 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
474 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
475 if (is_ex_exactly_of_type(b,mul)) {
476 term.push_back(expand_mul(ex_to_mul(b),numeric(k[l])));
478 term.push_back(power(b,k[l]));
482 ex const & b=a.op(l);
483 ASSERT(!is_ex_exactly_of_type(b,add));
484 ASSERT(!is_ex_exactly_of_type(b,power)||
485 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
486 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
487 if (is_ex_exactly_of_type(b,mul)) {
488 term.push_back(expand_mul(ex_to_mul(b),numeric(n-k_cum[m-2])));
490 term.push_back(power(b,n-k_cum[m-2]));
493 numeric f=binomial(numeric(n),numeric(k[0]));
494 for (l=1; l<m-1; l++) {
495 f=f*binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
500 cout << "begin term" << endl;
501 for (int i=0; i<m-1; i++) {
502 cout << "k[" << i << "]=" << k[i] << endl;
503 cout << "k_cum[" << i << "]=" << k_cum[i] << endl;
504 cout << "upper_limit[" << i << "]=" << upper_limit[i] << endl;
506 for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
507 cout << *cit << endl;
509 cout << "end term" << endl;
512 // TODO: optimize!!!!!!!!
513 sum.push_back((new mul(term))->setflag(status_flags::dynallocated));
517 while ((l>=0)&&((++k[l])>upper_limit[l])) {
523 // recalc k_cum[] and upper_limit[]
527 k_cum[l]=k_cum[l-1]+k[l];
529 for (int i=l+1; i<m-1; i++) {
530 k_cum[i]=k_cum[i-1]+k[i];
533 for (int i=l+1; i<m-1; i++) {
534 upper_limit[i]=n-k_cum[i-1];
537 return (new add(sum))->setflag(status_flags::dynallocated);
541 ex power::expand_add_2(add const & a) const
543 // special case: expand a^2 where a is an add
546 sum.reserve((a.seq.size()*(a.seq.size()+1))/2);
547 epvector::const_iterator last=a.seq.end();
549 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
550 ex const & b=a.recombine_pair_to_ex(*cit0);
551 ASSERT(!is_ex_exactly_of_type(b,add));
552 ASSERT(!is_ex_exactly_of_type(b,power)||
553 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
554 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
555 if (is_ex_exactly_of_type(b,mul)) {
556 sum.push_back(a.split_ex_to_pair(expand_mul(ex_to_mul(b),numTWO())));
558 sum.push_back(a.split_ex_to_pair((new power(b,exTWO()))->
559 setflag(status_flags::dynallocated)));
561 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
562 sum.push_back(a.split_ex_to_pair((new mul(a.recombine_pair_to_ex(*cit0),
563 a.recombine_pair_to_ex(*cit1)))->
564 setflag(status_flags::dynallocated),
569 ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
571 return (new add(sum))->setflag(status_flags::dynallocated);
575 ex power::expand_add_2(add const & a) const
577 // special case: expand a^2 where a is an add
580 unsigned a_nops=a.nops();
581 sum.reserve((a_nops*(a_nops+1))/2);
582 epvector::const_iterator last=a.seq.end();
584 // power(+(x,...,z;c),2)=power(+(x,...,z;0),2)+2*c*+(x,...,z;0)+c*c
585 // first part: ignore overall_coeff and expand other terms
586 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
587 ex const & r=(*cit0).rest;
588 ex const & c=(*cit0).coeff;
590 ASSERT(!is_ex_exactly_of_type(r,add));
591 ASSERT(!is_ex_exactly_of_type(r,power)||
592 !is_ex_exactly_of_type(ex_to_power(r).exponent,numeric)||
593 !ex_to_numeric(ex_to_power(r).exponent).is_pos_integer()||
594 !is_ex_exactly_of_type(ex_to_power(r).basis,add)||
595 !is_ex_exactly_of_type(ex_to_power(r).basis,mul)||
596 !is_ex_exactly_of_type(ex_to_power(r).basis,power));
598 if (are_ex_trivially_equal(c,exONE())) {
599 if (is_ex_exactly_of_type(r,mul)) {
600 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),exONE()));
602 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
606 if (is_ex_exactly_of_type(r,mul)) {
607 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),
608 ex_to_numeric(c).power_dyn(numTWO())));
610 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
611 ex_to_numeric(c).power_dyn(numTWO())));
615 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
616 ex const & r1=(*cit1).rest;
617 ex const & c1=(*cit1).coeff;
618 sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
619 numTWO().mul(ex_to_numeric(c)).mul_dyn(ex_to_numeric(c1))));
623 ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
625 // second part: add terms coming from overall_factor (if != 0)
626 if (!a.overall_coeff.is_equal(exZERO())) {
627 for (epvector::const_iterator cit=a.seq.begin(); cit!=a.seq.end(); ++cit) {
628 sum.push_back(a.combine_pair_with_coeff_to_pair(*cit,ex_to_numeric(a.overall_coeff).mul_dyn(numTWO())));
630 sum.push_back(expair(ex_to_numeric(a.overall_coeff).power_dyn(numTWO()),exONE()));
633 ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
635 return (new add(sum))->setflag(status_flags::dynallocated);
638 ex power::expand_mul(mul const & m, numeric const & n) const
640 // expand m^n where m is a mul and n is and integer
642 if (n.is_equal(numZERO())) {
647 distrseq.reserve(m.seq.size());
648 epvector::const_iterator last=m.seq.end();
649 epvector::const_iterator cit=m.seq.begin();
651 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
652 distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
654 // it is safe not to call mul::combine_pair_with_coeff_to_pair()
655 // since n is an integer
656 distrseq.push_back(expair((*cit).rest,
657 ex_to_numeric((*cit).coeff).mul(n)));
661 return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
662 ->setflag(status_flags::dynallocated);
666 ex power::expand_commutative_3(ex const & basis, numeric const & exponent,
667 unsigned options) const
674 add const & addref=static_cast<add const &>(*basis.bp);
678 ex first_operands=add(splitseq);
679 ex last_operand=addref.recombine_pair_to_ex(*(addref.seq.end()-1));
681 int n=exponent.to_int();
682 for (int k=0; k<=n; k++) {
683 distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))*
684 power(last_operand,numeric(n-k)));
686 return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
687 status_flags::expanded |
688 status_flags::dynallocated )).
694 ex power::expand_noncommutative(ex const & basis, numeric const & exponent,
695 unsigned options) const
697 ex rest_power=ex(power(basis,exponent.add(numMINUSONE()))).
698 expand(options | expand_options::internal_do_not_expand_power_operands);
700 return ex(mul(rest_power,basis),0).
701 expand(options | expand_options::internal_do_not_expand_mul_operands);
706 // static member variables
711 unsigned power::precedence=60;
717 const power some_power;
718 type_info const & typeid_power=typeid(some_power);