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