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
31 #ifndef NO_GINAC_NAMESPACE
33 #endif // ndef NO_GINAC_NAMESPACE
36 // default constructor, destructor, copy constructor assignment operator and helpers
43 debugmsg("mul default constructor",LOGLEVEL_CONSTRUCT);
44 tinfo_key = TINFO_mul;
49 debugmsg("mul destructor",LOGLEVEL_DESTRUCT);
53 mul::mul(mul const & other)
55 debugmsg("mul copy constructor",LOGLEVEL_CONSTRUCT);
59 mul const & mul::operator=(mul const & other)
61 debugmsg("mul operator=",LOGLEVEL_ASSIGNMENT);
71 void mul::copy(mul const & other)
73 expairseq::copy(other);
76 void mul::destroy(bool call_parent)
78 if (call_parent) expairseq::destroy(call_parent);
87 mul::mul(ex const & lh, ex const & rh)
89 debugmsg("mul constructor from ex,ex",LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_mul;
91 overall_coeff=exONE();
92 construct_from_2_ex(lh,rh);
93 GINAC_ASSERT(is_canonical());
96 mul::mul(exvector const & v)
98 debugmsg("mul constructor from exvector",LOGLEVEL_CONSTRUCT);
99 tinfo_key = TINFO_mul;
100 overall_coeff=exONE();
101 construct_from_exvector(v);
102 GINAC_ASSERT(is_canonical());
106 mul::mul(epvector const & v, bool do_not_canonicalize)
108 debugmsg("mul constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
109 tinfo_key = TINFO_mul;
110 if (do_not_canonicalize) {
112 #ifdef EXPAIRSEQ_USE_HASHTAB
113 combine_same_terms(); // to build hashtab
114 #endif // def EXPAIRSEQ_USE_HASHTAB
116 construct_from_epvector(v);
118 GINAC_ASSERT(is_canonical());
122 mul::mul(epvector const & v)
124 debugmsg("mul constructor from epvector",LOGLEVEL_CONSTRUCT);
125 tinfo_key = TINFO_mul;
126 overall_coeff=exONE();
127 construct_from_epvector(v);
128 GINAC_ASSERT(is_canonical());
131 mul::mul(epvector const & v, ex const & oc)
133 debugmsg("mul constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
134 tinfo_key = TINFO_mul;
136 construct_from_epvector(v);
137 GINAC_ASSERT(is_canonical());
140 mul::mul(epvector * vp, ex const & oc)
142 debugmsg("mul constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
143 tinfo_key = TINFO_mul;
146 construct_from_epvector(*vp);
148 GINAC_ASSERT(is_canonical());
151 mul::mul(ex const & lh, ex const & mh, ex const & rh)
153 debugmsg("mul constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
154 tinfo_key = TINFO_mul;
157 factors.push_back(lh);
158 factors.push_back(mh);
159 factors.push_back(rh);
160 overall_coeff=exONE();
161 construct_from_exvector(factors);
162 GINAC_ASSERT(is_canonical());
166 // functions overriding virtual functions from bases classes
171 basic * mul::duplicate() const
173 debugmsg("mul duplicate",LOGLEVEL_ASSIGNMENT);
174 return new mul(*this);
177 bool mul::info(unsigned inf) const
180 if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
181 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
182 if (!(recombine_pair_to_ex(*it).info(inf)))
187 return expairseq::info(inf);
191 typedef vector<int> intvector;
193 int mul::degree(symbol const & s) const
196 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
197 deg_sum+=(*cit).rest.degree(s) * ex_to_numeric((*cit).coeff).to_int();
202 int mul::ldegree(symbol const & s) const
205 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
206 deg_sum+=(*cit).rest.ldegree(s) * ex_to_numeric((*cit).coeff).to_int();
211 ex mul::coeff(symbol const & s, int const n) const
214 coeffseq.reserve(seq.size()+1);
217 // product of individual coeffs
218 // if a non-zero power of s is found, the resulting product will be 0
219 epvector::const_iterator it=seq.begin();
220 while (it!=seq.end()) {
221 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
224 coeffseq.push_back(overall_coeff);
225 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
228 epvector::const_iterator it=seq.begin();
230 while (it!=seq.end()) {
231 ex t=recombine_pair_to_ex(*it);
234 coeffseq.push_back(c);
237 coeffseq.push_back(t);
242 coeffseq.push_back(overall_coeff);
243 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
249 ex mul::eval(int level) const
251 // simplifications *(...,x;0) -> 0
252 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
256 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
258 epvector * evaled_seqp=evalchildren(level);
259 if (evaled_seqp!=0) {
260 // do more evaluation later
261 return (new mul(evaled_seqp,overall_coeff))->
262 setflag(status_flags::dynallocated);
265 #ifdef DO_GINAC_ASSERT
266 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
267 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
268 (!(ex_to_numeric((*cit).coeff).is_integer())));
269 GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
270 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
273 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
275 expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
276 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
277 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
280 #endif // def DO_GINAC_ASSERT
282 if (flags & status_flags::evaluated) {
283 GINAC_ASSERT(seq.size()>0);
284 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(exONE()));
288 int seq_size=seq.size();
289 if (overall_coeff.is_equal(exZERO())) {
292 } else if (seq_size==0) {
294 return overall_coeff;
295 } else if ((seq_size==1)&&overall_coeff.is_equal(exONE())) {
297 return recombine_pair_to_ex(*(seq.begin()));
298 } else if ((seq_size==1) &&
299 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
300 ex_to_numeric((*seq.begin()).coeff).is_equal(numONE())) {
301 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
302 add const & addref=ex_to_add((*seq.begin()).rest);
304 distrseq.reserve(addref.seq.size());
305 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
306 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
309 return (new add(distrseq,
310 ex_to_numeric(addref.overall_coeff).
311 mul_dyn(ex_to_numeric(overall_coeff))))
312 ->setflag(status_flags::dynallocated |
313 status_flags::evaluated );
318 exvector mul::get_indices(void) const
320 // return union of indices of factors
322 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
323 exvector subiv=(*cit).rest.get_indices();
324 iv.reserve(iv.size()+subiv.size());
325 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
332 ex mul::simplify_ncmul(exvector const & v) const
334 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
339 int mul::compare_same_type(basic const & other) const
341 return expairseq::compare_same_type(other);
344 bool mul::is_equal_same_type(basic const & other) const
346 return expairseq::is_equal_same_type(other);
349 unsigned mul::return_type(void) const
352 // mul without factors: should not happen, but commutes
353 return return_types::commutative;
356 bool all_commutative=1;
358 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
360 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
361 rt=(*cit).rest.return_type();
362 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
363 if ((rt==return_types::noncommutative)&&(all_commutative)) {
364 // first nc element found, remember position
365 cit_noncommutative_element=cit;
368 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
369 // another nc element found, compare type_infos
370 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
371 // diffent types -> mul is ncc
372 return return_types::noncommutative_composite;
376 // all factors checked
377 return all_commutative ? return_types::commutative : return_types::noncommutative;
380 unsigned mul::return_type_tinfo(void) const
383 // mul without factors: should not happen
386 // return type_info of first noncommutative element
387 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
388 if ((*cit).rest.return_type()==return_types::noncommutative) {
389 return (*cit).rest.return_type_tinfo();
392 // no noncommutative element found, should not happen
396 ex mul::thisexpairseq(epvector const & v, ex const & oc) const
398 return (new mul(v,oc))->setflag(status_flags::dynallocated);
401 ex mul::thisexpairseq(epvector * vp, ex const & oc) const
403 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
406 expair mul::split_ex_to_pair(ex const & e) const
408 if (is_ex_exactly_of_type(e,power)) {
409 power const & powerref=ex_to_power(e);
410 if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
411 return expair(powerref.basis,powerref.exponent);
414 return expair(e,exONE());
417 expair mul::combine_ex_with_coeff_to_pair(ex const & e,
420 // to avoid duplication of power simplification rules,
421 // we create a temporary power object
422 // otherwise it would be hard to correctly simplify
423 // expression like (4^(1/3))^(3/2)
424 if (are_ex_trivially_equal(c,exONE())) {
425 return split_ex_to_pair(e);
427 return split_ex_to_pair(power(e,c));
430 expair mul::combine_pair_with_coeff_to_pair(expair const & p,
433 // to avoid duplication of power simplification rules,
434 // we create a temporary power object
435 // otherwise it would be hard to correctly simplify
436 // expression like (4^(1/3))^(3/2)
437 if (are_ex_trivially_equal(c,exONE())) {
440 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
443 ex mul::recombine_pair_to_ex(expair const & p) const
445 // if (p.coeff.compare(exONE())==0) {
446 // if (are_ex_trivially_equal(p.coeff,exONE())) {
447 if (ex_to_numeric(p.coeff).is_equal(numONE())) {
450 return power(p.rest,p.coeff);
454 bool mul::expair_needs_further_processing(epp it)
456 if (is_ex_exactly_of_type((*it).rest,mul) &&
457 ex_to_numeric((*it).coeff).is_integer()) {
458 // combined pair is product with integer power -> expand it
459 *it=split_ex_to_pair(recombine_pair_to_ex(*it));
462 if (is_ex_exactly_of_type((*it).rest,numeric)) {
463 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
464 if (!ep.is_equal(*it)) {
465 // combined pair is a numeric power which can be simplified
469 if (ex_to_numeric((*it).coeff).is_equal(numONE())) {
470 // combined pair has coeff 1 and must be moved to the end
477 ex mul::default_overall_coeff(void) const
482 void mul::combine_overall_coeff(ex const & c)
484 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
485 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
486 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
489 void mul::combine_overall_coeff(ex const & c1, ex const & c2)
491 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
492 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
493 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
494 overall_coeff = ex_to_numeric(overall_coeff).
495 mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
498 bool mul::can_make_flat(expair const & p) const
500 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
501 // this assertion will probably fail somewhere
502 // it would require a more careful make_flat, obeying the power laws
503 // probably should return true only if p.coeff is integer
504 return ex_to_numeric(p.coeff).is_equal(numONE());
507 ex mul::expand(unsigned options) const
509 exvector sub_expanded_seq;
510 intvector positions_of_adds;
511 intvector number_of_add_operands;
513 epvector * expanded_seqp=expandchildren(options);
515 epvector const & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
517 positions_of_adds.resize(expanded_seq.size());
518 number_of_add_operands.resize(expanded_seq.size());
520 int number_of_adds=0;
521 int number_of_expanded_terms=1;
523 unsigned current_position=0;
524 epvector::const_iterator last=expanded_seq.end();
525 for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
526 if (is_ex_exactly_of_type((*cit).rest,add)&&
527 (ex_to_numeric((*cit).coeff).is_equal(numONE()))) {
528 positions_of_adds[number_of_adds]=current_position;
529 add const & expanded_addref=ex_to_add((*cit).rest);
530 int addref_nops=expanded_addref.nops();
531 number_of_add_operands[number_of_adds]=addref_nops;
532 number_of_expanded_terms *= addref_nops;
538 if (number_of_adds==0) {
539 if (expanded_seqp==0) {
540 return this->setflag(status_flags::expanded);
542 return (new mul(expanded_seqp,overall_coeff))->
543 setflag(status_flags::dynallocated ||
544 status_flags::expanded);
548 distrseq.reserve(number_of_expanded_terms);
551 k.resize(number_of_adds);
554 for (l=0; l<number_of_adds; l++) {
561 for (l=0; l<number_of_adds; l++) {
562 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
563 GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(exONE())==0);
564 term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
567 cout << "mul::expand() term begin" << endl;
568 for (epvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
569 cout << "rest" << endl;
570 (*cit).rest.printtree(cout);
571 cout << "coeff" << endl;
572 (*cit).coeff.printtree(cout);
574 cout << "mul::expand() term end" << endl;
576 distrseq.push_back((new mul(term,overall_coeff))->
577 setflag(status_flags::dynallocated |
578 status_flags::expanded));
582 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
589 if (expanded_seqp!=0) {
590 delete expanded_seqp;
593 cout << "mul::expand() distrseq begin" << endl;
594 for (exvector::const_iterator cit=distrseq.begin(); cit!=distrseq.end(); ++cit) {
595 (*cit).printtree(cout);
597 cout << "mul::expand() distrseq end" << endl;
600 return (new add(distrseq))->setflag(status_flags::dynallocated |
601 status_flags::expanded);
605 // new virtual functions which can be overridden by derived classes
611 // non-virtual functions in this class
614 epvector * mul::expandchildren(unsigned options) const
616 epvector::const_iterator last=seq.end();
617 epvector::const_iterator cit=seq.begin();
619 ex const & factor=recombine_pair_to_ex(*cit);
620 ex const & expanded_factor=factor.expand(options);
621 if (!are_ex_trivially_equal(factor,expanded_factor)) {
623 // something changed, copy seq, eval and return it
624 epvector *s=new epvector;
625 s->reserve(seq.size());
627 // copy parts of seq which are known not to have changed
628 epvector::const_iterator cit2=seq.begin();
633 // copy first changed element
634 s->push_back(split_ex_to_pair(expanded_factor));
638 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
646 return 0; // nothing has changed
650 // static member variables
655 unsigned mul::precedence=50;
663 type_info const & typeid_mul=typeid(some_mul);
665 #ifndef NO_GINAC_NAMESPACE
667 #endif // ndef NO_GINAC_NAMESPACE