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