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;
61 overall_coeff = _ex1();
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;
70 overall_coeff = _ex1();
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;
79 overall_coeff = _ex1();
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 bases 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.bp->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 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
249 if (!(recombine_pair_to_ex(*i).info(inf)))
252 return overall_coeff.info(inf);
254 case info_flags::algebraic: {
255 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
256 if ((recombine_pair_to_ex(*i).info(inf)))
262 return inherited::info(inf);
265 int mul::degree(const ex & s) const
268 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
269 if (ex_to<numeric>(cit->coeff).is_integer())
270 deg_sum+=cit->rest.degree(s) * ex_to<numeric>(cit->coeff).to_int();
275 int mul::ldegree(const ex & s) const
278 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
279 if (ex_to<numeric>(cit->coeff).is_integer())
280 deg_sum+=cit->rest.ldegree(s) * ex_to<numeric>(cit->coeff).to_int();
285 ex mul::coeff(const ex & s, int n) const
288 coeffseq.reserve(seq.size()+1);
291 // product of individual coeffs
292 // if a non-zero power of s is found, the resulting product will be 0
293 epvector::const_iterator it = seq.begin();
294 while (it!=seq.end()) {
295 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
298 coeffseq.push_back(overall_coeff);
299 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
302 epvector::const_iterator it=seq.begin();
303 bool coeff_found = 0;
304 while (it!=seq.end()) {
305 ex t = recombine_pair_to_ex(*it);
308 coeffseq.push_back(c);
311 coeffseq.push_back(t);
316 coeffseq.push_back(overall_coeff);
317 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
323 ex mul::eval(int level) const
325 // simplifications *(...,x;0) -> 0
326 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
330 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
332 epvector * evaled_seqp = evalchildren(level);
333 if (evaled_seqp!=0) {
334 // do more evaluation later
335 return (new mul(evaled_seqp,overall_coeff))->
336 setflag(status_flags::dynallocated);
339 #ifdef DO_GINAC_ASSERT
340 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
341 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul)) ||
342 (!(ex_to<numeric>((*cit).coeff).is_integer())));
343 GINAC_ASSERT(!(cit->is_canonical_numeric()));
344 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric))
345 print(print_tree(std::cerr));
346 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
348 expair p = split_ex_to_pair(recombine_pair_to_ex(*cit));
349 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
350 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
353 #endif // def DO_GINAC_ASSERT
355 if (flags & status_flags::evaluated) {
356 GINAC_ASSERT(seq.size()>0);
357 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1()));
361 int seq_size = seq.size();
362 if (overall_coeff.is_equal(_ex0())) {
365 } else if (seq_size==0) {
367 return overall_coeff;
368 } else if (seq_size==1 && overall_coeff.is_equal(_ex1())) {
370 return recombine_pair_to_ex(*(seq.begin()));
371 } else if ((seq_size==1) &&
372 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
373 ex_to<numeric>((*seq.begin()).coeff).is_equal(_num1())) {
374 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
375 const add & addref = ex_to<add>((*seq.begin()).rest);
377 distrseq.reserve(addref.seq.size());
378 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
379 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
381 return (new add(distrseq,
382 ex_to<numeric>(addref.overall_coeff).
383 mul_dyn(ex_to<numeric>(overall_coeff))))
384 ->setflag(status_flags::dynallocated | status_flags::evaluated);
389 ex mul::evalf(int level) const
392 return mul(seq,overall_coeff);
394 if (level==-max_recursion_level)
395 throw(std::runtime_error("max recursion level reached"));
398 s.reserve(seq.size());
401 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
402 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
405 return mul(s,overall_coeff.evalf(level));
408 ex mul::evalm(void) const
411 if (seq.size() == 1 && seq[0].coeff.is_equal(_ex1())
412 && is_ex_of_type(seq[0].rest, matrix))
413 return ex_to<matrix>(seq[0].rest).mul(ex_to<numeric>(overall_coeff));
415 // Evaluate children first, look whether there are any matrices at all
416 // (there can be either no matrices or one matrix; if there were more
417 // than one matrix, it would be a non-commutative product)
418 epvector *s = new epvector;
419 s->reserve(seq.size());
421 bool have_matrix = false;
422 epvector::iterator the_matrix;
424 epvector::const_iterator it = seq.begin(), itend = seq.end();
425 while (it != itend) {
426 const ex &m = recombine_pair_to_ex(*it).evalm();
427 s->push_back(split_ex_to_pair(m));
428 if (is_ex_of_type(m, matrix)) {
430 the_matrix = s->end() - 1;
437 // The product contained a matrix. We will multiply all other factors
439 matrix m = ex_to<matrix>(the_matrix->rest);
440 s->erase(the_matrix);
441 ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
442 return m.mul_scalar(scalar);
445 return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
448 ex mul::simplify_ncmul(const exvector & v) const
451 return inherited::simplify_ncmul(v);
454 // Find first noncommutative element and call its simplify_ncmul()
455 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
456 if (cit->rest.return_type() == return_types::noncommutative)
457 return cit->rest.simplify_ncmul(v);
459 return inherited::simplify_ncmul(v);
464 /** Implementation of ex::diff() for a product. It applies the product rule.
466 ex mul::derivative(const symbol & s) const
469 addseq.reserve(seq.size());
471 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
472 for (unsigned i=0; i!=seq.size(); ++i) {
473 epvector mulseq = seq;
474 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
475 seq[i].rest.diff(s));
476 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
478 return (new add(addseq))->setflag(status_flags::dynallocated);
481 int mul::compare_same_type(const basic & other) const
483 return inherited::compare_same_type(other);
486 bool mul::is_equal_same_type(const basic & other) const
488 return inherited::is_equal_same_type(other);
491 unsigned mul::return_type(void) const
494 // mul without factors: should not happen, but commutes
495 return return_types::commutative;
498 bool all_commutative = 1;
500 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
502 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
503 rt=(*cit).rest.return_type();
504 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
505 if ((rt==return_types::noncommutative)&&(all_commutative)) {
506 // first nc element found, remember position
507 cit_noncommutative_element = cit;
510 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
511 // another nc element found, compare type_infos
512 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
513 // diffent types -> mul is ncc
514 return return_types::noncommutative_composite;
518 // all factors checked
519 return all_commutative ? return_types::commutative : return_types::noncommutative;
522 unsigned mul::return_type_tinfo(void) const
525 return tinfo_key; // mul without factors: should not happen
527 // return type_info of first noncommutative element
528 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
529 if ((*cit).rest.return_type()==return_types::noncommutative)
530 return (*cit).rest.return_type_tinfo();
532 // no noncommutative element found, should not happen
536 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
538 return (new mul(v,oc))->setflag(status_flags::dynallocated);
541 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
543 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
546 expair mul::split_ex_to_pair(const ex & e) const
548 if (is_ex_exactly_of_type(e,power)) {
549 const power & powerref = ex_to<power>(e);
550 if (is_ex_exactly_of_type(powerref.exponent,numeric))
551 return expair(powerref.basis,powerref.exponent);
553 return expair(e,_ex1());
556 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
559 // to avoid duplication of power simplification rules,
560 // we create a temporary power object
561 // otherwise it would be hard to correctly simplify
562 // expression like (4^(1/3))^(3/2)
563 if (are_ex_trivially_equal(c,_ex1()))
564 return split_ex_to_pair(e);
566 return split_ex_to_pair(power(e,c));
569 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
572 // to avoid duplication of power simplification rules,
573 // we create a temporary power object
574 // otherwise it would be hard to correctly simplify
575 // expression like (4^(1/3))^(3/2)
576 if (are_ex_trivially_equal(c,_ex1()))
579 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
582 ex mul::recombine_pair_to_ex(const expair & p) const
584 if (ex_to<numeric>(p.coeff).is_equal(_num1()))
587 return power(p.rest,p.coeff);
590 bool mul::expair_needs_further_processing(epp it)
592 if (is_ex_exactly_of_type((*it).rest,mul) &&
593 ex_to<numeric>((*it).coeff).is_integer()) {
594 // combined pair is product with integer power -> expand it
595 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
598 if (is_ex_exactly_of_type((*it).rest,numeric)) {
599 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
600 if (!ep.is_equal(*it)) {
601 // combined pair is a numeric power which can be simplified
605 if (ex_to<numeric>((*it).coeff).is_equal(_num1())) {
606 // combined pair has coeff 1 and must be moved to the end
613 ex mul::default_overall_coeff(void) const
618 void mul::combine_overall_coeff(const ex & c)
620 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
621 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
622 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c));
625 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
627 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
628 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
629 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
630 overall_coeff = ex_to<numeric>(overall_coeff).mul_dyn(ex_to<numeric>(c1).power(ex_to<numeric>(c2)));
633 bool mul::can_make_flat(const expair & p) const
635 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
636 // this assertion will probably fail somewhere
637 // it would require a more careful make_flat, obeying the power laws
638 // probably should return true only if p.coeff is integer
639 return ex_to<numeric>(p.coeff).is_equal(_num1());
642 ex mul::expand(unsigned options) const
644 if (flags & status_flags::expanded)
647 exvector sub_expanded_seq;
649 epvector * expanded_seqp = expandchildren(options);
651 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
653 int number_of_adds = 0;
655 non_adds.reserve(expanded_seq.size());
656 epvector::const_iterator cit = expanded_seq.begin();
657 epvector::const_iterator last = expanded_seq.end();
658 ex last_expanded = _ex1();
660 if (is_ex_exactly_of_type((*cit).rest,add) &&
661 ((*cit).coeff.is_equal(_ex1()))) {
663 if (is_ex_exactly_of_type(last_expanded,add)) {
665 const add & add1 = ex_to<add>(last_expanded);
666 const add & add2 = ex_to<add>((*cit).rest);
667 int n1 = add1.nops();
668 int n2 = add2.nops();
670 distrseq.reserve(n1*n2);
671 for (int i1=0; i1<n1; ++i1) {
672 for (int i2=0; i2<n2; ++i2) {
673 distrseq.push_back(add1.op(i1)*add2.op(i2));
676 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
678 non_adds.push_back(split_ex_to_pair(last_expanded));
679 last_expanded = (*cit).rest;
682 non_adds.push_back(*cit);
687 delete expanded_seqp;
689 if (is_ex_exactly_of_type(last_expanded,add)) {
690 add const & finaladd = ex_to<add>(last_expanded);
692 int n = finaladd.nops();
694 for (int i=0; i<n; ++i) {
695 epvector factors = non_adds;
696 factors.push_back(split_ex_to_pair(finaladd.op(i)));
697 distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
699 return ((new add(distrseq))->
700 setflag(status_flags::dynallocated | status_flags::expanded));
702 non_adds.push_back(split_ex_to_pair(last_expanded));
703 return (new mul(non_adds,overall_coeff))->
704 setflag(status_flags::dynallocated | status_flags::expanded);
709 // new virtual functions which can be overridden by derived classes
715 // non-virtual functions in this class
719 /** Member-wise expand the expairs representing this sequence. This must be
720 * overridden from expairseq::expandchildren() and done iteratively in order
721 * to allow for early cancallations and thus safe memory.
724 * @return pointer to epvector containing expanded representation or zero
725 * pointer, if sequence is unchanged. */
726 epvector * mul::expandchildren(unsigned options) const
728 epvector::const_iterator last = seq.end();
729 epvector::const_iterator cit = seq.begin();
731 const ex & factor = recombine_pair_to_ex(*cit);
732 const ex & expanded_factor = factor.expand(options);
733 if (!are_ex_trivially_equal(factor,expanded_factor)) {
735 // something changed, copy seq, eval and return it
736 epvector *s = new epvector;
737 s->reserve(seq.size());
739 // copy parts of seq which are known not to have changed
740 epvector::const_iterator cit2 = seq.begin();
745 // copy first changed element
746 s->push_back(split_ex_to_pair(expanded_factor));
750 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
758 return 0; // nothing has changed