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
45 debugmsg("mul default ctor",LOGLEVEL_CONSTRUCT);
46 tinfo_key = TINFO_mul;
51 /** For use by copy ctor and assignment operator. */
52 void mul::copy(const mul & other)
54 inherited::copy(other);
57 void mul::destroy(bool call_parent)
59 if (call_parent) inherited::destroy(call_parent);
68 mul::mul(const ex & lh, const ex & rh)
70 debugmsg("mul ctor from ex,ex",LOGLEVEL_CONSTRUCT);
71 tinfo_key = TINFO_mul;
72 overall_coeff = _ex1();
73 construct_from_2_ex(lh,rh);
74 GINAC_ASSERT(is_canonical());
77 mul::mul(const exvector & v)
79 debugmsg("mul ctor from exvector",LOGLEVEL_CONSTRUCT);
80 tinfo_key = TINFO_mul;
81 overall_coeff = _ex1();
82 construct_from_exvector(v);
83 GINAC_ASSERT(is_canonical());
86 mul::mul(const epvector & v)
88 debugmsg("mul ctor from epvector",LOGLEVEL_CONSTRUCT);
89 tinfo_key = TINFO_mul;
90 overall_coeff = _ex1();
91 construct_from_epvector(v);
92 GINAC_ASSERT(is_canonical());
95 mul::mul(const epvector & v, const ex & oc)
97 debugmsg("mul ctor from epvector,ex",LOGLEVEL_CONSTRUCT);
98 tinfo_key = TINFO_mul;
100 construct_from_epvector(v);
101 GINAC_ASSERT(is_canonical());
104 mul::mul(epvector * vp, const ex & oc)
106 debugmsg("mul ctor from epvector *,ex",LOGLEVEL_CONSTRUCT);
107 tinfo_key = TINFO_mul;
110 construct_from_epvector(*vp);
112 GINAC_ASSERT(is_canonical());
115 mul::mul(const ex & lh, const ex & mh, const ex & rh)
117 debugmsg("mul ctor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
118 tinfo_key = TINFO_mul;
121 factors.push_back(lh);
122 factors.push_back(mh);
123 factors.push_back(rh);
124 overall_coeff = _ex1();
125 construct_from_exvector(factors);
126 GINAC_ASSERT(is_canonical());
133 /** Construct object from archive_node. */
134 mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
136 debugmsg("mul ctor from archive_node", LOGLEVEL_CONSTRUCT);
139 /** Unarchive the object. */
140 ex mul::unarchive(const archive_node &n, const lst &sym_lst)
142 return (new mul(n, sym_lst))->setflag(status_flags::dynallocated);
145 /** Archive the object. */
146 void mul::archive(archive_node &n) const
148 inherited::archive(n);
152 // functions overriding virtual functions from bases classes
157 void mul::print(std::ostream & os, unsigned upper_precedence) const
159 debugmsg("mul print",LOGLEVEL_PRINT);
160 if (precedence<=upper_precedence) os << "(";
162 // first print the overall numeric coefficient:
163 numeric coeff = ex_to_numeric(overall_coeff);
164 if (coeff.csgn()==-1) os << '-';
165 if (!coeff.is_equal(_num1()) &&
166 !coeff.is_equal(_num_1())) {
167 if (coeff.is_rational()) {
168 if (coeff.is_negative())
173 if (coeff.csgn()==-1)
174 (-coeff).print(os, precedence);
176 coeff.print(os, precedence);
180 // then proceed with the remaining factors:
181 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
187 recombine_pair_to_ex(*cit).print(os,precedence);
189 if (precedence<=upper_precedence) os << ")";
192 void mul::printraw(std::ostream & os) const
194 debugmsg("mul printraw",LOGLEVEL_PRINT);
197 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
199 (*it).rest.bp->printraw(os);
201 (*it).coeff.bp->printraw(os);
204 os << ",hash=" << hashvalue << ",flags=" << flags;
208 void mul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
210 debugmsg("mul print csrc", LOGLEVEL_PRINT);
211 if (precedence <= upper_precedence)
214 if (!overall_coeff.is_equal(_ex1())) {
215 overall_coeff.bp->printcsrc(os,type,precedence);
219 // Print arguments, separated by "*" or "/"
220 epvector::const_iterator it = seq.begin();
221 epvector::const_iterator itend = seq.end();
222 while (it != itend) {
224 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
225 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
226 if (type == csrc_types::ctype_cl_N)
232 // If the exponent is 1 or -1, it is left out
233 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
234 it->rest.bp->printcsrc(os, type, precedence);
236 // outer parens around ex needed for broken gcc-2.95 parser:
237 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
239 // Separator is "/" for negative integer powers, "*" otherwise
242 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
248 if (precedence <= upper_precedence)
252 bool mul::info(unsigned inf) const
255 case info_flags::polynomial:
256 case info_flags::integer_polynomial:
257 case info_flags::cinteger_polynomial:
258 case info_flags::rational_polynomial:
259 case info_flags::crational_polynomial:
260 case info_flags::rational_function: {
261 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
262 if (!(recombine_pair_to_ex(*i).info(inf)))
265 return overall_coeff.info(inf);
267 case info_flags::algebraic: {
268 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
269 if ((recombine_pair_to_ex(*i).info(inf)))
275 return inherited::info(inf);
278 int mul::degree(const symbol & s) const
281 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
282 if (ex_to_numeric(cit->coeff).is_integer())
283 deg_sum+=cit->rest.degree(s) * ex_to_numeric(cit->coeff).to_int();
288 int mul::ldegree(const symbol & s) const
291 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
292 if (ex_to_numeric(cit->coeff).is_integer())
293 deg_sum+=cit->rest.ldegree(s) * ex_to_numeric(cit->coeff).to_int();
298 ex mul::coeff(const symbol & s, int n) const
301 coeffseq.reserve(seq.size()+1);
304 // product of individual coeffs
305 // if a non-zero power of s is found, the resulting product will be 0
306 epvector::const_iterator it = seq.begin();
307 while (it!=seq.end()) {
308 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
311 coeffseq.push_back(overall_coeff);
312 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
315 epvector::const_iterator it=seq.begin();
316 bool coeff_found = 0;
317 while (it!=seq.end()) {
318 ex t = recombine_pair_to_ex(*it);
321 coeffseq.push_back(c);
324 coeffseq.push_back(t);
329 coeffseq.push_back(overall_coeff);
330 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
336 ex mul::eval(int level) const
338 // simplifications *(...,x;0) -> 0
339 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
343 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
345 epvector * evaled_seqp = evalchildren(level);
346 if (evaled_seqp!=0) {
347 // do more evaluation later
348 return (new mul(evaled_seqp,overall_coeff))->
349 setflag(status_flags::dynallocated);
352 #ifdef DO_GINAC_ASSERT
353 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
354 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul)) ||
355 (!(ex_to_numeric((*cit).coeff).is_integer())));
356 GINAC_ASSERT(!(cit->is_canonical_numeric()));
357 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric))
358 printtree(std::cerr,0);
359 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
361 expair p = split_ex_to_pair(recombine_pair_to_ex(*cit));
362 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
363 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
366 #endif // def DO_GINAC_ASSERT
368 if (flags & status_flags::evaluated) {
369 GINAC_ASSERT(seq.size()>0);
370 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1()));
374 int seq_size = seq.size();
375 if (overall_coeff.is_equal(_ex0())) {
378 } else if (seq_size==0) {
380 return overall_coeff;
381 } else if (seq_size==1 && overall_coeff.is_equal(_ex1())) {
383 return recombine_pair_to_ex(*(seq.begin()));
384 } else if ((seq_size==1) &&
385 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
386 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
387 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
388 const add & addref = ex_to_add((*seq.begin()).rest);
390 distrseq.reserve(addref.seq.size());
391 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
392 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
394 return (new add(distrseq,
395 ex_to_numeric(addref.overall_coeff).
396 mul_dyn(ex_to_numeric(overall_coeff))))
397 ->setflag(status_flags::dynallocated | status_flags::evaluated);
402 ex mul::evalf(int level) const
405 return mul(seq,overall_coeff);
407 if (level==-max_recursion_level)
408 throw(std::runtime_error("max recursion level reached"));
411 s.reserve(seq.size());
414 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
415 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
418 return mul(s,overall_coeff.evalf(level));
421 exvector mul::get_indices(void) const
423 // return union of indices of factors
425 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
426 exvector subiv=(*cit).rest.get_indices();
427 iv.reserve(iv.size()+subiv.size());
428 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2)
434 ex mul::simplify_ncmul(const exvector & v) const
436 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
441 /** Implementation of ex::diff() for a product. It applies the product rule.
443 ex mul::derivative(const symbol & s) const
446 addseq.reserve(seq.size());
448 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
449 for (unsigned i=0; i!=seq.size(); ++i) {
450 epvector mulseq = seq;
451 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
452 seq[i].rest.diff(s));
453 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
455 return (new add(addseq))->setflag(status_flags::dynallocated);
458 int mul::compare_same_type(const basic & other) const
460 return inherited::compare_same_type(other);
463 bool mul::is_equal_same_type(const basic & other) const
465 return inherited::is_equal_same_type(other);
468 unsigned mul::return_type(void) const
471 // mul without factors: should not happen, but commutes
472 return return_types::commutative;
475 bool all_commutative = 1;
477 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
479 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
480 rt=(*cit).rest.return_type();
481 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
482 if ((rt==return_types::noncommutative)&&(all_commutative)) {
483 // first nc element found, remember position
484 cit_noncommutative_element = cit;
487 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
488 // another nc element found, compare type_infos
489 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
490 // diffent types -> mul is ncc
491 return return_types::noncommutative_composite;
495 // all factors checked
496 return all_commutative ? return_types::commutative : return_types::noncommutative;
499 unsigned mul::return_type_tinfo(void) const
502 return tinfo_key; // mul without factors: should not happen
504 // return type_info of first noncommutative element
505 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
506 if ((*cit).rest.return_type()==return_types::noncommutative)
507 return (*cit).rest.return_type_tinfo();
509 // no noncommutative element found, should not happen
513 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
515 return (new mul(v,oc))->setflag(status_flags::dynallocated);
518 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
520 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
523 expair mul::split_ex_to_pair(const ex & e) const
525 if (is_ex_exactly_of_type(e,power)) {
526 const power & powerref = ex_to_power(e);
527 if (is_ex_exactly_of_type(powerref.exponent,numeric))
528 return expair(powerref.basis,powerref.exponent);
530 return expair(e,_ex1());
533 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
536 // to avoid duplication of power simplification rules,
537 // we create a temporary power object
538 // otherwise it would be hard to correctly simplify
539 // expression like (4^(1/3))^(3/2)
540 if (are_ex_trivially_equal(c,_ex1()))
541 return split_ex_to_pair(e);
543 return split_ex_to_pair(power(e,c));
546 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
549 // to avoid duplication of power simplification rules,
550 // we create a temporary power object
551 // otherwise it would be hard to correctly simplify
552 // expression like (4^(1/3))^(3/2)
553 if (are_ex_trivially_equal(c,_ex1()))
556 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
559 ex mul::recombine_pair_to_ex(const expair & p) const
561 if (ex_to_numeric(p.coeff).is_equal(_num1()))
564 return power(p.rest,p.coeff);
567 bool mul::expair_needs_further_processing(epp it)
569 if (is_ex_exactly_of_type((*it).rest,mul) &&
570 ex_to_numeric((*it).coeff).is_integer()) {
571 // combined pair is product with integer power -> expand it
572 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
575 if (is_ex_exactly_of_type((*it).rest,numeric)) {
576 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
577 if (!ep.is_equal(*it)) {
578 // combined pair is a numeric power which can be simplified
582 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
583 // combined pair has coeff 1 and must be moved to the end
590 ex mul::default_overall_coeff(void) const
595 void mul::combine_overall_coeff(const ex & c)
597 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
598 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
599 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
602 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
604 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
605 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
606 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
607 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
610 bool mul::can_make_flat(const expair & p) const
612 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
613 // this assertion will probably fail somewhere
614 // it would require a more careful make_flat, obeying the power laws
615 // probably should return true only if p.coeff is integer
616 return ex_to_numeric(p.coeff).is_equal(_num1());
619 ex mul::expand(unsigned options) const
621 if (flags & status_flags::expanded)
624 exvector sub_expanded_seq;
626 epvector * expanded_seqp = expandchildren(options);
628 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
630 int number_of_adds = 0;
632 non_adds.reserve(expanded_seq.size());
633 epvector::const_iterator cit = expanded_seq.begin();
634 epvector::const_iterator last = expanded_seq.end();
635 ex last_expanded = _ex1();
637 if (is_ex_exactly_of_type((*cit).rest,add) &&
638 ((*cit).coeff.is_equal(_ex1()))) {
640 if (is_ex_exactly_of_type(last_expanded,add)) {
642 const add & add1 = ex_to_add(last_expanded);
643 const add & add2 = ex_to_add((*cit).rest);
644 int n1 = add1.nops();
645 int n2 = add2.nops();
647 distrseq.reserve(n1*n2);
648 for (int i1=0; i1<n1; ++i1) {
649 for (int i2=0; i2<n2; ++i2) {
650 distrseq.push_back(add1.op(i1)*add2.op(i2));
653 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
655 non_adds.push_back(split_ex_to_pair(last_expanded));
656 last_expanded = (*cit).rest;
659 non_adds.push_back(*cit);
664 if (is_ex_exactly_of_type(last_expanded,add)) {
665 add const & finaladd = ex_to_add(last_expanded);
667 int n = finaladd.nops();
669 for (int i=0; i<n; ++i) {
670 epvector factors = non_adds;
671 factors.push_back(split_ex_to_pair(finaladd.op(i)));
672 distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
674 return ((new add(distrseq))->
675 setflag(status_flags::dynallocated | status_flags::expanded));
677 non_adds.push_back(split_ex_to_pair(last_expanded));
678 return (new mul(non_adds,overall_coeff))->
679 setflag(status_flags::dynallocated | status_flags::expanded);
684 // new virtual functions which can be overridden by derived classes
690 // non-virtual functions in this class
694 /** Member-wise expand the expairs representing this sequence. This must be
695 * overridden from expairseq::expandchildren() and done iteratively in order
696 * to allow for early cancallations and thus safe memory.
699 * @return pointer to epvector containing expanded representation or zero
700 * pointer, if sequence is unchanged. */
701 epvector * mul::expandchildren(unsigned options) const
703 epvector::const_iterator last = seq.end();
704 epvector::const_iterator cit = seq.begin();
706 const ex & factor = recombine_pair_to_ex(*cit);
707 const ex & expanded_factor = factor.expand(options);
708 if (!are_ex_trivially_equal(factor,expanded_factor)) {
710 // something changed, copy seq, eval and return it
711 epvector *s = new epvector;
712 s->reserve(seq.size());
714 // copy parts of seq which are known not to have changed
715 epvector::const_iterator cit2 = seq.begin();
720 // copy first changed element
721 s->push_back(split_ex_to_pair(expanded_factor));
725 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
733 return 0; // nothing has changed
737 // static member variables
742 unsigned mul::precedence = 50;