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
35 GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
38 // default ctor, dctor, copy ctor assignment operator and helpers
43 debugmsg("mul default ctor",LOGLEVEL_CONSTRUCT);
44 tinfo_key = TINFO_mul;
56 mul::mul(const ex & lh, const ex & rh)
58 debugmsg("mul ctor from ex,ex",LOGLEVEL_CONSTRUCT);
59 tinfo_key = TINFO_mul;
60 overall_coeff = _ex1();
61 construct_from_2_ex(lh,rh);
62 GINAC_ASSERT(is_canonical());
65 mul::mul(const exvector & v)
67 debugmsg("mul ctor from exvector",LOGLEVEL_CONSTRUCT);
68 tinfo_key = TINFO_mul;
69 overall_coeff = _ex1();
70 construct_from_exvector(v);
71 GINAC_ASSERT(is_canonical());
74 mul::mul(const epvector & v)
76 debugmsg("mul ctor from epvector",LOGLEVEL_CONSTRUCT);
77 tinfo_key = TINFO_mul;
78 overall_coeff = _ex1();
79 construct_from_epvector(v);
80 GINAC_ASSERT(is_canonical());
83 mul::mul(const epvector & v, const ex & oc)
85 debugmsg("mul ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
86 tinfo_key = TINFO_mul;
88 construct_from_epvector(v);
89 GINAC_ASSERT(is_canonical());
92 mul::mul(epvector * vp, const ex & oc)
94 debugmsg("mul ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
95 tinfo_key = TINFO_mul;
98 construct_from_epvector(*vp);
100 GINAC_ASSERT(is_canonical());
103 mul::mul(const ex & lh, const ex & mh, const ex & rh)
105 debugmsg("mul ctor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
106 tinfo_key = TINFO_mul;
109 factors.push_back(lh);
110 factors.push_back(mh);
111 factors.push_back(rh);
112 overall_coeff = _ex1();
113 construct_from_exvector(factors);
114 GINAC_ASSERT(is_canonical());
121 DEFAULT_ARCHIVING(mul)
124 // functions overriding virtual functions from bases classes
129 void mul::print(std::ostream & os, unsigned upper_precedence) const
131 debugmsg("mul print",LOGLEVEL_PRINT);
132 if (precedence<=upper_precedence) os << "(";
134 // first print the overall numeric coefficient:
135 numeric coeff = ex_to_numeric(overall_coeff);
136 if (coeff.csgn()==-1) os << '-';
137 if (!coeff.is_equal(_num1()) &&
138 !coeff.is_equal(_num_1())) {
139 if (coeff.is_rational()) {
140 if (coeff.is_negative())
145 if (coeff.csgn()==-1)
146 (-coeff).print(os, precedence);
148 coeff.print(os, precedence);
152 // then proceed with the remaining factors:
153 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
159 recombine_pair_to_ex(*cit).print(os,precedence);
161 if (precedence<=upper_precedence) os << ")";
164 void mul::printraw(std::ostream & os) const
166 debugmsg("mul printraw",LOGLEVEL_PRINT);
169 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
171 (*it).rest.bp->printraw(os);
173 (*it).coeff.bp->printraw(os);
176 os << ",hash=" << hashvalue << ",flags=" << flags;
180 void mul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
182 debugmsg("mul print csrc", LOGLEVEL_PRINT);
183 if (precedence <= upper_precedence)
186 if (!overall_coeff.is_equal(_ex1())) {
187 overall_coeff.bp->printcsrc(os,type,precedence);
191 // Print arguments, separated by "*" or "/"
192 epvector::const_iterator it = seq.begin();
193 epvector::const_iterator itend = seq.end();
194 while (it != itend) {
196 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
197 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
198 if (type == csrc_types::ctype_cl_N)
204 // If the exponent is 1 or -1, it is left out
205 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
206 it->rest.bp->printcsrc(os, type, precedence);
208 // outer parens around ex needed for broken gcc-2.95 parser:
209 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
211 // Separator is "/" for negative integer powers, "*" otherwise
214 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
220 if (precedence <= upper_precedence)
224 bool mul::info(unsigned inf) const
227 case info_flags::polynomial:
228 case info_flags::integer_polynomial:
229 case info_flags::cinteger_polynomial:
230 case info_flags::rational_polynomial:
231 case info_flags::crational_polynomial:
232 case info_flags::rational_function: {
233 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
234 if (!(recombine_pair_to_ex(*i).info(inf)))
237 return overall_coeff.info(inf);
239 case info_flags::algebraic: {
240 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
241 if ((recombine_pair_to_ex(*i).info(inf)))
247 return inherited::info(inf);
250 int mul::degree(const ex & s) const
253 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
254 if (ex_to_numeric(cit->coeff).is_integer())
255 deg_sum+=cit->rest.degree(s) * ex_to_numeric(cit->coeff).to_int();
260 int mul::ldegree(const ex & s) const
263 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
264 if (ex_to_numeric(cit->coeff).is_integer())
265 deg_sum+=cit->rest.ldegree(s) * ex_to_numeric(cit->coeff).to_int();
270 ex mul::coeff(const ex & s, int n) const
273 coeffseq.reserve(seq.size()+1);
276 // product of individual coeffs
277 // if a non-zero power of s is found, the resulting product will be 0
278 epvector::const_iterator it = seq.begin();
279 while (it!=seq.end()) {
280 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
283 coeffseq.push_back(overall_coeff);
284 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
287 epvector::const_iterator it=seq.begin();
288 bool coeff_found = 0;
289 while (it!=seq.end()) {
290 ex t = recombine_pair_to_ex(*it);
293 coeffseq.push_back(c);
296 coeffseq.push_back(t);
301 coeffseq.push_back(overall_coeff);
302 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
308 ex mul::eval(int level) const
310 // simplifications *(...,x;0) -> 0
311 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
315 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
317 epvector * evaled_seqp = evalchildren(level);
318 if (evaled_seqp!=0) {
319 // do more evaluation later
320 return (new mul(evaled_seqp,overall_coeff))->
321 setflag(status_flags::dynallocated);
324 #ifdef DO_GINAC_ASSERT
325 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
326 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul)) ||
327 (!(ex_to_numeric((*cit).coeff).is_integer())));
328 GINAC_ASSERT(!(cit->is_canonical_numeric()));
329 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric))
330 printtree(std::cerr,0);
331 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
333 expair p = split_ex_to_pair(recombine_pair_to_ex(*cit));
334 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
335 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
338 #endif // def DO_GINAC_ASSERT
340 if (flags & status_flags::evaluated) {
341 GINAC_ASSERT(seq.size()>0);
342 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1()));
346 int seq_size = seq.size();
347 if (overall_coeff.is_equal(_ex0())) {
350 } else if (seq_size==0) {
352 return overall_coeff;
353 } else if (seq_size==1 && overall_coeff.is_equal(_ex1())) {
355 return recombine_pair_to_ex(*(seq.begin()));
356 } else if ((seq_size==1) &&
357 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
358 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
359 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
360 const add & addref = ex_to_add((*seq.begin()).rest);
362 distrseq.reserve(addref.seq.size());
363 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
364 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
366 return (new add(distrseq,
367 ex_to_numeric(addref.overall_coeff).
368 mul_dyn(ex_to_numeric(overall_coeff))))
369 ->setflag(status_flags::dynallocated | status_flags::evaluated);
374 ex mul::evalf(int level) const
377 return mul(seq,overall_coeff);
379 if (level==-max_recursion_level)
380 throw(std::runtime_error("max recursion level reached"));
383 s.reserve(seq.size());
386 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
387 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
390 return mul(s,overall_coeff.evalf(level));
393 ex mul::simplify_ncmul(const exvector & v) const
395 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
400 /** Implementation of ex::diff() for a product. It applies the product rule.
402 ex mul::derivative(const symbol & s) const
405 addseq.reserve(seq.size());
407 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
408 for (unsigned i=0; i!=seq.size(); ++i) {
409 epvector mulseq = seq;
410 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
411 seq[i].rest.diff(s));
412 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
414 return (new add(addseq))->setflag(status_flags::dynallocated);
417 int mul::compare_same_type(const basic & other) const
419 return inherited::compare_same_type(other);
422 bool mul::is_equal_same_type(const basic & other) const
424 return inherited::is_equal_same_type(other);
427 unsigned mul::return_type(void) const
430 // mul without factors: should not happen, but commutes
431 return return_types::commutative;
434 bool all_commutative = 1;
436 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
438 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
439 rt=(*cit).rest.return_type();
440 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
441 if ((rt==return_types::noncommutative)&&(all_commutative)) {
442 // first nc element found, remember position
443 cit_noncommutative_element = cit;
446 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
447 // another nc element found, compare type_infos
448 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
449 // diffent types -> mul is ncc
450 return return_types::noncommutative_composite;
454 // all factors checked
455 return all_commutative ? return_types::commutative : return_types::noncommutative;
458 unsigned mul::return_type_tinfo(void) const
461 return tinfo_key; // mul without factors: should not happen
463 // return type_info of first noncommutative element
464 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
465 if ((*cit).rest.return_type()==return_types::noncommutative)
466 return (*cit).rest.return_type_tinfo();
468 // no noncommutative element found, should not happen
472 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
474 return (new mul(v,oc))->setflag(status_flags::dynallocated);
477 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
479 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
482 expair mul::split_ex_to_pair(const ex & e) const
484 if (is_ex_exactly_of_type(e,power)) {
485 const power & powerref = ex_to_power(e);
486 if (is_ex_exactly_of_type(powerref.exponent,numeric))
487 return expair(powerref.basis,powerref.exponent);
489 return expair(e,_ex1());
492 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
495 // to avoid duplication of power simplification rules,
496 // we create a temporary power object
497 // otherwise it would be hard to correctly simplify
498 // expression like (4^(1/3))^(3/2)
499 if (are_ex_trivially_equal(c,_ex1()))
500 return split_ex_to_pair(e);
502 return split_ex_to_pair(power(e,c));
505 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
508 // to avoid duplication of power simplification rules,
509 // we create a temporary power object
510 // otherwise it would be hard to correctly simplify
511 // expression like (4^(1/3))^(3/2)
512 if (are_ex_trivially_equal(c,_ex1()))
515 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
518 ex mul::recombine_pair_to_ex(const expair & p) const
520 if (ex_to_numeric(p.coeff).is_equal(_num1()))
523 return power(p.rest,p.coeff);
526 bool mul::expair_needs_further_processing(epp it)
528 if (is_ex_exactly_of_type((*it).rest,mul) &&
529 ex_to_numeric((*it).coeff).is_integer()) {
530 // combined pair is product with integer power -> expand it
531 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
534 if (is_ex_exactly_of_type((*it).rest,numeric)) {
535 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
536 if (!ep.is_equal(*it)) {
537 // combined pair is a numeric power which can be simplified
541 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
542 // combined pair has coeff 1 and must be moved to the end
549 ex mul::default_overall_coeff(void) const
554 void mul::combine_overall_coeff(const ex & c)
556 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
557 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
558 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
561 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
563 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
564 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
565 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
566 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
569 bool mul::can_make_flat(const expair & p) const
571 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
572 // this assertion will probably fail somewhere
573 // it would require a more careful make_flat, obeying the power laws
574 // probably should return true only if p.coeff is integer
575 return ex_to_numeric(p.coeff).is_equal(_num1());
578 ex mul::expand(unsigned options) const
580 if (flags & status_flags::expanded)
583 exvector sub_expanded_seq;
585 epvector * expanded_seqp = expandchildren(options);
587 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
589 int number_of_adds = 0;
591 non_adds.reserve(expanded_seq.size());
592 epvector::const_iterator cit = expanded_seq.begin();
593 epvector::const_iterator last = expanded_seq.end();
594 ex last_expanded = _ex1();
596 if (is_ex_exactly_of_type((*cit).rest,add) &&
597 ((*cit).coeff.is_equal(_ex1()))) {
599 if (is_ex_exactly_of_type(last_expanded,add)) {
601 const add & add1 = ex_to_add(last_expanded);
602 const add & add2 = ex_to_add((*cit).rest);
603 int n1 = add1.nops();
604 int n2 = add2.nops();
606 distrseq.reserve(n1*n2);
607 for (int i1=0; i1<n1; ++i1) {
608 for (int i2=0; i2<n2; ++i2) {
609 distrseq.push_back(add1.op(i1)*add2.op(i2));
612 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
614 non_adds.push_back(split_ex_to_pair(last_expanded));
615 last_expanded = (*cit).rest;
618 non_adds.push_back(*cit);
623 delete expanded_seqp;
625 if (is_ex_exactly_of_type(last_expanded,add)) {
626 add const & finaladd = ex_to_add(last_expanded);
628 int n = finaladd.nops();
630 for (int i=0; i<n; ++i) {
631 epvector factors = non_adds;
632 factors.push_back(split_ex_to_pair(finaladd.op(i)));
633 distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
635 return ((new add(distrseq))->
636 setflag(status_flags::dynallocated | status_flags::expanded));
638 non_adds.push_back(split_ex_to_pair(last_expanded));
639 return (new mul(non_adds,overall_coeff))->
640 setflag(status_flags::dynallocated | status_flags::expanded);
645 // new virtual functions which can be overridden by derived classes
651 // non-virtual functions in this class
655 /** Member-wise expand the expairs representing this sequence. This must be
656 * overridden from expairseq::expandchildren() and done iteratively in order
657 * to allow for early cancallations and thus safe memory.
660 * @return pointer to epvector containing expanded representation or zero
661 * pointer, if sequence is unchanged. */
662 epvector * mul::expandchildren(unsigned options) const
664 epvector::const_iterator last = seq.end();
665 epvector::const_iterator cit = seq.begin();
667 const ex & factor = recombine_pair_to_ex(*cit);
668 const ex & expanded_factor = factor.expand(options);
669 if (!are_ex_trivially_equal(factor,expanded_factor)) {
671 // something changed, copy seq, eval and return it
672 epvector *s = new epvector;
673 s->reserve(seq.size());
675 // copy parts of seq which are known not to have changed
676 epvector::const_iterator cit2 = seq.begin();
681 // copy first changed element
682 s->push_back(split_ex_to_pair(expanded_factor));
686 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
694 return 0; // nothing has changed
698 // static member variables
703 unsigned mul::precedence = 50;