]> www.ginac.de Git - ginac.git/blob - ginac/power.cpp
- modified the comment blocks so the copyright message no longer appears in
[ginac.git] / ginac / power.cpp
1 /** @file power.cpp
2  *
3  *  Implementation of GiNaC's symbolic exponentiation (basis^exponent). */
4
5 /*
6  *  GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <vector>
24 #include <iostream>
25 #include <stdexcept>
26
27 #include "power.h"
28 #include "expairseq.h"
29 #include "add.h"
30 #include "mul.h"
31 #include "numeric.h"
32 #include "relational.h"
33 #include "symbol.h"
34
35 typedef vector<int> intvector;
36
37 //////////
38 // default constructor, destructor, copy constructor assignment operator and helpers
39 //////////
40
41 // public
42
43 power::power() : basic(TINFO_power)
44 {
45     debugmsg("power default constructor",LOGLEVEL_CONSTRUCT);
46 }
47
48 power::~power()
49 {
50     debugmsg("power destructor",LOGLEVEL_DESTRUCT);
51     destroy(0);
52 }
53
54 power::power(power const & other)
55 {
56     debugmsg("power copy constructor",LOGLEVEL_CONSTRUCT);
57     copy(other);
58 }
59
60 power const & power::operator=(power const & other)
61 {
62     debugmsg("power operator=",LOGLEVEL_ASSIGNMENT);
63     if (this != &other) {
64         destroy(1);
65         copy(other);
66     }
67     return *this;
68 }
69
70 // protected
71
72 void power::copy(power const & other)
73 {
74     basic::copy(other);
75     basis=other.basis;
76     exponent=other.exponent;
77 }
78
79 void power::destroy(bool call_parent)
80 {
81     if (call_parent) basic::destroy(call_parent);
82 }
83
84 //////////
85 // other constructors
86 //////////
87
88 // public
89
90 power::power(ex const & lh, ex const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
91 {
92     debugmsg("power constructor from ex,ex",LOGLEVEL_CONSTRUCT);
93     ASSERT(basis.return_type()==return_types::commutative);
94 }
95
96 power::power(ex const & lh, numeric const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
97 {
98     debugmsg("power constructor from ex,numeric",LOGLEVEL_CONSTRUCT);
99     ASSERT(basis.return_type()==return_types::commutative);
100 }
101
102 //////////
103 // functions overriding virtual functions from bases classes
104 //////////
105
106 // public
107
108 basic * power::duplicate() const
109 {
110     debugmsg("power duplicate",LOGLEVEL_DUPLICATE);
111     return new power(*this);
112 }
113
114 bool power::info(unsigned inf) const
115 {
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);
120     } else {
121         return basic::info(inf);
122     }
123 }
124
125 int power::nops() const
126 {
127     return 2;
128 }
129
130 ex & power::let_op(int const i)
131 {
132     ASSERT(i>=0);
133     ASSERT(i<2);
134
135     return i==0 ? basis : exponent;
136 }
137
138 int power::degree(symbol const & s) const
139 {
140     if (is_exactly_of_type(*exponent.bp,numeric)) {
141         if ((*basis.bp).compare(s)==0)
142             return ex_to_numeric(exponent).to_int();
143         else
144             return basis.degree(s) * ex_to_numeric(exponent).to_int();
145     }
146     return 0;
147 }
148
149 int power::ldegree(symbol const & s) const 
150 {
151     if (is_exactly_of_type(*exponent.bp,numeric)) {
152         if ((*basis.bp).compare(s)==0)
153             return ex_to_numeric(exponent).to_int();
154         else
155             return basis.ldegree(s) * ex_to_numeric(exponent).to_int();
156     }
157     return 0;
158 }
159
160 ex power::coeff(symbol const & s, int const n) const
161 {
162     if ((*basis.bp).compare(s)!=0) {
163         // basis not equal to s
164         if (n==0) {
165             return *this;
166         } else {
167             return exZERO();
168         }
169     } else if (is_exactly_of_type(*exponent.bp,numeric)&&
170                (static_cast<numeric const &>(*exponent.bp).compare(numeric(n))==0)) {
171         return exONE();
172     }
173
174     return exZERO();
175 }
176
177 ex power::eval(int level) const
178 {
179     // simplifications: ^(x,0) -> 1 (0^0 handled here)
180     //                  ^(x,1) -> x
181     //                  ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
182     //                  ^(1,x) -> 1
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)
188     
189     debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
190
191     if ((level==1)&&(flags & status_flags::evaluated)) {
192         return *this;
193     } else if (level == -max_recursion_level) {
194         throw(std::runtime_error("max recursion level reached"));
195     }
196     
197     ex const & ebasis    = level==1 ? basis    : basis.eval(level-1);
198     ex const & eexponent = level==1 ? exponent : exponent.eval(level-1);
199
200     bool basis_is_numerical=0;
201     bool exponent_is_numerical=0;
202     numeric * num_basis;
203     numeric * num_exponent;
204
205     if (is_exactly_of_type(*ebasis.bp,numeric)) {
206         basis_is_numerical=1;
207         num_basis=static_cast<numeric *>(ebasis.bp);
208     }
209     if (is_exactly_of_type(*eexponent.bp,numeric)) {
210         exponent_is_numerical=1;
211         num_exponent=static_cast<numeric *>(eexponent.bp);
212     }
213
214     // ^(x,0) -> 1 (0^0 also handled here)
215     if (eexponent.is_zero())
216         return exONE();
217
218     // ^(x,1) -> x
219     if (eexponent.is_equal(exONE()))
220         return ebasis;
221
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"));
226         } else
227             return exZERO();
228     }
229
230     // ^(1,x) -> 1
231     if (ebasis.is_equal(exONE()))
232         return exONE();
233
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);
240         
241         if ((!basis_is_rational || !exponent_is_rational)
242             || res.is_rational()) {
243             return res;
244         }
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()) {
250             numeric r, q, n, m;
251             n = num_exponent->numer();
252             m = num_exponent->denom();
253             q = iquo(n, m, r);
254             if (r.is_negative()) {
255                 r = r.add(m);
256                 q = q.sub(numONE());
257             }
258             if (q.is_zero())  // the exponent was in the allowed range 0<(n/m)<1
259                 return this->hold();
260             else {
261                 epvector res(2);
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();
267                 */
268                 /* return (new mul(num_basis->power(q),
269                    power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
270                 */
271             }
272         }
273     }
274
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));
287             }
288         }
289     }
290     
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);
295     }
296
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);
313                 } else {
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);
323                     }
324                 }
325             }
326         }
327     }
328         
329     if (are_ex_trivially_equal(ebasis,basis) &&
330         are_ex_trivially_equal(eexponent,exponent)) {
331         return this->hold();
332     }
333     return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
334                                                    status_flags::evaluated);
335 }
336
337 ex power::evalf(int level) const
338 {
339     debugmsg("power evalf",LOGLEVEL_MEMBER_FUNCTION);
340
341     ex ebasis;
342     ex eexponent;
343     
344     if (level==1) {
345         ebasis=basis;
346         eexponent=exponent;
347     } else if (level == -max_recursion_level) {
348         throw(std::runtime_error("max recursion level reached"));
349     } else {
350         ebasis=basis.evalf(level-1);
351         eexponent=exponent.evalf(level-1);
352     }
353
354     return power(ebasis,eexponent);
355 }
356
357 ex power::subs(lst const & ls, lst const & lr) const
358 {
359     ex const & subsed_basis=basis.subs(ls,lr);
360     ex const & subsed_exponent=exponent.subs(ls,lr);
361
362     if (are_ex_trivially_equal(basis,subsed_basis)&&
363         are_ex_trivially_equal(exponent,subsed_exponent)) {
364         return *this;
365     }
366     
367     return power(subsed_basis, subsed_exponent);
368 }
369
370 ex power::simplify_ncmul(exvector const & v) const
371 {
372     return basic::simplify_ncmul(v);
373 }
374
375 // protected
376
377 int power::compare_same_type(basic const & other) const
378 {
379     ASSERT(is_exactly_of_type(other, power));
380     power const & o=static_cast<power const &>(const_cast<basic &>(other));
381
382     int cmpval;
383     cmpval=basis.compare(o.basis);
384     if (cmpval==0) {
385         return exponent.compare(o.exponent);
386     }
387     return cmpval;
388 }
389
390 unsigned power::return_type(void) const
391 {
392     return basis.return_type();
393 }
394    
395 unsigned power::return_type_tinfo(void) const
396 {
397     return basis.return_type_tinfo();
398 }
399
400 ex power::expand(unsigned options) const
401 {
402     ex expanded_basis=basis.expand(options);
403
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)) {
407             return this->hold();
408         } else {
409             return (new power(expanded_basis,exponent))->
410                     setflag(status_flags::dynallocated);
411         }
412     }
413
414     // integer numeric exponent
415     numeric const & num_exponent=ex_to_numeric(exponent);
416     int int_exponent = num_exponent.to_int();
417
418     if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
419         return expand_add(ex_to_add(expanded_basis), int_exponent);
420     }
421
422     if (is_ex_exactly_of_type(expanded_basis,mul)) {
423         return expand_mul(ex_to_mul(expanded_basis), num_exponent);
424     }
425
426     // cannot expand further
427     if (are_ex_trivially_equal(basis,expanded_basis)) {
428         return this->hold();
429     } else {
430         return (new power(expanded_basis,exponent))->
431                setflag(status_flags::dynallocated);
432     }
433 }
434
435 //////////
436 // new virtual functions which can be overridden by derived classes
437 //////////
438
439 // none
440
441 //////////
442 // non-virtual functions in this class
443 //////////
444
445 ex power::expand_add(add const & a, int const n) const
446 {
447     // expand a^n where a is an add and n is an integer
448
449     if (n==2) {
450         return expand_add_2(a);
451     }
452     
453     int m=a.nops();
454     exvector sum;
455     sum.reserve((n+1)*(m-1));
456     intvector k(m-1);
457     intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
458     intvector upper_limit(m-1);
459     int l;
460     
461     for (int l=0; l<m-1; l++) {
462         k[l]=0;
463         k_cum[l]=0;
464         upper_limit[l]=n;
465     }
466
467     while (1) {
468         exvector term;
469         term.reserve(m+1);
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])));
478             } else {
479                 term.push_back(power(b,k[l]));
480             }
481         }
482
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])));
490         } else {
491             term.push_back(power(b,n-k_cum[m-2]));
492         }
493
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]));
497         }
498         term.push_back(f);
499
500         /*
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;
506         }
507         for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
508             cout << *cit << endl;
509         }
510         cout << "end term" << endl;
511         */
512
513         // TODO: optimize!!!!!!!!
514         sum.push_back((new mul(term))->setflag(status_flags::dynallocated));
515         
516         // increment k[]
517         l=m-2;
518         while ((l>=0)&&((++k[l])>upper_limit[l])) {
519             k[l]=0;    
520             l--;
521         }
522         if (l<0) break;
523
524         // recalc k_cum[] and upper_limit[]
525         if (l==0) {
526             k_cum[0]=k[0];
527         } else {
528             k_cum[l]=k_cum[l-1]+k[l];
529         }
530         for (int i=l+1; i<m-1; i++) {
531             k_cum[i]=k_cum[i-1]+k[i];
532         }
533
534         for (int i=l+1; i<m-1; i++) {
535             upper_limit[i]=n-k_cum[i-1];
536         }   
537     }
538     return (new add(sum))->setflag(status_flags::dynallocated);
539 }
540
541 /*
542 ex power::expand_add_2(add const & a) const
543 {
544     // special case: expand a^2 where a is an add
545
546     epvector sum;
547     sum.reserve((a.seq.size()*(a.seq.size()+1))/2);
548     epvector::const_iterator last=a.seq.end();
549
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())));
558         } else {
559             sum.push_back(a.split_ex_to_pair((new power(b,exTWO()))->
560                                               setflag(status_flags::dynallocated)));
561         }
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),
566                                              exTWO()));
567         }
568     }
569
570     ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
571
572     return (new add(sum))->setflag(status_flags::dynallocated);
573 }
574 */
575
576 ex power::expand_add_2(add const & a) const
577 {
578     // special case: expand a^2 where a is an add
579
580     epvector sum;
581     unsigned a_nops=a.nops();
582     sum.reserve((a_nops*(a_nops+1))/2);
583     epvector::const_iterator last=a.seq.end();
584
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;
590         
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));
598
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()));
602             } else {
603                 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
604                                      exONE()));
605             }
606         } else {
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())));
610             } else {
611                 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
612                                      ex_to_numeric(c).power_dyn(numTWO())));
613             }
614         }
615             
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))));
621         }
622     }
623
624     ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
625
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())));
630         }
631         sum.push_back(expair(ex_to_numeric(a.overall_coeff).power_dyn(numTWO()),exONE()));
632     }
633         
634     ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
635     
636     return (new add(sum))->setflag(status_flags::dynallocated);
637 }
638
639 ex power::expand_mul(mul const & m, numeric const & n) const
640 {
641     // expand m^n where m is a mul and n is and integer
642
643     if (n.is_equal(numZERO())) {
644         return exONE();
645     }
646     
647     epvector distrseq;
648     distrseq.reserve(m.seq.size());
649     epvector::const_iterator last=m.seq.end();
650     epvector::const_iterator cit=m.seq.begin();
651     while (cit!=last) {
652         if (is_ex_exactly_of_type((*cit).rest,numeric)) {
653             distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
654         } else {
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)));
659         }
660         ++cit;
661     }
662     return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
663                  ->setflag(status_flags::dynallocated);
664 }
665
666 /*
667 ex power::expand_commutative_3(ex const & basis, numeric const & exponent,
668                              unsigned options) const
669 {
670     // obsolete
671
672     exvector distrseq;
673     epvector splitseq;
674
675     add const & addref=static_cast<add const &>(*basis.bp);
676
677     splitseq=addref.seq;
678     splitseq.pop_back();
679     ex first_operands=add(splitseq);
680     ex last_operand=addref.recombine_pair_to_ex(*(addref.seq.end()-1));
681     
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)));
686     }
687     return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
688                                            status_flags::expanded |
689                                            status_flags::dynallocated  )).
690            expand(options);
691 }
692 */
693
694 /*
695 ex power::expand_noncommutative(ex const & basis, numeric const & exponent,
696                                 unsigned options) const
697 {
698     ex rest_power=ex(power(basis,exponent.add(numMINUSONE()))).
699                   expand(options | expand_options::internal_do_not_expand_power_operands);
700
701     return ex(mul(rest_power,basis),0).
702            expand(options | expand_options::internal_do_not_expand_mul_operands);
703 }
704 */
705
706 //////////
707 // static member variables
708 //////////
709
710 // protected
711
712 unsigned power::precedence=60;
713
714 //////////
715 // global constants
716 //////////
717
718 const power some_power;
719 type_info const & typeid_power=typeid(some_power);