]> www.ginac.de Git - ginac.git/blob - ginac/mul.cpp
- sources now include "ginac.h" instead of <ginac/ginac.h>, changed Makefile.am
[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 int mul::compare_same_type(const basic & other) const
466 {
467     return inherited::compare_same_type(other);
468 }
469
470 bool mul::is_equal_same_type(const basic & other) const
471 {
472     return inherited::is_equal_same_type(other);
473 }
474
475 unsigned mul::return_type(void) const
476 {
477     if (seq.size()==0) {
478         // mul without factors: should not happen, but commutes
479         return return_types::commutative;
480     }
481
482     bool all_commutative=1;
483     unsigned rt;
484     epvector::const_iterator cit_noncommutative_element; // point to first found nc element
485
486     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
487         rt=(*cit).rest.return_type();
488         if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
489         if ((rt==return_types::noncommutative)&&(all_commutative)) {
490             // first nc element found, remember position
491             cit_noncommutative_element=cit;
492             all_commutative=0;
493         }
494         if ((rt==return_types::noncommutative)&&(!all_commutative)) {
495             // another nc element found, compare type_infos
496             if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
497                 // diffent types -> mul is ncc
498                 return return_types::noncommutative_composite;
499             }
500         }
501     }
502     // all factors checked
503     return all_commutative ? return_types::commutative : return_types::noncommutative;
504 }
505    
506 unsigned mul::return_type_tinfo(void) const
507 {
508     if (seq.size()==0) {
509         // mul without factors: should not happen
510         return tinfo_key;
511     }
512     // return type_info of first noncommutative element
513     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
514         if ((*cit).rest.return_type()==return_types::noncommutative) {
515             return (*cit).rest.return_type_tinfo();
516         }
517     }
518     // no noncommutative element found, should not happen
519     return tinfo_key;
520 }
521
522 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
523 {
524     return (new mul(v,oc))->setflag(status_flags::dynallocated);
525 }
526
527 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
528 {
529     return (new mul(vp,oc))->setflag(status_flags::dynallocated);
530 }
531
532 expair mul::split_ex_to_pair(const ex & e) const
533 {
534     if (is_ex_exactly_of_type(e,power)) {
535         const power & powerref=ex_to_power(e);
536         if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
537             return expair(powerref.basis,powerref.exponent);
538         }
539     }
540     return expair(e,_ex1());
541 }
542     
543 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
544                                           const ex & c) const
545 {
546     // to avoid duplication of power simplification rules,
547     // we create a temporary power object
548     // otherwise it would be hard to correctly simplify
549     // expression like (4^(1/3))^(3/2)
550     if (are_ex_trivially_equal(c,_ex1())) {
551         return split_ex_to_pair(e);
552     }
553     return split_ex_to_pair(power(e,c));
554 }
555     
556 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
557                                             const ex & c) const
558 {
559     // to avoid duplication of power simplification rules,
560     // we create a temporary power object
561     // otherwise it would be hard to correctly simplify
562     // expression like (4^(1/3))^(3/2)
563     if (are_ex_trivially_equal(c,_ex1())) {
564         return p;
565     }
566     return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
567 }
568     
569 ex mul::recombine_pair_to_ex(const expair & p) const
570 {
571     // if (p.coeff.compare(_ex1())==0) {
572     // if (are_ex_trivially_equal(p.coeff,_ex1())) {
573     if (ex_to_numeric(p.coeff).is_equal(_num1())) {
574         return p.rest;
575     } else {
576         return power(p.rest,p.coeff);
577     }
578 }
579
580 bool mul::expair_needs_further_processing(epp it)
581 {
582     if (is_ex_exactly_of_type((*it).rest,mul) &&
583         ex_to_numeric((*it).coeff).is_integer()) {
584         // combined pair is product with integer power -> expand it
585         *it=split_ex_to_pair(recombine_pair_to_ex(*it));
586         return true;
587     }
588     if (is_ex_exactly_of_type((*it).rest,numeric)) {
589         expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
590         if (!ep.is_equal(*it)) {
591             // combined pair is a numeric power which can be simplified
592             *it=ep;
593             return true;
594         }
595         if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
596             // combined pair has coeff 1 and must be moved to the end
597             return true;
598         }
599     }
600     return false;
601 }       
602
603 ex mul::default_overall_coeff(void) const
604 {
605     return _ex1();
606 }
607
608 void mul::combine_overall_coeff(const ex & c)
609 {
610     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
611     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
612     overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
613 }
614
615 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
616 {
617     GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
618     GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
619     GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
620     overall_coeff = ex_to_numeric(overall_coeff).
621                         mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
622 }
623
624 bool mul::can_make_flat(const expair & p) const
625 {
626     GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
627     // this assertion will probably fail somewhere
628     // it would require a more careful make_flat, obeying the power laws
629     // probably should return true only if p.coeff is integer
630     return ex_to_numeric(p.coeff).is_equal(_num1());
631 }
632
633 ex mul::expand(unsigned options) const
634 {
635     exvector sub_expanded_seq;
636     intvector positions_of_adds;
637     intvector number_of_add_operands;
638
639     epvector * expanded_seqp=expandchildren(options);
640
641     const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
642
643     positions_of_adds.resize(expanded_seq.size());
644     number_of_add_operands.resize(expanded_seq.size());
645
646     int number_of_adds=0;
647     int number_of_expanded_terms=1;
648
649     unsigned current_position=0;
650     epvector::const_iterator last=expanded_seq.end();
651     for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
652         if (is_ex_exactly_of_type((*cit).rest,add)&&
653             (ex_to_numeric((*cit).coeff).is_equal(_num1()))) {
654             positions_of_adds[number_of_adds]=current_position;
655             const add & expanded_addref=ex_to_add((*cit).rest);
656             unsigned addref_nops=expanded_addref.nops();
657             number_of_add_operands[number_of_adds]=addref_nops;
658             number_of_expanded_terms *= addref_nops;
659             number_of_adds++;
660         }
661         current_position++;
662     }
663
664     if (number_of_adds==0) {
665         if (expanded_seqp==0) {
666             return this->setflag(status_flags::expanded);
667         }
668         return (new mul(expanded_seqp,overall_coeff))->
669                      setflag(status_flags::dynallocated ||
670                              status_flags::expanded);
671     }
672
673     exvector distrseq;
674     distrseq.reserve(number_of_expanded_terms);
675
676     intvector k;
677     k.resize(number_of_adds);
678     
679     int l;
680     for (l=0; l<number_of_adds; l++) {
681         k[l]=0;
682     }
683
684     while (1) {
685         epvector term;
686         term=expanded_seq;
687         for (l=0; l<number_of_adds; l++) {
688             const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
689             GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(_ex1())==0);
690             term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
691         }
692         /*
693         cout << "mul::expand() term begin" << endl;
694         for (epvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
695             cout << "rest" << endl;
696             (*cit).rest.printtree(cout);
697             cout << "coeff" << endl;
698             (*cit).coeff.printtree(cout);
699         }
700         cout << "mul::expand() term end" << endl;
701         */
702         distrseq.push_back((new mul(term,overall_coeff))->
703                                 setflag(status_flags::dynallocated |
704                                         status_flags::expanded));
705
706         // increment k[]
707         l=number_of_adds-1;
708         while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
709             k[l]=0;    
710             l--;
711         }
712         if (l<0) break;
713     }
714
715     if (expanded_seqp!=0) {
716         delete expanded_seqp;
717     }
718     /*
719     cout << "mul::expand() distrseq begin" << endl;
720     for (exvector::const_iterator cit=distrseq.begin(); cit!=distrseq.end(); ++cit) {
721         (*cit).printtree(cout);
722     }
723     cout << "mul::expand() distrseq end" << endl;
724     */
725
726     return (new add(distrseq))->setflag(status_flags::dynallocated |
727                                         status_flags::expanded);
728 }
729
730 //////////
731 // new virtual functions which can be overridden by derived classes
732 //////////
733
734 // none
735
736 //////////
737 // non-virtual functions in this class
738 //////////
739
740 epvector * mul::expandchildren(unsigned options) const
741 {
742     epvector::const_iterator last=seq.end();
743     epvector::const_iterator cit=seq.begin();
744     while (cit!=last) {
745         const ex & factor=recombine_pair_to_ex(*cit);
746         const ex & expanded_factor=factor.expand(options);
747         if (!are_ex_trivially_equal(factor,expanded_factor)) {
748
749             // something changed, copy seq, eval and return it
750             epvector *s=new epvector;
751             s->reserve(seq.size());
752
753             // copy parts of seq which are known not to have changed
754             epvector::const_iterator cit2=seq.begin();
755             while (cit2!=cit) {
756                 s->push_back(*cit2);
757                 ++cit2;
758             }
759             // copy first changed element
760             s->push_back(split_ex_to_pair(expanded_factor));
761             ++cit2;
762             // copy rest
763             while (cit2!=last) {
764                 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
765                 ++cit2;
766             }
767             return s;
768         }
769         ++cit;
770     }
771     
772     return 0; // nothing has changed
773 }
774    
775 //////////
776 // static member variables
777 //////////
778
779 // protected
780
781 unsigned mul::precedence=50;
782
783
784 //////////
785 // global constants
786 //////////
787
788 const mul some_mul;
789 const type_info & typeid_mul=typeid(some_mul);
790
791 #ifndef NO_NAMESPACE_GINAC
792 } // namespace GiNaC
793 #endif // ndef NO_NAMESPACE_GINAC