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