3 * Implementation of GiNaC's sums 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("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.csgn()==-1)
180 (_num_1()*coeff).print(os, precedence);
182 coeff.print(os, precedence);
187 // print the overall numeric coefficient, if present:
188 if (!overall_coeff.is_zero()) {
189 if (overall_coeff.info(info_flags::positive)) os << '+';
192 if (precedence<=upper_precedence) os << ")";
195 void add::printraw(ostream & os) const
197 debugmsg("add printraw",LOGLEVEL_PRINT);
200 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
202 (*it).rest.bp->printraw(os);
204 (*it).coeff.bp->printraw(os);
207 os << ",hash=" << hashvalue << ",flags=" << flags;
211 void add::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
213 debugmsg("add print csrc", LOGLEVEL_PRINT);
214 if (precedence <= upper_precedence)
217 // Print arguments, separated by "+"
218 epvector::const_iterator it = seq.begin();
219 epvector::const_iterator itend = seq.end();
220 while (it != itend) {
222 // If the coefficient is -1, it is replaced by a single minus sign
223 if (it->coeff.compare(_num1()) == 0) {
224 it->rest.bp->printcsrc(os, type, precedence);
225 } else if (it->coeff.compare(_num_1()) == 0) {
227 it->rest.bp->printcsrc(os, type, precedence);
228 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
229 it->rest.bp->printcsrc(os, type, precedence);
231 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
232 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
234 it->rest.bp->printcsrc(os, type, precedence);
236 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
238 it->coeff.bp->printcsrc(os, type, precedence);
240 it->rest.bp->printcsrc(os, type, precedence);
243 // Separator is "+", except if the following expression would have a leading minus sign
245 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)))
249 if (!overall_coeff.is_equal(_ex0())) {
250 if (overall_coeff.info(info_flags::positive)) os << '+';
251 overall_coeff.bp->printcsrc(os,type,precedence);
254 if (precedence <= upper_precedence)
258 bool add::info(unsigned inf) const
261 if (inf==info_flags::polynomial ||
262 inf==info_flags::integer_polynomial ||
263 inf==info_flags::cinteger_polynomial ||
264 inf==info_flags::rational_polynomial ||
265 inf==info_flags::crational_polynomial ||
266 inf==info_flags::rational_function) {
267 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
268 if (!(recombine_pair_to_ex(*it).info(inf)))
271 return overall_coeff.info(inf);
273 return expairseq::info(inf);
277 int add::degree(symbol const & s) const
280 if (!overall_coeff.is_equal(_ex0())) {
284 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
285 cur_deg=(*cit).rest.degree(s);
286 if (cur_deg>deg) deg=cur_deg;
291 int add::ldegree(symbol const & s) const
294 if (!overall_coeff.is_equal(_ex0())) {
298 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
299 cur_deg=(*cit).rest.ldegree(s);
300 if (cur_deg<deg) deg=cur_deg;
305 ex add::coeff(symbol const & s, int const n) const
308 coeffseq.reserve(seq.size());
310 epvector::const_iterator it=seq.begin();
311 while (it!=seq.end()) {
312 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
317 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
319 return (new add(coeffseq))->setflag(status_flags::dynallocated);
322 ex add::eval(int level) const
324 // simplifications: +(;c) -> c
327 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
329 epvector * evaled_seqp=evalchildren(level);
330 if (evaled_seqp!=0) {
331 // do more evaluation later
332 return (new add(evaled_seqp,overall_coeff))->
333 setflag(status_flags::dynallocated);
336 #ifdef DO_GINAC_ASSERT
337 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
338 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
339 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
342 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
344 #endif // def DO_GINAC_ASSERT
346 if (flags & status_flags::evaluated) {
347 GINAC_ASSERT(seq.size()>0);
348 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex0()));
352 int seq_size=seq.size();
355 return overall_coeff;
356 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex0())) {
358 return recombine_pair_to_ex(*(seq.begin()));
363 exvector add::get_indices(void) const
365 // FIXME: all terms in the sum should have the same indices (compatible
366 // tensors) however this is not checked, since there is no function yet
367 // which compares indices (idxvector can be unsorted)
371 return (seq.begin())->rest.get_indices();
374 ex add::simplify_ncmul(exvector const & v) const
377 return expairseq::simplify_ncmul(v);
379 return (*seq.begin()).rest.simplify_ncmul(v);
384 int add::compare_same_type(basic const & other) const
386 return expairseq::compare_same_type(other);
389 bool add::is_equal_same_type(basic const & other) const
391 return expairseq::is_equal_same_type(other);
394 unsigned add::return_type(void) const
397 return return_types::commutative;
399 return (*seq.begin()).rest.return_type();
402 unsigned add::return_type_tinfo(void) const
407 return (*seq.begin()).rest.return_type_tinfo();
410 ex add::thisexpairseq(epvector const & v, ex const & oc) const
412 return (new add(v,oc))->setflag(status_flags::dynallocated);
415 ex add::thisexpairseq(epvector * vp, ex const & oc) const
417 return (new add(vp,oc))->setflag(status_flags::dynallocated);
420 expair add::split_ex_to_pair(ex const & e) const
422 if (is_ex_exactly_of_type(e,mul)) {
423 mul const & mulref=ex_to_mul(e);
424 ex numfactor=mulref.overall_coeff;
425 // mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
426 mul * mulcopyp=new mul(mulref);
427 mulcopyp->overall_coeff=_ex1();
428 mulcopyp->clearflag(status_flags::evaluated);
429 mulcopyp->clearflag(status_flags::hash_calculated);
430 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
432 return expair(e,_ex1());
435 expair add::combine_ex_with_coeff_to_pair(ex const & e,
438 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
439 if (is_ex_exactly_of_type(e,mul)) {
440 mul const & mulref=ex_to_mul(e);
441 ex numfactor=mulref.overall_coeff;
442 //mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
443 mul * mulcopyp=new mul(mulref);
444 mulcopyp->overall_coeff=_ex1();
445 mulcopyp->clearflag(status_flags::evaluated);
446 mulcopyp->clearflag(status_flags::hash_calculated);
447 if (are_ex_trivially_equal(c,_ex1())) {
448 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
449 } else if (are_ex_trivially_equal(numfactor,_ex1())) {
450 return expair(mulcopyp->setflag(status_flags::dynallocated),c);
452 return expair(mulcopyp->setflag(status_flags::dynallocated),
453 ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
454 } else if (is_ex_exactly_of_type(e,numeric)) {
455 if (are_ex_trivially_equal(c,_ex1())) {
456 return expair(e,_ex1());
458 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)),_ex1());
463 expair add::combine_pair_with_coeff_to_pair(expair const & p,
466 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
467 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
469 if (is_ex_exactly_of_type(p.rest,numeric)) {
470 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
471 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
474 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
477 ex add::recombine_pair_to_ex(expair const & p) const
479 //if (p.coeff.compare(_ex1())==0) {
480 //if (are_ex_trivially_equal(p.coeff,_ex1())) {
481 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
484 return p.rest*p.coeff;
488 ex add::expand(unsigned options) const
490 epvector * vp=expandchildren(options);
494 return (new add(vp,overall_coeff))->setflag(status_flags::expanded |
495 status_flags::dynallocated );
499 // new virtual functions which can be overridden by derived classes
505 // non-virtual functions in this class
511 // static member variables
516 unsigned add::precedence=40;
523 type_info const & typeid_add=typeid(some_add);
525 #ifndef NO_GINAC_NAMESPACE
527 #endif // ndef NO_GINAC_NAMESPACE