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
33 #ifndef NO_NAMESPACE_GINAC
35 #endif // ndef NO_NAMESPACE_GINAC
37 GINAC_IMPLEMENT_REGISTERED_CLASS(mul, expairseq)
40 // default constructor, destructor, copy constructor assignment operator and helpers
47 debugmsg("mul default constructor",LOGLEVEL_CONSTRUCT);
48 tinfo_key = TINFO_mul;
53 void mul::copy(const mul & other)
55 inherited::copy(other);
58 void mul::destroy(bool call_parent)
60 if (call_parent) inherited::destroy(call_parent);
69 mul::mul(const ex & lh, const ex & rh)
71 debugmsg("mul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
72 tinfo_key = TINFO_mul;
73 overall_coeff = _ex1();
74 construct_from_2_ex(lh,rh);
75 GINAC_ASSERT(is_canonical());
78 mul::mul(const exvector & v)
80 debugmsg("mul constructor from exvector",LOGLEVEL_CONSTRUCT);
81 tinfo_key = TINFO_mul;
82 overall_coeff = _ex1();
83 construct_from_exvector(v);
84 GINAC_ASSERT(is_canonical());
87 mul::mul(const epvector & v)
89 debugmsg("mul constructor from epvector",LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_mul;
91 overall_coeff = _ex1();
92 construct_from_epvector(v);
93 GINAC_ASSERT(is_canonical());
96 mul::mul(const epvector & v, const ex & oc)
98 debugmsg("mul constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
99 tinfo_key = TINFO_mul;
101 construct_from_epvector(v);
102 GINAC_ASSERT(is_canonical());
105 mul::mul(epvector * vp, const ex & oc)
107 debugmsg("mul constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
108 tinfo_key = TINFO_mul;
111 construct_from_epvector(*vp);
113 GINAC_ASSERT(is_canonical());
116 mul::mul(const ex & lh, const ex & mh, const ex & rh)
118 debugmsg("mul constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
119 tinfo_key = TINFO_mul;
122 factors.push_back(lh);
123 factors.push_back(mh);
124 factors.push_back(rh);
125 overall_coeff = _ex1();
126 construct_from_exvector(factors);
127 GINAC_ASSERT(is_canonical());
134 /** Construct object from archive_node. */
135 mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
137 debugmsg("mul constructor from archive_node", LOGLEVEL_CONSTRUCT);
140 /** Unarchive the object. */
141 ex mul::unarchive(const archive_node &n, const lst &sym_lst)
143 return (new mul(n, sym_lst))->setflag(status_flags::dynallocated);
146 /** Archive the object. */
147 void mul::archive(archive_node &n) const
149 inherited::archive(n);
153 // functions overriding virtual functions from bases classes
158 basic * mul::duplicate() const
160 debugmsg("mul duplicate",LOGLEVEL_ASSIGNMENT);
161 return new mul(*this);
164 void mul::print(std::ostream & os, unsigned upper_precedence) const
166 debugmsg("mul print",LOGLEVEL_PRINT);
167 if (precedence<=upper_precedence) os << "(";
169 // first print the overall numeric coefficient:
170 numeric coeff = ex_to_numeric(overall_coeff);
171 if (coeff.csgn()==-1) os << '-';
172 if (!coeff.is_equal(_num1()) &&
173 !coeff.is_equal(_num_1())) {
174 if (coeff.is_rational()) {
175 if (coeff.is_negative())
180 if (coeff.csgn()==-1)
181 (-coeff).print(os, precedence);
183 coeff.print(os, precedence);
187 // then proceed with the remaining factors:
188 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
194 recombine_pair_to_ex(*cit).print(os,precedence);
196 if (precedence<=upper_precedence) os << ")";
199 void mul::printraw(std::ostream & os) const
201 debugmsg("mul printraw",LOGLEVEL_PRINT);
204 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
206 (*it).rest.bp->printraw(os);
208 (*it).coeff.bp->printraw(os);
211 os << ",hash=" << hashvalue << ",flags=" << flags;
215 void mul::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
217 debugmsg("mul print csrc", LOGLEVEL_PRINT);
218 if (precedence <= upper_precedence)
221 if (!overall_coeff.is_equal(_ex1())) {
222 overall_coeff.bp->printcsrc(os,type,precedence);
226 // Print arguments, separated by "*" or "/"
227 epvector::const_iterator it = seq.begin();
228 epvector::const_iterator itend = seq.end();
229 while (it != itend) {
231 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
232 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
233 if (type == csrc_types::ctype_cl_N)
239 // If the exponent is 1 or -1, it is left out
240 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
241 it->rest.bp->printcsrc(os, type, precedence);
243 // outer parens around ex needed for broken gcc-2.95 parser:
244 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
246 // Separator is "/" for negative integer powers, "*" otherwise
249 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
255 if (precedence <= upper_precedence)
259 bool mul::info(unsigned inf) const
262 case info_flags::polynomial:
263 case info_flags::integer_polynomial:
264 case info_flags::cinteger_polynomial:
265 case info_flags::rational_polynomial:
266 case info_flags::crational_polynomial:
267 case info_flags::rational_function: {
268 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
269 if (!(recombine_pair_to_ex(*i).info(inf)))
272 return overall_coeff.info(inf);
274 case info_flags::algebraic: {
275 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
276 if ((recombine_pair_to_ex(*i).info(inf)))
282 return inherited::info(inf);
285 typedef std::vector<int> intvector;
287 int mul::degree(const symbol & s) const
290 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
291 if (ex_to_numeric(cit->coeff).is_integer())
292 deg_sum+=cit->rest.degree(s) * ex_to_numeric(cit->coeff).to_int();
297 int mul::ldegree(const symbol & s) const
300 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
301 if (ex_to_numeric(cit->coeff).is_integer())
302 deg_sum+=cit->rest.ldegree(s) * ex_to_numeric(cit->coeff).to_int();
307 ex mul::coeff(const symbol & s, int n) const
310 coeffseq.reserve(seq.size()+1);
313 // product of individual coeffs
314 // if a non-zero power of s is found, the resulting product will be 0
315 epvector::const_iterator it=seq.begin();
316 while (it!=seq.end()) {
317 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
320 coeffseq.push_back(overall_coeff);
321 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
324 epvector::const_iterator it=seq.begin();
326 while (it!=seq.end()) {
327 ex t=recombine_pair_to_ex(*it);
330 coeffseq.push_back(c);
333 coeffseq.push_back(t);
338 coeffseq.push_back(overall_coeff);
339 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
345 ex mul::eval(int level) const
347 // simplifications *(...,x;0) -> 0
348 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
352 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
354 epvector * evaled_seqp=evalchildren(level);
355 if (evaled_seqp!=0) {
356 // do more evaluation later
357 return (new mul(evaled_seqp,overall_coeff))->
358 setflag(status_flags::dynallocated);
361 #ifdef DO_GINAC_ASSERT
362 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
363 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
364 (!(ex_to_numeric((*cit).coeff).is_integer())));
365 GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
366 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
369 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
371 expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
372 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
373 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
376 #endif // def DO_GINAC_ASSERT
378 if (flags & status_flags::evaluated) {
379 GINAC_ASSERT(seq.size()>0);
380 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
384 int seq_size=seq.size();
385 if (overall_coeff.is_equal(_ex0())) {
388 } else if (seq_size==0) {
390 return overall_coeff;
391 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex1())) {
393 return recombine_pair_to_ex(*(seq.begin()));
394 } else if ((seq_size==1) &&
395 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
396 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
397 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
398 const add & addref=ex_to_add((*seq.begin()).rest);
400 distrseq.reserve(addref.seq.size());
401 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
402 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit, overall_coeff));
404 return (new add(distrseq,
405 ex_to_numeric(addref.overall_coeff).
406 mul_dyn(ex_to_numeric(overall_coeff))))
407 ->setflag(status_flags::dynallocated | status_flags::evaluated);
412 ex mul::evalf(int level) const
415 return mul(seq,overall_coeff);
417 if (level==-max_recursion_level)
418 throw(std::runtime_error("max recursion level reached"));
421 s.reserve(seq.size());
424 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
425 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
428 return mul(s,overall_coeff.evalf(level));
431 exvector mul::get_indices(void) const
433 // return union of indices of factors
435 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
436 exvector subiv=(*cit).rest.get_indices();
437 iv.reserve(iv.size()+subiv.size());
438 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
445 ex mul::simplify_ncmul(const exvector & v) const
447 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
452 /** Implementation of ex::diff() for a product. It applies the product rule.
454 ex mul::derivative(const symbol & s) const
457 addseq.reserve(seq.size());
459 // D(a*b*c) = D(a)*b*c + a*D(b)*c + a*b*D(c)
460 for (unsigned i=0; i!=seq.size(); ++i) {
461 epvector mulseq = seq;
462 mulseq[i] = split_ex_to_pair(power(seq[i].rest,seq[i].coeff - _ex1()) *
463 seq[i].rest.diff(s));
464 addseq.push_back((new mul(mulseq,overall_coeff*seq[i].coeff))->setflag(status_flags::dynallocated));
466 return (new add(addseq))->setflag(status_flags::dynallocated);
469 int mul::compare_same_type(const basic & other) const
471 return inherited::compare_same_type(other);
474 bool mul::is_equal_same_type(const basic & other) const
476 return inherited::is_equal_same_type(other);
479 unsigned mul::return_type(void) const
482 // mul without factors: should not happen, but commutes
483 return return_types::commutative;
486 bool all_commutative = 1;
488 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
490 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
491 rt=(*cit).rest.return_type();
492 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
493 if ((rt==return_types::noncommutative)&&(all_commutative)) {
494 // first nc element found, remember position
495 cit_noncommutative_element = cit;
498 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
499 // another nc element found, compare type_infos
500 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
501 // diffent types -> mul is ncc
502 return return_types::noncommutative_composite;
506 // all factors checked
507 return all_commutative ? return_types::commutative : return_types::noncommutative;
510 unsigned mul::return_type_tinfo(void) const
513 // mul without factors: should not happen
516 // return type_info of first noncommutative element
517 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
518 if ((*cit).rest.return_type()==return_types::noncommutative) {
519 return (*cit).rest.return_type_tinfo();
522 // no noncommutative element found, should not happen
526 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
528 return (new mul(v,oc))->setflag(status_flags::dynallocated);
531 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
533 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
536 expair mul::split_ex_to_pair(const ex & e) const
538 if (is_ex_exactly_of_type(e,power)) {
539 const power & powerref=ex_to_power(e);
540 if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
541 return expair(powerref.basis,powerref.exponent);
544 return expair(e,_ex1());
547 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
550 // to avoid duplication of power simplification rules,
551 // we create a temporary power object
552 // otherwise it would be hard to correctly simplify
553 // expression like (4^(1/3))^(3/2)
554 if (are_ex_trivially_equal(c,_ex1()))
555 return split_ex_to_pair(e);
557 return split_ex_to_pair(power(e,c));
560 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
563 // to avoid duplication of power simplification rules,
564 // we create a temporary power object
565 // otherwise it would be hard to correctly simplify
566 // expression like (4^(1/3))^(3/2)
567 if (are_ex_trivially_equal(c,_ex1()))
570 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
573 ex mul::recombine_pair_to_ex(const expair & p) const
575 if (ex_to_numeric(p.coeff).is_equal(_num1()))
578 return power(p.rest,p.coeff);
581 bool mul::expair_needs_further_processing(epp it)
583 if (is_ex_exactly_of_type((*it).rest,mul) &&
584 ex_to_numeric((*it).coeff).is_integer()) {
585 // combined pair is product with integer power -> expand it
586 *it=split_ex_to_pair(recombine_pair_to_ex(*it));
589 if (is_ex_exactly_of_type((*it).rest,numeric)) {
590 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
591 if (!ep.is_equal(*it)) {
592 // combined pair is a numeric power which can be simplified
596 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
597 // combined pair has coeff 1 and must be moved to the end
604 ex mul::default_overall_coeff(void) const
609 void mul::combine_overall_coeff(const ex & c)
611 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
612 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
613 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
616 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
618 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
619 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
620 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
621 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
624 bool mul::can_make_flat(const expair & p) const
626 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
627 // this assertion will probably fail somewhere
628 // it would require a more careful make_flat, obeying the power laws
629 // probably should return true only if p.coeff is integer
630 return ex_to_numeric(p.coeff).is_equal(_num1());
633 ex mul::expand(unsigned options) const
635 if (flags & status_flags::expanded)
638 exvector sub_expanded_seq;
639 intvector positions_of_adds;
641 epvector * expanded_seqp = expandchildren(options);
643 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
645 int number_of_adds = 0;
647 non_adds.reserve(expanded_seq.size());
648 epvector::const_iterator cit = expanded_seq.begin();
649 epvector::const_iterator last = expanded_seq.end();
650 ex last_expanded=_ex1();
652 if (is_ex_exactly_of_type((*cit).rest,add) &&
653 ((*cit).coeff.is_equal(_ex1()))) {
655 if (is_ex_exactly_of_type(last_expanded,add)) {
657 const add & add1 = ex_to_add(last_expanded);
658 const add & add2 = ex_to_add((*cit).rest);
659 int n1 = add1.nops();
660 int n2 = add2.nops();
662 distrseq.reserve(n1*n2);
663 for (int i1=0; i1<n1; ++i1) {
664 for (int i2=0; i2<n2; ++i2) {
665 distrseq.push_back(add1.op(i1)*add2.op(i2));
668 last_expanded = (new add(distrseq))->setflag(status_flags::dynallocated | status_flags::expanded);
670 non_adds.push_back(split_ex_to_pair(last_expanded));
671 last_expanded = (*cit).rest;
674 non_adds.push_back(*cit);
679 if (is_ex_exactly_of_type(last_expanded,add)) {
680 add const & finaladd = ex_to_add(last_expanded);
682 int n = finaladd.nops();
684 for (int i=0; i<n; ++i) {
685 epvector factors = non_adds;
686 factors.push_back(split_ex_to_pair(finaladd.op(i)));
687 distrseq.push_back((new mul(factors,overall_coeff))->setflag(status_flags::dynallocated | status_flags::expanded));
689 return ((new add(distrseq))->
690 setflag(status_flags::dynallocated | status_flags::expanded));
692 non_adds.push_back(split_ex_to_pair(last_expanded));
693 return (new mul(non_adds,overall_coeff))->
694 setflag(status_flags::dynallocated | status_flags::expanded);
699 // new virtual functions which can be overridden by derived classes
705 // non-virtual functions in this class
708 epvector * mul::expandchildren(unsigned options) const
710 epvector::const_iterator last = seq.end();
711 epvector::const_iterator cit = seq.begin();
713 const ex & factor = recombine_pair_to_ex(*cit);
714 const ex & expanded_factor = factor.expand(options);
715 if (!are_ex_trivially_equal(factor,expanded_factor)) {
717 // something changed, copy seq, eval and return it
718 epvector *s=new epvector;
719 s->reserve(seq.size());
721 // copy parts of seq which are known not to have changed
722 epvector::const_iterator cit2 = seq.begin();
727 // copy first changed element
728 s->push_back(split_ex_to_pair(expanded_factor));
732 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
740 return 0; // nothing has changed
744 // static member variables
749 unsigned mul::precedence = 50;
751 #ifndef NO_NAMESPACE_GINAC
753 #endif // ndef NO_NAMESPACE_GINAC