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_GINAC_NAMESPACE
35 #endif // ndef NO_GINAC_NAMESPACE
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(mul const & other)
59 debugmsg("mul copy constructor",LOGLEVEL_CONSTRUCT);
63 mul const & mul::operator=(mul const & other)
65 debugmsg("mul operator=",LOGLEVEL_ASSIGNMENT);
75 void mul::copy(mul const & other)
77 inherited::copy(other);
80 void mul::destroy(bool call_parent)
82 if (call_parent) inherited::destroy(call_parent);
91 mul::mul(ex const & lh, ex const & 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(exvector const & 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(epvector const & 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(epvector const & 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(epvector const & v, ex const & 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, ex const & 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(ex const & lh, ex const & mh, ex const & 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(symbol const & 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(symbol const & 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(symbol const & s, int const 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 add const & 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(exvector const & v) const
460 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
465 int mul::compare_same_type(basic const & other) const
467 return inherited::compare_same_type(other);
470 bool mul::is_equal_same_type(basic const & other) const
472 return inherited::is_equal_same_type(other);
475 unsigned mul::return_type(void) const
478 // mul without factors: should not happen, but commutes
479 return return_types::commutative;
482 bool all_commutative=1;
484 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
486 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
487 rt=(*cit).rest.return_type();
488 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
489 if ((rt==return_types::noncommutative)&&(all_commutative)) {
490 // first nc element found, remember position
491 cit_noncommutative_element=cit;
494 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
495 // another nc element found, compare type_infos
496 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
497 // diffent types -> mul is ncc
498 return return_types::noncommutative_composite;
502 // all factors checked
503 return all_commutative ? return_types::commutative : return_types::noncommutative;
506 unsigned mul::return_type_tinfo(void) const
509 // mul without factors: should not happen
512 // return type_info of first noncommutative element
513 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
514 if ((*cit).rest.return_type()==return_types::noncommutative) {
515 return (*cit).rest.return_type_tinfo();
518 // no noncommutative element found, should not happen
522 ex mul::thisexpairseq(epvector const & v, ex const & oc) const
524 return (new mul(v,oc))->setflag(status_flags::dynallocated);
527 ex mul::thisexpairseq(epvector * vp, ex const & oc) const
529 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
532 expair mul::split_ex_to_pair(ex const & e) const
534 if (is_ex_exactly_of_type(e,power)) {
535 power const & powerref=ex_to_power(e);
536 if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
537 return expair(powerref.basis,powerref.exponent);
540 return expair(e,_ex1());
543 expair mul::combine_ex_with_coeff_to_pair(ex const & e,
546 // to avoid duplication of power simplification rules,
547 // we create a temporary power object
548 // otherwise it would be hard to correctly simplify
549 // expression like (4^(1/3))^(3/2)
550 if (are_ex_trivially_equal(c,_ex1())) {
551 return split_ex_to_pair(e);
553 return split_ex_to_pair(power(e,c));
556 expair mul::combine_pair_with_coeff_to_pair(expair const & p,
559 // to avoid duplication of power simplification rules,
560 // we create a temporary power object
561 // otherwise it would be hard to correctly simplify
562 // expression like (4^(1/3))^(3/2)
563 if (are_ex_trivially_equal(c,_ex1())) {
566 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
569 ex mul::recombine_pair_to_ex(expair const & p) const
571 // if (p.coeff.compare(_ex1())==0) {
572 // if (are_ex_trivially_equal(p.coeff,_ex1())) {
573 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
576 return power(p.rest,p.coeff);
580 bool mul::expair_needs_further_processing(epp it)
582 if (is_ex_exactly_of_type((*it).rest,mul) &&
583 ex_to_numeric((*it).coeff).is_integer()) {
584 // combined pair is product with integer power -> expand it
585 *it=split_ex_to_pair(recombine_pair_to_ex(*it));
588 if (is_ex_exactly_of_type((*it).rest,numeric)) {
589 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
590 if (!ep.is_equal(*it)) {
591 // combined pair is a numeric power which can be simplified
595 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
596 // combined pair has coeff 1 and must be moved to the end
603 ex mul::default_overall_coeff(void) const
608 void mul::combine_overall_coeff(ex const & c)
610 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
611 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
612 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
615 void mul::combine_overall_coeff(ex const & c1, ex const & c2)
617 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
618 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
619 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
620 overall_coeff = ex_to_numeric(overall_coeff).
621 mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
624 bool mul::can_make_flat(expair const & 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 exvector sub_expanded_seq;
636 intvector positions_of_adds;
637 intvector number_of_add_operands;
639 epvector * expanded_seqp=expandchildren(options);
641 epvector const & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
643 positions_of_adds.resize(expanded_seq.size());
644 number_of_add_operands.resize(expanded_seq.size());
646 int number_of_adds=0;
647 int number_of_expanded_terms=1;
649 unsigned current_position=0;
650 epvector::const_iterator last=expanded_seq.end();
651 for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
652 if (is_ex_exactly_of_type((*cit).rest,add)&&
653 (ex_to_numeric((*cit).coeff).is_equal(_num1()))) {
654 positions_of_adds[number_of_adds]=current_position;
655 add const & expanded_addref=ex_to_add((*cit).rest);
656 unsigned addref_nops=expanded_addref.nops();
657 number_of_add_operands[number_of_adds]=addref_nops;
658 number_of_expanded_terms *= addref_nops;
664 if (number_of_adds==0) {
665 if (expanded_seqp==0) {
666 return this->setflag(status_flags::expanded);
668 return (new mul(expanded_seqp,overall_coeff))->
669 setflag(status_flags::dynallocated ||
670 status_flags::expanded);
674 distrseq.reserve(number_of_expanded_terms);
677 k.resize(number_of_adds);
680 for (l=0; l<number_of_adds; l++) {
687 for (l=0; l<number_of_adds; l++) {
688 add const & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
689 GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(_ex1())==0);
690 term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
693 cout << "mul::expand() term begin" << endl;
694 for (epvector::const_iterator cit=term.begin(); cit!=term.end(); ++cit) {
695 cout << "rest" << endl;
696 (*cit).rest.printtree(cout);
697 cout << "coeff" << endl;
698 (*cit).coeff.printtree(cout);
700 cout << "mul::expand() term end" << endl;
702 distrseq.push_back((new mul(term,overall_coeff))->
703 setflag(status_flags::dynallocated |
704 status_flags::expanded));
708 while ((l>=0)&&((++k[l])>=number_of_add_operands[l])) {
715 if (expanded_seqp!=0) {
716 delete expanded_seqp;
719 cout << "mul::expand() distrseq begin" << endl;
720 for (exvector::const_iterator cit=distrseq.begin(); cit!=distrseq.end(); ++cit) {
721 (*cit).printtree(cout);
723 cout << "mul::expand() distrseq end" << endl;
726 return (new add(distrseq))->setflag(status_flags::dynallocated |
727 status_flags::expanded);
731 // new virtual functions which can be overridden by derived classes
737 // non-virtual functions in this class
740 epvector * mul::expandchildren(unsigned options) const
742 epvector::const_iterator last=seq.end();
743 epvector::const_iterator cit=seq.begin();
745 ex const & factor=recombine_pair_to_ex(*cit);
746 ex const & expanded_factor=factor.expand(options);
747 if (!are_ex_trivially_equal(factor,expanded_factor)) {
749 // something changed, copy seq, eval and return it
750 epvector *s=new epvector;
751 s->reserve(seq.size());
753 // copy parts of seq which are known not to have changed
754 epvector::const_iterator cit2=seq.begin();
759 // copy first changed element
760 s->push_back(split_ex_to_pair(expanded_factor));
764 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
772 return 0; // nothing has changed
776 // static member variables
781 unsigned mul::precedence=50;
789 type_info const & typeid_mul=typeid(some_mul);
791 #ifndef NO_GINAC_NAMESPACE
793 #endif // ndef NO_GINAC_NAMESPACE