3 * Implementation of GiNaC's products of expressions. */
6 * GiNaC Copyright (C) 1999-2000 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 debugmsg("mul destructor",LOGLEVEL_DESTRUCT);
57 mul::mul(const mul & other)
59 debugmsg("mul copy constructor",LOGLEVEL_CONSTRUCT);
63 const mul & mul::operator=(const mul & other)
65 debugmsg("mul operator=",LOGLEVEL_ASSIGNMENT);
75 void mul::copy(const mul & other)
77 inherited::copy(other);
80 void mul::destroy(bool call_parent)
82 if (call_parent) inherited::destroy(call_parent);
91 mul::mul(const ex & lh, const ex & rh)
93 debugmsg("mul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_mul;
96 construct_from_2_ex(lh,rh);
97 GINAC_ASSERT(is_canonical());
100 mul::mul(const exvector & v)
102 debugmsg("mul constructor from exvector",LOGLEVEL_CONSTRUCT);
103 tinfo_key = TINFO_mul;
104 overall_coeff=_ex1();
105 construct_from_exvector(v);
106 GINAC_ASSERT(is_canonical());
110 mul::mul(const epvector & v, bool do_not_canonicalize)
112 debugmsg("mul constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
113 tinfo_key = TINFO_mul;
114 if (do_not_canonicalize) {
116 #ifdef EXPAIRSEQ_USE_HASHTAB
117 combine_same_terms(); // to build hashtab
118 #endif // def EXPAIRSEQ_USE_HASHTAB
120 construct_from_epvector(v);
122 GINAC_ASSERT(is_canonical());
126 mul::mul(const epvector & v)
128 debugmsg("mul constructor from epvector",LOGLEVEL_CONSTRUCT);
129 tinfo_key = TINFO_mul;
130 overall_coeff=_ex1();
131 construct_from_epvector(v);
132 GINAC_ASSERT(is_canonical());
135 mul::mul(const epvector & v, const ex & oc)
137 debugmsg("mul constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
138 tinfo_key = TINFO_mul;
140 construct_from_epvector(v);
141 GINAC_ASSERT(is_canonical());
144 mul::mul(epvector * vp, const ex & oc)
146 debugmsg("mul constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
147 tinfo_key = TINFO_mul;
150 construct_from_epvector(*vp);
152 GINAC_ASSERT(is_canonical());
155 mul::mul(const ex & lh, const ex & mh, const ex & rh)
157 debugmsg("mul constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
158 tinfo_key = TINFO_mul;
161 factors.push_back(lh);
162 factors.push_back(mh);
163 factors.push_back(rh);
164 overall_coeff=_ex1();
165 construct_from_exvector(factors);
166 GINAC_ASSERT(is_canonical());
173 /** Construct object from archive_node. */
174 mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
176 debugmsg("mul constructor from archive_node", LOGLEVEL_CONSTRUCT);
179 /** Unarchive the object. */
180 ex mul::unarchive(const archive_node &n, const lst &sym_lst)
182 return (new mul(n, sym_lst))->setflag(status_flags::dynallocated);
185 /** Archive the object. */
186 void mul::archive(archive_node &n) const
188 inherited::archive(n);
192 // functions overriding virtual functions from bases classes
197 basic * mul::duplicate() const
199 debugmsg("mul duplicate",LOGLEVEL_ASSIGNMENT);
200 return new mul(*this);
203 void mul::print(ostream & os, unsigned upper_precedence) const
205 debugmsg("mul print",LOGLEVEL_PRINT);
206 if (precedence<=upper_precedence) os << "(";
208 // first print the overall numeric coefficient:
209 numeric coeff = ex_to_numeric(overall_coeff);
210 if (coeff.csgn()==-1) os << '-';
211 if (!coeff.is_equal(_num1()) &&
212 !coeff.is_equal(_num_1())) {
213 if (coeff.is_rational()) {
214 if (coeff.is_negative())
219 if (coeff.csgn()==-1)
220 (-coeff).print(os, precedence);
222 coeff.print(os, precedence);
226 // then proceed with the remaining factors:
227 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
233 recombine_pair_to_ex(*cit).print(os,precedence);
235 if (precedence<=upper_precedence) os << ")";
238 void mul::printraw(ostream & os) const
240 debugmsg("mul printraw",LOGLEVEL_PRINT);
243 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
245 (*it).rest.bp->printraw(os);
247 (*it).coeff.bp->printraw(os);
250 os << ",hash=" << hashvalue << ",flags=" << flags;
254 void mul::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
256 debugmsg("mul print csrc", LOGLEVEL_PRINT);
257 if (precedence <= upper_precedence)
260 if (!overall_coeff.is_equal(_ex1())) {
261 overall_coeff.bp->printcsrc(os,type,precedence);
265 // Print arguments, separated by "*" or "/"
266 epvector::const_iterator it = seq.begin();
267 epvector::const_iterator itend = seq.end();
268 while (it != itend) {
270 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
271 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
272 if (type == csrc_types::ctype_cl_N)
278 // If the exponent is 1 or -1, it is left out
279 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
280 it->rest.bp->printcsrc(os, type, precedence);
282 // outer parens around ex needed for broken gcc-2.95 parser:
283 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
285 // Separator is "/" for negative integer powers, "*" otherwise
288 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
294 if (precedence <= upper_precedence)
298 bool mul::info(unsigned inf) const
301 if (inf==info_flags::polynomial ||
302 inf==info_flags::integer_polynomial ||
303 inf==info_flags::cinteger_polynomial ||
304 inf==info_flags::rational_polynomial ||
305 inf==info_flags::crational_polynomial ||
306 inf==info_flags::rational_function) {
307 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
308 if (!(recombine_pair_to_ex(*it).info(inf)))
311 return overall_coeff.info(inf);
313 return inherited::info(inf);
317 typedef vector<int> intvector;
319 int mul::degree(const symbol & s) const
322 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
323 deg_sum+=(*cit).rest.degree(s) * ex_to_numeric((*cit).coeff).to_int();
328 int mul::ldegree(const symbol & s) const
331 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
332 deg_sum+=(*cit).rest.ldegree(s) * ex_to_numeric((*cit).coeff).to_int();
337 ex mul::coeff(const symbol & s, int n) const
340 coeffseq.reserve(seq.size()+1);
343 // product of individual coeffs
344 // if a non-zero power of s is found, the resulting product will be 0
345 epvector::const_iterator it=seq.begin();
346 while (it!=seq.end()) {
347 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
350 coeffseq.push_back(overall_coeff);
351 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
354 epvector::const_iterator it=seq.begin();
356 while (it!=seq.end()) {
357 ex t=recombine_pair_to_ex(*it);
360 coeffseq.push_back(c);
363 coeffseq.push_back(t);
368 coeffseq.push_back(overall_coeff);
369 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
375 ex mul::eval(int level) const
377 // simplifications *(...,x;0) -> 0
378 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
382 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
384 epvector * evaled_seqp=evalchildren(level);
385 if (evaled_seqp!=0) {
386 // do more evaluation later
387 return (new mul(evaled_seqp,overall_coeff))->
388 setflag(status_flags::dynallocated);
391 #ifdef DO_GINAC_ASSERT
392 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
393 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
394 (!(ex_to_numeric((*cit).coeff).is_integer())));
395 GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
396 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
399 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
401 expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
402 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
403 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
406 #endif // def DO_GINAC_ASSERT
408 if (flags & status_flags::evaluated) {
409 GINAC_ASSERT(seq.size()>0);
410 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
414 int seq_size=seq.size();
415 if (overall_coeff.is_equal(_ex0())) {
418 } else if (seq_size==0) {
420 return overall_coeff;
421 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex1())) {
423 return recombine_pair_to_ex(*(seq.begin()));
424 } else if ((seq_size==1) &&
425 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
426 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
427 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
428 const add & addref=ex_to_add((*seq.begin()).rest);
430 distrseq.reserve(addref.seq.size());
431 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
432 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
435 return (new add(distrseq,
436 ex_to_numeric(addref.overall_coeff).
437 mul_dyn(ex_to_numeric(overall_coeff))))
438 ->setflag(status_flags::dynallocated |
439 status_flags::evaluated );
444 exvector mul::get_indices(void) const
446 // return union of indices of factors
448 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
449 exvector subiv=(*cit).rest.get_indices();
450 iv.reserve(iv.size()+subiv.size());
451 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
458 ex mul::simplify_ncmul(const exvector & v) const
460 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
465 /** Implementation of ex::diff() for a product. It applies the product rule.
467 ex mul::derivative(const symbol & s) const
470 new_seq.reserve(seq.size());
472 // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
473 for (unsigned i=0; i!=seq.size(); i++) {
474 epvector sub_seq=seq;
475 sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
476 power(sub_seq[i].rest,sub_seq[i].coeff-1)*
477 sub_seq[i].rest.diff(s));
478 new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
480 return (new add(new_seq))->setflag(status_flags::dynallocated);
483 int mul::compare_same_type(const basic & other) const
485 return inherited::compare_same_type(other);
488 bool mul::is_equal_same_type(const basic & other) const
490 return inherited::is_equal_same_type(other);
493 unsigned mul::return_type(void) const
496 // mul without factors: should not happen, but commutes
497 return return_types::commutative;
500 bool all_commutative=1;
502 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
504 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
505 rt=(*cit).rest.return_type();
506 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
507 if ((rt==return_types::noncommutative)&&(all_commutative)) {
508 // first nc element found, remember position
509 cit_noncommutative_element=cit;
512 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
513 // another nc element found, compare type_infos
514 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
515 // diffent types -> mul is ncc
516 return return_types::noncommutative_composite;
520 // all factors checked
521 return all_commutative ? return_types::commutative : return_types::noncommutative;
524 unsigned mul::return_type_tinfo(void) const
527 // mul without factors: should not happen
530 // return type_info of first noncommutative element
531 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
532 if ((*cit).rest.return_type()==return_types::noncommutative) {
533 return (*cit).rest.return_type_tinfo();
536 // no noncommutative element found, should not happen
540 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
542 return (new mul(v,oc))->setflag(status_flags::dynallocated);
545 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
547 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
550 expair mul::split_ex_to_pair(const ex & e) const
552 if (is_ex_exactly_of_type(e,power)) {
553 const power & powerref=ex_to_power(e);
554 if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
555 return expair(powerref.basis,powerref.exponent);
558 return expair(e,_ex1());
561 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
564 // to avoid duplication of power simplification rules,
565 // we create a temporary power object
566 // otherwise it would be hard to correctly simplify
567 // expression like (4^(1/3))^(3/2)
568 if (are_ex_trivially_equal(c,_ex1())) {
569 return split_ex_to_pair(e);
571 return split_ex_to_pair(power(e,c));
574 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
577 // to avoid duplication of power simplification rules,
578 // we create a temporary power object
579 // otherwise it would be hard to correctly simplify
580 // expression like (4^(1/3))^(3/2)
581 if (are_ex_trivially_equal(c,_ex1())) {
584 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
587 ex mul::recombine_pair_to_ex(const expair & p) const
589 // if (p.coeff.compare(_ex1())==0) {
590 // if (are_ex_trivially_equal(p.coeff,_ex1())) {
591 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
594 return power(p.rest,p.coeff);
598 bool mul::expair_needs_further_processing(epp it)
600 if (is_ex_exactly_of_type((*it).rest,mul) &&
601 ex_to_numeric((*it).coeff).is_integer()) {
602 // combined pair is product with integer power -> expand it
603 *it=split_ex_to_pair(recombine_pair_to_ex(*it));
606 if (is_ex_exactly_of_type((*it).rest,numeric)) {
607 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
608 if (!ep.is_equal(*it)) {
609 // combined pair is a numeric power which can be simplified
613 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
614 // combined pair has coeff 1 and must be moved to the end
621 ex mul::default_overall_coeff(void) const
626 void mul::combine_overall_coeff(const ex & c)
628 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
629 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
630 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
633 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
635 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
636 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
637 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
638 overall_coeff = ex_to_numeric(overall_coeff).
639 mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
642 bool mul::can_make_flat(const expair & p) const
644 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
645 // this assertion will probably fail somewhere
646 // it would require a more careful make_flat, obeying the power laws
647 // probably should return true only if p.coeff is integer
648 return ex_to_numeric(p.coeff).is_equal(_num1());
651 ex mul::expand(unsigned options) const
653 exvector sub_expanded_seq;
654 intvector positions_of_adds;
655 intvector number_of_add_operands;
657 epvector * expanded_seqp=expandchildren(options);
659 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
661 positions_of_adds.resize(expanded_seq.size());
662 number_of_add_operands.resize(expanded_seq.size());
664 int number_of_adds=0;
665 int number_of_expanded_terms=1;
667 unsigned current_position=0;
668 epvector::const_iterator last=expanded_seq.end();
669 for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
670 if (is_ex_exactly_of_type((*cit).rest,add)&&
671 (ex_to_numeric((*cit).coeff).is_equal(_num1()))) {
672 positions_of_adds[number_of_adds]=current_position;
673 const add & expanded_addref=ex_to_add((*cit).rest);
674 unsigned addref_nops=expanded_addref.nops();
675 number_of_add_operands[number_of_adds]=addref_nops;
676 number_of_expanded_terms *= addref_nops;
682 if (number_of_adds==0) {
683 if (expanded_seqp==0) {
684 return this->setflag(status_flags::expanded);
686 return (new mul(expanded_seqp,overall_coeff))->
687 setflag(status_flags::dynallocated ||
688 status_flags::expanded);
692 distrseq.reserve(number_of_expanded_terms);
695 k.resize(number_of_adds);
698 for (l=0; l<number_of_adds; l++) {
705 for (l=0; l<number_of_adds; l++) {
706 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
707 GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(_ex1())==0);
708 term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
711 cout << "mul::expand() term begin" << endl;
712 for (epvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
713 cout << "rest" << endl;
714 (*cit).rest.printtree(cout);
715 cout << "coeff" << endl;
716 (*cit).coeff.printtree(cout);
718 cout << "mul::expand() term end" << endl;
720 distrseq.push_back((new mul(term,overall_coeff))->
721 setflag(status_flags::dynallocated |
722 status_flags::expanded));
726 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
733 if (expanded_seqp!=0) {
734 delete expanded_seqp;
737 cout << "mul::expand() distrseq begin" << endl;
738 for (exvector::const_iterator cit=distrseq.begin(); cit!=distrseq.end(); ++cit) {
739 (*cit).printtree(cout);
741 cout << "mul::expand() distrseq end" << endl;
744 return (new add(distrseq))->setflag(status_flags::dynallocated |
745 status_flags::expanded);
749 // new virtual functions which can be overridden by derived classes
755 // non-virtual functions in this class
758 epvector * mul::expandchildren(unsigned options) const
760 epvector::const_iterator last=seq.end();
761 epvector::const_iterator cit=seq.begin();
763 const ex & factor=recombine_pair_to_ex(*cit);
764 const ex & expanded_factor=factor.expand(options);
765 if (!are_ex_trivially_equal(factor,expanded_factor)) {
767 // something changed, copy seq, eval and return it
768 epvector *s=new epvector;
769 s->reserve(seq.size());
771 // copy parts of seq which are known not to have changed
772 epvector::const_iterator cit2=seq.begin();
777 // copy first changed element
778 s->push_back(split_ex_to_pair(expanded_factor));
782 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
790 return 0; // nothing has changed
794 // static member variables
799 unsigned mul::precedence=50;
807 const type_info & typeid_mul=typeid(some_mul);
809 #ifndef NO_NAMESPACE_GINAC
811 #endif // ndef NO_NAMESPACE_GINAC