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