cee5cd7a7a9f4faf3405eb1f4c9af0b52bd26c02
[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-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #include "mul.h"
24 #include "add.h"
25 #include "power.h"
26 #include "operators.h"
27 #include "matrix.h"
28 #include "indexed.h"
29 #include "lst.h"
30 #include "archive.h"
31 #include "utils.h"
32 #include "symbol.h"
33 #include "compiler.h"
34
35 #include <iostream>
36 #include <limits>
37 #include <stdexcept>
38 #include <vector>
39
40 namespace GiNaC {
41
42 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(mul, expairseq,
43   print_func<print_context>(&mul::do_print).
44   print_func<print_latex>(&mul::do_print_latex).
45   print_func<print_csrc>(&mul::do_print_csrc).
46   print_func<print_tree>(&mul::do_print_tree).
47   print_func<print_python_repr>(&mul::do_print_python_repr))
48
49
50 //////////
51 // default constructor
52 //////////
53
54 mul::mul()
55 {
56 }
57
58 //////////
59 // other constructors
60 //////////
61
62 // public
63
64 mul::mul(const ex & lh, const ex & rh)
65 {
66         overall_coeff = _ex1;
67         construct_from_2_ex(lh,rh);
68         GINAC_ASSERT(is_canonical());
69 }
70
71 mul::mul(const exvector & v)
72 {
73         overall_coeff = _ex1;
74         construct_from_exvector(v);
75         GINAC_ASSERT(is_canonical());
76 }
77
78 mul::mul(const epvector & v)
79 {
80         overall_coeff = _ex1;
81         construct_from_epvector(v);
82         GINAC_ASSERT(is_canonical());
83 }
84
85 mul::mul(const epvector & v, const ex & oc, bool do_index_renaming)
86 {
87         overall_coeff = oc;
88         construct_from_epvector(v, do_index_renaming);
89         GINAC_ASSERT(is_canonical());
90 }
91
92 mul::mul(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming)
93 {
94         GINAC_ASSERT(vp.get()!=0);
95         overall_coeff = oc;
96         construct_from_epvector(*vp, do_index_renaming);
97         GINAC_ASSERT(is_canonical());
98 }
99
100 mul::mul(const ex & lh, const ex & mh, const ex & rh)
101 {
102         exvector factors;
103         factors.reserve(3);
104         factors.push_back(lh);
105         factors.push_back(mh);
106         factors.push_back(rh);
107         overall_coeff = _ex1;
108         construct_from_exvector(factors);
109         GINAC_ASSERT(is_canonical());
110 }
111
112 //////////
113 // archiving
114 //////////
115
116 //////////
117 // functions overriding virtual functions from base classes
118 //////////
119
120 void mul::print_overall_coeff(const print_context & c, const char *mul_sym) const
121 {
122         const numeric &coeff = ex_to<numeric>(overall_coeff);
123         if (coeff.csgn() == -1)
124                 c.s << '-';
125         if (!coeff.is_equal(*_num1_p) &&
126                 !coeff.is_equal(*_num_1_p)) {
127                 if (coeff.is_rational()) {
128                         if (coeff.is_negative())
129                                 (-coeff).print(c);
130                         else
131                                 coeff.print(c);
132                 } else {
133                         if (coeff.csgn() == -1)
134                                 (-coeff).print(c, precedence());
135                         else
136                                 coeff.print(c, precedence());
137                 }
138                 c.s << mul_sym;
139         }
140 }
141
142 void mul::do_print(const print_context & c, unsigned level) const
143 {
144         if (precedence() <= level)
145                 c.s << '(';
146
147         print_overall_coeff(c, "*");
148
149         epvector::const_iterator it = seq.begin(), itend = seq.end();
150         bool first = true;
151         while (it != itend) {
152                 if (!first)
153                         c.s << '*';
154                 else
155                         first = false;
156                 recombine_pair_to_ex(*it).print(c, precedence());
157                 ++it;
158         }
159
160         if (precedence() <= level)
161                 c.s << ')';
162 }
163
164 void mul::do_print_latex(const print_latex & c, unsigned level) const
165 {
166         if (precedence() <= level)
167                 c.s << "{(";
168
169         print_overall_coeff(c, " ");
170
171         // Separate factors into those with negative numeric exponent
172         // and all others
173         epvector::const_iterator it = seq.begin(), itend = seq.end();
174         exvector neg_powers, others;
175         while (it != itend) {
176                 GINAC_ASSERT(is_exactly_a<numeric>(it->coeff));
177                 if (ex_to<numeric>(it->coeff).is_negative())
178                         neg_powers.push_back(recombine_pair_to_ex(expair(it->rest, -(it->coeff))));
179                 else
180                         others.push_back(recombine_pair_to_ex(*it));
181                 ++it;
182         }
183
184         if (!neg_powers.empty()) {
185
186                 // Factors with negative exponent are printed as a fraction
187                 c.s << "\\frac{";
188                 mul(others).eval().print(c);
189                 c.s << "}{";
190                 mul(neg_powers).eval().print(c);
191                 c.s << "}";
192
193         } else {
194
195                 // All other factors are printed in the ordinary way
196                 exvector::const_iterator vit = others.begin(), vitend = others.end();
197                 while (vit != vitend) {
198                         c.s << ' ';
199                         vit->print(c, precedence());
200                         ++vit;
201                 }
202         }
203
204         if (precedence() <= level)
205                 c.s << ")}";
206 }
207
208 void mul::do_print_csrc(const print_csrc & c, unsigned level) const
209 {
210         if (precedence() <= level)
211                 c.s << "(";
212
213         if (!overall_coeff.is_equal(_ex1)) {
214                 if (overall_coeff.is_equal(_ex_1))
215                         c.s << "-";
216                 else {
217                         overall_coeff.print(c, precedence());
218                         c.s << "*";
219                 }
220         }
221
222         // Print arguments, separated by "*" or "/"
223         epvector::const_iterator it = seq.begin(), itend = seq.end();
224         while (it != itend) {
225
226                 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
227                 bool needclosingparenthesis = false;
228                 if (it == seq.begin() && it->coeff.info(info_flags::negint)) {
229                         if (is_a<print_csrc_cl_N>(c)) {
230                                 c.s << "recip(";
231                                 needclosingparenthesis = true;
232                         } else
233                                 c.s << "1.0/";
234                 }
235
236                 // If the exponent is 1 or -1, it is left out
237                 if (it->coeff.is_equal(_ex1) || it->coeff.is_equal(_ex_1))
238                         it->rest.print(c, precedence());
239                 else if (it->coeff.info(info_flags::negint))
240                         // Outer parens around ex needed for broken GCC parser:
241                         (ex(power(it->rest, -ex_to<numeric>(it->coeff)))).print(c, level);
242                 else
243                         // Outer parens around ex needed for broken GCC parser:
244                         (ex(power(it->rest, ex_to<numeric>(it->coeff)))).print(c, level);
245
246                 if (needclosingparenthesis)
247                         c.s << ")";
248
249                 // Separator is "/" for negative integer powers, "*" otherwise
250                 ++it;
251                 if (it != itend) {
252                         if (it->coeff.info(info_flags::negint))
253                                 c.s << "/";
254                         else
255                                 c.s << "*";
256                 }
257         }
258
259         if (precedence() <= level)
260                 c.s << ")";
261 }
262
263 void mul::do_print_python_repr(const print_python_repr & c, unsigned level) const
264 {
265         c.s << class_name() << '(';
266         op(0).print(c);
267         for (size_t i=1; i<nops(); ++i) {
268                 c.s << ',';
269                 op(i).print(c);
270         }
271         c.s << ')';
272 }
273
274 bool mul::info(unsigned inf) const
275 {
276         switch (inf) {
277                 case info_flags::polynomial:
278                 case info_flags::integer_polynomial:
279                 case info_flags::cinteger_polynomial:
280                 case info_flags::rational_polynomial:
281                 case info_flags::real:
282                 case info_flags::rational:
283                 case info_flags::integer:
284                 case info_flags::crational:
285                 case info_flags::cinteger:
286                 case info_flags::even:
287                 case info_flags::crational_polynomial:
288                 case info_flags::rational_function: {
289                         epvector::const_iterator i = seq.begin(), end = seq.end();
290                         while (i != end) {
291                                 if (!(recombine_pair_to_ex(*i).info(inf)))
292                                         return false;
293                                 ++i;
294                         }
295                         if (overall_coeff.is_equal(*_num1_p) && inf == info_flags::even)
296                                 return true;
297                         return overall_coeff.info(inf);
298                 }
299                 case info_flags::algebraic: {
300                         epvector::const_iterator i = seq.begin(), end = seq.end();
301                         while (i != end) {
302                                 if ((recombine_pair_to_ex(*i).info(inf)))
303                                         return true;
304                                 ++i;
305                         }
306                         return false;
307                 }
308                 case info_flags::positive:
309                 case info_flags::negative: {
310                         bool pos = true;
311                         epvector::const_iterator i = seq.begin(), end = seq.end();
312                         while (i != end) {
313                                 const ex& factor = recombine_pair_to_ex(*i++);
314                                 if (factor.info(info_flags::positive))
315                                         continue;
316                                 else if (factor.info(info_flags::negative))
317                                         pos = !pos;
318                                 else
319                                         return false;
320                         }
321                         if (overall_coeff.info(info_flags::negative))
322                                 pos = !pos;
323                         return (inf ==info_flags::positive? pos : !pos);
324                 }
325                 case info_flags::nonnegative: {
326                         bool pos = true;
327                         epvector::const_iterator i = seq.begin(), end = seq.end();
328                         while (i != end) {
329                                 const ex& factor = recombine_pair_to_ex(*i++);
330                                 if (factor.info(info_flags::nonnegative) || factor.info(info_flags::positive))
331                                         continue;
332                                 else if (factor.info(info_flags::negative))
333                                         pos = !pos;
334                                 else
335                                         return false;
336                         }
337                         return (overall_coeff.info(info_flags::negative)? pos : !pos);
338                 }
339                 case info_flags::posint:
340                 case info_flags::negint: {
341                         bool pos = true;
342                         epvector::const_iterator i = seq.begin(), end = seq.end();
343                         while (i != end) {
344                                 const ex& factor = recombine_pair_to_ex(*i++);
345                                 if (factor.info(info_flags::posint))
346                                         continue;
347                                 else if (factor.info(info_flags::negint))
348                                         pos = !pos;
349                                 else
350                                         return false;
351                         }
352                         if (overall_coeff.info(info_flags::negint))
353                                 pos = !pos;
354                         else if (!overall_coeff.info(info_flags::posint))
355                                 return false;
356                         return (inf ==info_flags::posint? pos : !pos); 
357                 }
358                 case info_flags::nonnegint: {
359                         bool pos = true;
360                         epvector::const_iterator i = seq.begin(), end = seq.end();
361                         while (i != end) {
362                                 const ex& factor = recombine_pair_to_ex(*i++);
363                                 if (factor.info(info_flags::nonnegint) || factor.info(info_flags::posint))
364                                         continue;
365                                 else if (factor.info(info_flags::negint))
366                                         pos = !pos;
367                                 else
368                                         return false;
369                         }
370                         if (overall_coeff.info(info_flags::negint))
371                                 pos = !pos;
372                         else if (!overall_coeff.info(info_flags::posint))
373                                 return false;
374                         return pos; 
375                 }
376         }
377         return inherited::info(inf);
378 }
379
380 bool mul::is_polynomial(const ex & var) const
381 {
382         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
383                 if (!i->rest.is_polynomial(var) ||
384                     (i->rest.has(var) && !i->coeff.info(info_flags::integer))) {
385                         return false;
386                 }
387         }
388         return true;
389 }
390
391 int mul::degree(const ex & s) const
392 {
393         // Sum up degrees of factors
394         int deg_sum = 0;
395         epvector::const_iterator i = seq.begin(), end = seq.end();
396         while (i != end) {
397                 if (ex_to<numeric>(i->coeff).is_integer())
398                         deg_sum += recombine_pair_to_ex(*i).degree(s);
399                 else {
400                         if (i->rest.has(s))
401                                 throw std::runtime_error("mul::degree() undefined degree because of non-integer exponent");
402                 }
403                 ++i;
404         }
405         return deg_sum;
406 }
407
408 int mul::ldegree(const ex & s) const
409 {
410         // Sum up degrees of factors
411         int deg_sum = 0;
412         epvector::const_iterator i = seq.begin(), end = seq.end();
413         while (i != end) {
414                 if (ex_to<numeric>(i->coeff).is_integer())
415                         deg_sum += recombine_pair_to_ex(*i).ldegree(s);
416                 else {
417                         if (i->rest.has(s))
418                                 throw std::runtime_error("mul::ldegree() undefined degree because of non-integer exponent");
419                 }
420                 ++i;
421         }
422         return deg_sum;
423 }
424
425 ex mul::coeff(const ex & s, int n) const
426 {
427         exvector coeffseq;
428         coeffseq.reserve(seq.size()+1);
429         
430         if (n==0) {
431                 // product of individual coeffs
432                 // if a non-zero power of s is found, the resulting product will be 0
433                 epvector::const_iterator i = seq.begin(), end = seq.end();
434                 while (i != end) {
435                         coeffseq.push_back(recombine_pair_to_ex(*i).coeff(s,n));
436                         ++i;
437                 }
438                 coeffseq.push_back(overall_coeff);
439                 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
440         }
441         
442         epvector::const_iterator i = seq.begin(), end = seq.end();
443         bool coeff_found = false;
444         while (i != end) {
445                 ex t = recombine_pair_to_ex(*i);
446                 ex c = t.coeff(s, n);
447                 if (!c.is_zero()) {
448                         coeffseq.push_back(c);
449                         coeff_found = 1;
450                 } else {
451                         coeffseq.push_back(t);
452                 }
453                 ++i;
454         }
455         if (coeff_found) {
456                 coeffseq.push_back(overall_coeff);
457                 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
458         }
459         
460         return _ex0;
461 }
462
463 /** Perform automatic term rewriting rules in this class.  In the following
464  *  x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
465  *  stand for such expressions that contain a plain number.
466  *  - *(...,x;0) -> 0
467  *  - *(+(x1,x2,...);c) -> *(+(*(x1,c),*(x2,c),...))
468  *  - *(x;1) -> x
469  *  - *(;c) -> c
470  *
471  *  @param level cut-off in recursive evaluation */
472 ex mul::eval(int level) const
473 {
474         std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
475         if (evaled_seqp.get()) {
476                 // do more evaluation later
477                 return (new mul(evaled_seqp, overall_coeff))->
478                            setflag(status_flags::dynallocated);
479         }
480         
481         if (flags & status_flags::evaluated) {
482                 GINAC_ASSERT(seq.size()>0);
483                 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1));
484                 return *this;
485         }
486         
487         size_t seq_size = seq.size();
488         if (overall_coeff.is_zero()) {
489                 // *(...,x;0) -> 0
490                 return _ex0;
491         } else if (seq_size==0) {
492                 // *(;c) -> c
493                 return overall_coeff;
494         } else if (seq_size==1 && overall_coeff.is_equal(_ex1)) {
495                 // *(x;1) -> x
496                 return recombine_pair_to_ex(*(seq.begin()));
497         } else if ((seq_size==1) &&
498                    is_exactly_a<add>((*seq.begin()).rest) &&
499                    ex_to<numeric>((*seq.begin()).coeff).is_equal(*_num1_p)) {
500                 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
501                 const add & addref = ex_to<add>((*seq.begin()).rest);
502                 std::auto_ptr<epvector> distrseq(new epvector);
503                 distrseq->reserve(addref.seq.size());
504                 epvector::const_iterator i = addref.seq.begin(), end = addref.seq.end();
505                 while (i != end) {
506                         distrseq->push_back(addref.combine_pair_with_coeff_to_pair(*i, overall_coeff));
507                         ++i;
508                 }
509                 return (new add(distrseq,
510                                 ex_to<numeric>(addref.overall_coeff).
511                                 mul_dyn(ex_to<numeric>(overall_coeff)))
512                        )->setflag(status_flags::dynallocated | status_flags::evaluated);
513         } else if ((seq_size >= 2) && (! (flags & status_flags::expanded))) {
514                 // Strip the content and the unit part from each term. Thus
515                 // things like (-x+a)*(3*x-3*a) automagically turn into - 3*(x-a)^2
516
517                 epvector::const_iterator last = seq.end();
518                 epvector::const_iterator i = seq.begin();
519                 epvector::const_iterator j = seq.begin();
520                 std::auto_ptr<epvector> s(new epvector);
521                 numeric oc = *_num1_p;
522                 bool something_changed = false;
523                 while (i!=last) {
524                         if (likely(! (is_a<add>(i->rest) && i->coeff.is_equal(_ex1)))) {
525                                 // power::eval has such a rule, no need to handle powers here
526                                 ++i;
527                                 continue;
528                         }
529
530                         // XXX: What is the best way to check if the polynomial is a primitive? 
531                         numeric c = i->rest.integer_content();
532                         const numeric lead_coeff =
533                                 ex_to<numeric>(ex_to<add>(i->rest).seq.begin()->coeff).div(c);
534                         const bool canonicalizable = lead_coeff.is_integer();
535
536                         // XXX: The main variable is chosen in a random way, so this code 
537                         // does NOT transform the term into the canonical form (thus, in some
538                         // very unlucky event it can even loop forever). Hopefully the main
539                         // variable will be the same for all terms in *this
540                         const bool unit_normal = lead_coeff.is_pos_integer();
541                         if (likely((c == *_num1_p) && ((! canonicalizable) || unit_normal))) {
542                                 ++i;
543                                 continue;
544                         }
545
546                         if (! something_changed) {
547                                 s->reserve(seq_size);
548                                 something_changed = true;
549                         }
550
551                         while ((j!=i) && (j!=last)) {
552                                 s->push_back(*j);
553                                 ++j;
554                         }
555
556                         if (! unit_normal)
557                                 c = c.mul(*_num_1_p);
558
559                         oc = oc.mul(c);
560
561                         // divide add by the number in place to save at least 2 .eval() calls
562                         const add& addref = ex_to<add>(i->rest);
563                         add* primitive = new add(addref);
564                         primitive->setflag(status_flags::dynallocated);
565                         primitive->clearflag(status_flags::hash_calculated);
566                         primitive->overall_coeff = ex_to<numeric>(primitive->overall_coeff).div_dyn(c);
567                         for (epvector::iterator ai = primitive->seq.begin(); ai != primitive->seq.end(); ++ai)
568                                 ai->coeff = ex_to<numeric>(ai->coeff).div_dyn(c);
569                         
570                         s->push_back(expair(*primitive, _ex1));
571
572                         ++i;
573                         ++j;
574                 }
575                 if (something_changed) {
576                         while (j!=last) {
577                                 s->push_back(*j);
578                                 ++j;
579                         }
580                         return (new mul(s, ex_to<numeric>(overall_coeff).mul_dyn(oc))
581                                )->setflag(status_flags::dynallocated);
582                 }
583         }
584
585         return this->hold();
586 }
587
588 ex mul::evalf(int level) const
589 {
590         if (level==1)
591                 return mul(seq,overall_coeff);
592         
593         if (level==-max_recursion_level)
594                 throw(std::runtime_error("max recursion level reached"));
595         
596         std::auto_ptr<epvector> s(new epvector);
597         s->reserve(seq.size());
598
599         --level;
600         epvector::const_iterator i = seq.begin(), end = seq.end();
601         while (i != end) {
602                 s->push_back(combine_ex_with_coeff_to_pair(i->rest.evalf(level),
603                                                            i->coeff));
604                 ++i;
605         }
606         return mul(s, overall_coeff.evalf(level));
607 }
608
609 void mul::find_real_imag(ex & rp, ex & ip) const
610 {
611         rp = overall_coeff.real_part();
612         ip = overall_coeff.imag_part();
613         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
614                 ex factor = recombine_pair_to_ex(*i);
615                 ex new_rp = factor.real_part();
616                 ex new_ip = factor.imag_part();
617                 if(new_ip.is_zero()) {
618                         rp *= new_rp;
619                         ip *= new_rp;
620                 } else {
621                         ex temp = rp*new_rp - ip*new_ip;
622                         ip = ip*new_rp + rp*new_ip;
623                         rp = temp;
624                 }
625         }
626         rp = rp.expand();
627         ip = ip.expand();
628 }
629
630 ex mul::real_part() const
631 {
632         ex rp, ip;
633         find_real_imag(rp, ip);
634         return rp;
635 }
636
637 ex mul::imag_part() const
638 {
639         ex rp, ip;
640         find_real_imag(rp, ip);
641         return ip;
642 }
643
644 ex mul::evalm() const
645 {
646         // numeric*matrix
647         if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1)
648          && is_a<matrix>(seq[0].rest))
649                 return ex_to<matrix>(seq[0].rest).mul(ex_to<numeric>(overall_coeff));
650
651         // Evaluate children first, look whether there are any matrices at all
652         // (there can be either no matrices or one matrix; if there were more
653         // than one matrix, it would be a non-commutative product)
654         std::auto_ptr<epvector> s(new epvector);
655         s->reserve(seq.size());
656
657         bool have_matrix = false;
658         epvector::iterator the_matrix;
659
660         epvector::const_iterator i = seq.begin(), end = seq.end();
661         while (i != end) {
662                 const ex &m = recombine_pair_to_ex(*i).evalm();
663                 s->push_back(split_ex_to_pair(m));
664                 if (is_a<matrix>(m)) {
665                         have_matrix = true;
666                         the_matrix = s->end() - 1;
667                 }
668                 ++i;
669         }
670
671         if (have_matrix) {
672
673                 // The product contained a matrix. We will multiply all other factors
674                 // into that matrix.
675                 matrix m = ex_to<matrix>(the_matrix->rest);
676                 s->erase(the_matrix);
677                 ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
678                 return m.mul_scalar(scalar);
679
680         } else
681                 return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
682 }
683
684 ex mul::eval_ncmul(const exvector & v) const
685 {
686         if (seq.empty())
687                 return inherited::eval_ncmul(v);
688
689         // Find first noncommutative element and call its eval_ncmul()
690         epvector::const_iterator i = seq.begin(), end = seq.end();
691         while (i != end) {
692                 if (i->rest.return_type() == return_types::noncommutative)
693                         return i->rest.eval_ncmul(v);
694                 ++i;
695         }
696         return inherited::eval_ncmul(v);
697 }
698
699 bool tryfactsubs(const ex & origfactor, const ex & patternfactor, int & nummatches, exmap& repls)
700 {       
701         ex origbase;
702         int origexponent;
703         int origexpsign;
704
705         if (is_exactly_a<power>(origfactor) && origfactor.op(1).info(info_flags::integer)) {
706                 origbase = origfactor.op(0);
707                 int expon = ex_to<numeric>(origfactor.op(1)).to_int();
708                 origexponent = expon > 0 ? expon : -expon;
709                 origexpsign = expon > 0 ? 1 : -1;
710         } else {
711                 origbase = origfactor;
712                 origexponent = 1;
713                 origexpsign = 1;
714         }
715
716         ex patternbase;
717         int patternexponent;
718         int patternexpsign;
719
720         if (is_exactly_a<power>(patternfactor) && patternfactor.op(1).info(info_flags::integer)) {
721                 patternbase = patternfactor.op(0);
722                 int expon = ex_to<numeric>(patternfactor.op(1)).to_int();
723                 patternexponent = expon > 0 ? expon : -expon;
724                 patternexpsign = expon > 0 ? 1 : -1;
725         } else {
726                 patternbase = patternfactor;
727                 patternexponent = 1;
728                 patternexpsign = 1;
729         }
730
731         exmap saverepls = repls;
732         if (origexponent < patternexponent || origexpsign != patternexpsign || !origbase.match(patternbase,saverepls))
733                 return false;
734         repls = saverepls;
735
736         int newnummatches = origexponent / patternexponent;
737         if (newnummatches < nummatches)
738                 nummatches = newnummatches;
739         return true;
740 }
741
742 /** Checks wheter e matches to the pattern pat and the (possibly to be updated)
743   * list of replacements repls. This matching is in the sense of algebraic
744   * substitutions. Matching starts with pat.op(factor) of the pattern because
745   * the factors before this one have already been matched. The (possibly
746   * updated) number of matches is in nummatches. subsed[i] is true for factors
747   * that already have been replaced by previous substitutions and matched[i]
748   * is true for factors that have been matched by the current match.
749   */
750 bool algebraic_match_mul_with_mul(const mul &e, const ex &pat, exmap& repls,
751                                   int factor, int &nummatches, const std::vector<bool> &subsed,
752                                   std::vector<bool> &matched)
753 {
754         GINAC_ASSERT(subsed.size() == e.nops());
755         GINAC_ASSERT(matched.size() == e.nops());
756
757         if (factor == (int)pat.nops())
758                 return true;
759
760         for (size_t i=0; i<e.nops(); ++i) {
761                 if(subsed[i] || matched[i])
762                         continue;
763                 exmap newrepls = repls;
764                 int newnummatches = nummatches;
765                 if (tryfactsubs(e.op(i), pat.op(factor), newnummatches, newrepls)) {
766                         matched[i] = true;
767                         if (algebraic_match_mul_with_mul(e, pat, newrepls, factor+1,
768                                         newnummatches, subsed, matched)) {
769                                 repls = newrepls;
770                                 nummatches = newnummatches;
771                                 return true;
772                         }
773                         else
774                                 matched[i] = false;
775                 }
776         }
777
778         return false;
779 }
780
781 bool mul::has(const ex & pattern, unsigned options) const
782 {
783         if(!(options&has_options::algebraic))
784                 return basic::has(pattern,options);
785         if(is_a<mul>(pattern)) {
786                 exmap repls;
787                 int nummatches = std::numeric_limits<int>::max();
788                 std::vector<bool> subsed(nops(), false);
789                 std::vector<bool> matched(nops(), false);
790                 if(algebraic_match_mul_with_mul(*this, pattern, repls, 0, nummatches,
791                                 subsed, matched))
792                         return true;
793         }
794         return basic::has(pattern, options);
795 }
796
797 ex mul::algebraic_subs_mul(const exmap & m, unsigned options) const
798 {       
799         std::vector<bool> subsed(nops(), false);
800         ex divide_by = 1;
801         ex multiply_by = 1;
802
803         for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
804
805                 if (is_exactly_a<mul>(it->first)) {
806 retry1:
807                         int nummatches = std::numeric_limits<int>::max();
808                         std::vector<bool> currsubsed(nops(), false);
809                         exmap repls;
810                         
811                         if(!algebraic_match_mul_with_mul(*this, it->first, repls, 0, nummatches, subsed, currsubsed))
812                                 continue;
813
814                         for (size_t j=0; j<subsed.size(); j++)
815                                 if (currsubsed[j])
816                                         subsed[j] = true;
817                         ex subsed_pattern
818                                 = it->first.subs(repls, subs_options::no_pattern);
819                         divide_by *= power(subsed_pattern, nummatches);
820                         ex subsed_result
821                                 = it->second.subs(repls, subs_options::no_pattern);
822                         multiply_by *= power(subsed_result, nummatches);
823                         goto retry1;
824
825                 } else {
826
827                         for (size_t j=0; j<this->nops(); j++) {
828                                 int nummatches = std::numeric_limits<int>::max();
829                                 exmap repls;
830                                 if (!subsed[j] && tryfactsubs(op(j), it->first, nummatches, repls)){
831                                         subsed[j] = true;
832                                         ex subsed_pattern
833                                                 = it->first.subs(repls, subs_options::no_pattern);
834                                         divide_by *= power(subsed_pattern, nummatches);
835                                         ex subsed_result
836                                                 = it->second.subs(repls, subs_options::no_pattern);
837                                         multiply_by *= power(subsed_result, nummatches);
838                                 }
839                         }
840                 }
841         }
842
843         bool subsfound = false;
844         for (size_t i=0; i<subsed.size(); i++) {
845                 if (subsed[i]) {
846                         subsfound = true;
847                         break;
848                 }
849         }
850         if (!subsfound)
851                 return subs_one_level(m, options | subs_options::algebraic);
852
853         return ((*this)/divide_by)*multiply_by;
854 }
855
856 ex mul::conjugate() const
857 {
858         // The base class' method is wrong here because we have to be careful at
859         // branch cuts. power::conjugate takes care of that already, so use it.
860         epvector *newepv = 0;
861         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
862                 if (newepv) {
863                         newepv->push_back(split_ex_to_pair(recombine_pair_to_ex(*i).conjugate()));
864                         continue;
865                 }
866                 ex x = recombine_pair_to_ex(*i);
867                 ex c = x.conjugate();
868                 if (c.is_equal(x)) {
869                         continue;
870                 }
871                 newepv = new epvector;
872                 newepv->reserve(seq.size());
873                 for (epvector::const_iterator j=seq.begin(); j!=i; ++j) {
874                         newepv->push_back(*j);
875                 }
876                 newepv->push_back(split_ex_to_pair(c));
877         }
878         ex x = overall_coeff.conjugate();
879         if (!newepv && are_ex_trivially_equal(x, overall_coeff)) {
880                 return *this;
881         }
882         ex result = thisexpairseq(newepv ? *newepv : seq, x);
883         delete newepv;
884         return result;
885 }
886
887
888 // protected
889
890 /** Implementation of ex::diff() for a product.  It applies the product rule.
891  *  @see ex::diff */
892 ex mul::derivative(const symbol & s) const
893 {
894         size_t num = seq.size();
895         exvector addseq;
896         addseq.reserve(num);
897         
898         // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
899         epvector mulseq = seq;
900         epvector::const_iterator i = seq.begin(), end = seq.end();
901         epvector::iterator i2 = mulseq.begin();
902         while (i != end) {
903                 expair ep = split_ex_to_pair(power(i->rest, i->coeff - _ex1) *
904                                              i->rest.diff(s));
905                 ep.swap(*i2);
906                 addseq.push_back((new mul(mulseq, overall_coeff * i->coeff))->setflag(status_flags::dynallocated));
907                 ep.swap(*i2);
908                 ++i; ++i2;
909         }
910         return (new add(addseq))->setflag(status_flags::dynallocated);
911 }
912
913 int mul::compare_same_type(const basic & other) const
914 {
915         return inherited::compare_same_type(other);
916 }
917
918 unsigned mul::return_type() const
919 {
920         if (seq.empty()) {
921                 // mul without factors: should not happen, but commutates
922                 return return_types::commutative;
923         }
924         
925         bool all_commutative = true;
926         epvector::const_iterator noncommutative_element; // point to first found nc element
927         
928         epvector::const_iterator i = seq.begin(), end = seq.end();
929         while (i != end) {
930                 unsigned rt = i->rest.return_type();
931                 if (rt == return_types::noncommutative_composite)
932                         return rt; // one ncc -> mul also ncc
933                 if ((rt == return_types::noncommutative) && (all_commutative)) {
934                         // first nc element found, remember position
935                         noncommutative_element = i;
936                         all_commutative = false;
937                 }
938                 if ((rt == return_types::noncommutative) && (!all_commutative)) {
939                         // another nc element found, compare type_infos
940                         if (noncommutative_element->rest.return_type_tinfo() != i->rest.return_type_tinfo()) {
941                                         // different types -> mul is ncc
942                                         return return_types::noncommutative_composite;
943                         }
944                 }
945                 ++i;
946         }
947         // all factors checked
948         return all_commutative ? return_types::commutative : return_types::noncommutative;
949 }
950
951 return_type_t mul::return_type_tinfo() const
952 {
953         if (seq.empty())
954                 return make_return_type_t<mul>(); // mul without factors: should not happen
955         
956         // return type_info of first noncommutative element
957         epvector::const_iterator i = seq.begin(), end = seq.end();
958         while (i != end) {
959                 if (i->rest.return_type() == return_types::noncommutative)
960                         return i->rest.return_type_tinfo();
961                 ++i;
962         }
963         // no noncommutative element found, should not happen
964         return make_return_type_t<mul>();
965 }
966
967 ex mul::thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming) const
968 {
969         return (new mul(v, oc, do_index_renaming))->setflag(status_flags::dynallocated);
970 }
971
972 ex mul::thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming) const
973 {
974         return (new mul(vp, oc, do_index_renaming))->setflag(status_flags::dynallocated);
975 }
976
977 expair mul::split_ex_to_pair(const ex & e) const
978 {
979         if (is_exactly_a<power>(e)) {
980                 const power & powerref = ex_to<power>(e);
981                 if (is_exactly_a<numeric>(powerref.exponent))
982                         return expair(powerref.basis,powerref.exponent);
983         }
984         return expair(e,_ex1);
985 }
986
987 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
988                                           const ex & c) const
989 {
990         // to avoid duplication of power simplification rules,
991         // we create a temporary power object
992         // otherwise it would be hard to correctly evaluate
993         // expression like (4^(1/3))^(3/2)
994         if (c.is_equal(_ex1))
995                 return split_ex_to_pair(e);
996
997         return split_ex_to_pair(power(e,c));
998 }
999
1000 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
1001                                             const ex & c) const
1002 {
1003         // to avoid duplication of power simplification rules,
1004         // we create a temporary power object
1005         // otherwise it would be hard to correctly evaluate
1006         // expression like (4^(1/3))^(3/2)
1007         if (c.is_equal(_ex1))
1008                 return p;
1009
1010         return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
1011 }
1012
1013 ex mul::recombine_pair_to_ex(const expair & p) const
1014 {
1015         if (ex_to<numeric>(p.coeff).is_equal(*_num1_p)) 
1016                 return p.rest;
1017         else
1018                 return (new power(p.rest,p.coeff))->setflag(status_flags::dynallocated);
1019 }
1020
1021 bool mul::expair_needs_further_processing(epp it)
1022 {
1023         if (is_exactly_a<mul>(it->rest) &&
1024             ex_to<numeric>(it->coeff).is_integer()) {
1025                 // combined pair is product with integer power -> expand it
1026                 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
1027                 return true;
1028         }
1029         if (is_exactly_a<numeric>(it->rest)) {
1030                 if (it->coeff.is_equal(_ex1)) {
1031                         // pair has coeff 1 and must be moved to the end
1032                         return true;
1033                 }
1034                 expair ep = split_ex_to_pair(recombine_pair_to_ex(*it));
1035                 if (!ep.is_equal(*it)) {
1036                         // combined pair is a numeric power which can be simplified
1037                         *it = ep;
1038                         return true;
1039                 }
1040         }
1041         return false;
1042 }       
1043
1044 ex mul::default_overall_coeff() const
1045 {
1046         return _ex1;
1047 }
1048
1049 void mul::combine_overall_coeff(const ex & c)
1050 {
1051         GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
1052         GINAC_ASSERT(is_exactly_a<numeric>(c));
1053         overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c));
1054 }
1055
1056 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
1057 {
1058         GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
1059         GINAC_ASSERT(is_exactly_a<numeric>(c1));
1060         GINAC_ASSERT(is_exactly_a<numeric>(c2));
1061         overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c1).power(ex_to<numeric>(c2)));
1062 }
1063
1064 bool mul::can_make_flat(const expair & p) const
1065 {
1066         GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
1067         // this assertion will probably fail somewhere
1068         // it would require a more careful make_flat, obeying the power laws
1069         // probably should return true only if p.coeff is integer
1070         return ex_to<numeric>(p.coeff).is_equal(*_num1_p);
1071 }
1072
1073 bool mul::can_be_further_expanded(const ex & e)
1074 {
1075         if (is_exactly_a<mul>(e)) {
1076                 for (epvector::const_iterator cit = ex_to<mul>(e).seq.begin(); cit != ex_to<mul>(e).seq.end(); ++cit) {
1077                         if (is_exactly_a<add>(cit->rest) && cit->coeff.info(info_flags::posint))
1078                                 return true;
1079                 }
1080         } else if (is_exactly_a<power>(e)) {
1081                 if (is_exactly_a<add>(e.op(0)) && e.op(1).info(info_flags::posint))
1082                         return true;
1083         }
1084         return false;
1085 }
1086
1087 ex mul::expand(unsigned options) const
1088 {
1089         {
1090         // trivial case: expanding the monomial (~ 30% of all calls)
1091                 epvector::const_iterator i = seq.begin(), seq_end = seq.end();
1092                 while ((i != seq.end()) &&  is_a<symbol>(i->rest) && i->coeff.info(info_flags::integer))
1093                         ++i;
1094                 if (i == seq_end) {
1095                         setflag(status_flags::expanded);
1096                         return *this;
1097                 }
1098         }
1099
1100         // do not rename indices if the object has no indices at all
1101         if ((!(options & expand_options::expand_rename_idx)) && 
1102                         this->info(info_flags::has_indices))
1103                 options |= expand_options::expand_rename_idx;
1104
1105         const bool skip_idx_rename = !(options & expand_options::expand_rename_idx);
1106
1107         // First, expand the children
1108         std::auto_ptr<epvector> expanded_seqp = expandchildren(options);
1109         const epvector & expanded_seq = (expanded_seqp.get() ? *expanded_seqp : seq);
1110
1111         // Now, look for all the factors that are sums and multiply each one out
1112         // with the next one that is found while collecting the factors which are
1113         // not sums
1114         ex last_expanded = _ex1;
1115
1116         epvector non_adds;
1117         non_adds.reserve(expanded_seq.size());
1118
1119         for (epvector::const_iterator cit = expanded_seq.begin(); cit != expanded_seq.end(); ++cit) {
1120                 if (is_exactly_a<add>(cit->rest) &&
1121                         (cit->coeff.is_equal(_ex1))) {
1122                         if (is_exactly_a<add>(last_expanded)) {
1123
1124                                 // Expand a product of two sums, aggressive version.
1125                                 // Caring for the overall coefficients in separate loops can
1126                                 // sometimes give a performance gain of up to 15%!
1127
1128                                 const int sizedifference = ex_to<add>(last_expanded).seq.size()-ex_to<add>(cit->rest).seq.size();
1129                                 // add2 is for the inner loop and should be the bigger of the two sums
1130                                 // in the presence of asymptotically good sorting:
1131                                 const add& add1 = (sizedifference<0 ? ex_to<add>(last_expanded) : ex_to<add>(cit->rest));
1132                                 const add& add2 = (sizedifference<0 ? ex_to<add>(cit->rest) : ex_to<add>(last_expanded));
1133                                 const epvector::const_iterator add1begin = add1.seq.begin();
1134                                 const epvector::const_iterator add1end   = add1.seq.end();
1135                                 const epvector::const_iterator add2begin = add2.seq.begin();
1136                                 const epvector::const_iterator add2end   = add2.seq.end();
1137                                 epvector distrseq;
1138                                 distrseq.reserve(add1.seq.size()+add2.seq.size());
1139
1140                                 // Multiply add2 with the overall coefficient of add1 and append it to distrseq:
1141                                 if (!add1.overall_coeff.is_zero()) {
1142                                         if (add1.overall_coeff.is_equal(_ex1))
1143                                                 distrseq.insert(distrseq.end(),add2begin,add2end);
1144                                         else
1145                                                 for (epvector::const_iterator i=add2begin; i!=add2end; ++i)
1146                                                         distrseq.push_back(expair(i->rest, ex_to<numeric>(i->coeff).mul_dyn(ex_to<numeric>(add1.overall_coeff))));
1147                                 }
1148
1149                                 // Multiply add1 with the overall coefficient of add2 and append it to distrseq:
1150                                 if (!add2.overall_coeff.is_zero()) {
1151                                         if (add2.overall_coeff.is_equal(_ex1))
1152                                                 distrseq.insert(distrseq.end(),add1begin,add1end);
1153                                         else
1154                                                 for (epvector::const_iterator i=add1begin; i!=add1end; ++i)
1155                                                         distrseq.push_back(expair(i->rest, ex_to<numeric>(i->coeff).mul_dyn(ex_to<numeric>(add2.overall_coeff))));
1156                                 }
1157
1158                                 // Compute the new overall coefficient and put it together:
1159                                 ex tmp_accu = (new add(distrseq, add1.overall_coeff*add2.overall_coeff))->setflag(status_flags::dynallocated);
1160
1161                                 exvector add1_dummy_indices, add2_dummy_indices, add_indices;
1162                                 lst dummy_subs;
1163
1164                                 if (!skip_idx_rename) {
1165                                         for (epvector::const_iterator i=add1begin; i!=add1end; ++i) {
1166                                                 add_indices = get_all_dummy_indices_safely(i->rest);
1167                                                 add1_dummy_indices.insert(add1_dummy_indices.end(), add_indices.begin(), add_indices.end());
1168                                         }
1169                                         for (epvector::const_iterator i=add2begin; i!=add2end; ++i) {
1170                                                 add_indices = get_all_dummy_indices_safely(i->rest);
1171                                                 add2_dummy_indices.insert(add2_dummy_indices.end(), add_indices.begin(), add_indices.end());
1172                                         }
1173
1174                                         sort(add1_dummy_indices.begin(), add1_dummy_indices.end(), ex_is_less());
1175                                         sort(add2_dummy_indices.begin(), add2_dummy_indices.end(), ex_is_less());
1176                                         dummy_subs = rename_dummy_indices_uniquely(add1_dummy_indices, add2_dummy_indices);
1177                                 }
1178
1179                                 // Multiply explicitly all non-numeric terms of add1 and add2:
1180                                 for (epvector::const_iterator i2=add2begin; i2!=add2end; ++i2) {
1181                                         // We really have to combine terms here in order to compactify
1182                                         // the result.  Otherwise it would become waayy tooo bigg.
1183                                         numeric oc(*_num0_p);
1184                                         epvector distrseq2;
1185                                         distrseq2.reserve(add1.seq.size());
1186                                         const ex i2_new = (skip_idx_rename || (dummy_subs.op(0).nops() == 0) ?
1187                                                         i2->rest :
1188                                                         i2->rest.subs(ex_to<lst>(dummy_subs.op(0)), 
1189                                                                 ex_to<lst>(dummy_subs.op(1)), subs_options::no_pattern));
1190                                         for (epvector::const_iterator i1=add1begin; i1!=add1end; ++i1) {
1191                                                 // Don't push_back expairs which might have a rest that evaluates to a numeric,
1192                                                 // since that would violate an invariant of expairseq:
1193                                                 const ex rest = (new mul(i1->rest, i2_new))->setflag(status_flags::dynallocated);
1194                                                 if (is_exactly_a<numeric>(rest)) {
1195                                                         oc += ex_to<numeric>(rest).mul(ex_to<numeric>(i1->coeff).mul(ex_to<numeric>(i2->coeff)));
1196                                                 } else {
1197                                                         distrseq2.push_back(expair(rest, ex_to<numeric>(i1->coeff).mul_dyn(ex_to<numeric>(i2->coeff))));
1198                                                 }
1199                                         }
1200                                         tmp_accu += (new add(distrseq2, oc))->setflag(status_flags::dynallocated);
1201                                 } 
1202                                 last_expanded = tmp_accu;
1203                         } else {
1204                                 if (!last_expanded.is_equal(_ex1))
1205                                         non_adds.push_back(split_ex_to_pair(last_expanded));
1206                                 last_expanded = cit->rest;
1207                         }
1208
1209                 } else {
1210                         non_adds.push_back(*cit);
1211                 }
1212         }
1213
1214         // Now the only remaining thing to do is to multiply the factors which
1215         // were not sums into the "last_expanded" sum
1216         if (is_exactly_a<add>(last_expanded)) {
1217                 size_t n = last_expanded.nops();
1218                 exvector distrseq;
1219                 distrseq.reserve(n);
1220                 exvector va;
1221                 if (! skip_idx_rename) {
1222                         va = get_all_dummy_indices_safely(mul(non_adds));
1223                         sort(va.begin(), va.end(), ex_is_less());
1224                 }
1225
1226                 for (size_t i=0; i<n; ++i) {
1227                         epvector factors = non_adds;
1228                         if (skip_idx_rename)
1229                                 factors.push_back(split_ex_to_pair(last_expanded.op(i)));
1230                         else
1231                                 factors.push_back(split_ex_to_pair(rename_dummy_indices_uniquely(va, last_expanded.op(i))));
1232                         ex term = (new mul(factors, overall_coeff))->setflag(status_flags::dynallocated);
1233                         if (can_be_further_expanded(term)) {
1234                                 distrseq.push_back(term.expand());
1235                         } else {
1236                                 if (options == 0)
1237                                         ex_to<basic>(term).setflag(status_flags::expanded);
1238                                 distrseq.push_back(term);
1239                         }
1240                 }
1241
1242                 return ((new add(distrseq))->
1243                         setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
1244         }
1245
1246         non_adds.push_back(split_ex_to_pair(last_expanded));
1247         ex result = (new mul(non_adds, overall_coeff))->setflag(status_flags::dynallocated);
1248         if (can_be_further_expanded(result)) {
1249                 return result.expand();
1250         } else {
1251                 if (options == 0)
1252                         ex_to<basic>(result).setflag(status_flags::expanded);
1253                 return result;
1254         }
1255 }
1256
1257   
1258 //////////
1259 // new virtual functions which can be overridden by derived classes
1260 //////////
1261
1262 // none
1263
1264 //////////
1265 // non-virtual functions in this class
1266 //////////
1267
1268
1269 /** Member-wise expand the expairs representing this sequence.  This must be
1270  *  overridden from expairseq::expandchildren() and done iteratively in order
1271  *  to allow for early cancallations and thus safe memory.
1272  *
1273  *  @see mul::expand()
1274  *  @return pointer to epvector containing expanded representation or zero
1275  *  pointer, if sequence is unchanged. */
1276 std::auto_ptr<epvector> mul::expandchildren(unsigned options) const
1277 {
1278         const epvector::const_iterator last = seq.end();
1279         epvector::const_iterator cit = seq.begin();
1280         while (cit!=last) {
1281                 const ex & factor = recombine_pair_to_ex(*cit);
1282                 const ex & expanded_factor = factor.expand(options);
1283                 if (!are_ex_trivially_equal(factor,expanded_factor)) {
1284                         
1285                         // something changed, copy seq, eval and return it
1286                         std::auto_ptr<epvector> s(new epvector);
1287                         s->reserve(seq.size());
1288                         
1289                         // copy parts of seq which are known not to have changed
1290                         epvector::const_iterator cit2 = seq.begin();
1291                         while (cit2!=cit) {
1292                                 s->push_back(*cit2);
1293                                 ++cit2;
1294                         }
1295
1296                         // copy first changed element
1297                         s->push_back(split_ex_to_pair(expanded_factor));
1298                         ++cit2;
1299
1300                         // copy rest
1301                         while (cit2!=last) {
1302                                 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
1303                                 ++cit2;
1304                         }
1305                         return s;
1306                 }
1307                 ++cit;
1308         }
1309         
1310         return std::auto_ptr<epvector>(0); // nothing has changed
1311 }
1312
1313 GINAC_BIND_UNARCHIVER(mul);
1314
1315 } // namespace GiNaC