- ASSERT macro renamed to GINAC_ASSERT
[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 #include "debugmsg.h"
35
36 namespace GiNaC {
37
38 typedef vector<int> intvector;
39
40 //////////
41 // default constructor, destructor, copy constructor assignment operator and helpers
42 //////////
43
44 // public
45
46 power::power() : basic(TINFO_power)
47 {
48     debugmsg("power default constructor",LOGLEVEL_CONSTRUCT);
49 }
50
51 power::~power()
52 {
53     debugmsg("power destructor",LOGLEVEL_DESTRUCT);
54     destroy(0);
55 }
56
57 power::power(power const & other)
58 {
59     debugmsg("power copy constructor",LOGLEVEL_CONSTRUCT);
60     copy(other);
61 }
62
63 power const & power::operator=(power const & other)
64 {
65     debugmsg("power operator=",LOGLEVEL_ASSIGNMENT);
66     if (this != &other) {
67         destroy(1);
68         copy(other);
69     }
70     return *this;
71 }
72
73 // protected
74
75 void power::copy(power const & other)
76 {
77     basic::copy(other);
78     basis=other.basis;
79     exponent=other.exponent;
80 }
81
82 void power::destroy(bool call_parent)
83 {
84     if (call_parent) basic::destroy(call_parent);
85 }
86
87 //////////
88 // other constructors
89 //////////
90
91 // public
92
93 power::power(ex const & lh, ex const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
94 {
95     debugmsg("power constructor from ex,ex",LOGLEVEL_CONSTRUCT);
96     GINAC_ASSERT(basis.return_type()==return_types::commutative);
97 }
98
99 power::power(ex const & lh, numeric const & rh) : basic(TINFO_power), basis(lh), exponent(rh)
100 {
101     debugmsg("power constructor from ex,numeric",LOGLEVEL_CONSTRUCT);
102     GINAC_ASSERT(basis.return_type()==return_types::commutative);
103 }
104
105 //////////
106 // functions overriding virtual functions from bases classes
107 //////////
108
109 // public
110
111 basic * power::duplicate() const
112 {
113     debugmsg("power duplicate",LOGLEVEL_DUPLICATE);
114     return new power(*this);
115 }
116
117 bool power::info(unsigned inf) const
118 {
119     if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial) {
120         return exponent.info(info_flags::nonnegint);
121     } else if (inf==info_flags::rational_function) {
122         return exponent.info(info_flags::integer);
123     } else {
124         return basic::info(inf);
125     }
126 }
127
128 int power::nops() const
129 {
130     return 2;
131 }
132
133 ex & power::let_op(int const i)
134 {
135     GINAC_ASSERT(i>=0);
136     GINAC_ASSERT(i<2);
137
138     return i==0 ? basis : exponent;
139 }
140
141 int power::degree(symbol const & s) const
142 {
143     if (is_exactly_of_type(*exponent.bp,numeric)) {
144         if ((*basis.bp).compare(s)==0)
145             return ex_to_numeric(exponent).to_int();
146         else
147             return basis.degree(s) * ex_to_numeric(exponent).to_int();
148     }
149     return 0;
150 }
151
152 int power::ldegree(symbol const & s) const 
153 {
154     if (is_exactly_of_type(*exponent.bp,numeric)) {
155         if ((*basis.bp).compare(s)==0)
156             return ex_to_numeric(exponent).to_int();
157         else
158             return basis.ldegree(s) * ex_to_numeric(exponent).to_int();
159     }
160     return 0;
161 }
162
163 ex power::coeff(symbol const & s, int const n) const
164 {
165     if ((*basis.bp).compare(s)!=0) {
166         // basis not equal to s
167         if (n==0) {
168             return *this;
169         } else {
170             return exZERO();
171         }
172     } else if (is_exactly_of_type(*exponent.bp,numeric)&&
173                (static_cast<numeric const &>(*exponent.bp).compare(numeric(n))==0)) {
174         return exONE();
175     }
176
177     return exZERO();
178 }
179
180 ex power::eval(int level) const
181 {
182     // simplifications: ^(x,0) -> 1 (0^0 handled here)
183     //                  ^(x,1) -> x
184     //                  ^(0,x) -> 0 (except if x is real and negative, in which case an exception is thrown)
185     //                  ^(1,x) -> 1
186     //                  ^(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)
187     //                  ^(^(x,c1),c2) -> ^(x,c1*c2) (c1, c2 numeric(), c2 integer or -1 < c1 <= 1, case c1=1 should not happen, see below!)
188     //                  ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
189     //                  ^(*(x,c1),c2) -> ^(x,c2)*c1^c2 (c1, c2 numeric(), c1>0)
190     //                  ^(*(x,c1),c2) -> ^(-x,c2)*c1^c2 (c1, c2 numeric(), c1<0)
191     
192     debugmsg("power eval",LOGLEVEL_MEMBER_FUNCTION);
193
194     if ((level==1)&&(flags & status_flags::evaluated)) {
195         return *this;
196     } else if (level == -max_recursion_level) {
197         throw(std::runtime_error("max recursion level reached"));
198     }
199     
200     ex const & ebasis    = level==1 ? basis    : basis.eval(level-1);
201     ex const & eexponent = level==1 ? exponent : exponent.eval(level-1);
202
203     bool basis_is_numerical=0;
204     bool exponent_is_numerical=0;
205     numeric * num_basis;
206     numeric * num_exponent;
207
208     if (is_exactly_of_type(*ebasis.bp,numeric)) {
209         basis_is_numerical=1;
210         num_basis=static_cast<numeric *>(ebasis.bp);
211     }
212     if (is_exactly_of_type(*eexponent.bp,numeric)) {
213         exponent_is_numerical=1;
214         num_exponent=static_cast<numeric *>(eexponent.bp);
215     }
216
217     // ^(x,0) -> 1 (0^0 also handled here)
218     if (eexponent.is_zero())
219         return exONE();
220
221     // ^(x,1) -> x
222     if (eexponent.is_equal(exONE()))
223         return ebasis;
224
225     // ^(0,x) -> 0 (except if x is real and negative)
226     if (ebasis.is_zero()) {
227         if (exponent_is_numerical && num_exponent->is_negative()) {
228             throw(std::overflow_error("power::eval(): division by zero"));
229         } else
230             return exZERO();
231     }
232
233     // ^(1,x) -> 1
234     if (ebasis.is_equal(exONE()))
235         return exONE();
236
237     if (basis_is_numerical && exponent_is_numerical) {
238         // ^(c1,c2) -> c1^c2 (c1, c2 numeric(),
239         // except if c1,c2 are rational, but c1^c2 is not)
240         bool basis_is_rational = num_basis->is_rational();
241         bool exponent_is_rational = num_exponent->is_rational();
242         numeric res = (*num_basis).power(*num_exponent);
243         
244         if ((!basis_is_rational || !exponent_is_rational)
245             || res.is_rational()) {
246             return res;
247         }
248         GINAC_ASSERT(!num_exponent->is_integer());  // has been handled by now
249         // ^(c1,n/m) -> *(c1^q,c1^(n/m-q)), 0<(n/m-h)<1, q integer
250         if (basis_is_rational && exponent_is_rational
251             && num_exponent->is_real()
252             && !num_exponent->is_integer()) {
253             numeric r, q, n, m;
254             n = num_exponent->numer();
255             m = num_exponent->denom();
256             q = iquo(n, m, r);
257             if (r.is_negative()) {
258                 r = r.add(m);
259                 q = q.sub(numONE());
260             }
261             if (q.is_zero())  // the exponent was in the allowed range 0<(n/m)<1
262                 return this->hold();
263             else {
264                 epvector res(2);
265                 res.push_back(expair(ebasis,r.div(m)));
266                 res.push_back(expair(ex(num_basis->power(q)),exONE()));
267                 return (new mul(res))->setflag(status_flags::dynallocated | status_flags::evaluated);
268                 /*return mul(num_basis->power(q),
269                            power(ex(*num_basis),ex(r.div(m)))).hold();
270                 */
271                 /* return (new mul(num_basis->power(q),
272                    power(*num_basis,r.div(m)).hold()))->setflag(status_flags::dynallocated | status_flags::evaluated);
273                 */
274             }
275         }
276     }
277
278     // ^(^(x,c1),c2) -> ^(x,c1*c2)
279     // (c1, c2 numeric(), c2 integer or -1 < c1 <= 1,
280     // case c1=1 should not happen, see below!)
281     if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,power)) {
282         power const & sub_power=ex_to_power(ebasis);
283         ex const & sub_basis=sub_power.basis;
284         ex const & sub_exponent=sub_power.exponent;
285         if (is_ex_exactly_of_type(sub_exponent,numeric)) {
286             numeric const & num_sub_exponent=ex_to_numeric(sub_exponent);
287             GINAC_ASSERT(num_sub_exponent!=numeric(1));
288             if (num_exponent->is_integer() || abs(num_sub_exponent)<1) {
289                 return power(sub_basis,num_sub_exponent.mul(*num_exponent));
290             }
291         }
292     }
293     
294     // ^(*(x,y,z),c1) -> *(x^c1,y^c1,z^c1) (c1 integer)
295     if (exponent_is_numerical && num_exponent->is_integer() &&
296         is_ex_exactly_of_type(ebasis,mul)) {
297         return expand_mul(ex_to_mul(ebasis), *num_exponent);
298     }
299
300     // ^(*(...,x;c1),c2) -> ^(*(...,x;1),c2)*c1^c2 (c1, c2 numeric(), c1>0)
301     // ^(*(...,x,c1),c2) -> ^(*(...,x;-1),c2)*(-c1)^c2 (c1, c2 numeric(), c1<0)
302     if (exponent_is_numerical && is_ex_exactly_of_type(ebasis,mul)) {
303         GINAC_ASSERT(!num_exponent->is_integer()); // should have been handled above
304         mul const & mulref=ex_to_mul(ebasis);
305         if (!mulref.overall_coeff.is_equal(exONE())) {
306             numeric const & num_coeff=ex_to_numeric(mulref.overall_coeff);
307             if (num_coeff.is_real()) {
308                 if (num_coeff.is_positive()>0) {
309                     mul * mulp=new mul(mulref);
310                     mulp->overall_coeff=exONE();
311                     mulp->clearflag(status_flags::evaluated);
312                     mulp->clearflag(status_flags::hash_calculated);
313                     return (new mul(power(*mulp,exponent),
314                                     power(num_coeff,*num_exponent)))->
315                         setflag(status_flags::dynallocated);
316                 } else {
317                     GINAC_ASSERT(num_coeff.compare(numZERO())<0);
318                     if (num_coeff.compare(numMINUSONE())!=0) {
319                         mul * mulp=new mul(mulref);
320                         mulp->overall_coeff=exMINUSONE();
321                         mulp->clearflag(status_flags::evaluated);
322                         mulp->clearflag(status_flags::hash_calculated);
323                         return (new mul(power(*mulp,exponent),
324                                         power(abs(num_coeff),*num_exponent)))->
325                             setflag(status_flags::dynallocated);
326                     }
327                 }
328             }
329         }
330     }
331         
332     if (are_ex_trivially_equal(ebasis,basis) &&
333         are_ex_trivially_equal(eexponent,exponent)) {
334         return this->hold();
335     }
336     return (new power(ebasis, eexponent))->setflag(status_flags::dynallocated |
337                                                    status_flags::evaluated);
338 }
339
340 ex power::evalf(int level) const
341 {
342     debugmsg("power evalf",LOGLEVEL_MEMBER_FUNCTION);
343
344     ex ebasis;
345     ex eexponent;
346     
347     if (level==1) {
348         ebasis=basis;
349         eexponent=exponent;
350     } else if (level == -max_recursion_level) {
351         throw(std::runtime_error("max recursion level reached"));
352     } else {
353         ebasis=basis.evalf(level-1);
354         eexponent=exponent.evalf(level-1);
355     }
356
357     return power(ebasis,eexponent);
358 }
359
360 ex power::subs(lst const & ls, lst const & lr) const
361 {
362     ex const & subsed_basis=basis.subs(ls,lr);
363     ex const & subsed_exponent=exponent.subs(ls,lr);
364
365     if (are_ex_trivially_equal(basis,subsed_basis)&&
366         are_ex_trivially_equal(exponent,subsed_exponent)) {
367         return *this;
368     }
369     
370     return power(subsed_basis, subsed_exponent);
371 }
372
373 ex power::simplify_ncmul(exvector const & v) const
374 {
375     return basic::simplify_ncmul(v);
376 }
377
378 // protected
379
380 int power::compare_same_type(basic const & other) const
381 {
382     GINAC_ASSERT(is_exactly_of_type(other, power));
383     power const & o=static_cast<power const &>(const_cast<basic &>(other));
384
385     int cmpval;
386     cmpval=basis.compare(o.basis);
387     if (cmpval==0) {
388         return exponent.compare(o.exponent);
389     }
390     return cmpval;
391 }
392
393 unsigned power::return_type(void) const
394 {
395     return basis.return_type();
396 }
397    
398 unsigned power::return_type_tinfo(void) const
399 {
400     return basis.return_type_tinfo();
401 }
402
403 ex power::expand(unsigned options) const
404 {
405     ex expanded_basis=basis.expand(options);
406
407     if (!is_ex_exactly_of_type(exponent,numeric)||
408         !ex_to_numeric(exponent).is_integer()) {
409         if (are_ex_trivially_equal(basis,expanded_basis)) {
410             return this->hold();
411         } else {
412             return (new power(expanded_basis,exponent))->
413                     setflag(status_flags::dynallocated);
414         }
415     }
416
417     // integer numeric exponent
418     numeric const & num_exponent=ex_to_numeric(exponent);
419     int int_exponent = num_exponent.to_int();
420
421     if (int_exponent > 0 && is_ex_exactly_of_type(expanded_basis,add)) {
422         return expand_add(ex_to_add(expanded_basis), int_exponent);
423     }
424
425     if (is_ex_exactly_of_type(expanded_basis,mul)) {
426         return expand_mul(ex_to_mul(expanded_basis), num_exponent);
427     }
428
429     // cannot expand further
430     if (are_ex_trivially_equal(basis,expanded_basis)) {
431         return this->hold();
432     } else {
433         return (new power(expanded_basis,exponent))->
434                setflag(status_flags::dynallocated);
435     }
436 }
437
438 //////////
439 // new virtual functions which can be overridden by derived classes
440 //////////
441
442 // none
443
444 //////////
445 // non-virtual functions in this class
446 //////////
447
448 ex power::expand_add(add const & a, int const n) const
449 {
450     // expand a^n where a is an add and n is an integer
451
452     if (n==2) {
453         return expand_add_2(a);
454     }
455     
456     int m=a.nops();
457     exvector sum;
458     sum.reserve((n+1)*(m-1));
459     intvector k(m-1);
460     intvector k_cum(m-1); // k_cum[l]:=sum(i=0,l,k[l]);
461     intvector upper_limit(m-1);
462     int l;
463     
464     for (int l=0; l<m-1; l++) {
465         k[l]=0;
466         k_cum[l]=0;
467         upper_limit[l]=n;
468     }
469
470     while (1) {
471         exvector term;
472         term.reserve(m+1);
473         for (l=0; l<m-1; l++) {
474             ex const & b=a.op(l);
475             GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
476             GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
477                    !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
478                    !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
479             if (is_ex_exactly_of_type(b,mul)) {
480                 term.push_back(expand_mul(ex_to_mul(b),numeric(k[l])));
481             } else {
482                 term.push_back(power(b,k[l]));
483             }
484         }
485
486         ex const & b=a.op(l);
487         GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
488         GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
489                !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
490                !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
491         if (is_ex_exactly_of_type(b,mul)) {
492             term.push_back(expand_mul(ex_to_mul(b),numeric(n-k_cum[m-2])));
493         } else {
494             term.push_back(power(b,n-k_cum[m-2]));
495         }
496
497         numeric f=binomial(numeric(n),numeric(k[0]));
498         for (l=1; l<m-1; l++) {
499             f=f*binomial(numeric(n-k_cum[l-1]),numeric(k[l]));
500         }
501         term.push_back(f);
502
503         /*
504         cout << "begin term" << endl;
505         for (int i=0; i<m-1; i++) {
506             cout << "k[" << i << "]=" << k[i] << endl;
507             cout << "k_cum[" << i << "]=" << k_cum[i] << endl;
508             cout << "upper_limit[" << i << "]=" << upper_limit[i] << endl;
509         }
510         for (exvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
511             cout << *cit << endl;
512         }
513         cout << "end term" << endl;
514         */
515
516         // TODO: optimize this
517         sum.push_back((new mul(term))->setflag(status_flags::dynallocated));
518         
519         // increment k[]
520         l=m-2;
521         while ((l>=0)&&((++k[l])>upper_limit[l])) {
522             k[l]=0;    
523             l--;
524         }
525         if (l<0) break;
526
527         // recalc k_cum[] and upper_limit[]
528         if (l==0) {
529             k_cum[0]=k[0];
530         } else {
531             k_cum[l]=k_cum[l-1]+k[l];
532         }
533         for (int i=l+1; i<m-1; i++) {
534             k_cum[i]=k_cum[i-1]+k[i];
535         }
536
537         for (int i=l+1; i<m-1; i++) {
538             upper_limit[i]=n-k_cum[i-1];
539         }   
540     }
541     return (new add(sum))->setflag(status_flags::dynallocated);
542 }
543
544 /*
545 ex power::expand_add_2(add const & a) const
546 {
547     // special case: expand a^2 where a is an add
548
549     epvector sum;
550     sum.reserve((a.seq.size()*(a.seq.size()+1))/2);
551     epvector::const_iterator last=a.seq.end();
552
553     for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
554         ex const & b=a.recombine_pair_to_ex(*cit0);
555         GINAC_ASSERT(!is_ex_exactly_of_type(b,add));
556         GINAC_ASSERT(!is_ex_exactly_of_type(b,power)||
557                !is_ex_exactly_of_type(ex_to_power(b).exponent,numeric)||
558                !ex_to_numeric(ex_to_power(b).exponent).is_pos_integer());
559         if (is_ex_exactly_of_type(b,mul)) {
560             sum.push_back(a.split_ex_to_pair(expand_mul(ex_to_mul(b),numTWO())));
561         } else {
562             sum.push_back(a.split_ex_to_pair((new power(b,exTWO()))->
563                                               setflag(status_flags::dynallocated)));
564         }
565         for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
566             sum.push_back(a.split_ex_to_pair((new mul(a.recombine_pair_to_ex(*cit0),
567                                                       a.recombine_pair_to_ex(*cit1)))->
568                                               setflag(status_flags::dynallocated),
569                                              exTWO()));
570         }
571     }
572
573     GINAC_ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
574
575     return (new add(sum))->setflag(status_flags::dynallocated);
576 }
577 */
578
579 ex power::expand_add_2(add const & a) const
580 {
581     // special case: expand a^2 where a is an add
582
583     epvector sum;
584     unsigned a_nops=a.nops();
585     sum.reserve((a_nops*(a_nops+1))/2);
586     epvector::const_iterator last=a.seq.end();
587
588     // power(+(x,...,z;c),2)=power(+(x,...,z;0),2)+2*c*+(x,...,z;0)+c*c
589     // first part: ignore overall_coeff and expand other terms
590     for (epvector::const_iterator cit0=a.seq.begin(); cit0!=last; ++cit0) {
591         ex const & r=(*cit0).rest;
592         ex const & c=(*cit0).coeff;
593         
594         GINAC_ASSERT(!is_ex_exactly_of_type(r,add));
595         GINAC_ASSERT(!is_ex_exactly_of_type(r,power)||
596                !is_ex_exactly_of_type(ex_to_power(r).exponent,numeric)||
597                !ex_to_numeric(ex_to_power(r).exponent).is_pos_integer()||
598                !is_ex_exactly_of_type(ex_to_power(r).basis,add)||
599                !is_ex_exactly_of_type(ex_to_power(r).basis,mul)||
600                !is_ex_exactly_of_type(ex_to_power(r).basis,power));
601
602         if (are_ex_trivially_equal(c,exONE())) {
603             if (is_ex_exactly_of_type(r,mul)) {
604                 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),exONE()));
605             } else {
606                 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
607                                      exONE()));
608             }
609         } else {
610             if (is_ex_exactly_of_type(r,mul)) {
611                 sum.push_back(expair(expand_mul(ex_to_mul(r),numTWO()),
612                                      ex_to_numeric(c).power_dyn(numTWO())));
613             } else {
614                 sum.push_back(expair((new power(r,exTWO()))->setflag(status_flags::dynallocated),
615                                      ex_to_numeric(c).power_dyn(numTWO())));
616             }
617         }
618             
619         for (epvector::const_iterator cit1=cit0+1; cit1!=last; ++cit1) {
620             ex const & r1=(*cit1).rest;
621             ex const & c1=(*cit1).coeff;
622             sum.push_back(a.combine_ex_with_coeff_to_pair((new mul(r,r1))->setflag(status_flags::dynallocated),
623                                                           numTWO().mul(ex_to_numeric(c)).mul_dyn(ex_to_numeric(c1))));
624         }
625     }
626
627     GINAC_ASSERT(sum.size()==(a.seq.size()*(a.seq.size()+1))/2);
628
629     // second part: add terms coming from overall_factor (if != 0)
630     if (!a.overall_coeff.is_equal(exZERO())) {
631         for (epvector::const_iterator cit=a.seq.begin(); cit!=a.seq.end(); ++cit) {
632             sum.push_back(a.combine_pair_with_coeff_to_pair(*cit,ex_to_numeric(a.overall_coeff).mul_dyn(numTWO())));
633         }
634         sum.push_back(expair(ex_to_numeric(a.overall_coeff).power_dyn(numTWO()),exONE()));
635     }
636         
637     GINAC_ASSERT(sum.size()==(a_nops*(a_nops+1))/2);
638     
639     return (new add(sum))->setflag(status_flags::dynallocated);
640 }
641
642 ex power::expand_mul(mul const & m, numeric const & n) const
643 {
644     // expand m^n where m is a mul and n is and integer
645
646     if (n.is_equal(numZERO())) {
647         return exONE();
648     }
649     
650     epvector distrseq;
651     distrseq.reserve(m.seq.size());
652     epvector::const_iterator last=m.seq.end();
653     epvector::const_iterator cit=m.seq.begin();
654     while (cit!=last) {
655         if (is_ex_exactly_of_type((*cit).rest,numeric)) {
656             distrseq.push_back(m.combine_pair_with_coeff_to_pair(*cit,n));
657         } else {
658             // it is safe not to call mul::combine_pair_with_coeff_to_pair()
659             // since n is an integer
660             distrseq.push_back(expair((*cit).rest,
661                                       ex_to_numeric((*cit).coeff).mul(n)));
662         }
663         ++cit;
664     }
665     return (new mul(distrseq,ex_to_numeric(m.overall_coeff).power_dyn(n)))
666                  ->setflag(status_flags::dynallocated);
667 }
668
669 /*
670 ex power::expand_commutative_3(ex const & basis, numeric const & exponent,
671                              unsigned options) const
672 {
673     // obsolete
674
675     exvector distrseq;
676     epvector splitseq;
677
678     add const & addref=static_cast<add const &>(*basis.bp);
679
680     splitseq=addref.seq;
681     splitseq.pop_back();
682     ex first_operands=add(splitseq);
683     ex last_operand=addref.recombine_pair_to_ex(*(addref.seq.end()-1));
684     
685     int n=exponent.to_int();
686     for (int k=0; k<=n; k++) {
687         distrseq.push_back(binomial(n,k)*power(first_operands,numeric(k))*
688                            power(last_operand,numeric(n-k)));
689     }
690     return ex((new add(distrseq))->setflag(status_flags::sub_expanded |
691                                            status_flags::expanded |
692                                            status_flags::dynallocated  )).
693            expand(options);
694 }
695 */
696
697 /*
698 ex power::expand_noncommutative(ex const & basis, numeric const & exponent,
699                                 unsigned options) const
700 {
701     ex rest_power=ex(power(basis,exponent.add(numMINUSONE()))).
702                   expand(options | expand_options::internal_do_not_expand_power_operands);
703
704     return ex(mul(rest_power,basis),0).
705            expand(options | expand_options::internal_do_not_expand_mul_operands);
706 }
707 */
708
709 //////////
710 // static member variables
711 //////////
712
713 // protected
714
715 unsigned power::precedence=60;
716
717 //////////
718 // global constants
719 //////////
720
721 const power some_power;
722 type_info const & typeid_power=typeid(some_power);
723
724 } // namespace GiNaC