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"
35 typedef vector<int> intvector;
38 // default constructor, destructor, copy constructor assignment operator and helpers
43 power::power() : basic(TINFO_power)
45 debugmsg("power default constructor",LOGLEVEL_CONSTRUCT);
50 debugmsg("power destructor",LOGLEVEL_DESTRUCT);
54 power::power(power const & other)
56 debugmsg("power copy constructor",LOGLEVEL_CONSTRUCT);
60 power const & power::operator=(power const & other)
62 debugmsg("power operator=",LOGLEVEL_ASSIGNMENT);
72 void power::copy(power const & other)
76 exponent=other.exponent;
79 void power::destroy(bool call_parent)
81 if (call_parent) basic::destroy(call_parent);
90 power::power(ex const & lh, ex const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
92 debugmsg("power constructor from ex,ex",LOGLEVEL_CONSTRUCT);
93 ASSERT(basis.return_type()==return_types::commutative);
96 power::power(ex const & lh, numeric const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
98 debugmsg("power constructor from ex,numeric",LOGLEVEL_CONSTRUCT);
99 ASSERT(basis.return_type()==return_types::commutative);
103 // functions overriding virtual functions from bases classes
108 basic * power::duplicate() const
110 debugmsg("power duplicate",LOGLEVEL_DUPLICATE);
111 return new power(*this);
114 bool power::info(unsigned inf) const
116 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial) {
117 return exponent.info(info_flags::nonnegint);
118 } else if (inf==info_flags::rational_function) {
119 return exponent.info(info_flags::integer);
121 return basic::info(inf);
125 int power::nops() const
130 ex & power::let_op(int const i)
135 return i==0 ? basis : exponent;
138 int power::degree(symbol const & s) const
140 if (is_exactly_of_type(*exponent.bp,numeric)) {
141 if ((*basis.bp).compare(s)==0)
142 return ex_to_numeric(exponent).to_int();
144 return basis.degree(s) * ex_to_numeric(exponent).to_int();
149 int power::ldegree(symbol const & s) const
151 if (is_exactly_of_type(*exponent.bp,numeric)) {
152 if ((*basis.bp).compare(s)==0)
153 return ex_to_numeric(exponent).to_int();
155 return basis.ldegree(s) * ex_to_numeric(exponent).to_int();
160 ex power::coeff(symbol const & s, int const n) const
162 if ((*basis.bp).compare(s)!=0) {
163 // basis not equal to s
169 } else if (is_exactly_of_type(*exponent.bp,numeric)&&
170 (static_cast<numeric const &>(*exponent.bp).compare(numeric(n))==0)) {
177 ex power::eval(int level) const
179 // simplifications: ^(x,0) -> 1 (0^0 handled here)
181 // ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
183 // ^(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)
184 // ^(^(x,c1),c2) -> ^(x,c1*c2) (c1, c2 numeric(), c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
185 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
186 // ^(*(x,c1),c2) -> ^(x,c2)*c1^c2 (c1, c2 numeric(), c1>0)
187 // ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1, c2 numeric(), c1<0)
189 debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
191 if ((level==1)&&(flags & status_flags::evaluated)) {
193 } else if (level == -max_recursion_level) {
194 throw(std::runtime_error("max recursion level reached"));
197 ex const & ebasis = level==1 ? basis : basis.eval(level-1);
198 ex const & eexponent = level==1 ? exponent : exponent.eval(level-1);
200 bool basis_is_numerical=0;
201 bool exponent_is_numerical=0;
203 numeric * num_exponent;
205 if (is_exactly_of_type(*ebasis.bp,numeric)) {
206 basis_is_numerical=1;
207 num_basis=static_cast<numeric *>(ebasis.bp);
209 if (is_exactly_of_type(*eexponent.bp,numeric)) {
210 exponent_is_numerical=1;
211 num_exponent=static_cast<numeric *>(eexponent.bp);
214 // ^(x,0) -> 1 (0^0 also handled here)
215 if (eexponent.is_zero())
219 if (eexponent.is_equal(exONE()))
222 // ^(0,x) -> 0 (except if x is real and negative)
223 if (ebasis.is_zero()) {
224 if (exponent_is_numerical && num_exponent->is_negative()) {
225 throw(std::overflow_error("power::eval(): division by zero"));
231 if (ebasis.is_equal(exONE()))
234 if (basis_is_numerical && exponent_is_numerical) {
235 // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
236 // except if c1,c2 are rational, but c1^c2 is not)
237 bool basis_is_rational = num_basis->is_rational();
238 bool exponent_is_rational = num_exponent->is_rational();
239 numeric res = (*num_basis).power(*num_exponent);
241 if ((!basis_is_rational || !exponent_is_rational)
242 || res.is_rational()) {
245 ASSERT(!num_exponent->is_integer()); // has been handled by now
246 // ^(c1,n/m) -> *(c1^q,c1^(n/m-q)), 0<(n/m-h)<1, q integer
247 if (basis_is_rational && exponent_is_rational
248 && num_exponent->is_real()
249 && !num_exponent->is_integer()) {
251 n = num_exponent->numer();
252 m = num_exponent->denom();
254 if (r.is_negative()) {
258 if (q.is_zero()) // the exponent was in the allowed range 0<(n/m)<1
262 res.push_back(expair(ebasis,r.div(m)));
263 res.push_back(expair(ex(num_basis->power(q)),exONE()));
264 return (new mul(res))->setflag(status_flags::dynallocated | status_flags::evaluated);
265 /*return mul(num_basis->power(q),
266 power(ex(*num_basis),ex(r.div(m)))).hold();
268 /* return (new mul(num_basis->power(q),
269 power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
275 // ^(^(x,c1),c2) -> ^(x,c1*c2)
276 // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
277 // case c1=1 should not happen, see below!)
278 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,power)) {
279 power const & sub_power=ex_to_power(ebasis);
280 ex const & sub_basis=sub_power.basis;
281 ex const & sub_exponent=sub_power.exponent;
282 if (is_ex_exactly_of_type(sub_exponent,numeric)) {
283 numeric const & num_sub_exponent=ex_to_numeric(sub_exponent);
284 ASSERT(num_sub_exponent!=numeric(1));
285 if (num_exponent->is_integer() || abs(num_sub_exponent)<1) {
286 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
291 // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
292 if (exponent_is_numerical && num_exponent->is_integer() &&
293 is_ex_exactly_of_type(ebasis,mul)) {
294 return expand_mul(ex_to_mul(ebasis), *num_exponent);
297 // ^(*(...,x;c1),c2) -> ^(*(...,x;1),c2)*c1^c2 (c1, c2 numeric(), c1>0)
298 // ^(*(...,x,c1),c2) -> ^(*(...,x;-1),c2)*(-c1)^c2 (c1, c2 numeric(), c1<0)
299 if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,mul)) {
300 ASSERT(!num_exponent->is_integer()); // should have been handled above
301 mul const & mulref=ex_to_mul(ebasis);
302 if (!mulref.overall_coeff.is_equal(exONE())) {
303 numeric const & num_coeff=ex_to_numeric(mulref.overall_coeff);
304 if (num_coeff.is_real()) {
305 if (num_coeff.is_positive()>0) {
306 mul * mulp=new mul(mulref);
307 mulp->overall_coeff=exONE();
308 mulp->clearflag(status_flags::evaluated);
309 mulp->clearflag(status_flags::hash_calculated);
310 return (new mul(power(*mulp,exponent),
311 power(num_coeff,*num_exponent)))->
312 setflag(status_flags::dynallocated);
314 ASSERT(num_coeff.compare(numZERO())<0);
315 if (num_coeff.compare(numMINUSONE())!=0) {
316 mul * mulp=new mul(mulref);
317 mulp->overall_coeff=exMINUSONE();
318 mulp->clearflag(status_flags::evaluated);
319 mulp->clearflag(status_flags::hash_calculated);
320 return (new mul(power(*mulp,exponent),
321 power(abs(num_coeff),*num_exponent)))->
322 setflag(status_flags::dynallocated);
329 if (are_ex_trivially_equal(ebasis,basis) &&
330 are_ex_trivially_equal(eexponent,exponent)) {
333 return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
334 status_flags::evaluated);
337 ex power::evalf(int level) const
339 debugmsg("power evalf",LOGLEVEL_MEMBER_FUNCTION);
347 } else if (level == -max_recursion_level) {
348 throw(std::runtime_error("max recursion level reached"));
350 ebasis=basis.evalf(level-1);
351 eexponent=exponent.evalf(level-1);
354 return power(ebasis,eexponent);
357 ex power::subs(lst const & ls, lst const & lr) const
359 ex const & subsed_basis=basis.subs(ls,lr);
360 ex const & subsed_exponent=exponent.subs(ls,lr);
362 if (are_ex_trivially_equal(basis,subsed_basis)&&
363 are_ex_trivially_equal(exponent,subsed_exponent)) {
367 return power(subsed_basis, subsed_exponent);
370 ex power::simplify_ncmul(exvector const & v) const
372 return basic::simplify_ncmul(v);
377 int power::compare_same_type(basic const & other) const
379 ASSERT(is_exactly_of_type(other, power));
380 power const & o=static_cast<power const &>(const_cast<basic &>(other));
383 cmpval=basis.compare(o.basis);
385 return exponent.compare(o.exponent);
390 unsigned power::return_type(void) const
392 return basis.return_type();
395 unsigned power::return_type_tinfo(void) const
397 return basis.return_type_tinfo();
400 ex power::expand(unsigned options) const
402 ex expanded_basis=basis.expand(options);
404 if (!is_ex_exactly_of_type(exponent,numeric)||
405 !ex_to_numeric(exponent).is_integer()) {
406 if (are_ex_trivially_equal(basis,expanded_basis)) {
409 return (new power(expanded_basis,exponent))->
410 setflag(status_flags::dynallocated);
414 // integer numeric exponent
415 numeric const & num_exponent=ex_to_numeric(exponent);
416 int int_exponent = num_exponent.to_int();
418 if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
419 return expand_add(ex_to_add(expanded_basis), int_exponent);
422 if (is_ex_exactly_of_type(expanded_basis,mul)) {
423 return expand_mul(ex_to_mul(expanded_basis), num_exponent);
426 // cannot expand further
427 if (are_ex_trivially_equal(basis,expanded_basis)) {
430 return (new power(expanded_basis,exponent))->
431 setflag(status_flags::dynallocated);
436 // new virtual functions which can be overridden by derived classes
442 // non-virtual functions in this class
445 ex power::expand_add(add const & a, int const n) const
447 // expand a^n where a is an add and n is an integer
450 return expand_add_2(a);
455 sum.reserve((n+1)*(m-1));
457 intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
458 intvector upper_limit(m-1);
461 for (int l=0; l<m-1; l++) {
470 for (l=0; l<m-1; l++) {
471 ex const & b=a.op(l);
472 ASSERT(!is_ex_exactly_of_type(b,add));
473 ASSERT(!is_ex_exactly_of_type(b,power)||
474 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
475 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
476 if (is_ex_exactly_of_type(b,mul)) {
477 term.push_back(expand_mul(ex_to_mul(b),numeric(k[l])));
479 term.push_back(power(b,k[l]));
483 ex const & b=a.op(l);
484 ASSERT(!is_ex_exactly_of_type(b,add));
485 ASSERT(!is_ex_exactly_of_type(b,power)||
486 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
487 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
488 if (is_ex_exactly_of_type(b,mul)) {
489 term.push_back(expand_mul(ex_to_mul(b),numeric(n-k_cum[m-2])));
491 term.push_back(power(b,n-k_cum[m-2]));
494 numeric f=binomial(numeric(n),numeric(k[0]));
495 for (l=1; l<m-1; l++) {
496 f=f*binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
501 cout << "begin term" << endl;
502 for (int i=0; i<m-1; i++) {
503 cout << "k[" << i << "]=" << k[i] << endl;
504 cout << "k_cum[" << i << "]=" << k_cum[i] << endl;
505 cout << "upper_limit[" << i << "]=" << upper_limit[i] << endl;
507 for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
508 cout << *cit << endl;
510 cout << "end term" << endl;
513 // TODO: optimize!!!!!!!!
514 sum.push_back((new mul(term))->setflag(status_flags::dynallocated));
518 while ((l>=0)&&((++k[l])>upper_limit[l])) {
524 // recalc k_cum[] and upper_limit[]
528 k_cum[l]=k_cum[l-1]+k[l];
530 for (int i=l+1; i<m-1; i++) {
531 k_cum[i]=k_cum[i-1]+k[i];
534 for (int i=l+1; i<m-1; i++) {
535 upper_limit[i]=n-k_cum[i-1];
538 return (new add(sum))->setflag(status_flags::dynallocated);
542 ex power::expand_add_2(add const & a) const
544 // special case: expand a^2 where a is an add
547 sum.reserve((a.seq.size()*(a.seq.size()+1))/2);
548 epvector::const_iterator last=a.seq.end();
550 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
551 ex const & b=a.recombine_pair_to_ex(*cit0);
552 ASSERT(!is_ex_exactly_of_type(b,add));
553 ASSERT(!is_ex_exactly_of_type(b,power)||
554 !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
555 !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
556 if (is_ex_exactly_of_type(b,mul)) {
557 sum.push_back(a.split_ex_to_pair(expand_mul(ex_to_mul(b),numTWO())));
559 sum.push_back(a.split_ex_to_pair((new power(b,exTWO()))->
560 setflag(status_flags::dynallocated)));
562 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
563 sum.push_back(a.split_ex_to_pair((new mul(a.recombine_pair_to_ex(*cit0),
564 a.recombine_pair_to_ex(*cit1)))->
565 setflag(status_flags::dynallocated),
570 ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
572 return (new add(sum))->setflag(status_flags::dynallocated);
576 ex power::expand_add_2(add const & a) const
578 // special case: expand a^2 where a is an add
581 unsigned a_nops=a.nops();
582 sum.reserve((a_nops*(a_nops+1))/2);
583 epvector::const_iterator last=a.seq.end();
585 // power(+(x,...,z;c),2)=power(+(x,...,z;0),2)+2*c*+(x,...,z;0)+c*c
586 // first part: ignore overall_coeff and expand other terms
587 for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
588 ex const & r=(*cit0).rest;
589 ex const & c=(*cit0).coeff;
591 ASSERT(!is_ex_exactly_of_type(r,add));
592 ASSERT(!is_ex_exactly_of_type(r,power)||
593 !is_ex_exactly_of_type(ex_to_power(r).exponent,numeric)||
594 !ex_to_numeric(ex_to_power(r).exponent).is_pos_integer()||
595 !is_ex_exactly_of_type(ex_to_power(r).basis,add)||
596 !is_ex_exactly_of_type(ex_to_power(r).basis,mul)||
597 !is_ex_exactly_of_type(ex_to_power(r).basis,power));
599 if (are_ex_trivially_equal(c,exONE())) {
600 if (is_ex_exactly_of_type(r,mul)) {
601 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),exONE()));
603 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
607 if (is_ex_exactly_of_type(r,mul)) {
608 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),
609 ex_to_numeric(c).power_dyn(numTWO())));
611 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
612 ex_to_numeric(c).power_dyn(numTWO())));
616 for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
617 ex const & r1=(*cit1).rest;
618 ex const & c1=(*cit1).coeff;
619 sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
620 numTWO().mul(ex_to_numeric(c)).mul_dyn(ex_to_numeric(c1))));
624 ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
626 // second part: add terms coming from overall_factor (if != 0)
627 if (!a.overall_coeff.is_equal(exZERO())) {
628 for (epvector::const_iterator cit=a.seq.begin(); cit!=a.seq.end(); ++cit) {
629 sum.push_back(a.combine_pair_with_coeff_to_pair(*cit,ex_to_numeric(a.overall_coeff).mul_dyn(numTWO())));
631 sum.push_back(expair(ex_to_numeric(a.overall_coeff).power_dyn(numTWO()),exONE()));
634 ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
636 return (new add(sum))->setflag(status_flags::dynallocated);
639 ex power::expand_mul(mul const & m, numeric const & n) const
641 // expand m^n where m is a mul and n is and integer
643 if (n.is_equal(numZERO())) {
648 distrseq.reserve(m.seq.size());
649 epvector::const_iterator last=m.seq.end();
650 epvector::const_iterator cit=m.seq.begin();
652 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
653 distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
655 // it is safe not to call mul::combine_pair_with_coeff_to_pair()
656 // since n is an integer
657 distrseq.push_back(expair((*cit).rest,
658 ex_to_numeric((*cit).coeff).mul(n)));
662 return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
663 ->setflag(status_flags::dynallocated);
667 ex power::expand_commutative_3(ex const & basis, numeric const & exponent,
668 unsigned options) const
675 add const & addref=static_cast<add const &>(*basis.bp);
679 ex first_operands=add(splitseq);
680 ex last_operand=addref.recombine_pair_to_ex(*(addref.seq.end()-1));
682 int n=exponent.to_int();
683 for (int k=0; k<=n; k++) {
684 distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))*
685 power(last_operand,numeric(n-k)));
687 return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
688 status_flags::expanded |
689 status_flags::dynallocated )).
695 ex power::expand_noncommutative(ex const & basis, numeric const & exponent,
696 unsigned options) const
698 ex rest_power=ex(power(basis,exponent.add(numMINUSONE()))).
699 expand(options | expand_options::internal_do_not_expand_power_operands);
701 return ex(mul(rest_power,basis),0).
702 expand(options | expand_options::internal_do_not_expand_mul_operands);
707 // static member variables
712 unsigned power::precedence=60;
718 const power some_power;
719 type_info const & typeid_power=typeid(some_power);