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