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;
95 overall_coeff = _ex1();
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());
109 mul::mul(const epvector & v)
111 debugmsg("mul constructor from epvector",LOGLEVEL_CONSTRUCT);
112 tinfo_key = TINFO_mul;
113 overall_coeff = _ex1();
114 construct_from_epvector(v);
115 GINAC_ASSERT(is_canonical());
118 mul::mul(const epvector & v, const ex & oc)
120 debugmsg("mul constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
121 tinfo_key = TINFO_mul;
123 construct_from_epvector(v);
124 GINAC_ASSERT(is_canonical());
127 mul::mul(epvector * vp, const ex & oc)
129 debugmsg("mul constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
130 tinfo_key = TINFO_mul;
133 construct_from_epvector(*vp);
135 GINAC_ASSERT(is_canonical());
138 mul::mul(const ex & lh, const ex & mh, const ex & rh)
140 debugmsg("mul constructor from ex,ex,ex",LOGLEVEL_CONSTRUCT);
141 tinfo_key = TINFO_mul;
144 factors.push_back(lh);
145 factors.push_back(mh);
146 factors.push_back(rh);
147 overall_coeff = _ex1();
148 construct_from_exvector(factors);
149 GINAC_ASSERT(is_canonical());
156 /** Construct object from archive_node. */
157 mul::mul(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
159 debugmsg("mul constructor from archive_node", LOGLEVEL_CONSTRUCT);
162 /** Unarchive the object. */
163 ex mul::unarchive(const archive_node &n, const lst &sym_lst)
165 return (new mul(n, sym_lst))->setflag(status_flags::dynallocated);
168 /** Archive the object. */
169 void mul::archive(archive_node &n) const
171 inherited::archive(n);
175 // functions overriding virtual functions from bases classes
180 basic * mul::duplicate() const
182 debugmsg("mul duplicate",LOGLEVEL_ASSIGNMENT);
183 return new mul(*this);
186 void mul::print(ostream & os, unsigned upper_precedence) const
188 debugmsg("mul print",LOGLEVEL_PRINT);
189 if (precedence<=upper_precedence) os << "(";
191 // first print the overall numeric coefficient:
192 numeric coeff = ex_to_numeric(overall_coeff);
193 if (coeff.csgn()==-1) os << '-';
194 if (!coeff.is_equal(_num1()) &&
195 !coeff.is_equal(_num_1())) {
196 if (coeff.is_rational()) {
197 if (coeff.is_negative())
202 if (coeff.csgn()==-1)
203 (-coeff).print(os, precedence);
205 coeff.print(os, precedence);
209 // then proceed with the remaining factors:
210 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
216 recombine_pair_to_ex(*cit).print(os,precedence);
218 if (precedence<=upper_precedence) os << ")";
221 void mul::printraw(ostream & os) const
223 debugmsg("mul printraw",LOGLEVEL_PRINT);
226 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
228 (*it).rest.bp->printraw(os);
230 (*it).coeff.bp->printraw(os);
233 os << ",hash=" << hashvalue << ",flags=" << flags;
237 void mul::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
239 debugmsg("mul print csrc", LOGLEVEL_PRINT);
240 if (precedence <= upper_precedence)
243 if (!overall_coeff.is_equal(_ex1())) {
244 overall_coeff.bp->printcsrc(os,type,precedence);
248 // Print arguments, separated by "*" or "/"
249 epvector::const_iterator it = seq.begin();
250 epvector::const_iterator itend = seq.end();
251 while (it != itend) {
253 // If the first argument is a negative integer power, it gets printed as "1.0/<expr>"
254 if (it == seq.begin() && ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0) {
255 if (type == csrc_types::ctype_cl_N)
261 // If the exponent is 1 or -1, it is left out
262 if (it->coeff.compare(_ex1()) == 0 || it->coeff.compare(_num_1()) == 0)
263 it->rest.bp->printcsrc(os, type, precedence);
265 // outer parens around ex needed for broken gcc-2.95 parser:
266 (ex(power(it->rest, abs(ex_to_numeric(it->coeff))))).bp->printcsrc(os, type, upper_precedence);
268 // Separator is "/" for negative integer powers, "*" otherwise
271 if (ex_to_numeric(it->coeff).is_integer() && it->coeff.compare(_num0()) < 0)
277 if (precedence <= upper_precedence)
281 bool mul::info(unsigned inf) const
284 if (inf==info_flags::polynomial ||
285 inf==info_flags::integer_polynomial ||
286 inf==info_flags::cinteger_polynomial ||
287 inf==info_flags::rational_polynomial ||
288 inf==info_flags::crational_polynomial ||
289 inf==info_flags::rational_function) {
290 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
291 if (!(recombine_pair_to_ex(*it).info(inf)))
294 return overall_coeff.info(inf);
296 return inherited::info(inf);
300 typedef vector<int> intvector;
302 int mul::degree(const symbol & s) const
305 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
306 deg_sum+=(*cit).rest.degree(s) * ex_to_numeric((*cit).coeff).to_int();
311 int mul::ldegree(const symbol & s) const
314 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
315 deg_sum+=(*cit).rest.ldegree(s) * ex_to_numeric((*cit).coeff).to_int();
320 ex mul::coeff(const symbol & s, int n) const
323 coeffseq.reserve(seq.size()+1);
326 // product of individual coeffs
327 // if a non-zero power of s is found, the resulting product will be 0
328 epvector::const_iterator it=seq.begin();
329 while (it!=seq.end()) {
330 coeffseq.push_back(recombine_pair_to_ex(*it).coeff(s,n));
333 coeffseq.push_back(overall_coeff);
334 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
337 epvector::const_iterator it=seq.begin();
339 while (it!=seq.end()) {
340 ex t=recombine_pair_to_ex(*it);
343 coeffseq.push_back(c);
346 coeffseq.push_back(t);
351 coeffseq.push_back(overall_coeff);
352 return (new mul(coeffseq))->setflag(status_flags::dynallocated);
358 ex mul::eval(int level) const
360 // simplifications *(...,x;0) -> 0
361 // *(+(x,y,...);c) -> *(+(*(x,c),*(y,c),...)) (c numeric())
365 debugmsg("mul eval",LOGLEVEL_MEMBER_FUNCTION);
367 epvector * evaled_seqp=evalchildren(level);
368 if (evaled_seqp!=0) {
369 // do more evaluation later
370 return (new mul(evaled_seqp,overall_coeff))->
371 setflag(status_flags::dynallocated);
374 #ifdef DO_GINAC_ASSERT
375 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
376 GINAC_ASSERT((!is_ex_exactly_of_type((*cit).rest,mul))||
377 (!(ex_to_numeric((*cit).coeff).is_integer())));
378 GINAC_ASSERT(!((*cit).is_numeric_with_coeff_1()));
379 if (is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric)) {
382 GINAC_ASSERT(!is_ex_exactly_of_type(recombine_pair_to_ex(*cit),numeric));
384 expair p=split_ex_to_pair(recombine_pair_to_ex(*cit));
385 GINAC_ASSERT(p.rest.is_equal((*cit).rest));
386 GINAC_ASSERT(p.coeff.is_equal((*cit).coeff));
389 #endif // def DO_GINAC_ASSERT
391 if (flags & status_flags::evaluated) {
392 GINAC_ASSERT(seq.size()>0);
393 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex1()));
397 int seq_size=seq.size();
398 if (overall_coeff.is_equal(_ex0())) {
401 } else if (seq_size==0) {
403 return overall_coeff;
404 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex1())) {
406 return recombine_pair_to_ex(*(seq.begin()));
407 } else if ((seq_size==1) &&
408 is_ex_exactly_of_type((*seq.begin()).rest,add) &&
409 ex_to_numeric((*seq.begin()).coeff).is_equal(_num1())) {
410 // *(+(x,y,...);c) -> +(*(x,c),*(y,c),...) (c numeric(), no powers of +())
411 const add & addref=ex_to_add((*seq.begin()).rest);
413 distrseq.reserve(addref.seq.size());
414 for (epvector::const_iterator cit=addref.seq.begin(); cit!=addref.seq.end(); ++cit) {
415 distrseq.push_back(addref.combine_pair_with_coeff_to_pair(*cit,
418 return (new add(distrseq,
419 ex_to_numeric(addref.overall_coeff).
420 mul_dyn(ex_to_numeric(overall_coeff))))
421 ->setflag(status_flags::dynallocated |
422 status_flags::evaluated );
427 ex mul::evalf(int level) const
430 return mul(seq,overall_coeff);
432 if (level==-max_recursion_level)
433 throw(std::runtime_error("max recursion level reached"));
436 s.reserve(seq.size());
439 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
440 s.push_back(combine_ex_with_coeff_to_pair((*it).rest.evalf(level),
443 return mul(s,overall_coeff.evalf(level));
446 exvector mul::get_indices(void) const
448 // return union of indices of factors
450 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
451 exvector subiv=(*cit).rest.get_indices();
452 iv.reserve(iv.size()+subiv.size());
453 for (exvector::const_iterator cit2=subiv.begin(); cit2!=subiv.end(); ++cit2) {
460 ex mul::simplify_ncmul(const exvector & v) const
462 throw(std::logic_error("mul::simplify_ncmul() should never have been called!"));
467 /** Implementation of ex::diff() for a product. It applies the product rule.
469 ex mul::derivative(const symbol & s) const
472 new_seq.reserve(seq.size());
474 // D(a*b*c)=D(a)*b*c+a*D(b)*c+a*b*D(c)
475 for (unsigned i=0; i!=seq.size(); i++) {
476 epvector sub_seq = seq;
477 sub_seq[i] = split_ex_to_pair(sub_seq[i].coeff*
478 power(sub_seq[i].rest,sub_seq[i].coeff-1)*
479 sub_seq[i].rest.diff(s));
480 new_seq.push_back((new mul(sub_seq,overall_coeff))->setflag(status_flags::dynallocated));
482 return (new add(new_seq))->setflag(status_flags::dynallocated);
485 int mul::compare_same_type(const basic & other) const
487 return inherited::compare_same_type(other);
490 bool mul::is_equal_same_type(const basic & other) const
492 return inherited::is_equal_same_type(other);
495 unsigned mul::return_type(void) const
498 // mul without factors: should not happen, but commutes
499 return return_types::commutative;
502 bool all_commutative = 1;
504 epvector::const_iterator cit_noncommutative_element; // point to first found nc element
506 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
507 rt=(*cit).rest.return_type();
508 if (rt==return_types::noncommutative_composite) return rt; // one ncc -> mul also ncc
509 if ((rt==return_types::noncommutative)&&(all_commutative)) {
510 // first nc element found, remember position
511 cit_noncommutative_element = cit;
514 if ((rt==return_types::noncommutative)&&(!all_commutative)) {
515 // another nc element found, compare type_infos
516 if ((*cit_noncommutative_element).rest.return_type_tinfo()!=(*cit).rest.return_type_tinfo()) {
517 // diffent types -> mul is ncc
518 return return_types::noncommutative_composite;
522 // all factors checked
523 return all_commutative ? return_types::commutative : return_types::noncommutative;
526 unsigned mul::return_type_tinfo(void) const
529 // mul without factors: should not happen
532 // return type_info of first noncommutative element
533 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
534 if ((*cit).rest.return_type()==return_types::noncommutative) {
535 return (*cit).rest.return_type_tinfo();
538 // no noncommutative element found, should not happen
542 ex mul::thisexpairseq(const epvector & v, const ex & oc) const
544 return (new mul(v,oc))->setflag(status_flags::dynallocated);
547 ex mul::thisexpairseq(epvector * vp, const ex & oc) const
549 return (new mul(vp,oc))->setflag(status_flags::dynallocated);
552 expair mul::split_ex_to_pair(const ex & e) const
554 if (is_ex_exactly_of_type(e,power)) {
555 const power & powerref=ex_to_power(e);
556 if (is_ex_exactly_of_type(powerref.exponent,numeric)) {
557 return expair(powerref.basis,powerref.exponent);
560 return expair(e,_ex1());
563 expair mul::combine_ex_with_coeff_to_pair(const ex & e,
566 // to avoid duplication of power simplification rules,
567 // we create a temporary power object
568 // otherwise it would be hard to correctly simplify
569 // expression like (4^(1/3))^(3/2)
570 if (are_ex_trivially_equal(c,_ex1())) {
571 return split_ex_to_pair(e);
573 return split_ex_to_pair(power(e,c));
576 expair mul::combine_pair_with_coeff_to_pair(const expair & p,
579 // to avoid duplication of power simplification rules,
580 // we create a temporary power object
581 // otherwise it would be hard to correctly simplify
582 // expression like (4^(1/3))^(3/2)
583 if (are_ex_trivially_equal(c,_ex1())) {
586 return split_ex_to_pair(power(recombine_pair_to_ex(p),c));
589 ex mul::recombine_pair_to_ex(const expair & p) const
591 // if (p.coeff.compare(_ex1())==0) {
592 // if (are_ex_trivially_equal(p.coeff,_ex1())) {
593 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
596 return power(p.rest,p.coeff);
600 bool mul::expair_needs_further_processing(epp it)
602 if (is_ex_exactly_of_type((*it).rest,mul) &&
603 ex_to_numeric((*it).coeff).is_integer()) {
604 // combined pair is product with integer power -> expand it
605 *it=split_ex_to_pair(recombine_pair_to_ex(*it));
608 if (is_ex_exactly_of_type((*it).rest,numeric)) {
609 expair ep=split_ex_to_pair(recombine_pair_to_ex(*it));
610 if (!ep.is_equal(*it)) {
611 // combined pair is a numeric power which can be simplified
615 if (ex_to_numeric((*it).coeff).is_equal(_num1())) {
616 // combined pair has coeff 1 and must be moved to the end
623 ex mul::default_overall_coeff(void) const
628 void mul::combine_overall_coeff(const ex & c)
630 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
631 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
632 overall_coeff = ex_to_numeric(overall_coeff).mul_dyn(ex_to_numeric(c));
635 void mul::combine_overall_coeff(const ex & c1, const ex & c2)
637 GINAC_ASSERT(is_ex_exactly_of_type(overall_coeff,numeric));
638 GINAC_ASSERT(is_ex_exactly_of_type(c1,numeric));
639 GINAC_ASSERT(is_ex_exactly_of_type(c2,numeric));
640 overall_coeff = ex_to_numeric(overall_coeff).
641 mul_dyn(ex_to_numeric(c1).power(ex_to_numeric(c2)));
644 bool mul::can_make_flat(const expair & p) const
646 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
647 // this assertion will probably fail somewhere
648 // it would require a more careful make_flat, obeying the power laws
649 // probably should return true only if p.coeff is integer
650 return ex_to_numeric(p.coeff).is_equal(_num1());
653 ex mul::expand(unsigned options) const
655 if (flags & status_flags::expanded)
658 exvector sub_expanded_seq;
659 intvector positions_of_adds;
660 intvector number_of_add_operands;
662 epvector * expanded_seqp = expandchildren(options);
664 const epvector & expanded_seq = expanded_seqp==0 ? seq : *expanded_seqp;
666 positions_of_adds.resize(expanded_seq.size());
667 number_of_add_operands.resize(expanded_seq.size());
669 int number_of_adds = 0;
670 int number_of_expanded_terms = 1;
672 unsigned current_position = 0;
673 epvector::const_iterator last = expanded_seq.end();
674 for (epvector::const_iterator cit=expanded_seq.begin(); cit!=last; ++cit) {
675 if (is_ex_exactly_of_type((*cit).rest,add)&&
676 (ex_to_numeric((*cit).coeff).is_equal(_num1()))) {
677 positions_of_adds[number_of_adds] = current_position;
678 const add & expanded_addref = ex_to_add((*cit).rest);
679 unsigned addref_nops = expanded_addref.nops();
680 number_of_add_operands[number_of_adds] = addref_nops;
681 number_of_expanded_terms *= addref_nops;
687 if (number_of_adds==0) {
688 if (expanded_seqp==0) {
689 return this->setflag(status_flags::expanded);
691 return (new mul(expanded_seqp,overall_coeff))->
692 setflag(status_flags::dynallocated |
693 status_flags::expanded);
697 distrseq.reserve(number_of_expanded_terms);
700 k.resize(number_of_adds);
703 for (l=0; l<number_of_adds; l++) {
710 for (l=0; l<number_of_adds; l++) {
711 const add & addref=ex_to_add(expanded_seq[positions_of_adds[l]].rest);
712 GINAC_ASSERT(term[positions_of_adds[l]].coeff.compare(_ex1())==0);
713 term[positions_of_adds[l]]=split_ex_to_pair(addref.op(k[l]));
715 distrseq.push_back((new mul(term,overall_coeff))->
716 setflag(status_flags::dynallocated |
717 status_flags::expanded));
721 while ((l>=0) && ((++k[l])>=number_of_add_operands[l])) {
728 if (expanded_seqp!=0)
729 delete expanded_seqp;
731 return (new add(distrseq))->setflag(status_flags::dynallocated |
732 status_flags::expanded);
736 // new virtual functions which can be overridden by derived classes
742 // non-virtual functions in this class
745 epvector * mul::expandchildren(unsigned options) const
747 epvector::const_iterator last = seq.end();
748 epvector::const_iterator cit = seq.begin();
750 const ex & factor = recombine_pair_to_ex(*cit);
751 const ex & expanded_factor = factor.expand(options);
752 if (!are_ex_trivially_equal(factor,expanded_factor)) {
754 // something changed, copy seq, eval and return it
755 epvector *s=new epvector;
756 s->reserve(seq.size());
758 // copy parts of seq which are known not to have changed
759 epvector::const_iterator cit2 = seq.begin();
764 // copy first changed element
765 s->push_back(split_ex_to_pair(expanded_factor));
769 s->push_back(split_ex_to_pair(recombine_pair_to_ex(*cit2).expand(options)));
777 return 0; // nothing has changed
781 // static member variables
786 unsigned mul::precedence = 50;
794 const type_info & typeid_mul = typeid(some_mul);
796 #ifndef NO_NAMESPACE_GINAC
798 #endif // ndef NO_NAMESPACE_GINAC