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