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
32 #ifndef NO_GINAC_NAMESPACE
34 #endif // ndef NO_GINAC_NAMESPACE
36 GINAC_IMPLEMENT_REGISTERED_CLASS(add, expairseq)
39 // default constructor, destructor, copy constructor assignment operator and helpers
46 debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
47 tinfo_key = TINFO_add;
52 debugmsg("add destructor",LOGLEVEL_DESTRUCT);
56 add::add(add const & other)
58 debugmsg("add copy constructor",LOGLEVEL_CONSTRUCT);
62 add const & add::operator=(add const & other)
64 debugmsg("add operator=",LOGLEVEL_ASSIGNMENT);
74 void add::copy(add const & other)
76 inherited::copy(other);
79 void add::destroy(bool call_parent)
81 if (call_parent) inherited::destroy(call_parent);
90 add::add(ex const & lh, ex const & rh)
92 debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
93 tinfo_key = TINFO_add;
95 construct_from_2_ex(lh,rh);
96 GINAC_ASSERT(is_canonical());
99 add::add(exvector const & v)
101 debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
102 tinfo_key = TINFO_add;
103 overall_coeff=_ex0();
104 construct_from_exvector(v);
105 GINAC_ASSERT(is_canonical());
109 add::add(epvector const & v, bool do_not_canonicalize)
111 debugmsg("add constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
112 tinfo_key = TINFO_add;
113 if (do_not_canonicalize) {
115 #ifdef EXPAIRSEQ_USE_HASHTAB
116 combine_same_terms(); // to build hashtab
117 #endif // def EXPAIRSEQ_USE_HASHTAB
119 construct_from_epvector(v);
121 GINAC_ASSERT(is_canonical());
125 add::add(epvector const & v)
127 debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
128 tinfo_key = TINFO_add;
129 overall_coeff=_ex0();
130 construct_from_epvector(v);
131 GINAC_ASSERT(is_canonical());
134 add::add(epvector const & v, ex const & oc)
136 debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
137 tinfo_key = TINFO_add;
139 construct_from_epvector(v);
140 GINAC_ASSERT(is_canonical());
143 add::add(epvector * vp, ex const & oc)
145 debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
146 tinfo_key = TINFO_add;
149 construct_from_epvector(*vp);
151 GINAC_ASSERT(is_canonical());
158 /** Construct object from archive_node. */
159 add::add(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
161 debugmsg("add constructor from archive_node", LOGLEVEL_CONSTRUCT);
164 /** Unarchive the object. */
165 ex add::unarchive(const archive_node &n, const lst &sym_lst)
167 return (new add(n, sym_lst))->setflag(status_flags::dynallocated);
170 /** Archive the object. */
171 void add::archive(archive_node &n) const
173 inherited::archive(n);
177 // functions overriding virtual functions from bases classes
182 basic * add::duplicate() const
184 debugmsg("add duplicate",LOGLEVEL_DUPLICATE);
185 return new add(*this);
188 /*void add::print(ostream & os, unsigned upper_precedence) const
190 debugmsg("add print",LOGLEVEL_PRINT);
191 if (precedence<=upper_precedence) os << "(";
194 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
195 coeff = ex_to_numeric(cit->coeff);
197 if (coeff.csgn()==-1) os << '-'; else os << '+';
199 if (coeff.csgn()==-1) os << '-';
202 if (!coeff.is_equal(_num1()) &&
203 !coeff.is_equal(_num_1())) {
204 if (coeff.is_rational()) {
205 if (coeff.is_negative())
210 if (coeff.csgn()==-1)
211 (-coeff).print(os, precedence);
213 coeff.print(os, precedence);
219 // print the overall numeric coefficient, if present:
220 if (!overall_coeff.is_zero()) {
221 if (overall_coeff.info(info_flags::positive)) os << '+';
224 if (precedence<=upper_precedence) os << ")";
227 void add::print(ostream & os, unsigned upper_precedence) const
229 debugmsg("add print",LOGLEVEL_PRINT);
230 if (precedence<=upper_precedence) os << "(";
233 // first print the overall numeric coefficient, if present:
234 if (!overall_coeff.is_zero()) {
238 // then proceed with the remaining factors:
239 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
240 coeff = ex_to_numeric(cit->coeff);
242 if (coeff.csgn()==-1) os << '-'; else os << '+';
244 if (coeff.csgn()==-1) os << '-';
247 if (!coeff.is_equal(_num1()) &&
248 !coeff.is_equal(_num_1())) {
249 if (coeff.is_rational()) {
250 if (coeff.is_negative())
255 if (coeff.csgn()==-1)
256 (-coeff).print(os, precedence);
258 coeff.print(os, precedence);
264 if (precedence<=upper_precedence) os << ")";
267 void add::printraw(ostream & os) const
269 debugmsg("add printraw",LOGLEVEL_PRINT);
272 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
274 (*it).rest.bp->printraw(os);
276 (*it).coeff.bp->printraw(os);
279 os << ",hash=" << hashvalue << ",flags=" << flags;
283 void add::printcsrc(ostream & os, unsigned type, unsigned upper_precedence) const
285 debugmsg("add print csrc", LOGLEVEL_PRINT);
286 if (precedence <= upper_precedence)
289 // Print arguments, separated by "+"
290 epvector::const_iterator it = seq.begin();
291 epvector::const_iterator itend = seq.end();
292 while (it != itend) {
294 // If the coefficient is -1, it is replaced by a single minus sign
295 if (it->coeff.compare(_num1()) == 0) {
296 it->rest.bp->printcsrc(os, type, precedence);
297 } else if (it->coeff.compare(_num_1()) == 0) {
299 it->rest.bp->printcsrc(os, type, precedence);
300 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
301 it->rest.bp->printcsrc(os, type, precedence);
303 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
304 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
306 it->rest.bp->printcsrc(os, type, precedence);
308 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
310 it->coeff.bp->printcsrc(os, type, precedence);
312 it->rest.bp->printcsrc(os, type, precedence);
315 // Separator is "+", except if the following expression would have a leading minus sign
317 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)))
321 if (!overall_coeff.is_equal(_ex0())) {
322 if (overall_coeff.info(info_flags::positive)) os << '+';
323 overall_coeff.bp->printcsrc(os,type,precedence);
326 if (precedence <= upper_precedence)
330 bool add::info(unsigned inf) const
333 if (inf==info_flags::polynomial ||
334 inf==info_flags::integer_polynomial ||
335 inf==info_flags::cinteger_polynomial ||
336 inf==info_flags::rational_polynomial ||
337 inf==info_flags::crational_polynomial ||
338 inf==info_flags::rational_function) {
339 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
340 if (!(recombine_pair_to_ex(*it).info(inf)))
343 return overall_coeff.info(inf);
345 return inherited::info(inf);
349 int add::degree(symbol const & s) const
352 if (!overall_coeff.is_equal(_ex0())) {
356 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
357 cur_deg=(*cit).rest.degree(s);
358 if (cur_deg>deg) deg=cur_deg;
363 int add::ldegree(symbol const & s) const
366 if (!overall_coeff.is_equal(_ex0())) {
370 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
371 cur_deg=(*cit).rest.ldegree(s);
372 if (cur_deg<deg) deg=cur_deg;
377 ex add::coeff(symbol const & s, int const n) const
380 coeffseq.reserve(seq.size());
382 epvector::const_iterator it=seq.begin();
383 while (it!=seq.end()) {
384 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
389 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
391 return (new add(coeffseq))->setflag(status_flags::dynallocated);
394 ex add::eval(int level) const
396 // simplifications: +(;c) -> c
399 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
401 epvector * evaled_seqp=evalchildren(level);
402 if (evaled_seqp!=0) {
403 // do more evaluation later
404 return (new add(evaled_seqp,overall_coeff))->
405 setflag(status_flags::dynallocated);
408 #ifdef DO_GINAC_ASSERT
409 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
410 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
411 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
414 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
416 #endif // def DO_GINAC_ASSERT
418 if (flags & status_flags::evaluated) {
419 GINAC_ASSERT(seq.size()>0);
420 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex0()));
424 int seq_size=seq.size();
427 return overall_coeff;
428 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex0())) {
430 return recombine_pair_to_ex(*(seq.begin()));
435 exvector add::get_indices(void) const
437 // FIXME: all terms in the sum should have the same indices (compatible
438 // tensors) however this is not checked, since there is no function yet
439 // which compares indices (idxvector can be unsorted)
443 return (seq.begin())->rest.get_indices();
446 ex add::simplify_ncmul(exvector const & v) const
449 return inherited::simplify_ncmul(v);
451 return (*seq.begin()).rest.simplify_ncmul(v);
456 int add::compare_same_type(basic const & other) const
458 return inherited::compare_same_type(other);
461 bool add::is_equal_same_type(basic const & other) const
463 return inherited::is_equal_same_type(other);
466 unsigned add::return_type(void) const
469 return return_types::commutative;
471 return (*seq.begin()).rest.return_type();
474 unsigned add::return_type_tinfo(void) const
479 return (*seq.begin()).rest.return_type_tinfo();
482 ex add::thisexpairseq(epvector const & v, ex const & oc) const
484 return (new add(v,oc))->setflag(status_flags::dynallocated);
487 ex add::thisexpairseq(epvector * vp, ex const & oc) const
489 return (new add(vp,oc))->setflag(status_flags::dynallocated);
492 expair add::split_ex_to_pair(ex const & e) const
494 if (is_ex_exactly_of_type(e,mul)) {
495 mul const & mulref=ex_to_mul(e);
496 ex numfactor=mulref.overall_coeff;
497 // mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
498 mul * mulcopyp=new mul(mulref);
499 mulcopyp->overall_coeff=_ex1();
500 mulcopyp->clearflag(status_flags::evaluated);
501 mulcopyp->clearflag(status_flags::hash_calculated);
502 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
504 return expair(e,_ex1());
507 expair add::combine_ex_with_coeff_to_pair(ex const & e,
510 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
511 if (is_ex_exactly_of_type(e,mul)) {
512 mul const & mulref=ex_to_mul(e);
513 ex numfactor=mulref.overall_coeff;
514 //mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
515 mul * mulcopyp=new mul(mulref);
516 mulcopyp->overall_coeff=_ex1();
517 mulcopyp->clearflag(status_flags::evaluated);
518 mulcopyp->clearflag(status_flags::hash_calculated);
519 if (are_ex_trivially_equal(c,_ex1())) {
520 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
521 } else if (are_ex_trivially_equal(numfactor,_ex1())) {
522 return expair(mulcopyp->setflag(status_flags::dynallocated),c);
524 return expair(mulcopyp->setflag(status_flags::dynallocated),
525 ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
526 } else if (is_ex_exactly_of_type(e,numeric)) {
527 if (are_ex_trivially_equal(c,_ex1())) {
528 return expair(e,_ex1());
530 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)),_ex1());
535 expair add::combine_pair_with_coeff_to_pair(expair const & p,
538 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
539 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
541 if (is_ex_exactly_of_type(p.rest,numeric)) {
542 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
543 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
546 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
549 ex add::recombine_pair_to_ex(expair const & p) const
551 //if (p.coeff.compare(_ex1())==0) {
552 //if (are_ex_trivially_equal(p.coeff,_ex1())) {
553 if (ex_to_numeric(p.coeff).is_equal(_num1())) {
556 return p.rest*p.coeff;
560 ex add::expand(unsigned options) const
562 epvector * vp=expandchildren(options);
566 return (new add(vp,overall_coeff))->setflag(status_flags::expanded |
567 status_flags::dynallocated );
571 // new virtual functions which can be overridden by derived classes
577 // non-virtual functions in this class
583 // static member variables
588 unsigned add::precedence=40;
595 type_info const & typeid_add=typeid(some_add);
597 #ifndef NO_GINAC_NAMESPACE
599 #endif // ndef NO_GINAC_NAMESPACE