3 * Implementation of GiNaC's products of expressions. */
6 * GiNaC Copyright (C) 1999 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
32 #ifndef NO_GINAC_NAMESPACE
34 #endif // ndef NO_GINAC_NAMESPACE
37 // default constructor, destructor, copy constructor assignment operator and helpers
44 debugmsg("mul default constructor",LOGLEVEL_CONSTRUCT);
45 tinfo_key = TINFO_mul;
50 debugmsg("mul destructor",LOGLEVEL_DESTRUCT);
54 mul::mul(mul const & other)
56 debugmsg("mul copy constructor",LOGLEVEL_CONSTRUCT);
60 mul const & mul::operator=(mul const & other)
62 debugmsg("mul operator=",LOGLEVEL_ASSIGNMENT);
72 void mul::copy(mul const & other)
74 expairseq::copy(other);
77 void mul::destroy(bool call_parent)
79 if (call_parent) expairseq::destroy(call_parent);
88 mul::mul(ex const & lh, ex const & rh)
90 debugmsg("mul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
91 tinfo_key = TINFO_mul;
93 construct_from_2_ex(lh,rh);
94 GINAC_ASSERT(is_canonical());
97 mul::mul(exvector const & v)
99 debugmsg("mul constructor from exvector",LOGLEVEL_CONSTRUCT);
100 tinfo_key = TINFO_mul;
101 overall_coeff=_ex1();
102 construct_from_exvector(v);
103 GINAC_ASSERT(is_canonical());
107 mul::mul(epvector const & v, bool do_not_canonicalize)
109 debugmsg("mul constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
110 tinfo_key = TINFO_mul;
111 if (do_not_canonicalize) {
113 #ifdef EXPAIRSEQ_USE_HASHTAB
114 combine_same_terms(); // to build hashtab
115 #endif // def EXPAIRSEQ_USE_HASHTAB
117 construct_from_epvector(v);
119 GINAC_ASSERT(is_canonical());
123 mul::mul(epvector const & v)
125 debugmsg("mul constructor from epvector",LOGLEVEL_CONSTRUCT);
126 tinfo_key = TINFO_mul;
127 overall_coeff=_ex1();
128 construct_from_epvector(v);
129 GINAC_ASSERT(is_canonical());
132 mul::mul(epvector const & v, ex const & oc)
134 debugmsg("mul constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
135 tinfo_key = TINFO_mul;
137 construct_from_epvector(v);
138 GINAC_ASSERT(is_canonical());
141 mul::mul(epvector * vp, ex const & oc)
143 debugmsg("mul constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
144 tinfo_key = TINFO_mul;
147 construct_from_epvector(*vp);
149 GINAC_ASSERT(is_canonical());
152 mul::mul(ex const & lh, ex const & mh, ex const & rh)
154 debugmsg("mul constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
155 tinfo_key = TINFO_mul;
158 factors.push_back(lh);
159 factors.push_back(mh);
160 factors.push_back(rh);
161 overall_coeff=_ex1();
162 construct_from_exvector(factors);
163 GINAC_ASSERT(is_canonical());
167 // functions overriding virtual functions from bases classes
172 basic * mul::duplicate() const
174 debugmsg("mul duplicate",LOGLEVEL_ASSIGNMENT);
175 return new mul(*this);
178 void mul::print(ostream & os, unsigned upper_precedence) const
180 debugmsg("mul print",LOGLEVEL_PRINT);
181 if (precedence<=upper_precedence) os << "(";
183 // first print the overall numeric coefficient:
184 if (ex_to_numeric(overall_coeff).csgn()==-1) os << '-';
185 if (!overall_coeff.is_equal(_ex1()) &&
186 !overall_coeff.is_equal(_ex_1())) {
187 if (ex_to_numeric(overall_coeff).csgn()==-1)
188 (_num_1()*overall_coeff).print(os, precedence);
190 overall_coeff.print(os, precedence);
193 // then proceed with the remaining factors:
194 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
200 recombine_pair_to_ex(*cit).print(os,precedence);
202 if (precedence<=upper_precedence) os << ")";
205 void mul::printraw(ostream & os) const
207 debugmsg("mul printraw",LOGLEVEL_PRINT);
210 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
212 (*it).rest.bp->printraw(os);
214 (*it).coeff.bp->printraw(os);
217 os << ",hash=" << hashvalue << ",flags=" << flags;
221 void mul::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
223 debugmsg("mul print csrc", LOGLEVEL_PRINT);
224 if (precedence <= upper_precedence)
227 if (!overall_coeff.is_equal(_ex1())) {
228 overall_coeff.bp->printcsrc(os,type,precedence);
232 // Print arguments, separated by "*" or "/"
233 epvector::const_iterator it = seq.begin();
234 epvector::const_iterator itend = seq.end();
235 while (it != itend) {
237 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
238 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
239 if (type == csrc_types::ctype_cl_N)
245 // If the exponent is 1 or -1, it is left out
246 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
247 it->rest.bp->printcsrc(os, type, precedence);
249 // outer parens around ex needed for broken gcc-2.95 parser:
250 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
252 // Separator is "/" for negative integer powers, "*" otherwise
255 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
261 if (precedence <= upper_precedence)
265 bool mul::info(unsigned inf) const
268 if (inf==info_flags::polynomial ||
269 inf==info_flags::integer_polynomial ||
270 inf==info_flags::cinteger_polynomial ||
271 inf==info_flags::rational_polynomial ||
272 inf==info_flags::crational_polynomial ||
273 inf==info_flags::rational_function) {
274 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
275 if (!(recombine_pair_to_ex(*it).info(inf)))
278 return overall_coeff.info(inf);
280 return expairseq::info(inf);
284 typedef vector<int> intvector;
286 int mul::degree(symbol const & s) const
289 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
290 deg_sum+=(*cit).rest.degree(s) * ex_to_numeric((*cit).coeff).to_int();
295 int mul::ldegree(symbol const & s) const
298 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
299 deg_sum+=(*cit).rest.ldegree(s) * ex_to_numeric((*cit).coeff).to_int();
304 ex mul::coeff(symbol const & s, int const n) const
307 coeffseq.reserve(seq.size()+1);
310 // product of individual coeffs
311 // if a non-zero power of s is found, the resulting product will be 0
312 epvector::const_iterator it=seq.begin();
313 while (it!=seq.end()) {
314 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
317 coeffseq.push_back(overall_coeff);
318 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
321 epvector::const_iterator it=seq.begin();
323 while (it!=seq.end()) {
324 ex t=recombine_pair_to_ex(*it);
327 coeffseq.push_back(c);
330 coeffseq.push_back(t);
335 coeffseq.push_back(overall_coeff);
336 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
342 ex mul::eval(int level) const
344 // simplifications *(...,x;0) -> 0
345 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
349 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
351 epvector * evaled_seqp=evalchildren(level);
352 if (evaled_seqp!=0) {
353 // do more evaluation later
354 return (new mul(evaled_seqp,overall_coeff))->
355 setflag(status_flags::dynallocated);
358 #ifdef DO_GINAC_ASSERT
359 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
360 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
361 (!(ex_to_numeric((*cit).coeff).is_integer())));
362 GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
363 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
366 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
368 expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
369 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
370 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
373 #endif // def DO_GINAC_ASSERT
375 if (flags & status_flags::evaluated) {
376 GINAC_ASSERT(seq.size()>0);
377 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
381 int seq_size=seq.size();
382 if (overall_coeff.is_equal(_ex0())) {
385 } else if (seq_size==0) {
387 return overall_coeff;
388 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex1())) {
390 return recombine_pair_to_ex(*(seq.begin()));
391 } else if ((seq_size==1) &&
392 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
393 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
394 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
395 add const & addref=ex_to_add((*seq.begin()).rest);
397 distrseq.reserve(addref.seq.size());
398 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
399 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
402 return (new add(distrseq,
403 ex_to_numeric(addref.overall_coeff).
404 mul_dyn(ex_to_numeric(overall_coeff))))
405 ->setflag(status_flags::dynallocated |
406 status_flags::evaluated );
411 exvector mul::get_indices(void) const
413 // return union of indices of factors
415 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
416 exvector subiv=(*cit).rest.get_indices();
417 iv.reserve(iv.size()+subiv.size());
418 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
425 ex mul::simplify_ncmul(exvector const & v) const
427 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
432 int mul::compare_same_type(basic const & other) const
434 return expairseq::compare_same_type(other);
437 bool mul::is_equal_same_type(basic const & other) const
439 return expairseq::is_equal_same_type(other);
442 unsigned mul::return_type(void) const
445 // mul without factors: should not happen, but commutes
446 return return_types::commutative;
449 bool all_commutative=1;
451 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
453 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
454 rt=(*cit).rest.return_type();
455 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
456 if ((rt==return_types::noncommutative)&&(all_commutative)) {
457 // first nc element found, remember position
458 cit_noncommutative_element=cit;
461 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
462 // another nc element found, compare type_infos
463 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
464 // diffent types -> mul is ncc
465 return return_types::noncommutative_composite;
469 // all factors checked
470 return all_commutative ? return_types::commutative : return_types::noncommutative;
473 unsigned mul::return_type_tinfo(void) const
476 // mul without factors: should not happen
479 // return type_info of first noncommutative element
480 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
481 if ((*cit).rest.return_type()==return_types::noncommutative) {
482 return (*cit).rest.return_type_tinfo();
485 // no noncommutative element found, should not happen
489 ex mul::thisexpairseq(epvector const & v, ex const & oc) const
491 return (new mul(v,oc))->setflag(status_flags::dynallocated);
494 ex mul::thisexpairseq(epvector * vp, ex const & oc) const
496 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
499 expair mul::split_ex_to_pair(ex const & e) const
501 if (is_ex_exactly_of_type(e,power)) {
502 power const & powerref=ex_to_power(e);
503 if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
504 return expair(powerref.basis,powerref.exponent);
507 return expair(e,_ex1());
510 expair mul::combine_ex_with_coeff_to_pair(ex const & e,
513 // to avoid duplication of power simplification rules,
514 // we create a temporary power object
515 // otherwise it would be hard to correctly simplify
516 // expression like (4^(1/3))^(3/2)
517 if (are_ex_trivially_equal(c,_ex1())) {
518 return split_ex_to_pair(e);
520 return split_ex_to_pair(power(e,c));
523 expair mul::combine_pair_with_coeff_to_pair(expair const & p,
526 // to avoid duplication of power simplification rules,
527 // we create a temporary power object
528 // otherwise it would be hard to correctly simplify
529 // expression like (4^(1/3))^(3/2)
530 if (are_ex_trivially_equal(c,_ex1())) {
533 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
536 ex mul::recombine_pair_to_ex(expair const & p) const
538 // if (p.coeff.compare(_ex1())==0) {
539 // if (are_ex_trivially_equal(p.coeff,_ex1())) {
540 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
543 return power(p.rest,p.coeff);
547 bool mul::expair_needs_further_processing(epp it)
549 if (is_ex_exactly_of_type((*it).rest,mul) &&
550 ex_to_numeric((*it).coeff).is_integer()) {
551 // combined pair is product with integer power -> expand it
552 *it=split_ex_to_pair(recombine_pair_to_ex(*it));
555 if (is_ex_exactly_of_type((*it).rest,numeric)) {
556 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
557 if (!ep.is_equal(*it)) {
558 // combined pair is a numeric power which can be simplified
562 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
563 // combined pair has coeff 1 and must be moved to the end
570 ex mul::default_overall_coeff(void) const
575 void mul::combine_overall_coeff(ex const & c)
577 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
578 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
579 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
582 void mul::combine_overall_coeff(ex const & c1, ex const & c2)
584 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
585 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
586 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
587 overall_coeff = ex_to_numeric(overall_coeff).
588 mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
591 bool mul::can_make_flat(expair const & p) const
593 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
594 // this assertion will probably fail somewhere
595 // it would require a more careful make_flat, obeying the power laws
596 // probably should return true only if p.coeff is integer
597 return ex_to_numeric(p.coeff).is_equal(_num1());
600 ex mul::expand(unsigned options) const
602 exvector sub_expanded_seq;
603 intvector positions_of_adds;
604 intvector number_of_add_operands;
606 epvector * expanded_seqp=expandchildren(options);
608 epvector const & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
610 positions_of_adds.resize(expanded_seq.size());
611 number_of_add_operands.resize(expanded_seq.size());
613 int number_of_adds=0;
614 int number_of_expanded_terms=1;
616 unsigned current_position=0;
617 epvector::const_iterator last=expanded_seq.end();
618 for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
619 if (is_ex_exactly_of_type((*cit).rest,add)&&
620 (ex_to_numeric((*cit).coeff).is_equal(_num1()))) {
621 positions_of_adds[number_of_adds]=current_position;
622 add const & expanded_addref=ex_to_add((*cit).rest);
623 int addref_nops=expanded_addref.nops();
624 number_of_add_operands[number_of_adds]=addref_nops;
625 number_of_expanded_terms *= addref_nops;
631 if (number_of_adds==0) {
632 if (expanded_seqp==0) {
633 return this->setflag(status_flags::expanded);
635 return (new mul(expanded_seqp,overall_coeff))->
636 setflag(status_flags::dynallocated ||
637 status_flags::expanded);
641 distrseq.reserve(number_of_expanded_terms);
644 k.resize(number_of_adds);
647 for (l=0; l<number_of_adds; l++) {
654 for (l=0; l<number_of_adds; l++) {
655 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
656 GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(_ex1())==0);
657 term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
660 cout << "mul::expand() term begin" << endl;
661 for (epvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
662 cout << "rest" << endl;
663 (*cit).rest.printtree(cout);
664 cout << "coeff" << endl;
665 (*cit).coeff.printtree(cout);
667 cout << "mul::expand() term end" << endl;
669 distrseq.push_back((new mul(term,overall_coeff))->
670 setflag(status_flags::dynallocated |
671 status_flags::expanded));
675 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
682 if (expanded_seqp!=0) {
683 delete expanded_seqp;
686 cout << "mul::expand() distrseq begin" << endl;
687 for (exvector::const_iterator cit=distrseq.begin(); cit!=distrseq.end(); ++cit) {
688 (*cit).printtree(cout);
690 cout << "mul::expand() distrseq end" << endl;
693 return (new add(distrseq))->setflag(status_flags::dynallocated |
694 status_flags::expanded);
698 // new virtual functions which can be overridden by derived classes
704 // non-virtual functions in this class
707 epvector * mul::expandchildren(unsigned options) const
709 epvector::const_iterator last=seq.end();
710 epvector::const_iterator cit=seq.begin();
712 ex const & factor=recombine_pair_to_ex(*cit);
713 ex const & expanded_factor=factor.expand(options);
714 if (!are_ex_trivially_equal(factor,expanded_factor)) {
716 // something changed, copy seq, eval and return it
717 epvector *s=new epvector;
718 s->reserve(seq.size());
720 // copy parts of seq which are known not to have changed
721 epvector::const_iterator cit2=seq.begin();
726 // copy first changed element
727 s->push_back(split_ex_to_pair(expanded_factor));
731 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
739 return 0; // nothing has changed
743 // static member variables
748 unsigned mul::precedence=50;
756 type_info const & typeid_mul=typeid(some_mul);
758 #ifndef NO_GINAC_NAMESPACE
760 #endif // ndef NO_GINAC_NAMESPACE