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_of_type(c, print_tree)) {
136 inherited::print(c, level);
138 } else if (is_of_type(c, print_csrc)) {
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_of_type(c, print_csrc_cl_N))
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_of_type(c, print_latex))
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_of_type(c, print_latex))
215 // Then proceed with the remaining factors
216 epvector::const_iterator it = seq.begin(), itend = seq.end();
217 while (it != itend) {
219 if (is_of_type(c, print_latex))
226 recombine_pair_to_ex(*it).print(c, precedence());
230 if (precedence() <= level) {
231 if (is_of_type(c, print_latex))
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 && is_ex_of_type(seq[0].rest, matrix))
412 return ex_to_matrix(seq[0].rest).mul(ex_to_numeric(overall_coeff));
414 // Evaluate children first, look whether there are any matrices at all
415 // (there can be either no matrices or one matrix; if there were more
416 // than one matrix, it would be a non-commutative product)
417 epvector *s = new epvector;
418 s->reserve(seq.size());
420 bool have_matrix = false;
421 epvector::iterator the_matrix;
423 epvector::const_iterator it = seq.begin(), itend = seq.end();
424 while (it != itend) {
425 const ex &m = recombine_pair_to_ex(*it).evalm();
426 s->push_back(split_ex_to_pair(m));
427 if (is_ex_of_type(m, matrix)) {
429 the_matrix = s->end() - 1;
436 // The product contained a matrix. We will multiply all other factors
438 matrix m = ex_to_matrix(the_matrix->rest);
439 s->erase(the_matrix);
440 ex scalar = (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
441 return m.mul_scalar(scalar);
444 return (new mul(s, overall_coeff))->setflag(status_flags::dynallocated);
447 ex mul::simplify_ncmul(const exvector & v) const
450 return inherited::simplify_ncmul(v);
453 // Find first noncommutative element and call its simplify_ncmul()
454 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
455 if (cit->rest.return_type() == return_types::noncommutative)
456 return cit->rest.simplify_ncmul(v);
458 return inherited::simplify_ncmul(v);
463 /** Implementation of ex::diff() for a product. It applies the product rule.
465 ex mul::derivative(const symbol & s) const
468 addseq.reserve(seq.size());
470 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
471 for (unsigned i=0; i!=seq.size(); ++i) {
472 epvector mulseq = seq;
473 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
474 seq[i].rest.diff(s));
475 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
477 return (new add(addseq))->setflag(status_flags::dynallocated);
480 int mul::compare_same_type(const basic & other) const
482 return inherited::compare_same_type(other);
485 bool mul::is_equal_same_type(const basic & other) const
487 return inherited::is_equal_same_type(other);
490 unsigned mul::return_type(void) const
493 // mul without factors: should not happen, but commutes
494 return return_types::commutative;
497 bool all_commutative = 1;
499 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
501 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
502 rt=(*cit).rest.return_type();
503 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
504 if ((rt==return_types::noncommutative)&&(all_commutative)) {
505 // first nc element found, remember position
506 cit_noncommutative_element = cit;
509 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
510 // another nc element found, compare type_infos
511 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
512 // diffent types -> mul is ncc
513 return return_types::noncommutative_composite;
517 // all factors checked
518 return all_commutative ? return_types::commutative : return_types::noncommutative;
521 unsigned mul::return_type_tinfo(void) const
524 return tinfo_key; // mul without factors: should not happen
526 // return type_info of first noncommutative element
527 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
528 if ((*cit).rest.return_type()==return_types::noncommutative)
529 return (*cit).rest.return_type_tinfo();
531 // no noncommutative element found, should not happen
535 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
537 return (new mul(v,oc))->setflag(status_flags::dynallocated);
540 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
542 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
545 expair mul::split_ex_to_pair(const ex & e) const
547 if (is_ex_exactly_of_type(e,power)) {
548 const power & powerref = ex_to_power(e);
549 if (is_ex_exactly_of_type(powerref.exponent,numeric))
550 return expair(powerref.basis,powerref.exponent);
552 return expair(e,_ex1());
555 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
558 // to avoid duplication of power simplification rules,
559 // we create a temporary power object
560 // otherwise it would be hard to correctly simplify
561 // expression like (4^(1/3))^(3/2)
562 if (are_ex_trivially_equal(c,_ex1()))
563 return split_ex_to_pair(e);
565 return split_ex_to_pair(power(e,c));
568 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
571 // to avoid duplication of power simplification rules,
572 // we create a temporary power object
573 // otherwise it would be hard to correctly simplify
574 // expression like (4^(1/3))^(3/2)
575 if (are_ex_trivially_equal(c,_ex1()))
578 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
581 ex mul::recombine_pair_to_ex(const expair & p) const
583 if (ex_to_numeric(p.coeff).is_equal(_num1()))
586 return power(p.rest,p.coeff);
589 bool mul::expair_needs_further_processing(epp it)
591 if (is_ex_exactly_of_type((*it).rest,mul) &&
592 ex_to_numeric((*it).coeff).is_integer()) {
593 // combined pair is product with integer power -> expand it
594 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
597 if (is_ex_exactly_of_type((*it).rest,numeric)) {
598 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
599 if (!ep.is_equal(*it)) {
600 // combined pair is a numeric power which can be simplified
604 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
605 // combined pair has coeff 1 and must be moved to the end
612 ex mul::default_overall_coeff(void) const
617 void mul::combine_overall_coeff(const ex & c)
619 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
620 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
621 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
624 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
626 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
627 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
628 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
629 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
632 bool mul::can_make_flat(const expair & p) const
634 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
635 // this assertion will probably fail somewhere
636 // it would require a more careful make_flat, obeying the power laws
637 // probably should return true only if p.coeff is integer
638 return ex_to_numeric(p.coeff).is_equal(_num1());
641 ex mul::expand(unsigned options) const
643 if (flags & status_flags::expanded)
646 exvector sub_expanded_seq;
648 epvector * expanded_seqp = expandchildren(options);
650 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
652 int number_of_adds = 0;
654 non_adds.reserve(expanded_seq.size());
655 epvector::const_iterator cit = expanded_seq.begin();
656 epvector::const_iterator last = expanded_seq.end();
657 ex last_expanded = _ex1();
659 if (is_ex_exactly_of_type((*cit).rest,add) &&
660 ((*cit).coeff.is_equal(_ex1()))) {
662 if (is_ex_exactly_of_type(last_expanded,add)) {
664 const add & add1 = ex_to_add(last_expanded);
665 const add & add2 = ex_to_add((*cit).rest);
666 int n1 = add1.nops();
667 int n2 = add2.nops();
669 distrseq.reserve(n1*n2);
670 for (int i1=0; i1<n1; ++i1) {
671 for (int i2=0; i2<n2; ++i2) {
672 distrseq.push_back(add1.op(i1)*add2.op(i2));
675 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
677 non_adds.push_back(split_ex_to_pair(last_expanded));
678 last_expanded = (*cit).rest;
681 non_adds.push_back(*cit);
686 delete expanded_seqp;
688 if (is_ex_exactly_of_type(last_expanded,add)) {
689 add const & finaladd = ex_to_add(last_expanded);
691 int n = finaladd.nops();
693 for (int i=0; i<n; ++i) {
694 epvector factors = non_adds;
695 factors.push_back(split_ex_to_pair(finaladd.op(i)));
696 distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
698 return ((new add(distrseq))->
699 setflag(status_flags::dynallocated | status_flags::expanded));
701 non_adds.push_back(split_ex_to_pair(last_expanded));
702 return (new mul(non_adds,overall_coeff))->
703 setflag(status_flags::dynallocated | status_flags::expanded);
708 // new virtual functions which can be overridden by derived classes
714 // non-virtual functions in this class
718 /** Member-wise expand the expairs representing this sequence. This must be
719 * overridden from expairseq::expandchildren() and done iteratively in order
720 * to allow for early cancallations and thus safe memory.
723 * @return pointer to epvector containing expanded representation or zero
724 * pointer, if sequence is unchanged. */
725 epvector * mul::expandchildren(unsigned options) const
727 epvector::const_iterator last = seq.end();
728 epvector::const_iterator cit = seq.begin();
730 const ex & factor = recombine_pair_to_ex(*cit);
731 const ex & expanded_factor = factor.expand(options);
732 if (!are_ex_trivially_equal(factor,expanded_factor)) {
734 // something changed, copy seq, eval and return it
735 epvector *s = new epvector;
736 s->reserve(seq.size());
738 // copy parts of seq which are known not to have changed
739 epvector::const_iterator cit2 = seq.begin();
744 // copy first changed element
745 s->push_back(split_ex_to_pair(expanded_factor));
749 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
757 return 0; // nothing has changed