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(const print_context & c, unsigned level) const
131 debugmsg("mul print", LOGLEVEL_PRINT);
133 if (is_of_type(c, print_tree)) {
135 inherited::print(c, level);
137 } else if (is_of_type(c, print_csrc)) {
141 if (!overall_coeff.is_equal(_ex1())) {
142 overall_coeff.bp->print(c, precedence);
146 // Print arguments, separated by "*" or "/"
147 epvector::const_iterator it = seq.begin(), itend = seq.end();
148 while (it != itend) {
150 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
151 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
152 if (is_of_type(c, print_csrc_cl_N))
158 // If the exponent is 1 or -1, it is left out
159 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
160 it->rest.print(c, precedence);
162 // Outer parens around ex needed for broken gcc-2.95 parser:
163 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).print(c, level);
166 // Separator is "/" for negative integer powers, "*" otherwise
169 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
176 if (precedence <= level)
181 if (precedence <= level) {
182 if (is_of_type(c, print_latex))
190 // First print the overall numeric coefficient
191 numeric coeff = ex_to_numeric(overall_coeff);
192 if (coeff.csgn() == -1)
194 if (!coeff.is_equal(_num1()) &&
195 !coeff.is_equal(_num_1())) {
196 if (coeff.is_rational()) {
197 if (coeff.is_negative())
202 if (coeff.csgn() == -1)
203 (-coeff).print(c, precedence);
205 coeff.print(c, precedence);
207 if (is_of_type(c, print_latex))
213 // Then proceed with the remaining factors
214 epvector::const_iterator it = seq.begin(), itend = seq.end();
215 while (it != itend) {
217 if (is_of_type(c, print_latex))
224 recombine_pair_to_ex(*it).print(c, precedence);
228 if (precedence <= level) {
229 if (is_of_type(c, print_latex))
237 bool mul::info(unsigned inf) const
240 case info_flags::polynomial:
241 case info_flags::integer_polynomial:
242 case info_flags::cinteger_polynomial:
243 case info_flags::rational_polynomial:
244 case info_flags::crational_polynomial:
245 case info_flags::rational_function: {
246 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
247 if (!(recombine_pair_to_ex(*i).info(inf)))
250 return overall_coeff.info(inf);
252 case info_flags::algebraic: {
253 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
254 if ((recombine_pair_to_ex(*i).info(inf)))
260 return inherited::info(inf);
263 int mul::degree(const ex & s) const
266 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
267 if (ex_to_numeric(cit->coeff).is_integer())
268 deg_sum+=cit->rest.degree(s) * ex_to_numeric(cit->coeff).to_int();
273 int mul::ldegree(const ex & s) const
276 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
277 if (ex_to_numeric(cit->coeff).is_integer())
278 deg_sum+=cit->rest.ldegree(s) * ex_to_numeric(cit->coeff).to_int();
283 ex mul::coeff(const ex & s, int n) const
286 coeffseq.reserve(seq.size()+1);
289 // product of individual coeffs
290 // if a non-zero power of s is found, the resulting product will be 0
291 epvector::const_iterator it = seq.begin();
292 while (it!=seq.end()) {
293 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
296 coeffseq.push_back(overall_coeff);
297 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
300 epvector::const_iterator it=seq.begin();
301 bool coeff_found = 0;
302 while (it!=seq.end()) {
303 ex t = recombine_pair_to_ex(*it);
306 coeffseq.push_back(c);
309 coeffseq.push_back(t);
314 coeffseq.push_back(overall_coeff);
315 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
321 ex mul::eval(int level) const
323 // simplifications *(...,x;0) -> 0
324 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
328 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
330 epvector * evaled_seqp = evalchildren(level);
331 if (evaled_seqp!=0) {
332 // do more evaluation later
333 return (new mul(evaled_seqp,overall_coeff))->
334 setflag(status_flags::dynallocated);
337 #ifdef DO_GINAC_ASSERT
338 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
339 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul)) ||
340 (!(ex_to_numeric((*cit).coeff).is_integer())));
341 GINAC_ASSERT(!(cit->is_canonical_numeric()));
342 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric))
343 print(print_tree(std::cerr));
344 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
346 expair p = split_ex_to_pair(recombine_pair_to_ex(*cit));
347 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
348 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
351 #endif // def DO_GINAC_ASSERT
353 if (flags & status_flags::evaluated) {
354 GINAC_ASSERT(seq.size()>0);
355 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_equal(_ex1()));
359 int seq_size = seq.size();
360 if (overall_coeff.is_equal(_ex0())) {
363 } else if (seq_size==0) {
365 return overall_coeff;
366 } else if (seq_size==1 && overall_coeff.is_equal(_ex1())) {
368 return recombine_pair_to_ex(*(seq.begin()));
369 } else if ((seq_size==1) &&
370 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
371 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
372 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
373 const add & addref = ex_to_add((*seq.begin()).rest);
375 distrseq.reserve(addref.seq.size());
376 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
377 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
379 return (new add(distrseq,
380 ex_to_numeric(addref.overall_coeff).
381 mul_dyn(ex_to_numeric(overall_coeff))))
382 ->setflag(status_flags::dynallocated | status_flags::evaluated);
387 ex mul::evalf(int level) const
390 return mul(seq,overall_coeff);
392 if (level==-max_recursion_level)
393 throw(std::runtime_error("max recursion level reached"));
396 s.reserve(seq.size());
399 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
400 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
403 return mul(s,overall_coeff.evalf(level));
406 ex mul::simplify_ncmul(const exvector & v) const
408 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
413 /** Implementation of ex::diff() for a product. It applies the product rule.
415 ex mul::derivative(const symbol & s) const
418 addseq.reserve(seq.size());
420 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
421 for (unsigned i=0; i!=seq.size(); ++i) {
422 epvector mulseq = seq;
423 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
424 seq[i].rest.diff(s));
425 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
427 return (new add(addseq))->setflag(status_flags::dynallocated);
430 int mul::compare_same_type(const basic & other) const
432 return inherited::compare_same_type(other);
435 bool mul::is_equal_same_type(const basic & other) const
437 return inherited::is_equal_same_type(other);
440 unsigned mul::return_type(void) const
443 // mul without factors: should not happen, but commutes
444 return return_types::commutative;
447 bool all_commutative = 1;
449 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
451 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
452 rt=(*cit).rest.return_type();
453 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
454 if ((rt==return_types::noncommutative)&&(all_commutative)) {
455 // first nc element found, remember position
456 cit_noncommutative_element = cit;
459 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
460 // another nc element found, compare type_infos
461 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
462 // diffent types -> mul is ncc
463 return return_types::noncommutative_composite;
467 // all factors checked
468 return all_commutative ? return_types::commutative : return_types::noncommutative;
471 unsigned mul::return_type_tinfo(void) const
474 return tinfo_key; // mul without factors: should not happen
476 // return type_info of first noncommutative element
477 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
478 if ((*cit).rest.return_type()==return_types::noncommutative)
479 return (*cit).rest.return_type_tinfo();
481 // no noncommutative element found, should not happen
485 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
487 return (new mul(v,oc))->setflag(status_flags::dynallocated);
490 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
492 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
495 expair mul::split_ex_to_pair(const ex & e) const
497 if (is_ex_exactly_of_type(e,power)) {
498 const power & powerref = ex_to_power(e);
499 if (is_ex_exactly_of_type(powerref.exponent,numeric))
500 return expair(powerref.basis,powerref.exponent);
502 return expair(e,_ex1());
505 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
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()))
513 return split_ex_to_pair(e);
515 return split_ex_to_pair(power(e,c));
518 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
521 // to avoid duplication of power simplification rules,
522 // we create a temporary power object
523 // otherwise it would be hard to correctly simplify
524 // expression like (4^(1/3))^(3/2)
525 if (are_ex_trivially_equal(c,_ex1()))
528 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
531 ex mul::recombine_pair_to_ex(const expair & p) const
533 if (ex_to_numeric(p.coeff).is_equal(_num1()))
536 return power(p.rest,p.coeff);
539 bool mul::expair_needs_further_processing(epp it)
541 if (is_ex_exactly_of_type((*it).rest,mul) &&
542 ex_to_numeric((*it).coeff).is_integer()) {
543 // combined pair is product with integer power -> expand it
544 *it = split_ex_to_pair(recombine_pair_to_ex(*it));
547 if (is_ex_exactly_of_type((*it).rest,numeric)) {
548 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
549 if (!ep.is_equal(*it)) {
550 // combined pair is a numeric power which can be simplified
554 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
555 // combined pair has coeff 1 and must be moved to the end
562 ex mul::default_overall_coeff(void) const
567 void mul::combine_overall_coeff(const ex & c)
569 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
570 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
571 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
574 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
576 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
577 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
578 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
579 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
582 bool mul::can_make_flat(const expair & p) const
584 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
585 // this assertion will probably fail somewhere
586 // it would require a more careful make_flat, obeying the power laws
587 // probably should return true only if p.coeff is integer
588 return ex_to_numeric(p.coeff).is_equal(_num1());
591 ex mul::expand(unsigned options) const
593 if (flags & status_flags::expanded)
596 exvector sub_expanded_seq;
598 epvector * expanded_seqp = expandchildren(options);
600 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
602 int number_of_adds = 0;
604 non_adds.reserve(expanded_seq.size());
605 epvector::const_iterator cit = expanded_seq.begin();
606 epvector::const_iterator last = expanded_seq.end();
607 ex last_expanded = _ex1();
609 if (is_ex_exactly_of_type((*cit).rest,add) &&
610 ((*cit).coeff.is_equal(_ex1()))) {
612 if (is_ex_exactly_of_type(last_expanded,add)) {
614 const add & add1 = ex_to_add(last_expanded);
615 const add & add2 = ex_to_add((*cit).rest);
616 int n1 = add1.nops();
617 int n2 = add2.nops();
619 distrseq.reserve(n1*n2);
620 for (int i1=0; i1<n1; ++i1) {
621 for (int i2=0; i2<n2; ++i2) {
622 distrseq.push_back(add1.op(i1)*add2.op(i2));
625 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
627 non_adds.push_back(split_ex_to_pair(last_expanded));
628 last_expanded = (*cit).rest;
631 non_adds.push_back(*cit);
636 delete expanded_seqp;
638 if (is_ex_exactly_of_type(last_expanded,add)) {
639 add const & finaladd = ex_to_add(last_expanded);
641 int n = finaladd.nops();
643 for (int i=0; i<n; ++i) {
644 epvector factors = non_adds;
645 factors.push_back(split_ex_to_pair(finaladd.op(i)));
646 distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
648 return ((new add(distrseq))->
649 setflag(status_flags::dynallocated | status_flags::expanded));
651 non_adds.push_back(split_ex_to_pair(last_expanded));
652 return (new mul(non_adds,overall_coeff))->
653 setflag(status_flags::dynallocated | status_flags::expanded);
658 // new virtual functions which can be overridden by derived classes
664 // non-virtual functions in this class
668 /** Member-wise expand the expairs representing this sequence. This must be
669 * overridden from expairseq::expandchildren() and done iteratively in order
670 * to allow for early cancallations and thus safe memory.
673 * @return pointer to epvector containing expanded representation or zero
674 * pointer, if sequence is unchanged. */
675 epvector * mul::expandchildren(unsigned options) const
677 epvector::const_iterator last = seq.end();
678 epvector::const_iterator cit = seq.begin();
680 const ex & factor = recombine_pair_to_ex(*cit);
681 const ex & expanded_factor = factor.expand(options);
682 if (!are_ex_trivially_equal(factor,expanded_factor)) {
684 // something changed, copy seq, eval and return it
685 epvector *s = new epvector;
686 s->reserve(seq.size());
688 // copy parts of seq which are known not to have changed
689 epvector::const_iterator cit2 = seq.begin();
694 // copy first changed element
695 s->push_back(split_ex_to_pair(expanded_factor));
699 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
707 return 0; // nothing has changed
711 // static member variables
716 unsigned mul::precedence = 50;