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
28 typedef vector<int> intvector;
31 // default constructor, destructor, copy constructor assignment operator and helpers
36 power::power() : basic(TINFO_POWER)
38 debugmsg("power default constructor",LOGLEVEL_CONSTRUCT);
43 debugmsg("power destructor",LOGLEVEL_DESTRUCT);
47 power::power(power const & other)
49 debugmsg("power copy constructor",LOGLEVEL_CONSTRUCT);
53 power const & power::operator=(power const & other)
55 debugmsg("power operator=",LOGLEVEL_ASSIGNMENT);
65 void power::copy(power const & other)
69 exponent=other.exponent;
72 void power::destroy(bool call_parent)
74 if (call_parent) basic::destroy(call_parent);
83 power::power(ex const & lh, ex const & rh) : basic(TINFO_POWER), basis(lh), exponent(rh)
85 debugmsg("power constructor from ex,ex",LOGLEVEL_CONSTRUCT);
86 ASSERT(basis.return_type()==return_types::commutative);
89 power::power(ex const & lh, numeric const & rh) : basic(TINFO_POWER), basis(lh), exponent(rh)
91 debugmsg("power constructor from ex,numeric",LOGLEVEL_CONSTRUCT);
92 ASSERT(basis.return_type()==return_types::commutative);
96 // functions overriding virtual functions from bases classes
101 basic * power::duplicate() const
103 debugmsg("power duplicate",LOGLEVEL_DUPLICATE);
104 return new power(*this);
107 bool power::info(unsigned inf) const
109 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial) {
110 return exponent.info(info_flags::nonnegint);
111 } else if (inf==info_flags::rational_function) {
112 return exponent.info(info_flags::integer);
114 return basic::info(inf);
118 int power::nops() const
123 ex & power::let_op(int const i)
128 return i==0 ? basis : exponent;
131 int power::degree(symbol const & s) const
133 if (is_exactly_of_type(*exponent.bp,numeric)) {
134 if ((*basis.bp).compare(s)==0)
135 return ex_to_numeric(exponent).to_int();
137 return basis.degree(s) * ex_to_numeric(exponent).to_int();
142 int power::ldegree(symbol const & s) const
144 if (is_exactly_of_type(*exponent.bp,numeric)) {
145 if ((*basis.bp).compare(s)==0)
146 return ex_to_numeric(exponent).to_int();
148 return basis.ldegree(s) * ex_to_numeric(exponent).to_int();
153 ex power::coeff(symbol const & s, int const n) const
155 if ((*basis.bp).compare(s)!=0) {
156 // basis not equal to s
162 } else if (is_exactly_of_type(*exponent.bp,numeric)&&
163 (static_cast<numeric const &>(*exponent.bp).compare(numeric(n))==0)) {
170 ex power::eval(int level) const
172 // simplifications: ^(x,0) -> 1 (0^0 handled here)
174 // ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
176 // ^(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)
177 // ^(^(x,c1),c2) -> ^(x,c1*c2) (c1, c2 numeric(), c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
178 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
179 // ^(*(x,c1),c2) -> ^(x,c2)*c1^c2 (c1, c2 numeric(), c1>0)
180 // ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1, c2 numeric(), c1<0)
182 debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
184 if ((level==1)&&(flags & status_flags::evaluated)) {
186 } else if (level == -max_recursion_level) {
187 throw(std::runtime_error("max recursion level reached"));
190 ex const & ebasis = level==1 ? basis : basis.eval(level-1);
191 ex const & eexponent = level==1 ? exponent : exponent.eval(level-1);
193 bool basis_is_numerical=0;
194 bool exponent_is_numerical=0;
196 numeric * num_exponent;
198 if (is_exactly_of_type(*ebasis.bp,numeric)) {
199 basis_is_numerical=1;
200 num_basis=static_cast<numeric *>(ebasis.bp);
202 if (is_exactly_of_type(*eexponent.bp,numeric)) {
203 exponent_is_numerical=1;
204 num_exponent=static_cast<numeric *>(eexponent.bp);
207 // ^(x,0) -> 1 (0^0 also handled here)
208 if (eexponent.is_zero())
212 if (eexponent.is_equal(exONE()))
215 // ^(0,x) -> 0 (except if x is real and negative)
216 if (ebasis.is_zero()) {
217 if (exponent_is_numerical && num_exponent->is_negative()) {
218 throw(std::overflow_error("power::eval(): division by zero"));
224 if (ebasis.is_equal(exONE()))
227 if (basis_is_numerical && exponent_is_numerical) {
228 // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
229 // except if c1,c2 are rational, but c1^c2 is not)
230 bool basis_is_rational = num_basis->is_rational();
231 bool exponent_is_rational = num_exponent->is_rational();
232 numeric res = (*num_basis).power(*num_exponent);
234 if ((!basis_is_rational || !exponent_is_rational)
235 || res.is_rational()) {
238 ASSERT(!num_exponent->is_integer()); // has been handled by now
239 // ^(c1,n/m) -> *(c1^q,c1^(n/m-q)), 0<(n/m-h)<1, q integer
240 if (basis_is_rational && exponent_is_rational
241 && num_exponent->is_real()
242 && !num_exponent->is_integer()) {
244 n = num_exponent->numer();
245 m = num_exponent->denom();
247 if (r.is_negative()) {
251 if (q.is_zero()) // the exponent was in the allowed range 0<(n/m)<1
255 res.push_back(expair(ebasis,r.div(m)));
256 res.push_back(expair(ex(num_basis->power(q)),exONE()));
257 return (new mul(res))->setflag(status_flags::dynallocated | status_flags::evaluated);
258 /*return mul(num_basis->power(q),
259 power(ex(*num_basis),ex(r.div(m)))).hold();
261 /* return (new mul(num_basis->power(q),
262 power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
268 // ^(^(x,c1),c2) -> ^(x,c1*c2)
269 // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
270 // case c1=1 should not happen, see below!)
271 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,power)) {
272 power const & sub_power=ex_to_power(ebasis);
273 ex const & sub_basis=sub_power.basis;
274 ex const & sub_exponent=sub_power.exponent;
275 if (is_ex_exactly_of_type(sub_exponent,numeric)) {
276 numeric const & num_sub_exponent=ex_to_numeric(sub_exponent);
277 ASSERT(num_sub_exponent!=numeric(1));
278 if (num_exponent->is_integer() || abs(num_sub_exponent)<1) {
279 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
284 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
285 if (exponent_is_numerical && num_exponent->is_integer() &&
286 is_ex_exactly_of_type(ebasis,mul)) {
287 return expand_mul(ex_to_mul(ebasis), *num_exponent);
290 // ^(*(...,x;c1),c2) -> ^(*(...,x;1),c2)*c1^c2 (c1, c2 numeric(), c1>0)
291 // ^(*(...,x,c1),c2) -> ^(*(...,x;-1),c2)*(-c1)^c2 (c1, c2 numeric(), c1<0)
292 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,mul)) {
293 ASSERT(!num_exponent->is_integer()); // should have been handled above
294 mul const & mulref=ex_to_mul(ebasis);
295 if (!mulref.overall_coeff.is_equal(exONE())) {
296 numeric const & num_coeff=ex_to_numeric(mulref.overall_coeff);
297 if (num_coeff.is_real()) {
298 if (num_coeff.is_positive()>0) {
299 mul * mulp=new mul(mulref);
300 mulp->overall_coeff=exONE();
301 mulp->clearflag(status_flags::evaluated);
302 mulp->clearflag(status_flags::hash_calculated);
303 return (new mul(power(*mulp,exponent),
304 power(num_coeff,*num_exponent)))->
305 setflag(status_flags::dynallocated);
307 ASSERT(num_coeff.compare(numZERO())<0);
308 if (num_coeff.compare(numMINUSONE())!=0) {
309 mul * mulp=new mul(mulref);
310 mulp->overall_coeff=exMINUSONE();
311 mulp->clearflag(status_flags::evaluated);
312 mulp->clearflag(status_flags::hash_calculated);
313 return (new mul(power(*mulp,exponent),
314 power(abs(num_coeff),*num_exponent)))->
315 setflag(status_flags::dynallocated);
322 if (are_ex_trivially_equal(ebasis,basis) &&
323 are_ex_trivially_equal(eexponent,exponent)) {
326 return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
327 status_flags::evaluated);
330 ex power::evalf(int level) const
332 debugmsg("power evalf",LOGLEVEL_MEMBER_FUNCTION);
340 } else if (level == -max_recursion_level) {
341 throw(std::runtime_error("max recursion level reached"));
343 ebasis=basis.evalf(level-1);
344 eexponent=exponent.evalf(level-1);
347 return power(ebasis,eexponent);
350 ex power::subs(lst const & ls, lst const & lr) const
352 ex const & subsed_basis=basis.subs(ls,lr);
353 ex const & subsed_exponent=exponent.subs(ls,lr);
355 if (are_ex_trivially_equal(basis,subsed_basis)&&
356 are_ex_trivially_equal(exponent,subsed_exponent)) {
360 return power(subsed_basis, subsed_exponent);
363 ex power::simplify_ncmul(exvector const & v) const
365 return basic::simplify_ncmul(v);
370 int power::compare_same_type(basic const & other) const
372 ASSERT(is_exactly_of_type(other, power));
373 power const & o=static_cast<power const &>(const_cast<basic &>(other));
376 cmpval=basis.compare(o.basis);
378 return exponent.compare(o.exponent);
383 unsigned power::return_type(void) const
385 return basis.return_type();
388 unsigned power::return_type_tinfo(void) const
390 return basis.return_type_tinfo();
393 ex power::expand(unsigned options) const
395 ex expanded_basis=basis.expand(options);
397 if (!is_ex_exactly_of_type(exponent,numeric)||
398 !ex_to_numeric(exponent).is_integer()) {
399 if (are_ex_trivially_equal(basis,expanded_basis)) {
402 return (new power(expanded_basis,exponent))->
403 setflag(status_flags::dynallocated);
407 // integer numeric exponent
408 numeric const & num_exponent=ex_to_numeric(exponent);
409 int int_exponent = num_exponent.to_int();
411 if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
412 return expand_add(ex_to_add(expanded_basis), int_exponent);
415 if (is_ex_exactly_of_type(expanded_basis,mul)) {
416 return expand_mul(ex_to_mul(expanded_basis), num_exponent);
419 // cannot expand further
420 if (are_ex_trivially_equal(basis,expanded_basis)) {
423 return (new power(expanded_basis,exponent))->
424 setflag(status_flags::dynallocated);
429 // new virtual functions which can be overridden by derived classes
435 // non-virtual functions in this class
438 ex power::expand_add(add const & a, int const n) const
440 // expand a^n where a is an add and n is an integer
443 return expand_add_2(a);
448 sum.reserve((n+1)*(m-1));
450 intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
451 intvector upper_limit(m-1);
454 for (int l=0; l<m-1; l++) {
463 for (l=0; l<m-1; l++) {
464 ex const & b=a.op(l);
465 ASSERT(!is_ex_exactly_of_type(b,add));
466 ASSERT(!is_ex_exactly_of_type(b,power)||
467 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
468 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
469 if (is_ex_exactly_of_type(b,mul)) {
470 term.push_back(expand_mul(ex_to_mul(b),numeric(k[l])));
472 term.push_back(power(b,k[l]));
476 ex const & b=a.op(l);
477 ASSERT(!is_ex_exactly_of_type(b,add));
478 ASSERT(!is_ex_exactly_of_type(b,power)||
479 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
480 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
481 if (is_ex_exactly_of_type(b,mul)) {
482 term.push_back(expand_mul(ex_to_mul(b),numeric(n-k_cum[m-2])));
484 term.push_back(power(b,n-k_cum[m-2]));
487 numeric f=binomial(numeric(n),numeric(k[0]));
488 for (l=1; l<m-1; l++) {
489 f=f*binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
494 cout << "begin term" << endl;
495 for (int i=0; i<m-1; i++) {
496 cout << "k[" << i << "]=" << k[i] << endl;
497 cout << "k_cum[" << i << "]=" << k_cum[i] << endl;
498 cout << "upper_limit[" << i << "]=" << upper_limit[i] << endl;
500 for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
501 cout << *cit << endl;
503 cout << "end term" << endl;
506 // TODO: optimize!!!!!!!!
507 sum.push_back((new mul(term))->setflag(status_flags::dynallocated));
511 while ((l>=0)&&((++k[l])>upper_limit[l])) {
517 // recalc k_cum[] and upper_limit[]
521 k_cum[l]=k_cum[l-1]+k[l];
523 for (int i=l+1; i<m-1; i++) {
524 k_cum[i]=k_cum[i-1]+k[i];
527 for (int i=l+1; i<m-1; i++) {
528 upper_limit[i]=n-k_cum[i-1];
531 return (new add(sum))->setflag(status_flags::dynallocated);
535 ex power::expand_add_2(add const & a) const
537 // special case: expand a^2 where a is an add
540 sum.reserve((a.seq.size()*(a.seq.size()+1))/2);
541 epvector::const_iterator last=a.seq.end();
543 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
544 ex const & b=a.recombine_pair_to_ex(*cit0);
545 ASSERT(!is_ex_exactly_of_type(b,add));
546 ASSERT(!is_ex_exactly_of_type(b,power)||
547 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
548 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
549 if (is_ex_exactly_of_type(b,mul)) {
550 sum.push_back(a.split_ex_to_pair(expand_mul(ex_to_mul(b),numTWO())));
552 sum.push_back(a.split_ex_to_pair((new power(b,exTWO()))->
553 setflag(status_flags::dynallocated)));
555 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
556 sum.push_back(a.split_ex_to_pair((new mul(a.recombine_pair_to_ex(*cit0),
557 a.recombine_pair_to_ex(*cit1)))->
558 setflag(status_flags::dynallocated),
563 ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
565 return (new add(sum))->setflag(status_flags::dynallocated);
569 ex power::expand_add_2(add const & a) const
571 // special case: expand a^2 where a is an add
574 unsigned a_nops=a.nops();
575 sum.reserve((a_nops*(a_nops+1))/2);
576 epvector::const_iterator last=a.seq.end();
578 // power(+(x,...,z;c),2)=power(+(x,...,z;0),2)+2*c*+(x,...,z;0)+c*c
579 // first part: ignore overall_coeff and expand other terms
580 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
581 ex const & r=(*cit0).rest;
582 ex const & c=(*cit0).coeff;
584 ASSERT(!is_ex_exactly_of_type(r,add));
585 ASSERT(!is_ex_exactly_of_type(r,power)||
586 !is_ex_exactly_of_type(ex_to_power(r).exponent,numeric)||
587 !ex_to_numeric(ex_to_power(r).exponent).is_pos_integer()||
588 !is_ex_exactly_of_type(ex_to_power(r).basis,add)||
589 !is_ex_exactly_of_type(ex_to_power(r).basis,mul)||
590 !is_ex_exactly_of_type(ex_to_power(r).basis,power));
592 if (are_ex_trivially_equal(c,exONE())) {
593 if (is_ex_exactly_of_type(r,mul)) {
594 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),exONE()));
596 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
600 if (is_ex_exactly_of_type(r,mul)) {
601 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),
602 ex_to_numeric(c).power_dyn(numTWO())));
604 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
605 ex_to_numeric(c).power_dyn(numTWO())));
609 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
610 ex const & r1=(*cit1).rest;
611 ex const & c1=(*cit1).coeff;
612 sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
613 numTWO().mul(ex_to_numeric(c)).mul_dyn(ex_to_numeric(c1))));
617 ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
619 // second part: add terms coming from overall_factor (if != 0)
620 if (!a.overall_coeff.is_equal(exZERO())) {
621 for (epvector::const_iterator cit=a.seq.begin(); cit!=a.seq.end(); ++cit) {
622 sum.push_back(a.combine_pair_with_coeff_to_pair(*cit,ex_to_numeric(a.overall_coeff).mul_dyn(numTWO())));
624 sum.push_back(expair(ex_to_numeric(a.overall_coeff).power_dyn(numTWO()),exONE()));
627 ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
629 return (new add(sum))->setflag(status_flags::dynallocated);
632 ex power::expand_mul(mul const & m, numeric const & n) const
634 // expand m^n where m is a mul and n is and integer
636 if (n.is_equal(numZERO())) {
641 distrseq.reserve(m.seq.size());
642 epvector::const_iterator last=m.seq.end();
643 epvector::const_iterator cit=m.seq.begin();
645 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
646 distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
648 // it is safe not to call mul::combine_pair_with_coeff_to_pair()
649 // since n is an integer
650 distrseq.push_back(expair((*cit).rest,
651 ex_to_numeric((*cit).coeff).mul(n)));
655 return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
656 ->setflag(status_flags::dynallocated);
660 ex power::expand_commutative_3(ex const & basis, numeric const & exponent,
661 unsigned options) const
668 add const & addref=static_cast<add const &>(*basis.bp);
672 ex first_operands=add(splitseq);
673 ex last_operand=addref.recombine_pair_to_ex(*(addref.seq.end()-1));
675 int n=exponent.to_int();
676 for (int k=0; k<=n; k++) {
677 distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))*
678 power(last_operand,numeric(n-k)));
680 return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
681 status_flags::expanded |
682 status_flags::dynallocated )).
688 ex power::expand_noncommutative(ex const & basis, numeric const & exponent,
689 unsigned options) const
691 ex rest_power=ex(power(basis,exponent.add(numMINUSONE()))).
692 expand(options | expand_options::internal_do_not_expand_power_operands);
694 return ex(mul(rest_power,basis),0).
695 expand(options | expand_options::internal_do_not_expand_mul_operands);
700 // static member variables
705 unsigned power::precedence=60;
711 const power some_power;
712 type_info const & typeid_power=typeid(some_power);