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