3 * Implementation of GiNaC's products of expressions. */
6 * GiNaC Copyright (C) 1999-2001 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
36 GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
39 // default ctor, dctor, copy ctor assignment operator and helpers
44 debugmsg("mul default ctor",LOGLEVEL_CONSTRUCT);
45 tinfo_key = TINFO_mul;
57 mul::mul(const ex & lh, const ex & rh)
59 debugmsg("mul ctor from ex,ex",LOGLEVEL_CONSTRUCT);
60 tinfo_key = TINFO_mul;
62 construct_from_2_ex(lh,rh);
63 GINAC_ASSERT(is_canonical());
66 mul::mul(const exvector & v)
68 debugmsg("mul ctor from exvector",LOGLEVEL_CONSTRUCT);
69 tinfo_key = TINFO_mul;
71 construct_from_exvector(v);
72 GINAC_ASSERT(is_canonical());
75 mul::mul(const epvector & v)
77 debugmsg("mul ctor from epvector",LOGLEVEL_CONSTRUCT);
78 tinfo_key = TINFO_mul;
80 construct_from_epvector(v);
81 GINAC_ASSERT(is_canonical());
84 mul::mul(const epvector & v, const ex & oc)
86 debugmsg("mul ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
87 tinfo_key = TINFO_mul;
89 construct_from_epvector(v);
90 GINAC_ASSERT(is_canonical());
93 mul::mul(epvector * vp, const ex & oc)
95 debugmsg("mul ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
96 tinfo_key = TINFO_mul;
99 construct_from_epvector(*vp);
101 GINAC_ASSERT(is_canonical());
104 mul::mul(const ex & lh, const ex & mh, const ex & rh)
106 debugmsg("mul ctor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
107 tinfo_key = TINFO_mul;
110 factors.push_back(lh);
111 factors.push_back(mh);
112 factors.push_back(rh);
113 overall_coeff = _ex1;
114 construct_from_exvector(factors);
115 GINAC_ASSERT(is_canonical());
122 DEFAULT_ARCHIVING(mul)
125 // functions overriding virtual functions from base classes
130 void mul::print(const print_context & c, unsigned level) const
132 debugmsg("mul print", LOGLEVEL_PRINT);
134 if (is_a<print_tree>(c)) {
136 inherited::print(c, level);
138 } else if (is_a<print_csrc>(c)) {
140 if (precedence() <= level)
143 if (!overall_coeff.is_equal(_ex1)) {
144 overall_coeff.print(c, precedence());
148 // Print arguments, separated by "*" or "/"
149 epvector::const_iterator it = seq.begin(), itend = seq.end();
150 while (it != itend) {
152 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
153 if (it == seq.begin() && ex_to<numeric>(it->coeff).is_integer() && it->coeff.compare(_num0) < 0) {
154 if (is_a<print_csrc_cl_N>(c))
160 // If the exponent is 1 or -1, it is left out
161 if (it->coeff.compare(_ex1) == 0 || it->coeff.compare(_num_1) == 0)
162 it->rest.print(c, precedence());
164 // Outer parens around ex needed for broken gcc-2.95 parser:
165 (ex(power(it->rest, abs(ex_to<numeric>(it->coeff))))).print(c, level);
168 // Separator is "/" for negative integer powers, "*" otherwise
171 if (ex_to<numeric>(it->coeff).is_integer() && it->coeff.compare(_num0) < 0)
178 if (precedence() <= level)
183 if (precedence() <= level) {
184 if (is_a<print_latex>(c))
192 // First print the overall numeric coefficient
193 numeric coeff = ex_to<numeric>(overall_coeff);
194 if (coeff.csgn() == -1)
196 if (!coeff.is_equal(_num1) &&
197 !coeff.is_equal(_num_1)) {
198 if (coeff.is_rational()) {
199 if (coeff.is_negative())
204 if (coeff.csgn() == -1)
205 (-coeff).print(c, precedence());
207 coeff.print(c, precedence());
209 if (is_a<print_latex>(c))
215 // Then proceed with the remaining factors
216 epvector::const_iterator it = seq.begin(), itend = seq.end();
217 while (it != itend) {
219 if (is_a<print_latex>(c))
226 recombine_pair_to_ex(*it).print(c, precedence());
230 if (precedence() <= level) {
231 if (is_a<print_latex>(c))
239 bool mul::info(unsigned inf) const
242 case info_flags::polynomial:
243 case info_flags::integer_polynomial:
244 case info_flags::cinteger_polynomial:
245 case info_flags::rational_polynomial:
246 case info_flags::crational_polynomial:
247 case info_flags::rational_function: {
248 epvector::const_iterator i = seq.begin(), end = seq.end();
250 if (!(recombine_pair_to_ex(*i).info(inf)))
254 return overall_coeff.info(inf);
256 case info_flags::algebraic: {
257 epvector::const_iterator i = seq.begin(), end = seq.end();
259 if ((recombine_pair_to_ex(*i).info(inf)))
266 return inherited::info(inf);
269 int mul::degree(const ex & s) const
271 // Sum up degrees of factors
273 epvector::const_iterator i = seq.begin(), end = seq.end();
275 if (ex_to<numeric>(i->coeff).is_integer())
276 deg_sum += i->rest.degree(s) * ex_to<numeric>(i->coeff).to_int();
282 int mul::ldegree(const ex & s) const
284 // Sum up degrees of factors
286 epvector::const_iterator i = seq.begin(), end = seq.end();
288 if (ex_to<numeric>(i->coeff).is_integer())
289 deg_sum += i->rest.ldegree(s) * ex_to<numeric>(i->coeff).to_int();
295 ex mul::coeff(const ex & s, int n) const
298 coeffseq.reserve(seq.size()+1);
301 // product of individual coeffs
302 // if a non-zero power of s is found, the resulting product will be 0
303 epvector::const_iterator i = seq.begin(), end = seq.end();
305 coeffseq.push_back(recombine_pair_to_ex(*i).coeff(s,n));
308 coeffseq.push_back(overall_coeff);
309 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
312 epvector::const_iterator i = seq.begin(), end = seq.end();
313 bool coeff_found = false;
315 ex t = recombine_pair_to_ex(*i);
316 ex c = t.coeff(s, n);
318 coeffseq.push_back(c);
321 coeffseq.push_back(t);
326 coeffseq.push_back(overall_coeff);
327 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
333 /** Perform automatic term rewriting rules in this class. In the following
334 * x, x1, x2,... stand for a symbolic variables of type ex and c, c1, c2...
335 * stand for such expressions that contain a plain number.
337 * - *(+(x1,x2,...);c) -> *(+(*(x1,c),*(x2,c),...))
341 * @param level cut-off in recursive evaluation */
342 ex mul::eval(int level) const
344 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
346 epvector *evaled_seqp = evalchildren(level);
348 // do more evaluation later
349 return (new mul(evaled_seqp,overall_coeff))->
350 setflag(status_flags::dynallocated);
353 #ifdef DO_GINAC_ASSERT
354 epvector::const_iterator i = seq.begin(), end = seq.end();
356 GINAC_ASSERT((!is_exactly_a<mul>(i->rest)) ||
357 (!(ex_to<numeric>(i->coeff).is_integer())));
358 GINAC_ASSERT(!(i->is_canonical_numeric()));
359 if (is_ex_exactly_of_type(recombine_pair_to_ex(*i), numeric))
360 print(print_tree(std::cerr));
361 GINAC_ASSERT(!is_exactly_a<numeric>(recombine_pair_to_ex(*i)));
363 expair p = split_ex_to_pair(recombine_pair_to_ex(*i));
364 GINAC_ASSERT(p.rest.is_equal(i->rest));
365 GINAC_ASSERT(p.coeff.is_equal(i->coeff));
369 #endif // def DO_GINAC_ASSERT
371 if (flags & status_flags::evaluated) {
372 GINAC_ASSERT(seq.size()>0);
373 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1));
377 int seq_size = seq.size();
378 if (overall_coeff.is_zero()) {
381 } else if (seq_size==0) {
383 return overall_coeff;
384 } else if (seq_size==1 && overall_coeff.is_equal(_ex1)) {
386 return recombine_pair_to_ex(*(seq.begin()));
387 } else if ((seq_size==1) &&
388 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
389 ex_to<numeric>((*seq.begin()).coeff).is_equal(_num1)) {
390 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
391 const add & addref = ex_to<add>((*seq.begin()).rest);
392 epvector *distrseq = new epvector();
393 distrseq->reserve(addref.seq.size());
394 epvector::const_iterator i = addref.seq.begin(), end = addref.seq.end();
396 distrseq->push_back(addref.combine_pair_with_coeff_to_pair(*i, overall_coeff));
399 return (new add(distrseq,
400 ex_to<numeric>(addref.overall_coeff).
401 mul_dyn(ex_to<numeric>(overall_coeff))))
402 ->setflag(status_flags::dynallocated | status_flags::evaluated);
407 ex mul::evalf(int level) const
410 return mul(seq,overall_coeff);
412 if (level==-max_recursion_level)
413 throw(std::runtime_error("max recursion level reached"));
415 epvector *s = new epvector();
416 s->reserve(seq.size());
419 epvector::const_iterator i = seq.begin(), end = seq.end();
421 s->push_back(combine_ex_with_coeff_to_pair(i->rest.evalf(level),
425 return mul(s, overall_coeff.evalf(level));
428 ex mul::evalm(void) const
431 if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1)
432 && is_ex_of_type(seq[0].rest, matrix))
433 return ex_to<matrix>(seq[0].rest).mul(ex_to<numeric>(overall_coeff));
435 // Evaluate children first, look whether there are any matrices at all
436 // (there can be either no matrices or one matrix; if there were more
437 // than one matrix, it would be a non-commutative product)
438 epvector *s = new epvector;
439 s->reserve(seq.size());
441 bool have_matrix = false;
442 epvector::iterator the_matrix;
444 epvector::const_iterator i = seq.begin(), end = seq.end();
446 const ex &m = recombine_pair_to_ex(*i).evalm();
447 s->push_back(split_ex_to_pair(m));
448 if (is_ex_of_type(m, matrix)) {
450 the_matrix = s->end() - 1;
457 // The product contained a matrix. We will multiply all other factors
459 matrix m = ex_to<matrix>(the_matrix->rest);
460 s->erase(the_matrix);
461 ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
462 return m.mul_scalar(scalar);
465 return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
468 ex mul::simplify_ncmul(const exvector & v) const
471 return inherited::simplify_ncmul(v);
473 // Find first noncommutative element and call its simplify_ncmul()
474 epvector::const_iterator i = seq.begin(), end = seq.end();
476 if (i->rest.return_type() == return_types::noncommutative)
477 return i->rest.simplify_ncmul(v);
480 return inherited::simplify_ncmul(v);
485 /** Implementation of ex::diff() for a product. It applies the product rule.
487 ex mul::derivative(const symbol & s) const
489 unsigned num = seq.size();
493 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
494 epvector mulseq = seq;
495 epvector::const_iterator i = seq.begin(), end = seq.end();
496 epvector::iterator i2 = mulseq.begin();
498 expair ep = split_ex_to_pair(power(i->rest, i->coeff - _ex1) *
501 addseq.push_back((new mul(mulseq, overall_coeff * i->coeff))->setflag(status_flags::dynallocated));
505 return (new add(addseq))->setflag(status_flags::dynallocated);
508 int mul::compare_same_type(const basic & other) const
510 return inherited::compare_same_type(other);
513 bool mul::is_equal_same_type(const basic & other) const
515 return inherited::is_equal_same_type(other);
518 unsigned mul::return_type(void) const
521 // mul without factors: should not happen, but commutes
522 return return_types::commutative;
525 bool all_commutative = true;
526 epvector::const_iterator noncommutative_element; // point to first found nc element
528 epvector::const_iterator i = seq.begin(), end = seq.end();
530 unsigned rt = i->rest.return_type();
531 if (rt == return_types::noncommutative_composite)
532 return rt; // one ncc -> mul also ncc
533 if ((rt == return_types::noncommutative) && (all_commutative)) {
534 // first nc element found, remember position
535 noncommutative_element = i;
536 all_commutative = false;
538 if ((rt == return_types::noncommutative) && (!all_commutative)) {
539 // another nc element found, compare type_infos
540 if (noncommutative_element->rest.return_type_tinfo() != i->rest.return_type_tinfo()) {
541 // diffent types -> mul is ncc
542 return return_types::noncommutative_composite;
547 // all factors checked
548 return all_commutative ? return_types::commutative : return_types::noncommutative;
551 unsigned mul::return_type_tinfo(void) const
554 return tinfo_key; // mul without factors: should not happen
556 // return type_info of first noncommutative element
557 epvector::const_iterator i = seq.begin(), end = seq.end();
559 if (i->rest.return_type() == return_types::noncommutative)
560 return i->rest.return_type_tinfo();
563 // no noncommutative element found, should not happen
567 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
569 return (new mul(v, oc))->setflag(status_flags::dynallocated);
572 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
574 return (new mul(vp, oc))->setflag(status_flags::dynallocated);
577 expair mul::split_ex_to_pair(const ex & e) const
579 if (is_ex_exactly_of_type(e,power)) {
580 const power & powerref = ex_to<power>(e);
581 if (is_ex_exactly_of_type(powerref.exponent,numeric))
582 return expair(powerref.basis,powerref.exponent);
584 return expair(e,_ex1);
587 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
590 // to avoid duplication of power simplification rules,
591 // we create a temporary power object
592 // otherwise it would be hard to correctly simplify
593 // expression like (4^(1/3))^(3/2)
594 if (are_ex_trivially_equal(c,_ex1))
595 return split_ex_to_pair(e);
597 return split_ex_to_pair(power(e,c));
600 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
603 // to avoid duplication of power simplification rules,
604 // we create a temporary power object
605 // otherwise it would be hard to correctly simplify
606 // expression like (4^(1/3))^(3/2)
607 if (are_ex_trivially_equal(c,_ex1))
610 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
613 ex mul::recombine_pair_to_ex(const expair & p) const
615 if (ex_to<numeric>(p.coeff).is_equal(_num1))
618 return power(p.rest,p.coeff);
621 bool mul::expair_needs_further_processing(epp it)
623 if (is_ex_exactly_of_type((*it).rest,mul) &&
624 ex_to<numeric>((*it).coeff).is_integer()) {
625 // combined pair is product with integer power -> expand it
626 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
629 if (is_ex_exactly_of_type((*it).rest,numeric)) {
630 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
631 if (!ep.is_equal(*it)) {
632 // combined pair is a numeric power which can be simplified
636 if (ex_to<numeric>((*it).coeff).is_equal(_num1)) {
637 // combined pair has coeff 1 and must be moved to the end
644 ex mul::default_overall_coeff(void) const
649 void mul::combine_overall_coeff(const ex & c)
651 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
652 GINAC_ASSERT(is_exactly_a<numeric>(c));
653 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c));
656 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
658 GINAC_ASSERT(is_exactly_a<numeric>(overall_coeff));
659 GINAC_ASSERT(is_exactly_a<numeric>(c1));
660 GINAC_ASSERT(is_exactly_a<numeric>(c2));
661 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c1).power(ex_to<numeric>(c2)));
664 bool mul::can_make_flat(const expair & p) const
666 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
667 // this assertion will probably fail somewhere
668 // it would require a more careful make_flat, obeying the power laws
669 // probably should return true only if p.coeff is integer
670 return ex_to<numeric>(p.coeff).is_equal(_num1);
673 ex mul::expand(unsigned options) const
675 // First, expand the children
676 epvector * expanded_seqp = expandchildren(options);
677 const epvector & expanded_seq = (expanded_seqp == NULL) ? seq : *expanded_seqp;
679 // Now, look for all the factors that are sums and multiply each one out
680 // with the next one that is found while collecting the factors which are
682 int number_of_adds = 0;
683 ex last_expanded = _ex1;
685 non_adds.reserve(expanded_seq.size());
686 epvector::const_iterator cit = expanded_seq.begin(), last = expanded_seq.end();
687 while (cit != last) {
688 if (is_ex_exactly_of_type(cit->rest, add) &&
689 (cit->coeff.is_equal(_ex1))) {
691 if (is_ex_exactly_of_type(last_expanded, add)) {
692 const add & add1 = ex_to<add>(last_expanded);
693 const add & add2 = ex_to<add>(cit->rest);
694 int n1 = add1.nops();
695 int n2 = add2.nops();
697 distrseq.reserve(n1*n2);
698 for (int i1=0; i1<n1; ++i1) {
699 for (int i2=0; i2<n2; ++i2) {
700 distrseq.push_back(add1.op(i1) * add2.op(i2));
703 last_expanded = (new add(distrseq))->
704 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
706 non_adds.push_back(split_ex_to_pair(last_expanded));
707 last_expanded = cit->rest;
710 non_adds.push_back(*cit);
715 delete expanded_seqp;
717 // Now the only remaining thing to do is to multiply the factors which
718 // were not sums into the "last_expanded" sum
719 if (is_ex_exactly_of_type(last_expanded, add)) {
720 const add & finaladd = ex_to<add>(last_expanded);
722 int n = finaladd.nops();
724 for (int i=0; i<n; ++i) {
725 epvector factors = non_adds;
726 factors.push_back(split_ex_to_pair(finaladd.op(i)));
727 distrseq.push_back((new mul(factors, overall_coeff))->
728 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
730 return ((new add(distrseq))->
731 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0)));
733 non_adds.push_back(split_ex_to_pair(last_expanded));
734 return (new mul(non_adds, overall_coeff))->
735 setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));
740 // new virtual functions which can be overridden by derived classes
746 // non-virtual functions in this class
750 /** Member-wise expand the expairs representing this sequence. This must be
751 * overridden from expairseq::expandchildren() and done iteratively in order
752 * to allow for early cancallations and thus safe memory.
755 * @return pointer to epvector containing expanded representation or zero
756 * pointer, if sequence is unchanged. */
757 epvector * mul::expandchildren(unsigned options) const
759 epvector::const_iterator last = seq.end();
760 epvector::const_iterator cit = seq.begin();
762 const ex & factor = recombine_pair_to_ex(*cit);
763 const ex & expanded_factor = factor.expand(options);
764 if (!are_ex_trivially_equal(factor,expanded_factor)) {
766 // something changed, copy seq, eval and return it
767 epvector *s = new epvector;
768 s->reserve(seq.size());
770 // copy parts of seq which are known not to have changed
771 epvector::const_iterator cit2 = seq.begin();
776 // copy first changed element
777 s->push_back(split_ex_to_pair(expanded_factor));
781 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
789 return 0; // nothing has changed