3 * Implementation of GiNaC's products of expressions. */
6 * GiNaC Copyright (C) 1999-2004 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_OPT(mul, expairseq,
40 print_func<print_context>(&mul::do_print).
41 print_func<print_latex>(&mul::do_print_latex).
42 print_func<print_csrc>(&mul::do_print_csrc).
43 print_func<print_tree>(&mul::do_print_tree).
44 print_func<print_python_repr>(&mul::do_print_python_repr))
48 // default constructor
53 tinfo_key = TINFO_mul;
62 mul::mul(const ex & lh, const ex & rh)
64 tinfo_key = TINFO_mul;
66 construct_from_2_ex(lh,rh);
67 GINAC_ASSERT(is_canonical());
70 mul::mul(const exvector & v)
72 tinfo_key = TINFO_mul;
74 construct_from_exvector(v);
75 GINAC_ASSERT(is_canonical());
78 mul::mul(const epvector & v)
80 tinfo_key = TINFO_mul;
82 construct_from_epvector(v);
83 GINAC_ASSERT(is_canonical());
86 mul::mul(const epvector & v, const ex & oc)
88 tinfo_key = TINFO_mul;
90 construct_from_epvector(v);
91 GINAC_ASSERT(is_canonical());
94 mul::mul(std::auto_ptr<epvector> vp, const ex & oc)
96 tinfo_key = TINFO_mul;
97 GINAC_ASSERT(vp.get()!=0);
99 construct_from_epvector(*vp);
100 GINAC_ASSERT(is_canonical());
103 mul::mul(const ex & lh, const ex & mh, const ex & rh)
105 tinfo_key = TINFO_mul;
108 factors.push_back(lh);
109 factors.push_back(mh);
110 factors.push_back(rh);
111 overall_coeff = _ex1;
112 construct_from_exvector(factors);
113 GINAC_ASSERT(is_canonical());
120 DEFAULT_ARCHIVING(mul)
123 // functions overriding virtual functions from base classes
126 void mul::print_overall_coeff(const print_context & c, const char *mul_sym) const
128 const numeric &coeff = ex_to<numeric>(overall_coeff);
129 if (coeff.csgn() == -1)
131 if (!coeff.is_equal(_num1) &&
132 !coeff.is_equal(_num_1)) {
133 if (coeff.is_rational()) {
134 if (coeff.is_negative())
139 if (coeff.csgn() == -1)
140 (-coeff).print(c, precedence());
142 coeff.print(c, precedence());
148 void mul::do_print(const print_context & c, unsigned level) const
150 if (precedence() <= level)
153 print_overall_coeff(c, "*");
155 epvector::const_iterator it = seq.begin(), itend = seq.end();
157 while (it != itend) {
162 recombine_pair_to_ex(*it).print(c, precedence());
166 if (precedence() <= level)
170 void mul::do_print_latex(const print_latex & c, unsigned level) const
172 if (precedence() <= level)
175 print_overall_coeff(c, " ");
177 // Separate factors into those with negative numeric exponent
179 epvector::const_iterator it = seq.begin(), itend = seq.end();
180 exvector neg_powers, others;
181 while (it != itend) {
182 GINAC_ASSERT(is_exactly_a<numeric>(it->coeff));
183 if (ex_to<numeric>(it->coeff).is_negative())
184 neg_powers.push_back(recombine_pair_to_ex(expair(it->rest, -(it->coeff))));
186 others.push_back(recombine_pair_to_ex(*it));
190 if (!neg_powers.empty()) {
192 // Factors with negative exponent are printed as a fraction
194 mul(others).eval().print(c);
196 mul(neg_powers).eval().print(c);
201 // All other factors are printed in the ordinary way
202 exvector::const_iterator vit = others.begin(), vitend = others.end();
203 while (vit != vitend) {
205 vit->print(c, precedence());
210 if (precedence() <= level)
214 void mul::do_print_csrc(const print_csrc & c, unsigned level) const
216 if (precedence() <= level)
219 if (!overall_coeff.is_equal(_ex1)) {
220 overall_coeff.print(c, precedence());
224 // Print arguments, separated by "*" or "/"
225 epvector::const_iterator it = seq.begin(), itend = seq.end();
226 while (it != itend) {
228 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
229 bool needclosingparenthesis = false;
230 if (it == seq.begin() && it->coeff.info(info_flags::negint)) {
231 if (is_a<print_csrc_cl_N>(c)) {
233 needclosingparenthesis = true;
238 // If the exponent is 1 or -1, it is left out
239 if (it->coeff.is_equal(_ex1) || it->coeff.is_equal(_ex_1))
240 it->rest.print(c, precedence());
241 else if (it->coeff.info(info_flags::negint))
242 // Outer parens around ex needed for broken GCC parser:
243 (ex(power(it->rest, -ex_to<numeric>(it->coeff)))).print(c, level);
245 // Outer parens around ex needed for broken GCC parser:
246 (ex(power(it->rest, ex_to<numeric>(it->coeff)))).print(c, level);
248 if (needclosingparenthesis)
251 // Separator is "/" for negative integer powers, "*" otherwise
254 if (it->coeff.info(info_flags::negint))
261 if (precedence() <= level)
265 void mul::do_print_python_repr(const print_python_repr & c, unsigned level) const
267 c.s << class_name() << '(';
269 for (size_t i=1; i<nops(); ++i) {
276 bool mul::info(unsigned inf) const
279 case info_flags::polynomial:
280 case info_flags::integer_polynomial:
281 case info_flags::cinteger_polynomial:
282 case info_flags::rational_polynomial:
283 case info_flags::crational_polynomial:
284 case info_flags::rational_function: {
285 epvector::const_iterator i = seq.begin(), end = seq.end();
287 if (!(recombine_pair_to_ex(*i).info(inf)))
291 return overall_coeff.info(inf);
293 case info_flags::algebraic: {
294 epvector::const_iterator i = seq.begin(), end = seq.end();
296 if ((recombine_pair_to_ex(*i).info(inf)))
303 return inherited::info(inf);
306 int mul::degree(const ex & s) const
308 // Sum up degrees of factors
310 epvector::const_iterator i = seq.begin(), end = seq.end();
312 if (ex_to<numeric>(i->coeff).is_integer())
313 deg_sum += i->rest.degree(s) * ex_to<numeric>(i->coeff).to_int();
319 int mul::ldegree(const ex & s) const
321 // Sum up degrees of factors
323 epvector::const_iterator i = seq.begin(), end = seq.end();
325 if (ex_to<numeric>(i->coeff).is_integer())
326 deg_sum += i->rest.ldegree(s) * ex_to<numeric>(i->coeff).to_int();
332 ex mul::coeff(const ex & s, int n) const
335 coeffseq.reserve(seq.size()+1);
338 // product of individual coeffs
339 // if a non-zero power of s is found, the resulting product will be 0
340 epvector::const_iterator i = seq.begin(), end = seq.end();
342 coeffseq.push_back(recombine_pair_to_ex(*i).coeff(s,n));
345 coeffseq.push_back(overall_coeff);
346 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
349 epvector::const_iterator i = seq.begin(), end = seq.end();
350 bool coeff_found = false;
352 ex t = recombine_pair_to_ex(*i);
353 ex c = t.coeff(s, n);
355 coeffseq.push_back(c);
358 coeffseq.push_back(t);
363 coeffseq.push_back(overall_coeff);
364 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
370 /** Perform automatic term rewriting rules in this class. In the following
371 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
372 * stand for such expressions that contain a plain number.
374 * - *(+(x1,x2,...);c) -> *(+(*(x1,c),*(x2,c),...))
378 * @param level cut-off in recursive evaluation */
379 ex mul::eval(int level) const
381 std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
382 if (evaled_seqp.get()) {
383 // do more evaluation later
384 return (new mul(evaled_seqp, overall_coeff))->
385 setflag(status_flags::dynallocated);
388 #ifdef DO_GINAC_ASSERT
389 epvector::const_iterator i = seq.begin(), end = seq.end();
391 GINAC_ASSERT((!is_exactly_a<mul>(i->rest)) ||
392 (!(ex_to<numeric>(i->coeff).is_integer())));
393 GINAC_ASSERT(!(i->is_canonical_numeric()));
394 if (is_exactly_a<numeric>(recombine_pair_to_ex(*i)))
395 print(print_tree(std::cerr));
396 GINAC_ASSERT(!is_exactly_a<numeric>(recombine_pair_to_ex(*i)));
398 expair p = split_ex_to_pair(recombine_pair_to_ex(*i));
399 GINAC_ASSERT(p.rest.is_equal(i->rest));
400 GINAC_ASSERT(p.coeff.is_equal(i->coeff));
404 #endif // def DO_GINAC_ASSERT
406 if (flags & status_flags::evaluated) {
407 GINAC_ASSERT(seq.size()>0);
408 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1));
412 int seq_size = seq.size();
413 if (overall_coeff.is_zero()) {
416 } else if (seq_size==0) {
418 return overall_coeff;
419 } else if (seq_size==1 && overall_coeff.is_equal(_ex1)) {
421 return recombine_pair_to_ex(*(seq.begin()));
422 } else if ((seq_size==1) &&
423 is_exactly_a<add>((*seq.begin()).rest) &&
424 ex_to<numeric>((*seq.begin()).coeff).is_equal(_num1)) {
425 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
426 const add & addref = ex_to<add>((*seq.begin()).rest);
427 std::auto_ptr<epvector> distrseq(new epvector);
428 distrseq->reserve(addref.seq.size());
429 epvector::const_iterator i = addref.seq.begin(), end = addref.seq.end();
431 distrseq->push_back(addref.combine_pair_with_coeff_to_pair(*i, overall_coeff));
434 return (new add(distrseq,
435 ex_to<numeric>(addref.overall_coeff).
436 mul_dyn(ex_to<numeric>(overall_coeff))))
437 ->setflag(status_flags::dynallocated | status_flags::evaluated);
442 ex mul::evalf(int level) const
445 return mul(seq,overall_coeff);
447 if (level==-max_recursion_level)
448 throw(std::runtime_error("max recursion level reached"));
450 std::auto_ptr<epvector> s(new epvector);
451 s->reserve(seq.size());
454 epvector::const_iterator i = seq.begin(), end = seq.end();
456 s->push_back(combine_ex_with_coeff_to_pair(i->rest.evalf(level),
460 return mul(s, overall_coeff.evalf(level));
463 ex mul::evalm() const
466 if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1)
467 && is_a<matrix>(seq[0].rest))
468 return ex_to<matrix>(seq[0].rest).mul(ex_to<numeric>(overall_coeff));
470 // Evaluate children first, look whether there are any matrices at all
471 // (there can be either no matrices or one matrix; if there were more
472 // than one matrix, it would be a non-commutative product)
473 std::auto_ptr<epvector> s(new epvector);
474 s->reserve(seq.size());
476 bool have_matrix = false;
477 epvector::iterator the_matrix;
479 epvector::const_iterator i = seq.begin(), end = seq.end();
481 const ex &m = recombine_pair_to_ex(*i).evalm();
482 s->push_back(split_ex_to_pair(m));
483 if (is_a<matrix>(m)) {
485 the_matrix = s->end() - 1;
492 // The product contained a matrix. We will multiply all other factors
494 matrix m = ex_to<matrix>(the_matrix->rest);
495 s->erase(the_matrix);
496 ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
497 return m.mul_scalar(scalar);
500 return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
503 ex mul::eval_ncmul(const exvector & v) const
506 return inherited::eval_ncmul(v);
508 // Find first noncommutative element and call its eval_ncmul()
509 epvector::const_iterator i = seq.begin(), end = seq.end();
511 if (i->rest.return_type() == return_types::noncommutative)
512 return i->rest.eval_ncmul(v);
515 return inherited::eval_ncmul(v);
518 bool tryfactsubs(const ex & origfactor, const ex & patternfactor, int & nummatches, lst & repls)
524 if (is_exactly_a<power>(origfactor) && origfactor.op(1).info(info_flags::integer)) {
525 origbase = origfactor.op(0);
526 int expon = ex_to<numeric>(origfactor.op(1)).to_int();
527 origexponent = expon > 0 ? expon : -expon;
528 origexpsign = expon > 0 ? 1 : -1;
530 origbase = origfactor;
539 if (is_exactly_a<power>(patternfactor) && patternfactor.op(1).info(info_flags::integer)) {
540 patternbase = patternfactor.op(0);
541 int expon = ex_to<numeric>(patternfactor.op(1)).to_int();
542 patternexponent = expon > 0 ? expon : -expon;
543 patternexpsign = expon > 0 ? 1 : -1;
545 patternbase = patternfactor;
550 lst saverepls = repls;
551 if (origexponent < patternexponent || origexpsign != patternexpsign || !origbase.match(patternbase,saverepls))
555 int newnummatches = origexponent / patternexponent;
556 if (newnummatches < nummatches)
557 nummatches = newnummatches;
561 ex mul::algebraic_subs_mul(const exmap & m, unsigned options) const
563 std::vector<bool> subsed(seq.size(), false);
564 exvector subsresult(seq.size());
566 for (exmap::const_iterator it = m.begin(); it != m.end(); ++it) {
568 if (is_exactly_a<mul>(it->first)) {
570 int nummatches = std::numeric_limits<int>::max();
571 std::vector<bool> currsubsed(seq.size(), false);
575 for (size_t j=0; j<it->first.nops(); j++) {
577 for (size_t k=0; k<nops(); k++) {
578 if (currsubsed[k] || subsed[k])
580 if (tryfactsubs(op(k), it->first.op(j), nummatches, repls)) {
581 currsubsed[k] = true;
594 bool foundfirstsubsedfactor = false;
595 for (size_t j=0; j<subsed.size(); j++) {
597 if (foundfirstsubsedfactor)
598 subsresult[j] = op(j);
600 foundfirstsubsedfactor = true;
601 subsresult[j] = op(j) * power(it->second.subs(ex(repls), subs_options::no_pattern) / it->first.subs(ex(repls), subs_options::no_pattern), nummatches);
609 int nummatches = std::numeric_limits<int>::max();
612 for (size_t j=0; j<this->nops(); j++) {
613 if (!subsed[j] && tryfactsubs(op(j), it->first, nummatches, repls)) {
615 subsresult[j] = op(j) * power(it->second.subs(ex(repls), subs_options::no_pattern) / it->first.subs(ex(repls), subs_options::no_pattern), nummatches);
621 bool subsfound = false;
622 for (size_t i=0; i<subsed.size(); i++) {
629 return subs_one_level(m, options | subs_options::algebraic);
631 exvector ev; ev.reserve(nops());
632 for (size_t i=0; i<nops(); i++) {
634 ev.push_back(subsresult[i]);
639 return (new mul(ev))->setflag(status_flags::dynallocated);
644 /** Implementation of ex::diff() for a product. It applies the product rule.
646 ex mul::derivative(const symbol & s) const
648 size_t num = seq.size();
652 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
653 epvector mulseq = seq;
654 epvector::const_iterator i = seq.begin(), end = seq.end();
655 epvector::iterator i2 = mulseq.begin();
657 expair ep = split_ex_to_pair(power(i->rest, i->coeff - _ex1) *
660 addseq.push_back((new mul(mulseq, overall_coeff * i->coeff))->setflag(status_flags::dynallocated));
664 return (new add(addseq))->setflag(status_flags::dynallocated);
667 int mul::compare_same_type(const basic & other) const
669 return inherited::compare_same_type(other);
672 unsigned mul::return_type() const
675 // mul without factors: should not happen, but commutates
676 return return_types::commutative;
679 bool all_commutative = true;
680 epvector::const_iterator noncommutative_element; // point to first found nc element
682 epvector::const_iterator i = seq.begin(), end = seq.end();
684 unsigned rt = i->rest.return_type();
685 if (rt == return_types::noncommutative_composite)
686 return rt; // one ncc -> mul also ncc
687 if ((rt == return_types::noncommutative) && (all_commutative)) {
688 // first nc element found, remember position
689 noncommutative_element = i;
690 all_commutative = false;
692 if ((rt == return_types::noncommutative) && (!all_commutative)) {
693 // another nc element found, compare type_infos
694 if (noncommutative_element->rest.return_type_tinfo() != i->rest.return_type_tinfo()) {
695 // diffent types -> mul is ncc
696 return return_types::noncommutative_composite;
701 // all factors checked
702 return all_commutative ? return_types::commutative : return_types::noncommutative;
705 unsigned mul::return_type_tinfo() const
708 return tinfo_key; // mul without factors: should not happen
710 // return type_info of first noncommutative element
711 epvector::const_iterator i = seq.begin(), end = seq.end();
713 if (i->rest.return_type() == return_types::noncommutative)
714 return i->rest.return_type_tinfo();
717 // no noncommutative element found, should not happen
721 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
723 return (new mul(v, oc))->setflag(status_flags::dynallocated);
726 ex mul::thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc) const
728 return (new mul(vp, oc))->setflag(status_flags::dynallocated);
731 expair mul::split_ex_to_pair(const ex & e) const
733 if (is_exactly_a<power>(e)) {
734 const power & powerref = ex_to<power>(e);
735 if (is_exactly_a<numeric>(powerref.exponent))
736 return expair(powerref.basis,powerref.exponent);
738 return expair(e,_ex1);
741 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
744 // to avoid duplication of power simplification rules,
745 // we create a temporary power object
746 // otherwise it would be hard to correctly evaluate
747 // expression like (4^(1/3))^(3/2)
748 if (c.is_equal(_ex1))
749 return split_ex_to_pair(e);
751 return split_ex_to_pair(power(e,c));
754 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
757 // to avoid duplication of power simplification rules,
758 // we create a temporary power object
759 // otherwise it would be hard to correctly evaluate
760 // expression like (4^(1/3))^(3/2)
761 if (c.is_equal(_ex1))
764 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
767 ex mul::recombine_pair_to_ex(const expair & p) const
769 if (ex_to<numeric>(p.coeff).is_equal(_num1))
772 return (new power(p.rest,p.coeff))->setflag(status_flags::dynallocated);
775 bool mul::expair_needs_further_processing(epp it)
777 if (is_exactly_a<mul>(it->rest) &&
778 ex_to<numeric>(it->coeff).is_integer()) {
779 // combined pair is product with integer power -> expand it
780 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
783 if (is_exactly_a<numeric>(it->rest)) {
784 expair ep = split_ex_to_pair(recombine_pair_to_ex(*it));
785 if (!ep.is_equal(*it)) {
786 // combined pair is a numeric power which can be simplified
790 if (it->coeff.is_equal(_ex1)) {
791 // combined pair has coeff 1 and must be moved to the end
798 ex mul::default_overall_coeff() const
803 void mul::combine_overall_coeff(const ex & c)
805 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
806 GINAC_ASSERT(is_exactly_a<numeric>(c));
807 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c));
810 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
812 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
813 GINAC_ASSERT(is_exactly_a<numeric>(c1));
814 GINAC_ASSERT(is_exactly_a<numeric>(c2));
815 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c1).power(ex_to<numeric>(c2)));
818 bool mul::can_make_flat(const expair & p) const
820 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
821 // this assertion will probably fail somewhere
822 // it would require a more careful make_flat, obeying the power laws
823 // probably should return true only if p.coeff is integer
824 return ex_to<numeric>(p.coeff).is_equal(_num1);
827 bool mul::can_be_further_expanded(const ex & e)
829 if (is_exactly_a<mul>(e)) {
830 for (epvector::const_iterator cit = ex_to<mul>(e).seq.begin(); cit != ex_to<mul>(e).seq.end(); ++cit) {
831 if (is_exactly_a<add>(cit->rest) && cit->coeff.info(info_flags::posint))
834 } else if (is_exactly_a<power>(e)) {
835 if (is_exactly_a<add>(e.op(0)) && e.op(1).info(info_flags::posint))
841 ex mul::expand(unsigned options) const
843 // First, expand the children
844 std::auto_ptr<epvector> expanded_seqp = expandchildren(options);
845 const epvector & expanded_seq = (expanded_seqp.get() ? *expanded_seqp : seq);
847 // Now, look for all the factors that are sums and multiply each one out
848 // with the next one that is found while collecting the factors which are
850 ex last_expanded = _ex1;
851 bool need_reexpand = false;
854 non_adds.reserve(expanded_seq.size());
856 for (epvector::const_iterator cit = expanded_seq.begin(); cit != expanded_seq.end(); ++cit) {
857 if (is_exactly_a<add>(cit->rest) &&
858 (cit->coeff.is_equal(_ex1))) {
859 if (is_exactly_a<add>(last_expanded)) {
861 // Expand a product of two sums, aggressive version.
862 // Caring for the overall coefficients in separate loops can
863 // sometimes give a performance gain of up to 15%!
865 const int sizedifference = ex_to<add>(last_expanded).seq.size()-ex_to<add>(cit->rest).seq.size();
866 // add2 is for the inner loop and should be the bigger of the two sums
867 // in the presence of asymptotically good sorting:
868 const add& add1 = (sizedifference<0 ? ex_to<add>(last_expanded) : ex_to<add>(cit->rest));
869 const add& add2 = (sizedifference<0 ? ex_to<add>(cit->rest) : ex_to<add>(last_expanded));
870 const epvector::const_iterator add1begin = add1.seq.begin();
871 const epvector::const_iterator add1end = add1.seq.end();
872 const epvector::const_iterator add2begin = add2.seq.begin();
873 const epvector::const_iterator add2end = add2.seq.end();
875 distrseq.reserve(add1.seq.size()+add2.seq.size());
877 // Multiply add2 with the overall coefficient of add1 and append it to distrseq:
878 if (!add1.overall_coeff.is_zero()) {
879 if (add1.overall_coeff.is_equal(_ex1))
880 distrseq.insert(distrseq.end(),add2begin,add2end);
882 for (epvector::const_iterator i=add2begin; i!=add2end; ++i)
883 distrseq.push_back(expair(i->rest, ex_to<numeric>(i->coeff).mul_dyn(ex_to<numeric>(add1.overall_coeff))));
886 // Multiply add1 with the overall coefficient of add2 and append it to distrseq:
887 if (!add2.overall_coeff.is_zero()) {
888 if (add2.overall_coeff.is_equal(_ex1))
889 distrseq.insert(distrseq.end(),add1begin,add1end);
891 for (epvector::const_iterator i=add1begin; i!=add1end; ++i)
892 distrseq.push_back(expair(i->rest, ex_to<numeric>(i->coeff).mul_dyn(ex_to<numeric>(add2.overall_coeff))));
895 // Compute the new overall coefficient and put it together:
896 ex tmp_accu = (new add(distrseq, add1.overall_coeff*add2.overall_coeff))->setflag(status_flags::dynallocated);
898 // Multiply explicitly all non-numeric terms of add1 and add2:
899 for (epvector::const_iterator i1=add1begin; i1!=add1end; ++i1) {
900 // We really have to combine terms here in order to compactify
901 // the result. Otherwise it would become waayy tooo bigg.
904 for (epvector::const_iterator i2=add2begin; i2!=add2end; ++i2) {
905 // Don't push_back expairs which might have a rest that evaluates to a numeric,
906 // since that would violate an invariant of expairseq:
907 const ex rest = (new mul(i1->rest, i2->rest))->setflag(status_flags::dynallocated);
908 if (is_exactly_a<numeric>(rest))
909 oc += ex_to<numeric>(rest).mul(ex_to<numeric>(i1->coeff).mul(ex_to<numeric>(i2->coeff)));
911 distrseq.push_back(expair(rest, ex_to<numeric>(i1->coeff).mul_dyn(ex_to<numeric>(i2->coeff))));
913 tmp_accu += (new add(distrseq, oc))->setflag(status_flags::dynallocated);
915 last_expanded = tmp_accu;
918 if (!last_expanded.is_equal(_ex1))
919 non_adds.push_back(split_ex_to_pair(last_expanded));
920 last_expanded = cit->rest;
924 non_adds.push_back(*cit);
928 // Now the only remaining thing to do is to multiply the factors which
929 // were not sums into the "last_expanded" sum
930 if (is_exactly_a<add>(last_expanded)) {
931 size_t n = last_expanded.nops();
935 for (size_t i=0; i<n; ++i) {
936 epvector factors = non_adds;
937 factors.push_back(split_ex_to_pair(last_expanded.op(i)));
938 ex term = (new mul(factors, overall_coeff))->setflag(status_flags::dynallocated);
939 if (can_be_further_expanded(term))
940 distrseq.push_back(term.expand());
943 ex_to<basic>(term).setflag(status_flags::expanded);
944 distrseq.push_back(term);
948 return ((new add(distrseq))->
949 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
952 non_adds.push_back(split_ex_to_pair(last_expanded));
953 ex result = (new mul(non_adds, overall_coeff))->setflag(status_flags::dynallocated);
954 if (can_be_further_expanded(result)) {
955 return result.expand();
958 ex_to<basic>(result).setflag(status_flags::expanded);
965 // new virtual functions which can be overridden by derived classes
971 // non-virtual functions in this class
975 /** Member-wise expand the expairs representing this sequence. This must be
976 * overridden from expairseq::expandchildren() and done iteratively in order
977 * to allow for early cancallations and thus safe memory.
980 * @return pointer to epvector containing expanded representation or zero
981 * pointer, if sequence is unchanged. */
982 std::auto_ptr<epvector> mul::expandchildren(unsigned options) const
984 const epvector::const_iterator last = seq.end();
985 epvector::const_iterator cit = seq.begin();
987 const ex & factor = recombine_pair_to_ex(*cit);
988 const ex & expanded_factor = factor.expand(options);
989 if (!are_ex_trivially_equal(factor,expanded_factor)) {
991 // something changed, copy seq, eval and return it
992 std::auto_ptr<epvector> s(new epvector);
993 s->reserve(seq.size());
995 // copy parts of seq which are known not to have changed
996 epvector::const_iterator cit2 = seq.begin();
1002 // copy first changed element
1003 s->push_back(split_ex_to_pair(expanded_factor));
1007 while (cit2!=last) {
1008 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
1016 return std::auto_ptr<epvector>(0); // nothing has changed
1019 } // namespace GiNaC