replaced "precedence" static member variable by virtual precedence() function
[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                         if (is_of_type(c, print_latex))
184                                 c.s << "{(";
185                         else
186                                 c.s << "(";
187                 }
188
189                 bool first = true;
190
191                 // First print the overall numeric coefficient
192                 numeric coeff = ex_to_numeric(overall_coeff);
193                 if (coeff.csgn() == -1)
194                         c.s << '-';
195                 if (!coeff.is_equal(_num1()) &&
196                         !coeff.is_equal(_num_1())) {
197                         if (coeff.is_rational()) {
198                                 if (coeff.is_negative())
199                                         (-coeff).print(c);
200                                 else
201                                         coeff.print(c);
202                         } else {
203                                 if (coeff.csgn() == -1)
204                                         (-coeff).print(c, precedence());
205                                 else
206                                         coeff.print(c, precedence());
207                         }
208                         if (is_of_type(c, print_latex))
209                                 c.s << ' ';
210                         else
211                                 c.s << '*';
212                 }
213
214                 // Then proceed with the remaining factors
215                 epvector::const_iterator it = seq.begin(), itend = seq.end();
216                 while (it != itend) {
217                         if (!first) {
218                                 if (is_of_type(c, print_latex))
219                                         c.s << ' ';
220                                 else
221                                         c.s << '*';
222                         } else {
223                                 first = false;
224                         }
225                         recombine_pair_to_ex(*it).print(c, precedence());
226                         it++;
227                 }
228
229                 if (precedence() <= level) {
230                         if (is_of_type(c, print_latex))
231                                 c.s << ")}";
232                         else
233                                 c.s << ")";
234                 }
235         }
236 }
237
238 bool mul::info(unsigned inf) const
239 {
240         switch (inf) {
241                 case info_flags::polynomial:
242                 case info_flags::integer_polynomial:
243                 case info_flags::cinteger_polynomial:
244                 case info_flags::rational_polynomial:
245                 case info_flags::crational_polynomial:
246                 case info_flags::rational_function: {
247                         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
248                                 if (!(recombine_pair_to_ex(*i).info(inf)))
249                                         return false;
250                         }
251                         return overall_coeff.info(inf);
252                 }
253                 case info_flags::algebraic: {
254                         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
255                                 if ((recombine_pair_to_ex(*i).info(inf)))
256                                         return true;
257                         }
258                         return false;
259                 }
260         }
261         return inherited::info(inf);
262 }
263
264 int mul::degree(const ex & s) const
265 {
266         int deg_sum = 0;
267         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
268                 if (ex_to_numeric(cit->coeff).is_integer())
269                         deg_sum+=cit->rest.degree(s) * ex_to_numeric(cit->coeff).to_int();
270         }
271         return deg_sum;
272 }
273
274 int mul::ldegree(const ex & s) const
275 {
276         int deg_sum = 0;
277         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
278                 if (ex_to_numeric(cit->coeff).is_integer())
279                         deg_sum+=cit->rest.ldegree(s) * ex_to_numeric(cit->coeff).to_int();
280         }
281         return deg_sum;
282 }
283
284 ex mul::coeff(const ex & s, int n) const
285 {
286         exvector coeffseq;
287         coeffseq.reserve(seq.size()+1);
288         
289         if (n==0) {
290                 // product of individual coeffs
291                 // if a non-zero power of s is found, the resulting product will be 0
292                 epvector::const_iterator it = seq.begin();
293                 while (it!=seq.end()) {
294                         coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
295                         ++it;
296                 }
297                 coeffseq.push_back(overall_coeff);
298                 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
299         }
300         
301         epvector::const_iterator it=seq.begin();
302         bool coeff_found = 0;
303         while (it!=seq.end()) {
304                 ex t = recombine_pair_to_ex(*it);
305                 ex c = t.coeff(s,n);
306                 if (!c.is_zero()) {
307                         coeffseq.push_back(c);
308                         coeff_found = 1;
309                 } else {
310                         coeffseq.push_back(t);
311                 }
312                 ++it;
313         }
314         if (coeff_found) {
315                 coeffseq.push_back(overall_coeff);
316                 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
317         }
318         
319         return _ex0();
320 }
321
322 ex mul::eval(int level) const
323 {
324         // simplifications  *(...,x;0) -> 0
325         //                  *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
326         //                  *(x;1) -> x
327         //                  *(;c) -> c
328         
329         debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
330         
331         epvector * evaled_seqp = evalchildren(level);
332         if (evaled_seqp!=0) {
333                 // do more evaluation later
334                 return (new mul(evaled_seqp,overall_coeff))->
335                            setflag(status_flags::dynallocated);
336         }
337         
338 #ifdef DO_GINAC_ASSERT
339         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
340                 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul)) ||
341                              (!(ex_to_numeric((*cit).coeff).is_integer())));
342                 GINAC_ASSERT(!(cit->is_canonical_numeric()));
343                 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric))
344                     print(print_tree(std::cerr));
345                 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
346                 /* for paranoia */
347                 expair p = split_ex_to_pair(recombine_pair_to_ex(*cit));
348                 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
349                 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
350                 /* end paranoia */
351         }
352 #endif // def DO_GINAC_ASSERT
353         
354         if (flags & status_flags::evaluated) {
355                 GINAC_ASSERT(seq.size()>0);
356                 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1()));
357                 return *this;
358         }
359         
360         int seq_size = seq.size();
361         if (overall_coeff.is_equal(_ex0())) {
362                 // *(...,x;0) -> 0
363                 return _ex0();
364         } else if (seq_size==0) {
365                 // *(;c) -> c
366                 return overall_coeff;
367         } else if (seq_size==1 && overall_coeff.is_equal(_ex1())) {
368                 // *(x;1) -> x
369                 return recombine_pair_to_ex(*(seq.begin()));
370         } else if ((seq_size==1) &&
371                    is_ex_exactly_of_type((*seq.begin()).rest,add) &&
372                    ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
373                 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
374                 const add & addref = ex_to_add((*seq.begin()).rest);
375                 epvector distrseq;
376                 distrseq.reserve(addref.seq.size());
377                 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
378                         distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
379                 }
380                 return (new add(distrseq,
381                                 ex_to_numeric(addref.overall_coeff).
382                                 mul_dyn(ex_to_numeric(overall_coeff))))
383                       ->setflag(status_flags::dynallocated | status_flags::evaluated);
384         }
385         return this->hold();
386 }
387
388 ex mul::evalf(int level) const
389 {
390         if (level==1)
391                 return mul(seq,overall_coeff);
392         
393         if (level==-max_recursion_level)
394                 throw(std::runtime_error("max recursion level reached"));
395         
396         epvector s;
397         s.reserve(seq.size());
398         
399         --level;
400         for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
401                 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
402                                                           (*it).coeff));
403         }
404         return mul(s,overall_coeff.evalf(level));
405 }
406
407 ex mul::simplify_ncmul(const exvector & v) const
408 {
409         if (seq.size()==0) {
410                 return inherited::simplify_ncmul(v);
411         }
412
413         // Find first noncommutative element and call its simplify_ncmul()
414         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
415                 if (cit->rest.return_type() == return_types::noncommutative)
416                         return cit->rest.simplify_ncmul(v);
417         }
418         return inherited::simplify_ncmul(v);
419 }
420
421 // protected
422
423 /** Implementation of ex::diff() for a product.  It applies the product rule.
424  *  @see ex::diff */
425 ex mul::derivative(const symbol & s) const
426 {
427         exvector addseq;
428         addseq.reserve(seq.size());
429         
430         // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
431         for (unsigned i=0; i!=seq.size(); ++i) {
432                 epvector mulseq = seq;
433                 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
434                                              seq[i].rest.diff(s));
435                 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
436         }
437         return (new add(addseq))->setflag(status_flags::dynallocated);
438 }
439
440 int mul::compare_same_type(const basic & other) const
441 {
442         return inherited::compare_same_type(other);
443 }
444
445 bool mul::is_equal_same_type(const basic & other) const
446 {
447         return inherited::is_equal_same_type(other);
448 }
449
450 unsigned mul::return_type(void) const
451 {
452         if (seq.size()==0) {
453                 // mul without factors: should not happen, but commutes
454                 return return_types::commutative;
455         }
456         
457         bool all_commutative = 1;
458         unsigned rt;
459         epvector::const_iterator cit_noncommutative_element; // point to first found nc element
460         
461         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
462                 rt=(*cit).rest.return_type();
463                 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
464                 if ((rt==return_types::noncommutative)&&(all_commutative)) {
465                         // first nc element found, remember position
466                         cit_noncommutative_element = cit;
467                         all_commutative = 0;
468                 }
469                 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
470                         // another nc element found, compare type_infos
471                         if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
472                                 // diffent types -> mul is ncc
473                                 return return_types::noncommutative_composite;
474                         }
475                 }
476         }
477         // all factors checked
478         return all_commutative ? return_types::commutative : return_types::noncommutative;
479 }
480    
481 unsigned mul::return_type_tinfo(void) const
482 {
483         if (seq.size()==0)
484                 return tinfo_key;  // mul without factors: should not happen
485         
486         // return type_info of first noncommutative element
487         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
488                 if ((*cit).rest.return_type()==return_types::noncommutative)
489                         return (*cit).rest.return_type_tinfo();
490         }
491         // no noncommutative element found, should not happen
492         return tinfo_key;
493 }
494
495 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
496 {
497         return (new mul(v,oc))->setflag(status_flags::dynallocated);
498 }
499
500 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
501 {
502         return (new mul(vp,oc))->setflag(status_flags::dynallocated);
503 }
504
505 expair mul::split_ex_to_pair(const ex & e) const
506 {
507         if (is_ex_exactly_of_type(e,power)) {
508                 const power & powerref = ex_to_power(e);
509                 if (is_ex_exactly_of_type(powerref.exponent,numeric))
510                         return expair(powerref.basis,powerref.exponent);
511         }
512         return expair(e,_ex1());
513 }
514         
515 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
516                                           const ex & c) const
517 {
518         // to avoid duplication of power simplification rules,
519         // we create a temporary power object
520         // otherwise it would be hard to correctly simplify
521         // expression like (4^(1/3))^(3/2)
522         if (are_ex_trivially_equal(c,_ex1()))
523                 return split_ex_to_pair(e);
524         
525         return split_ex_to_pair(power(e,c));
526 }
527         
528 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
529                                             const ex & c) const
530 {
531         // to avoid duplication of power simplification rules,
532         // we create a temporary power object
533         // otherwise it would be hard to correctly simplify
534         // expression like (4^(1/3))^(3/2)
535         if (are_ex_trivially_equal(c,_ex1()))
536                 return p;
537         
538         return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
539 }
540         
541 ex mul::recombine_pair_to_ex(const expair & p) const
542 {
543         if (ex_to_numeric(p.coeff).is_equal(_num1())) 
544                 return p.rest;
545         else
546                 return power(p.rest,p.coeff);
547 }
548
549 bool mul::expair_needs_further_processing(epp it)
550 {
551         if (is_ex_exactly_of_type((*it).rest,mul) &&
552                 ex_to_numeric((*it).coeff).is_integer()) {
553                 // combined pair is product with integer power -> expand it
554                 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
555                 return true;
556         }
557         if (is_ex_exactly_of_type((*it).rest,numeric)) {
558                 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
559                 if (!ep.is_equal(*it)) {
560                         // combined pair is a numeric power which can be simplified
561                         *it = ep;
562                         return true;
563                 }
564                 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
565                         // combined pair has coeff 1 and must be moved to the end
566                         return true;
567                 }
568         }
569         return false;
570 }       
571
572 ex mul::default_overall_coeff(void) const
573 {
574         return _ex1();
575 }
576
577 void mul::combine_overall_coeff(const ex & c)
578 {
579         GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
580         GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
581         overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
582 }
583
584 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
585 {
586         GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
587         GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
588         GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
589         overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
590 }
591
592 bool mul::can_make_flat(const expair & p) const
593 {
594         GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
595         // this assertion will probably fail somewhere
596         // it would require a more careful make_flat, obeying the power laws
597         // probably should return true only if p.coeff is integer
598         return ex_to_numeric(p.coeff).is_equal(_num1());
599 }
600
601 ex mul::expand(unsigned options) const
602 {
603         if (flags & status_flags::expanded)
604                 return *this;
605         
606         exvector sub_expanded_seq;
607         
608         epvector * expanded_seqp = expandchildren(options);
609         
610         const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
611         
612         int number_of_adds = 0;
613         epvector non_adds;
614         non_adds.reserve(expanded_seq.size());
615         epvector::const_iterator cit = expanded_seq.begin();
616         epvector::const_iterator last = expanded_seq.end();
617         ex last_expanded = _ex1();
618         while (cit!=last) {
619                 if (is_ex_exactly_of_type((*cit).rest,add) &&
620                         ((*cit).coeff.is_equal(_ex1()))) {
621                         ++number_of_adds;
622                         if (is_ex_exactly_of_type(last_expanded,add)) {
623                                 // expand adds
624                                 const add & add1 = ex_to_add(last_expanded);
625                                 const add & add2 = ex_to_add((*cit).rest);
626                                 int n1 = add1.nops();
627                                 int n2 = add2.nops();
628                                 exvector distrseq;
629                                 distrseq.reserve(n1*n2);
630                                 for (int i1=0; i1<n1; ++i1) {
631                                         for (int i2=0; i2<n2; ++i2) {
632                                                 distrseq.push_back(add1.op(i1)*add2.op(i2));
633                                         }
634                                 }
635                                 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
636                         } else {
637                                 non_adds.push_back(split_ex_to_pair(last_expanded));
638                                 last_expanded = (*cit).rest;
639                         }
640                 } else {
641                         non_adds.push_back(*cit);
642                 }
643                 ++cit;
644         }
645         if (expanded_seqp)
646                 delete expanded_seqp;
647
648         if (is_ex_exactly_of_type(last_expanded,add)) {
649                 add const & finaladd = ex_to_add(last_expanded);
650                 exvector distrseq;
651                 int n = finaladd.nops();
652                 distrseq.reserve(n);
653                 for (int i=0; i<n; ++i) {
654                         epvector factors = non_adds;
655                         factors.push_back(split_ex_to_pair(finaladd.op(i)));
656                         distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
657                 }
658                 return ((new add(distrseq))->
659                         setflag(status_flags::dynallocated | status_flags::expanded));
660         }
661         non_adds.push_back(split_ex_to_pair(last_expanded));
662         return (new mul(non_adds,overall_coeff))->
663                 setflag(status_flags::dynallocated | status_flags::expanded);
664 }
665
666   
667 //////////
668 // new virtual functions which can be overridden by derived classes
669 //////////
670
671 // none
672
673 //////////
674 // non-virtual functions in this class
675 //////////
676
677
678 /** Member-wise expand the expairs representing this sequence.  This must be
679  *  overridden from expairseq::expandchildren() and done iteratively in order
680  *  to allow for early cancallations and thus safe memory.
681  *
682  *  @see mul::expand()
683  *  @return pointer to epvector containing expanded representation or zero
684  *  pointer, if sequence is unchanged. */
685 epvector * mul::expandchildren(unsigned options) const
686 {
687         epvector::const_iterator last = seq.end();
688         epvector::const_iterator cit = seq.begin();
689         while (cit!=last) {
690                 const ex & factor = recombine_pair_to_ex(*cit);
691                 const ex & expanded_factor = factor.expand(options);
692                 if (!are_ex_trivially_equal(factor,expanded_factor)) {
693                         
694                         // something changed, copy seq, eval and return it
695                         epvector *s = new epvector;
696                         s->reserve(seq.size());
697                         
698                         // copy parts of seq which are known not to have changed
699                         epvector::const_iterator cit2 = seq.begin();
700                         while (cit2!=cit) {
701                                 s->push_back(*cit2);
702                                 ++cit2;
703                         }
704                         // copy first changed element
705                         s->push_back(split_ex_to_pair(expanded_factor));
706                         ++cit2;
707                         // copy rest
708                         while (cit2!=last) {
709                                 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
710                                 ++cit2;
711                         }
712                         return s;
713                 }
714                 ++cit;
715         }
716         
717         return 0; // nothing has changed
718 }
719
720 } // namespace GiNaC