- diff() is now only defined on classes ex and basic, where it handles
[ginac.git] / ginac / mul.cpp
1 /** @file mul.cpp
2  *
3  *  Implementation of GiNaC's products of expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2000 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 <stdexcept>
25
26 #include "mul.h"
27 #include "add.h"
28 #include "power.h"
29 #include "archive.h"
30 #include "debugmsg.h"
31 #include "utils.h"
32
33 #ifndef NO_NAMESPACE_GINAC
34 namespace GiNaC {
35 #endif // ndef NO_NAMESPACE_GINAC
36
37 GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
38
39 //////////
40 // default constructor, destructor, copy constructor assignment operator and helpers
41 //////////
42
43 // public
44
45 mul::mul()
46 {
47     debugmsg("mul default constructor",LOGLEVEL_CONSTRUCT);
48     tinfo_key = TINFO_mul;
49 }
50
51 mul::~mul()
52 {
53     debugmsg("mul destructor",LOGLEVEL_DESTRUCT);
54     destroy(0);
55 }
56
57 mul::mul(const mul & other)
58 {
59     debugmsg("mul copy constructor",LOGLEVEL_CONSTRUCT);
60     copy(other);
61 }
62
63 const mul & mul::operator=(const mul & other)
64 {
65     debugmsg("mul operator=",LOGLEVEL_ASSIGNMENT);
66     if (this != &other) {
67         destroy(1);
68         copy(other);
69     }
70     return *this;
71 }
72
73 // protected
74
75 void mul::copy(const mul & other)
76 {
77     inherited::copy(other);
78 }
79
80 void mul::destroy(bool call_parent)
81 {
82     if (call_parent) inherited::destroy(call_parent);
83 }
84
85 //////////
86 // other constructors
87 //////////
88
89 // public
90
91 mul::mul(const ex & lh, const ex & rh)
92 {
93     debugmsg("mul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
94     tinfo_key = TINFO_mul;
95     overall_coeff=_ex1();
96     construct_from_2_ex(lh,rh);
97     GINAC_ASSERT(is_canonical());
98 }
99
100 mul::mul(const exvector & v)
101 {
102     debugmsg("mul constructor from exvector",LOGLEVEL_CONSTRUCT);
103     tinfo_key = TINFO_mul;
104     overall_coeff=_ex1();
105     construct_from_exvector(v);
106     GINAC_ASSERT(is_canonical());
107 }
108
109 /*
110 mul::mul(const epvector & v, bool do_not_canonicalize)
111 {
112     debugmsg("mul constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
113     tinfo_key = TINFO_mul;
114     if (do_not_canonicalize) {
115         seq=v;
116 #ifdef EXPAIRSEQ_USE_HASHTAB
117         combine_same_terms(); // to build hashtab
118 #endif // def EXPAIRSEQ_USE_HASHTAB
119     } else {
120         construct_from_epvector(v);
121     }
122     GINAC_ASSERT(is_canonical());
123 }
124 */
125
126 mul::mul(const epvector & v)
127 {
128     debugmsg("mul constructor from epvector",LOGLEVEL_CONSTRUCT);
129     tinfo_key = TINFO_mul;
130     overall_coeff=_ex1();
131     construct_from_epvector(v);
132     GINAC_ASSERT(is_canonical());
133 }
134
135 mul::mul(const epvector & v, const ex & oc)
136 {
137     debugmsg("mul constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
138     tinfo_key = TINFO_mul;
139     overall_coeff=oc;
140     construct_from_epvector(v);
141     GINAC_ASSERT(is_canonical());
142 }
143
144 mul::mul(epvector * vp, const ex & oc)
145 {
146     debugmsg("mul constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
147     tinfo_key = TINFO_mul;
148     GINAC_ASSERT(vp!=0);
149     overall_coeff=oc;
150     construct_from_epvector(*vp);
151     delete vp;
152     GINAC_ASSERT(is_canonical());
153 }
154
155 mul::mul(const ex & lh, const ex & mh, const ex & rh)
156 {
157     debugmsg("mul constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
158     tinfo_key = TINFO_mul;
159     exvector factors;
160     factors.reserve(3);
161     factors.push_back(lh);
162     factors.push_back(mh);
163     factors.push_back(rh);
164     overall_coeff=_ex1();
165     construct_from_exvector(factors);
166     GINAC_ASSERT(is_canonical());
167 }
168
169 //////////
170 // archiving
171 //////////
172
173 /** Construct object from archive_node. */
174 mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
175 {
176     debugmsg("mul constructor from archive_node", LOGLEVEL_CONSTRUCT);
177 }
178
179 /** Unarchive the object. */
180 ex mul::unarchive(const archive_node &n, const lst &sym_lst)
181 {
182     return (new mul(n, sym_lst))->setflag(status_flags::dynallocated);
183 }
184
185 /** Archive the object. */
186 void mul::archive(archive_node &n) const
187 {
188     inherited::archive(n);
189 }
190
191 //////////
192 // functions overriding virtual functions from bases classes
193 //////////
194
195 // public
196
197 basic * mul::duplicate() const
198 {
199     debugmsg("mul duplicate",LOGLEVEL_ASSIGNMENT);
200     return new mul(*this);
201 }
202
203 void mul::print(ostream & os, unsigned upper_precedence) const
204 {
205     debugmsg("mul print",LOGLEVEL_PRINT);
206     if (precedence<=upper_precedence) os << "(";
207     bool first=true;
208     // first print the overall numeric coefficient:
209     numeric coeff = ex_to_numeric(overall_coeff);
210     if (coeff.csgn()==-1) os << '-';
211     if (!coeff.is_equal(_num1()) &&
212         !coeff.is_equal(_num_1())) {
213         if (coeff.is_rational()) {
214             if (coeff.is_negative())
215                 os << -coeff;
216             else
217                 os << coeff;
218         } else {
219             if (coeff.csgn()==-1)
220                 (-coeff).print(os, precedence);
221             else
222                 coeff.print(os, precedence);
223         }
224         os << '*';
225     }
226     // then proceed with the remaining factors:
227     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
228         if (!first) {
229             os << '*';
230         } else {
231             first=false;
232         }
233         recombine_pair_to_ex(*cit).print(os,precedence);
234     }
235     if (precedence<=upper_precedence) os << ")";
236 }
237
238 void mul::printraw(ostream & os) const
239 {
240     debugmsg("mul printraw",LOGLEVEL_PRINT);
241
242     os << "*(";
243     for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
244         os << "(";
245         (*it).rest.bp->printraw(os);
246         os << ",";
247         (*it).coeff.bp->printraw(os);
248         os << "),";
249     }
250     os << ",hash=" << hashvalue << ",flags=" << flags;
251     os << ")";
252 }
253
254 void mul::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
255 {
256     debugmsg("mul print csrc", LOGLEVEL_PRINT);
257     if (precedence <= upper_precedence)
258         os << "(";
259
260     if (!overall_coeff.is_equal(_ex1())) {
261         overall_coeff.bp->printcsrc(os,type,precedence);
262         os << "*";
263     }
264     
265     // Print arguments, separated by "*" or "/"
266     epvector::const_iterator it = seq.begin();
267     epvector::const_iterator itend = seq.end();
268     while (it != itend) {
269
270         // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
271         if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
272             if (type == csrc_types::ctype_cl_N)
273                 os << "recip(";
274             else
275                 os << "1.0/";
276         }
277
278         // If the exponent is 1 or -1, it is left out
279         if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
280             it->rest.bp->printcsrc(os, type, precedence);
281         else
282             // outer parens around ex needed for broken gcc-2.95 parser:
283             (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
284
285         // Separator is "/" for negative integer powers, "*" otherwise
286         it++;
287         if (it != itend) {
288             if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
289                 os << "/";
290             else
291                 os << "*";
292         }
293     }
294     if (precedence <= upper_precedence)
295         os << ")";
296 }
297
298 bool mul::info(unsigned inf) const
299 {
300     // TODO: optimize
301     if (inf==info_flags::polynomial ||
302         inf==info_flags::integer_polynomial ||
303         inf==info_flags::cinteger_polynomial ||
304         inf==info_flags::rational_polynomial ||
305         inf==info_flags::crational_polynomial ||
306         inf==info_flags::rational_function) {
307         for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
308             if (!(recombine_pair_to_ex(*it).info(inf)))
309                 return false;
310         }
311         return overall_coeff.info(inf);
312     } else {
313         return inherited::info(inf);
314     }
315 }
316
317 typedef vector<int> intvector;
318
319 int mul::degree(const symbol & s) const
320 {
321     int deg_sum=0;
322     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
323         deg_sum+=(*cit).rest.degree(s) * ex_to_numeric((*cit).coeff).to_int();
324     }
325     return deg_sum;
326 }
327
328 int mul::ldegree(const symbol & s) const
329 {
330     int deg_sum=0;
331     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
332         deg_sum+=(*cit).rest.ldegree(s) * ex_to_numeric((*cit).coeff).to_int();
333     }
334     return deg_sum;
335 }
336
337 ex mul::coeff(const symbol & s, int n) const
338 {
339     exvector coeffseq;
340     coeffseq.reserve(seq.size()+1);
341     
342     if (n==0) {
343         // product of individual coeffs
344         // if a non-zero power of s is found, the resulting product will be 0
345         epvector::const_iterator it=seq.begin();
346         while (it!=seq.end()) {
347             coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
348             ++it;
349         }
350         coeffseq.push_back(overall_coeff);
351         return (new mul(coeffseq))->setflag(status_flags::dynallocated);
352     }
353          
354     epvector::const_iterator it=seq.begin();
355     bool coeff_found=0;
356     while (it!=seq.end()) {
357         ex t=recombine_pair_to_ex(*it);
358         ex c=t.coeff(s,n);
359         if (!c.is_zero()) {
360             coeffseq.push_back(c);
361             coeff_found=1;
362         } else {
363             coeffseq.push_back(t);
364         }
365         ++it;
366     }
367     if (coeff_found) {
368         coeffseq.push_back(overall_coeff);
369         return (new mul(coeffseq))->setflag(status_flags::dynallocated);
370     }
371     
372     return _ex0();
373 }
374
375 ex mul::eval(int level) const
376 {
377     // simplifications  *(...,x;0) -> 0
378     //                  *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
379     //                  *(x;1) -> x
380     //                  *(;c) -> c
381
382     debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
383
384     epvector * evaled_seqp=evalchildren(level);
385     if (evaled_seqp!=0) {
386         // do more evaluation later
387         return (new mul(evaled_seqp,overall_coeff))->
388                    setflag(status_flags::dynallocated);
389     }
390
391 #ifdef DO_GINAC_ASSERT
392     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
393         GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
394                (!(ex_to_numeric((*cit).coeff).is_integer())));
395         GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
396         if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
397             printtree(cerr,0);
398         }
399         GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
400         /* for paranoia */
401         expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
402         GINAC_ASSERT(p.rest.is_equal((*cit).rest));
403         GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
404         /* end paranoia */
405     }
406 #endif // def DO_GINAC_ASSERT
407
408     if (flags & status_flags::evaluated) {
409         GINAC_ASSERT(seq.size()>0);
410         GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
411         return *this;
412     }
413
414     int seq_size=seq.size();
415     if (overall_coeff.is_equal(_ex0())) {
416         // *(...,x;0) -> 0
417         return _ex0();
418     } else if (seq_size==0) {
419         // *(;c) -> c
420         return overall_coeff;
421     } else if ((seq_size==1)&&overall_coeff.is_equal(_ex1())) {
422         // *(x;1) -> x
423         return recombine_pair_to_ex(*(seq.begin()));
424     } else if ((seq_size==1) &&
425                is_ex_exactly_of_type((*seq.begin()).rest,add) &&
426                ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
427         // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
428         const add & addref=ex_to_add((*seq.begin()).rest);
429         epvector distrseq;
430         distrseq.reserve(addref.seq.size());
431         for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
432             distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
433                                    overall_coeff));
434         }
435         return (new add(distrseq,
436                         ex_to_numeric(addref.overall_coeff).
437                         mul_dyn(ex_to_numeric(overall_coeff))))
438             ->setflag(status_flags::dynallocated  |
439                       status_flags::evaluated );
440     }
441     return this->hold();
442 }
443
444 exvector mul::get_indices(void) const
445 {
446     // return union of indices of factors
447     exvector iv;
448     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
449         exvector subiv=(*cit).rest.get_indices();
450         iv.reserve(iv.size()+subiv.size());
451         for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
452             iv.push_back(*cit2);
453         }
454     }
455     return iv;
456 }
457
458 ex mul::simplify_ncmul(const exvector & v) const
459 {
460     throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
461 }
462
463 // protected
464
465 /** Implementation of ex::diff() for a product. It applies the product rule.
466  *  @see ex::diff */
467 ex mul::derivative(const symbol & s) const
468 {
469     exvector new_seq;
470     new_seq.reserve(seq.size());
471
472     // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
473     for (unsigned i=0; i!=seq.size(); i++) {
474         epvector sub_seq=seq;
475         sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
476                                       power(sub_seq[i].rest,sub_seq[i].coeff-1)*
477                                       sub_seq[i].rest.diff(s));
478         new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
479     }
480     return (new add(new_seq))->setflag(status_flags::dynallocated);
481 }
482
483 int mul::compare_same_type(const basic & other) const
484 {
485     return inherited::compare_same_type(other);
486 }
487
488 bool mul::is_equal_same_type(const basic & other) const
489 {
490     return inherited::is_equal_same_type(other);
491 }
492
493 unsigned mul::return_type(void) const
494 {
495     if (seq.size()==0) {
496         // mul without factors: should not happen, but commutes
497         return return_types::commutative;
498     }
499
500     bool all_commutative=1;
501     unsigned rt;
502     epvector::const_iterator cit_noncommutative_element; // point to first found nc element
503
504     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
505         rt=(*cit).rest.return_type();
506         if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
507         if ((rt==return_types::noncommutative)&&(all_commutative)) {
508             // first nc element found, remember position
509             cit_noncommutative_element=cit;
510             all_commutative=0;
511         }
512         if ((rt==return_types::noncommutative)&&(!all_commutative)) {
513             // another nc element found, compare type_infos
514             if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
515                 // diffent types -> mul is ncc
516                 return return_types::noncommutative_composite;
517             }
518         }
519     }
520     // all factors checked
521     return all_commutative ? return_types::commutative : return_types::noncommutative;
522 }
523    
524 unsigned mul::return_type_tinfo(void) const
525 {
526     if (seq.size()==0) {
527         // mul without factors: should not happen
528         return tinfo_key;
529     }
530     // return type_info of first noncommutative element
531     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
532         if ((*cit).rest.return_type()==return_types::noncommutative) {
533             return (*cit).rest.return_type_tinfo();
534         }
535     }
536     // no noncommutative element found, should not happen
537     return tinfo_key;
538 }
539
540 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
541 {
542     return (new mul(v,oc))->setflag(status_flags::dynallocated);
543 }
544
545 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
546 {
547     return (new mul(vp,oc))->setflag(status_flags::dynallocated);
548 }
549
550 expair mul::split_ex_to_pair(const ex & e) const
551 {
552     if (is_ex_exactly_of_type(e,power)) {
553         const power & powerref=ex_to_power(e);
554         if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
555             return expair(powerref.basis,powerref.exponent);
556         }
557     }
558     return expair(e,_ex1());
559 }
560     
561 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
562                                           const ex & c) const
563 {
564     // to avoid duplication of power simplification rules,
565     // we create a temporary power object
566     // otherwise it would be hard to correctly simplify
567     // expression like (4^(1/3))^(3/2)
568     if (are_ex_trivially_equal(c,_ex1())) {
569         return split_ex_to_pair(e);
570     }
571     return split_ex_to_pair(power(e,c));
572 }
573     
574 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
575                                             const ex & c) const
576 {
577     // to avoid duplication of power simplification rules,
578     // we create a temporary power object
579     // otherwise it would be hard to correctly simplify
580     // expression like (4^(1/3))^(3/2)
581     if (are_ex_trivially_equal(c,_ex1())) {
582         return p;
583     }
584     return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
585 }
586     
587 ex mul::recombine_pair_to_ex(const expair & p) const
588 {
589     // if (p.coeff.compare(_ex1())==0) {
590     // if (are_ex_trivially_equal(p.coeff,_ex1())) {
591     if (ex_to_numeric(p.coeff).is_equal(_num1())) {
592         return p.rest;
593     } else {
594         return power(p.rest,p.coeff);
595     }
596 }
597
598 bool mul::expair_needs_further_processing(epp it)
599 {
600     if (is_ex_exactly_of_type((*it).rest,mul) &&
601         ex_to_numeric((*it).coeff).is_integer()) {
602         // combined pair is product with integer power -> expand it
603         *it=split_ex_to_pair(recombine_pair_to_ex(*it));
604         return true;
605     }
606     if (is_ex_exactly_of_type((*it).rest,numeric)) {
607         expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
608         if (!ep.is_equal(*it)) {
609             // combined pair is a numeric power which can be simplified
610             *it=ep;
611             return true;
612         }
613         if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
614             // combined pair has coeff 1 and must be moved to the end
615             return true;
616         }
617     }
618     return false;
619 }       
620
621 ex mul::default_overall_coeff(void) const
622 {
623     return _ex1();
624 }
625
626 void mul::combine_overall_coeff(const ex & c)
627 {
628     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
629     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
630     overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
631 }
632
633 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
634 {
635     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
636     GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
637     GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
638     overall_coeff = ex_to_numeric(overall_coeff).
639                         mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
640 }
641
642 bool mul::can_make_flat(const expair & p) const
643 {
644     GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
645     // this assertion will probably fail somewhere
646     // it would require a more careful make_flat, obeying the power laws
647     // probably should return true only if p.coeff is integer
648     return ex_to_numeric(p.coeff).is_equal(_num1());
649 }
650
651 ex mul::expand(unsigned options) const
652 {
653     exvector sub_expanded_seq;
654     intvector positions_of_adds;
655     intvector number_of_add_operands;
656
657     epvector * expanded_seqp=expandchildren(options);
658
659     const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
660
661     positions_of_adds.resize(expanded_seq.size());
662     number_of_add_operands.resize(expanded_seq.size());
663
664     int number_of_adds=0;
665     int number_of_expanded_terms=1;
666
667     unsigned current_position=0;
668     epvector::const_iterator last=expanded_seq.end();
669     for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
670         if (is_ex_exactly_of_type((*cit).rest,add)&&
671             (ex_to_numeric((*cit).coeff).is_equal(_num1()))) {
672             positions_of_adds[number_of_adds]=current_position;
673             const add & expanded_addref=ex_to_add((*cit).rest);
674             unsigned addref_nops=expanded_addref.nops();
675             number_of_add_operands[number_of_adds]=addref_nops;
676             number_of_expanded_terms *= addref_nops;
677             number_of_adds++;
678         }
679         current_position++;
680     }
681
682     if (number_of_adds==0) {
683         if (expanded_seqp==0) {
684             return this->setflag(status_flags::expanded);
685         }
686         return (new mul(expanded_seqp,overall_coeff))->
687                      setflag(status_flags::dynallocated ||
688                              status_flags::expanded);
689     }
690
691     exvector distrseq;
692     distrseq.reserve(number_of_expanded_terms);
693
694     intvector k;
695     k.resize(number_of_adds);
696     
697     int l;
698     for (l=0; l<number_of_adds; l++) {
699         k[l]=0;
700     }
701
702     while (1) {
703         epvector term;
704         term=expanded_seq;
705         for (l=0; l<number_of_adds; l++) {
706             const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
707             GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(_ex1())==0);
708             term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
709         }
710         /*
711         cout << "mul::expand() term begin" << endl;
712         for (epvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
713             cout << "rest" << endl;
714             (*cit).rest.printtree(cout);
715             cout << "coeff" << endl;
716             (*cit).coeff.printtree(cout);
717         }
718         cout << "mul::expand() term end" << endl;
719         */
720         distrseq.push_back((new mul(term,overall_coeff))->
721                                 setflag(status_flags::dynallocated |
722                                         status_flags::expanded));
723
724         // increment k[]
725         l=number_of_adds-1;
726         while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
727             k[l]=0;    
728             l--;
729         }
730         if (l<0) break;
731     }
732
733     if (expanded_seqp!=0) {
734         delete expanded_seqp;
735     }
736     /*
737     cout << "mul::expand() distrseq begin" << endl;
738     for (exvector::const_iterator cit=distrseq.begin(); cit!=distrseq.end(); ++cit) {
739         (*cit).printtree(cout);
740     }
741     cout << "mul::expand() distrseq end" << endl;
742     */
743
744     return (new add(distrseq))->setflag(status_flags::dynallocated |
745                                         status_flags::expanded);
746 }
747
748 //////////
749 // new virtual functions which can be overridden by derived classes
750 //////////
751
752 // none
753
754 //////////
755 // non-virtual functions in this class
756 //////////
757
758 epvector * mul::expandchildren(unsigned options) const
759 {
760     epvector::const_iterator last=seq.end();
761     epvector::const_iterator cit=seq.begin();
762     while (cit!=last) {
763         const ex & factor=recombine_pair_to_ex(*cit);
764         const ex & expanded_factor=factor.expand(options);
765         if (!are_ex_trivially_equal(factor,expanded_factor)) {
766
767             // something changed, copy seq, eval and return it
768             epvector *s=new epvector;
769             s->reserve(seq.size());
770
771             // copy parts of seq which are known not to have changed
772             epvector::const_iterator cit2=seq.begin();
773             while (cit2!=cit) {
774                 s->push_back(*cit2);
775                 ++cit2;
776             }
777             // copy first changed element
778             s->push_back(split_ex_to_pair(expanded_factor));
779             ++cit2;
780             // copy rest
781             while (cit2!=last) {
782                 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
783                 ++cit2;
784             }
785             return s;
786         }
787         ++cit;
788     }
789     
790     return 0; // nothing has changed
791 }
792    
793 //////////
794 // static member variables
795 //////////
796
797 // protected
798
799 unsigned mul::precedence=50;
800
801
802 //////////
803 // global constants
804 //////////
805
806 const mul some_mul;
807 const type_info & typeid_mul=typeid(some_mul);
808
809 #ifndef NO_NAMESPACE_GINAC
810 } // namespace GiNaC
811 #endif // ndef NO_NAMESPACE_GINAC