3 * Implementation of GiNaC's sums 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
31 #ifndef NO_GINAC_NAMESPACE
33 #endif // ndef NO_GINAC_NAMESPACE
36 // default constructor, destructor, copy constructor assignment operator and helpers
43 debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
44 tinfo_key = TINFO_add;
49 debugmsg("add destructor",LOGLEVEL_DESTRUCT);
53 add::add(add const & other)
55 debugmsg("add copy constructor",LOGLEVEL_CONSTRUCT);
59 add const & add::operator=(add const & other)
61 debugmsg("add operator=",LOGLEVEL_ASSIGNMENT);
71 void add::copy(add const & other)
73 expairseq::copy(other);
76 void add::destroy(bool call_parent)
78 if (call_parent) expairseq::destroy(call_parent);
87 add::add(ex const & lh, ex const & rh)
89 debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_add;
92 construct_from_2_ex(lh,rh);
93 GINAC_ASSERT(is_canonical());
96 add::add(exvector const & v)
98 debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
99 tinfo_key = TINFO_add;
100 overall_coeff=_ex0();
101 construct_from_exvector(v);
102 GINAC_ASSERT(is_canonical());
106 add::add(epvector const & v, bool do_not_canonicalize)
108 debugmsg("add constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
109 tinfo_key = TINFO_add;
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 add::add(epvector const & v)
124 debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
125 tinfo_key = TINFO_add;
126 overall_coeff=_ex0();
127 construct_from_epvector(v);
128 GINAC_ASSERT(is_canonical());
131 add::add(epvector const & v, ex const & oc)
133 debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
134 tinfo_key = TINFO_add;
136 construct_from_epvector(v);
137 GINAC_ASSERT(is_canonical());
140 add::add(epvector * vp, ex const & oc)
142 debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
143 tinfo_key = TINFO_add;
146 construct_from_epvector(*vp);
148 GINAC_ASSERT(is_canonical());
152 // functions overriding virtual functions from bases classes
157 basic * add::duplicate() const
159 debugmsg("add duplicate",LOGLEVEL_DUPLICATE);
160 return new add(*this);
163 /*void add::print(ostream & os, unsigned upper_precedence) const
165 debugmsg("add print",LOGLEVEL_PRINT);
166 if (precedence<=upper_precedence) os << "(";
169 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
170 coeff = ex_to_numeric(cit->coeff);
172 if (coeff.csgn()==-1) os << '-'; else os << '+';
174 if (coeff.csgn()==-1) os << '-';
177 if (!coeff.is_equal(_num1()) &&
178 !coeff.is_equal(_num_1())) {
179 if (coeff.is_rational()) {
180 if (coeff.is_negative())
185 if (coeff.csgn()==-1)
186 (-coeff).print(os, precedence);
188 coeff.print(os, precedence);
194 // print the overall numeric coefficient, if present:
195 if (!overall_coeff.is_zero()) {
196 if (overall_coeff.info(info_flags::positive)) os << '+';
199 if (precedence<=upper_precedence) os << ")";
202 void add::print(ostream & os, unsigned upper_precedence) const
204 debugmsg("add print",LOGLEVEL_PRINT);
205 if (precedence<=upper_precedence) os << "(";
208 // first print the overall numeric coefficient, if present:
209 if (!overall_coeff.is_zero()) {
213 // then proceed with the remaining factors:
214 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
215 coeff = ex_to_numeric(cit->coeff);
217 if (coeff.csgn()==-1) os << '-'; else os << '+';
219 if (coeff.csgn()==-1) os << '-';
222 if (!coeff.is_equal(_num1()) &&
223 !coeff.is_equal(_num_1())) {
224 if (coeff.is_rational()) {
225 if (coeff.is_negative())
230 if (coeff.csgn()==-1)
231 (-coeff).print(os, precedence);
233 coeff.print(os, precedence);
239 if (precedence<=upper_precedence) os << ")";
242 void add::printraw(ostream & os) const
244 debugmsg("add printraw",LOGLEVEL_PRINT);
247 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
249 (*it).rest.bp->printraw(os);
251 (*it).coeff.bp->printraw(os);
254 os << ",hash=" << hashvalue << ",flags=" << flags;
258 void add::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
260 debugmsg("add print csrc", LOGLEVEL_PRINT);
261 if (precedence <= upper_precedence)
264 // Print arguments, separated by "+"
265 epvector::const_iterator it = seq.begin();
266 epvector::const_iterator itend = seq.end();
267 while (it != itend) {
269 // If the coefficient is -1, it is replaced by a single minus sign
270 if (it->coeff.compare(_num1()) == 0) {
271 it->rest.bp->printcsrc(os, type, precedence);
272 } else if (it->coeff.compare(_num_1()) == 0) {
274 it->rest.bp->printcsrc(os, type, precedence);
275 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
276 it->rest.bp->printcsrc(os, type, precedence);
278 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
279 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
281 it->rest.bp->printcsrc(os, type, precedence);
283 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
285 it->coeff.bp->printcsrc(os, type, precedence);
287 it->rest.bp->printcsrc(os, type, precedence);
290 // Separator is "+", except if the following expression would have a leading minus sign
292 if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_ex_exactly_of_type(it->rest, numeric) && it->rest.compare(_num0()) < 0)))
296 if (!overall_coeff.is_equal(_ex0())) {
297 if (overall_coeff.info(info_flags::positive)) os << '+';
298 overall_coeff.bp->printcsrc(os,type,precedence);
301 if (precedence <= upper_precedence)
305 bool add::info(unsigned inf) const
308 if (inf==info_flags::polynomial ||
309 inf==info_flags::integer_polynomial ||
310 inf==info_flags::cinteger_polynomial ||
311 inf==info_flags::rational_polynomial ||
312 inf==info_flags::crational_polynomial ||
313 inf==info_flags::rational_function) {
314 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
315 if (!(recombine_pair_to_ex(*it).info(inf)))
318 return overall_coeff.info(inf);
320 return expairseq::info(inf);
324 int add::degree(symbol const & s) const
327 if (!overall_coeff.is_equal(_ex0())) {
331 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
332 cur_deg=(*cit).rest.degree(s);
333 if (cur_deg>deg) deg=cur_deg;
338 int add::ldegree(symbol const & s) const
341 if (!overall_coeff.is_equal(_ex0())) {
345 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
346 cur_deg=(*cit).rest.ldegree(s);
347 if (cur_deg<deg) deg=cur_deg;
352 ex add::coeff(symbol const & s, int const n) const
355 coeffseq.reserve(seq.size());
357 epvector::const_iterator it=seq.begin();
358 while (it!=seq.end()) {
359 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
364 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
366 return (new add(coeffseq))->setflag(status_flags::dynallocated);
369 ex add::eval(int level) const
371 // simplifications: +(;c) -> c
374 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
376 epvector * evaled_seqp=evalchildren(level);
377 if (evaled_seqp!=0) {
378 // do more evaluation later
379 return (new add(evaled_seqp,overall_coeff))->
380 setflag(status_flags::dynallocated);
383 #ifdef DO_GINAC_ASSERT
384 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
385 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
386 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
389 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
391 #endif // def DO_GINAC_ASSERT
393 if (flags & status_flags::evaluated) {
394 GINAC_ASSERT(seq.size()>0);
395 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex0()));
399 int seq_size=seq.size();
402 return overall_coeff;
403 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex0())) {
405 return recombine_pair_to_ex(*(seq.begin()));
410 exvector add::get_indices(void) const
412 // FIXME: all terms in the sum should have the same indices (compatible
413 // tensors) however this is not checked, since there is no function yet
414 // which compares indices (idxvector can be unsorted)
418 return (seq.begin())->rest.get_indices();
421 ex add::simplify_ncmul(exvector const & v) const
424 return expairseq::simplify_ncmul(v);
426 return (*seq.begin()).rest.simplify_ncmul(v);
431 int add::compare_same_type(basic const & other) const
433 return expairseq::compare_same_type(other);
436 bool add::is_equal_same_type(basic const & other) const
438 return expairseq::is_equal_same_type(other);
441 unsigned add::return_type(void) const
444 return return_types::commutative;
446 return (*seq.begin()).rest.return_type();
449 unsigned add::return_type_tinfo(void) const
454 return (*seq.begin()).rest.return_type_tinfo();
457 ex add::thisexpairseq(epvector const & v, ex const & oc) const
459 return (new add(v,oc))->setflag(status_flags::dynallocated);
462 ex add::thisexpairseq(epvector * vp, ex const & oc) const
464 return (new add(vp,oc))->setflag(status_flags::dynallocated);
467 expair add::split_ex_to_pair(ex const & e) const
469 if (is_ex_exactly_of_type(e,mul)) {
470 mul const & mulref=ex_to_mul(e);
471 ex numfactor=mulref.overall_coeff;
472 // mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
473 mul * mulcopyp=new mul(mulref);
474 mulcopyp->overall_coeff=_ex1();
475 mulcopyp->clearflag(status_flags::evaluated);
476 mulcopyp->clearflag(status_flags::hash_calculated);
477 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
479 return expair(e,_ex1());
482 expair add::combine_ex_with_coeff_to_pair(ex const & e,
485 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
486 if (is_ex_exactly_of_type(e,mul)) {
487 mul const & mulref=ex_to_mul(e);
488 ex numfactor=mulref.overall_coeff;
489 //mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
490 mul * mulcopyp=new mul(mulref);
491 mulcopyp->overall_coeff=_ex1();
492 mulcopyp->clearflag(status_flags::evaluated);
493 mulcopyp->clearflag(status_flags::hash_calculated);
494 if (are_ex_trivially_equal(c,_ex1())) {
495 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
496 } else if (are_ex_trivially_equal(numfactor,_ex1())) {
497 return expair(mulcopyp->setflag(status_flags::dynallocated),c);
499 return expair(mulcopyp->setflag(status_flags::dynallocated),
500 ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
501 } else if (is_ex_exactly_of_type(e,numeric)) {
502 if (are_ex_trivially_equal(c,_ex1())) {
503 return expair(e,_ex1());
505 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)),_ex1());
510 expair add::combine_pair_with_coeff_to_pair(expair const & p,
513 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
514 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
516 if (is_ex_exactly_of_type(p.rest,numeric)) {
517 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
518 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
521 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
524 ex add::recombine_pair_to_ex(expair const & p) const
526 //if (p.coeff.compare(_ex1())==0) {
527 //if (are_ex_trivially_equal(p.coeff,_ex1())) {
528 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
531 return p.rest*p.coeff;
535 ex add::expand(unsigned options) const
537 epvector * vp=expandchildren(options);
541 return (new add(vp,overall_coeff))->setflag(status_flags::expanded |
542 status_flags::dynallocated );
546 // new virtual functions which can be overridden by derived classes
552 // non-virtual functions in this class
558 // static member variables
563 unsigned add::precedence=40;
570 type_info const & typeid_add=typeid(some_add);
572 #ifndef NO_GINAC_NAMESPACE
574 #endif // ndef NO_GINAC_NAMESPACE