3 * Implementation of GiNaC's products of expressions. */
6 * GiNaC Copyright (C) 1999-2002 Johannes Gutenberg University Mainz, Germany
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.
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.
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
31 #include "operators.h"
39 GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
42 // default ctor, dtor, copy ctor, assignment operator and helpers
47 tinfo_key = TINFO_mul;
59 mul::mul(const ex & lh, const ex & rh)
61 tinfo_key = TINFO_mul;
63 construct_from_2_ex(lh,rh);
64 GINAC_ASSERT(is_canonical());
67 mul::mul(const exvector & v)
69 tinfo_key = TINFO_mul;
71 construct_from_exvector(v);
72 GINAC_ASSERT(is_canonical());
75 mul::mul(const epvector & v)
77 tinfo_key = TINFO_mul;
79 construct_from_epvector(v);
80 GINAC_ASSERT(is_canonical());
83 mul::mul(const epvector & v, const ex & oc)
85 tinfo_key = TINFO_mul;
87 construct_from_epvector(v);
88 GINAC_ASSERT(is_canonical());
91 mul::mul(epvector * vp, const ex & oc)
93 tinfo_key = TINFO_mul;
96 construct_from_epvector(*vp);
98 GINAC_ASSERT(is_canonical());
101 mul::mul(const ex & lh, const ex & mh, const ex & rh)
103 tinfo_key = TINFO_mul;
106 factors.push_back(lh);
107 factors.push_back(mh);
108 factors.push_back(rh);
109 overall_coeff = _ex1;
110 construct_from_exvector(factors);
111 GINAC_ASSERT(is_canonical());
118 DEFAULT_ARCHIVING(mul)
121 // functions overriding virtual functions from base classes
125 void mul::print(const print_context & c, unsigned level) const
127 if (is_a<print_tree>(c)) {
129 inherited::print(c, level);
131 } else if (is_a<print_csrc>(c)) {
133 if (precedence() <= level)
136 if (!overall_coeff.is_equal(_ex1)) {
137 overall_coeff.print(c, precedence());
141 // Print arguments, separated by "*" or "/"
142 epvector::const_iterator it = seq.begin(), itend = seq.end();
143 while (it != itend) {
145 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
146 bool needclosingparenthesis = false;
147 if (it == seq.begin() && it->coeff.info(info_flags::negint)) {
148 if (is_a<print_csrc_cl_N>(c)) {
150 needclosingparenthesis = true;
155 // If the exponent is 1 or -1, it is left out
156 if (it->coeff.is_equal(_ex1) || it->coeff.is_equal(_ex_1))
157 it->rest.print(c, precedence());
158 else if (it->coeff.info(info_flags::negint))
159 // Outer parens around ex needed for broken GCC parser:
160 (ex(power(it->rest, -ex_to<numeric>(it->coeff)))).print(c, level);
162 // Outer parens around ex needed for broken GCC parser:
163 (ex(power(it->rest, ex_to<numeric>(it->coeff)))).print(c, level);
165 if (needclosingparenthesis)
168 // Separator is "/" for negative integer powers, "*" otherwise
171 if (it->coeff.info(info_flags::negint))
178 if (precedence() <= level)
181 } else if (is_a<print_python_repr>(c)) {
182 c.s << class_name() << '(';
184 for (unsigned i=1; i<nops(); ++i) {
191 if (precedence() <= level) {
192 if (is_a<print_latex>(c))
198 // First print the overall numeric coefficient
199 const numeric &coeff = ex_to<numeric>(overall_coeff);
200 if (coeff.csgn() == -1)
202 if (!coeff.is_equal(_num1) &&
203 !coeff.is_equal(_num_1)) {
204 if (coeff.is_rational()) {
205 if (coeff.is_negative())
210 if (coeff.csgn() == -1)
211 (-coeff).print(c, precedence());
213 coeff.print(c, precedence());
215 if (is_a<print_latex>(c))
221 // Then proceed with the remaining factors
222 epvector::const_iterator it = seq.begin(), itend = seq.end();
223 if (is_a<print_latex>(c)) {
225 // Separate factors into those with negative numeric exponent
227 exvector neg_powers, others;
228 while (it != itend) {
229 GINAC_ASSERT(is_exactly_a<numeric>(it->coeff));
230 if (ex_to<numeric>(it->coeff).is_negative())
231 neg_powers.push_back(recombine_pair_to_ex(expair(it->rest, -(it->coeff))));
233 others.push_back(recombine_pair_to_ex(*it));
237 if (!neg_powers.empty()) {
239 // Factors with negative exponent are printed as a fraction
241 mul(others).eval().print(c);
243 mul(neg_powers).eval().print(c);
248 // All other factors are printed in the ordinary way
249 exvector::const_iterator vit = others.begin(), vitend = others.end();
250 while (vit != vitend) {
252 vit->print(c, precedence());
260 while (it != itend) {
265 recombine_pair_to_ex(*it).print(c, precedence());
270 if (precedence() <= level) {
271 if (is_a<print_latex>(c))
279 bool mul::info(unsigned inf) const
282 case info_flags::polynomial:
283 case info_flags::integer_polynomial:
284 case info_flags::cinteger_polynomial:
285 case info_flags::rational_polynomial:
286 case info_flags::crational_polynomial:
287 case info_flags::rational_function: {
288 epvector::const_iterator i = seq.begin(), end = seq.end();
290 if (!(recombine_pair_to_ex(*i).info(inf)))
294 return overall_coeff.info(inf);
296 case info_flags::algebraic: {
297 epvector::const_iterator i = seq.begin(), end = seq.end();
299 if ((recombine_pair_to_ex(*i).info(inf)))
306 return inherited::info(inf);
309 int mul::degree(const ex & s) const
311 // Sum up degrees of factors
313 epvector::const_iterator i = seq.begin(), end = seq.end();
315 if (ex_to<numeric>(i->coeff).is_integer())
316 deg_sum += i->rest.degree(s) * ex_to<numeric>(i->coeff).to_int();
322 int mul::ldegree(const ex & s) const
324 // Sum up degrees of factors
326 epvector::const_iterator i = seq.begin(), end = seq.end();
328 if (ex_to<numeric>(i->coeff).is_integer())
329 deg_sum += i->rest.ldegree(s) * ex_to<numeric>(i->coeff).to_int();
335 ex mul::coeff(const ex & s, int n) const
338 coeffseq.reserve(seq.size()+1);
341 // product of individual coeffs
342 // if a non-zero power of s is found, the resulting product will be 0
343 epvector::const_iterator i = seq.begin(), end = seq.end();
345 coeffseq.push_back(recombine_pair_to_ex(*i).coeff(s,n));
348 coeffseq.push_back(overall_coeff);
349 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
352 epvector::const_iterator i = seq.begin(), end = seq.end();
353 bool coeff_found = false;
355 ex t = recombine_pair_to_ex(*i);
356 ex c = t.coeff(s, n);
358 coeffseq.push_back(c);
361 coeffseq.push_back(t);
366 coeffseq.push_back(overall_coeff);
367 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
373 /** Perform automatic term rewriting rules in this class. In the following
374 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
375 * stand for such expressions that contain a plain number.
377 * - *(+(x1,x2,...);c) -> *(+(*(x1,c),*(x2,c),...))
381 * @param level cut-off in recursive evaluation */
382 ex mul::eval(int level) const
384 epvector *evaled_seqp = evalchildren(level);
386 // do more evaluation later
387 return (new mul(evaled_seqp,overall_coeff))->
388 setflag(status_flags::dynallocated);
391 #ifdef DO_GINAC_ASSERT
392 epvector::const_iterator i = seq.begin(), end = seq.end();
394 GINAC_ASSERT((!is_exactly_a<mul>(i->rest)) ||
395 (!(ex_to<numeric>(i->coeff).is_integer())));
396 GINAC_ASSERT(!(i->is_canonical_numeric()));
397 if (is_exactly_a<numeric>(recombine_pair_to_ex(*i)))
398 print(print_tree(std::cerr));
399 GINAC_ASSERT(!is_exactly_a<numeric>(recombine_pair_to_ex(*i)));
401 expair p = split_ex_to_pair(recombine_pair_to_ex(*i));
402 GINAC_ASSERT(p.rest.is_equal(i->rest));
403 GINAC_ASSERT(p.coeff.is_equal(i->coeff));
407 #endif // def DO_GINAC_ASSERT
409 if (flags & status_flags::evaluated) {
410 GINAC_ASSERT(seq.size()>0);
411 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1));
415 int seq_size = seq.size();
416 if (overall_coeff.is_zero()) {
419 } else if (seq_size==0) {
421 return overall_coeff;
422 } else if (seq_size==1 && overall_coeff.is_equal(_ex1)) {
424 return recombine_pair_to_ex(*(seq.begin()));
425 } else if ((seq_size==1) &&
426 is_exactly_a<add>((*seq.begin()).rest) &&
427 ex_to<numeric>((*seq.begin()).coeff).is_equal(_num1)) {
428 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
429 const add & addref = ex_to<add>((*seq.begin()).rest);
430 epvector *distrseq = new epvector();
431 distrseq->reserve(addref.seq.size());
432 epvector::const_iterator i = addref.seq.begin(), end = addref.seq.end();
434 distrseq->push_back(addref.combine_pair_with_coeff_to_pair(*i, overall_coeff));
437 return (new add(distrseq,
438 ex_to<numeric>(addref.overall_coeff).
439 mul_dyn(ex_to<numeric>(overall_coeff))))
440 ->setflag(status_flags::dynallocated | status_flags::evaluated);
445 ex mul::evalf(int level) const
448 return mul(seq,overall_coeff);
450 if (level==-max_recursion_level)
451 throw(std::runtime_error("max recursion level reached"));
453 epvector *s = new epvector();
454 s->reserve(seq.size());
457 epvector::const_iterator i = seq.begin(), end = seq.end();
459 s->push_back(combine_ex_with_coeff_to_pair(i->rest.evalf(level),
463 return mul(s, overall_coeff.evalf(level));
466 ex mul::evalm(void) const
469 if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1)
470 && is_a<matrix>(seq[0].rest))
471 return ex_to<matrix>(seq[0].rest).mul(ex_to<numeric>(overall_coeff));
473 // Evaluate children first, look whether there are any matrices at all
474 // (there can be either no matrices or one matrix; if there were more
475 // than one matrix, it would be a non-commutative product)
476 epvector *s = new epvector;
477 s->reserve(seq.size());
479 bool have_matrix = false;
480 epvector::iterator the_matrix;
482 epvector::const_iterator i = seq.begin(), end = seq.end();
484 const ex &m = recombine_pair_to_ex(*i).evalm();
485 s->push_back(split_ex_to_pair(m));
486 if (is_a<matrix>(m)) {
488 the_matrix = s->end() - 1;
495 // The product contained a matrix. We will multiply all other factors
497 matrix m = ex_to<matrix>(the_matrix->rest);
498 s->erase(the_matrix);
499 ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
500 return m.mul_scalar(scalar);
503 return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
506 ex mul::eval_ncmul(const exvector & v) const
509 return inherited::eval_ncmul(v);
511 // Find first noncommutative element and call its eval_ncmul()
512 epvector::const_iterator i = seq.begin(), end = seq.end();
514 if (i->rest.return_type() == return_types::noncommutative)
515 return i->rest.eval_ncmul(v);
518 return inherited::eval_ncmul(v);
521 bool tryfactsubs(const ex & origfactor, const ex & patternfactor, unsigned & nummatches, lst & repls)
527 if (is_exactly_a<power>(origfactor) && origfactor.op(1).info(info_flags::integer)) {
528 origbase = origfactor.op(0);
529 int expon = ex_to<numeric>(origfactor.op(1)).to_int();
530 origexponent = expon > 0 ? expon : -expon;
531 origexpsign = expon > 0 ? 1 : -1;
533 origbase = origfactor;
542 if (is_exactly_a<power>(patternfactor) && patternfactor.op(1).info(info_flags::integer)) {
543 patternbase = patternfactor.op(0);
544 int expon = ex_to<numeric>(patternfactor.op(1)).to_int();
545 patternexponent = expon > 0 ? expon : -expon;
546 patternexpsign = expon > 0 ? 1 : -1;
548 patternbase = patternfactor;
553 lst saverepls = repls;
554 if (origexponent < patternexponent || origexpsign != patternexpsign || !origbase.match(patternbase,saverepls))
558 int newnummatches = origexponent / patternexponent;
559 if (newnummatches < nummatches)
560 nummatches = newnummatches;
564 ex mul::algebraic_subs_mul(const lst & ls, const lst & lr, unsigned options) const
566 std::vector<bool> subsed(seq.size(), false);
567 exvector subsresult(seq.size());
569 for (int i=0; i<ls.nops(); i++) {
571 if (is_exactly_a<mul>(ls.op(i))) {
573 unsigned nummatches = std::numeric_limits<unsigned>::max();
574 std::vector<bool> currsubsed(seq.size(), false);
578 for (int j=0; j<ls.op(i).nops(); j++) {
580 for (int k=0; k<nops(); k++) {
581 if (currsubsed[k] || subsed[k])
583 if (tryfactsubs(op(k), ls.op(i).op(j), nummatches, repls)) {
584 currsubsed[k] = true;
597 bool foundfirstsubsedfactor = false;
598 for (int j=0; j<subsed.size(); j++) {
600 if (foundfirstsubsedfactor)
601 subsresult[j] = op(j);
603 foundfirstsubsedfactor = true;
604 subsresult[j] = op(j) * power(lr.op(i).subs(ex(repls), subs_options::subs_no_pattern) / ls.op(i).subs(ex(repls), subs_options::subs_no_pattern), nummatches);
612 unsigned nummatches = std::numeric_limits<unsigned>::max();
615 for (int j=0; j<this->nops(); j++) {
616 if (!subsed[j] && tryfactsubs(op(j), ls.op(i), nummatches, repls)) {
618 subsresult[j] = op(j) * power(lr.op(i).subs(ex(repls), subs_options::subs_no_pattern) / ls.op(i).subs(ex(repls), subs_options::subs_no_pattern), nummatches);
624 bool subsfound = false;
625 for (int i=0; i<subsed.size(); i++) {
632 return basic::subs(ls, lr, options | subs_options::subs_algebraic);
634 exvector ev; ev.reserve(nops());
635 for (int i=0; i<nops(); i++) {
637 ev.push_back(subsresult[i]);
642 return (new mul(ev))->setflag(status_flags::dynallocated);
647 /** Implementation of ex::diff() for a product. It applies the product rule.
649 ex mul::derivative(const symbol & s) const
651 unsigned num = seq.size();
655 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
656 epvector mulseq = seq;
657 epvector::const_iterator i = seq.begin(), end = seq.end();
658 epvector::iterator i2 = mulseq.begin();
660 expair ep = split_ex_to_pair(power(i->rest, i->coeff - _ex1) *
663 addseq.push_back((new mul(mulseq, overall_coeff * i->coeff))->setflag(status_flags::dynallocated));
667 return (new add(addseq))->setflag(status_flags::dynallocated);
670 int mul::compare_same_type(const basic & other) const
672 return inherited::compare_same_type(other);
675 unsigned mul::return_type(void) const
678 // mul without factors: should not happen, but commutes
679 return return_types::commutative;
682 bool all_commutative = true;
683 epvector::const_iterator noncommutative_element; // point to first found nc element
685 epvector::const_iterator i = seq.begin(), end = seq.end();
687 unsigned rt = i->rest.return_type();
688 if (rt == return_types::noncommutative_composite)
689 return rt; // one ncc -> mul also ncc
690 if ((rt == return_types::noncommutative) && (all_commutative)) {
691 // first nc element found, remember position
692 noncommutative_element = i;
693 all_commutative = false;
695 if ((rt == return_types::noncommutative) && (!all_commutative)) {
696 // another nc element found, compare type_infos
697 if (noncommutative_element->rest.return_type_tinfo() != i->rest.return_type_tinfo()) {
698 // diffent types -> mul is ncc
699 return return_types::noncommutative_composite;
704 // all factors checked
705 return all_commutative ? return_types::commutative : return_types::noncommutative;
708 unsigned mul::return_type_tinfo(void) const
711 return tinfo_key; // mul without factors: should not happen
713 // return type_info of first noncommutative element
714 epvector::const_iterator i = seq.begin(), end = seq.end();
716 if (i->rest.return_type() == return_types::noncommutative)
717 return i->rest.return_type_tinfo();
720 // no noncommutative element found, should not happen
724 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
726 return (new mul(v, oc))->setflag(status_flags::dynallocated);
729 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
731 return (new mul(vp, oc))->setflag(status_flags::dynallocated);
734 expair mul::split_ex_to_pair(const ex & e) const
736 if (is_exactly_a<power>(e)) {
737 const power & powerref = ex_to<power>(e);
738 if (is_exactly_a<numeric>(powerref.exponent))
739 return expair(powerref.basis,powerref.exponent);
741 return expair(e,_ex1);
744 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
747 // to avoid duplication of power simplification rules,
748 // we create a temporary power object
749 // otherwise it would be hard to correctly evaluate
750 // expression like (4^(1/3))^(3/2)
751 if (c.is_equal(_ex1))
752 return split_ex_to_pair(e);
754 return split_ex_to_pair(power(e,c));
757 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
760 // to avoid duplication of power simplification rules,
761 // we create a temporary power object
762 // otherwise it would be hard to correctly evaluate
763 // expression like (4^(1/3))^(3/2)
764 if (c.is_equal(_ex1))
767 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
770 ex mul::recombine_pair_to_ex(const expair & p) const
772 if (ex_to<numeric>(p.coeff).is_equal(_num1))
775 return (new power(p.rest,p.coeff))->setflag(status_flags::dynallocated);
778 bool mul::expair_needs_further_processing(epp it)
780 if (is_exactly_a<mul>(it->rest) &&
781 ex_to<numeric>(it->coeff).is_integer()) {
782 // combined pair is product with integer power -> expand it
783 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
786 if (is_exactly_a<numeric>(it->rest)) {
787 expair ep = split_ex_to_pair(recombine_pair_to_ex(*it));
788 if (!ep.is_equal(*it)) {
789 // combined pair is a numeric power which can be simplified
793 if (it->coeff.is_equal(_ex1)) {
794 // combined pair has coeff 1 and must be moved to the end
801 ex mul::default_overall_coeff(void) const
806 void mul::combine_overall_coeff(const ex & c)
808 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
809 GINAC_ASSERT(is_exactly_a<numeric>(c));
810 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c));
813 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
815 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
816 GINAC_ASSERT(is_exactly_a<numeric>(c1));
817 GINAC_ASSERT(is_exactly_a<numeric>(c2));
818 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c1).power(ex_to<numeric>(c2)));
821 bool mul::can_make_flat(const expair & p) const
823 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
824 // this assertion will probably fail somewhere
825 // it would require a more careful make_flat, obeying the power laws
826 // probably should return true only if p.coeff is integer
827 return ex_to<numeric>(p.coeff).is_equal(_num1);
830 ex mul::expand(unsigned options) const
832 // First, expand the children
833 epvector * expanded_seqp = expandchildren(options);
834 const epvector & expanded_seq = (expanded_seqp == NULL) ? seq : *expanded_seqp;
836 // Now, look for all the factors that are sums and multiply each one out
837 // with the next one that is found while collecting the factors which are
839 int number_of_adds = 0;
840 ex last_expanded = _ex1;
842 non_adds.reserve(expanded_seq.size());
843 epvector::const_iterator cit = expanded_seq.begin(), last = expanded_seq.end();
844 while (cit != last) {
845 if (is_exactly_a<add>(cit->rest) &&
846 (cit->coeff.is_equal(_ex1))) {
848 if (is_exactly_a<add>(last_expanded)) {
850 // Expand a product of two sums, simple and robust version.
851 const add & add1 = ex_to<add>(last_expanded);
852 const add & add2 = ex_to<add>(cit->rest);
853 const int n1 = add1.nops();
854 const int n2 = add2.nops();
857 distrseq.reserve(n2);
858 for (int i1=0; i1<n1; ++i1) {
860 // cache the first operand (for efficiency):
861 const ex op1 = add1.op(i1);
862 for (int i2=0; i2<n2; ++i2)
863 distrseq.push_back(op1 * add2.op(i2));
864 tmp_accu += (new add(distrseq))->
865 setflag(status_flags::dynallocated);
867 last_expanded = tmp_accu;
869 // Expand a product of two sums, aggressive version.
870 // Caring for the overall coefficients in separate loops can
871 // sometimes give a performance gain of up to 15%!
873 const int sizedifference = ex_to<add>(last_expanded).seq.size()-ex_to<add>(cit->rest).seq.size();
874 // add2 is for the inner loop and should be the bigger of the two sums
875 // in the presence of asymptotically good sorting:
876 const add& add1 = (sizedifference<0 ? ex_to<add>(last_expanded) : ex_to<add>(cit->rest));
877 const add& add2 = (sizedifference<0 ? ex_to<add>(cit->rest) : ex_to<add>(last_expanded));
878 const epvector::const_iterator add1begin = add1.seq.begin();
879 const epvector::const_iterator add1end = add1.seq.end();
880 const epvector::const_iterator add2begin = add2.seq.begin();
881 const epvector::const_iterator add2end = add2.seq.end();
883 distrseq.reserve(add1.seq.size()+add2.seq.size());
884 // Multiply add2 with the overall coefficient of add1 and append it to distrseq:
885 if (!add1.overall_coeff.is_zero()) {
886 if (add1.overall_coeff.is_equal(_ex1))
887 distrseq.insert(distrseq.end(),add2begin,add2end);
889 for (epvector::const_iterator i=add2begin; i!=add2end; ++i)
890 distrseq.push_back(expair(i->rest, ex_to<numeric>(i->coeff).mul_dyn(ex_to<numeric>(add1.overall_coeff))));
892 // Multiply add1 with the overall coefficient of add2 and append it to distrseq:
893 if (!add2.overall_coeff.is_zero()) {
894 if (add2.overall_coeff.is_equal(_ex1))
895 distrseq.insert(distrseq.end(),add1begin,add1end);
897 for (epvector::const_iterator i=add1begin; i!=add1end; ++i)
898 distrseq.push_back(expair(i->rest, ex_to<numeric>(i->coeff).mul_dyn(ex_to<numeric>(add2.overall_coeff))));
900 // Compute the new overall coefficient and put it together:
901 ex tmp_accu = (new add(distrseq, add1.overall_coeff*add2.overall_coeff))->setflag(status_flags::dynallocated);
902 // Multiply explicitly all non-numeric terms of add1 and add2:
903 for (epvector::const_iterator i1=add1begin; i1!=add1end; ++i1) {
904 // We really have to combine terms here in order to compactify
905 // the result. Otherwise it would become waayy tooo bigg.
908 for (epvector::const_iterator i2=add2begin; i2!=add2end; ++i2) {
909 // Don't push_back expairs which might have a rest that evaluates to a numeric,
910 // since that would violate an invariant of expairseq:
911 const ex rest = (new mul(i1->rest, i2->rest))->setflag(status_flags::dynallocated);
912 if (is_exactly_a<numeric>(rest))
913 oc += ex_to<numeric>(rest).mul(ex_to<numeric>(i1->coeff).mul(ex_to<numeric>(i2->coeff)));
915 distrseq.push_back(expair(rest, ex_to<numeric>(i1->coeff).mul_dyn(ex_to<numeric>(i2->coeff))));
917 tmp_accu += (new add(distrseq, oc))->setflag(status_flags::dynallocated);
919 last_expanded = tmp_accu;
922 non_adds.push_back(split_ex_to_pair(last_expanded));
923 last_expanded = cit->rest;
926 non_adds.push_back(*cit);
931 delete expanded_seqp;
933 // Now the only remaining thing to do is to multiply the factors which
934 // were not sums into the "last_expanded" sum
935 if (is_exactly_a<add>(last_expanded)) {
936 const add & finaladd = ex_to<add>(last_expanded);
938 int n = finaladd.nops();
940 for (int i=0; i<n; ++i) {
941 epvector factors = non_adds;
942 factors.push_back(split_ex_to_pair(finaladd.op(i)));
943 distrseq.push_back((new mul(factors, overall_coeff))->
944 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
946 return ((new add(distrseq))->
947 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
949 non_adds.push_back(split_ex_to_pair(last_expanded));
950 return (new mul(non_adds, overall_coeff))->
951 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
956 // new virtual functions which can be overridden by derived classes
962 // non-virtual functions in this class
966 /** Member-wise expand the expairs representing this sequence. This must be
967 * overridden from expairseq::expandchildren() and done iteratively in order
968 * to allow for early cancallations and thus safe memory.
971 * @return pointer to epvector containing expanded representation or zero
972 * pointer, if sequence is unchanged. */
973 epvector * mul::expandchildren(unsigned options) const
975 const epvector::const_iterator last = seq.end();
976 epvector::const_iterator cit = seq.begin();
978 const ex & factor = recombine_pair_to_ex(*cit);
979 const ex & expanded_factor = factor.expand(options);
980 if (!are_ex_trivially_equal(factor,expanded_factor)) {
982 // something changed, copy seq, eval and return it
983 epvector *s = new epvector;
984 s->reserve(seq.size());
986 // copy parts of seq which are known not to have changed
987 epvector::const_iterator cit2 = seq.begin();
992 // copy first changed element
993 s->push_back(split_ex_to_pair(expanded_factor));
997 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
1005 return 0; // nothing has changed
1008 } // namespace GiNaC