3 * Implementation of GiNaC's sums of expressions. */
6 * GiNaC Copyright (C) 1999-2001 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_NAMESPACE_GINAC
34 #endif // ndef NO_NAMESPACE_GINAC
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 void add::copy(const add & other)
54 inherited::copy(other);
57 void add::destroy(bool call_parent)
59 if (call_parent) inherited::destroy(call_parent);
68 add::add(const ex & lh, const ex & rh)
70 debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
71 tinfo_key = TINFO_add;
72 overall_coeff = _ex0();
73 construct_from_2_ex(lh,rh);
74 GINAC_ASSERT(is_canonical());
77 add::add(const exvector & v)
79 debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
80 tinfo_key = TINFO_add;
81 overall_coeff = _ex0();
82 construct_from_exvector(v);
83 GINAC_ASSERT(is_canonical());
87 add::add(const epvector & v, bool do_not_canonicalize)
89 debugmsg("add constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
90 tinfo_key = TINFO_add;
91 if (do_not_canonicalize) {
93 #ifdef EXPAIRSEQ_USE_HASHTAB
94 combine_same_terms(); // to build hashtab
95 #endif // def EXPAIRSEQ_USE_HASHTAB
97 construct_from_epvector(v);
99 GINAC_ASSERT(is_canonical());
103 add::add(const epvector & v)
105 debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
106 tinfo_key = TINFO_add;
107 overall_coeff = _ex0();
108 construct_from_epvector(v);
109 GINAC_ASSERT(is_canonical());
112 add::add(const epvector & v, const ex & oc)
114 debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
115 tinfo_key = TINFO_add;
117 construct_from_epvector(v);
118 GINAC_ASSERT(is_canonical());
121 add::add(epvector * vp, const ex & oc)
123 debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
124 tinfo_key = TINFO_add;
127 construct_from_epvector(*vp);
129 GINAC_ASSERT(is_canonical());
136 /** Construct object from archive_node. */
137 add::add(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
139 debugmsg("add constructor from archive_node", LOGLEVEL_CONSTRUCT);
142 /** Unarchive the object. */
143 ex add::unarchive(const archive_node &n, const lst &sym_lst)
145 return (new add(n, sym_lst))->setflag(status_flags::dynallocated);
148 /** Archive the object. */
149 void add::archive(archive_node &n) const
151 inherited::archive(n);
155 // functions overriding virtual functions from bases classes
160 basic * add::duplicate() const
162 debugmsg("add duplicate",LOGLEVEL_DUPLICATE);
163 return new add(*this);
166 void add::print(std::ostream & os, unsigned upper_precedence) const
168 debugmsg("add print",LOGLEVEL_PRINT);
169 if (precedence<=upper_precedence) os << "(";
172 // first print the overall numeric coefficient, if present:
173 if (!overall_coeff.is_zero()) {
177 // then proceed with the remaining factors:
178 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
179 coeff = ex_to_numeric(cit->coeff);
181 if (coeff.csgn()==-1) os << '-'; else os << '+';
183 if (coeff.csgn()==-1) os << '-';
186 if (!coeff.is_equal(_num1()) &&
187 !coeff.is_equal(_num_1())) {
188 if (coeff.is_rational()) {
189 if (coeff.is_negative())
194 if (coeff.csgn()==-1)
195 (-coeff).print(os, precedence);
197 coeff.print(os, precedence);
201 cit->rest.print(os, precedence);
203 if (precedence<=upper_precedence) os << ")";
206 void add::printraw(std::ostream & os) const
208 debugmsg("add printraw",LOGLEVEL_PRINT);
211 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
213 (*it).rest.bp->printraw(os);
215 (*it).coeff.bp->printraw(os);
218 os << ",hash=" << hashvalue << ",flags=" << flags;
222 void add::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
224 debugmsg("add print csrc", LOGLEVEL_PRINT);
225 if (precedence <= upper_precedence)
228 // Print arguments, separated by "+"
229 epvector::const_iterator it = seq.begin();
230 epvector::const_iterator itend = seq.end();
231 while (it != itend) {
233 // If the coefficient is -1, it is replaced by a single minus sign
234 if (it->coeff.compare(_num1()) == 0) {
235 it->rest.bp->printcsrc(os, type, precedence);
236 } else if (it->coeff.compare(_num_1()) == 0) {
238 it->rest.bp->printcsrc(os, type, precedence);
239 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
240 it->rest.bp->printcsrc(os, type, precedence);
242 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
243 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
245 it->rest.bp->printcsrc(os, type, precedence);
247 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
249 it->coeff.bp->printcsrc(os, type, precedence);
251 it->rest.bp->printcsrc(os, type, precedence);
254 // Separator is "+", except if the following expression would have a leading minus sign
256 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)))
260 if (!overall_coeff.is_equal(_ex0())) {
261 if (overall_coeff.info(info_flags::positive)) os << '+';
262 overall_coeff.bp->printcsrc(os,type,precedence);
265 if (precedence <= upper_precedence)
269 bool add::info(unsigned inf) const
272 case info_flags::polynomial:
273 case info_flags::integer_polynomial:
274 case info_flags::cinteger_polynomial:
275 case info_flags::rational_polynomial:
276 case info_flags::crational_polynomial:
277 case info_flags::rational_function: {
278 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
279 if (!(recombine_pair_to_ex(*i).info(inf)))
282 return overall_coeff.info(inf);
284 case info_flags::algebraic: {
285 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
286 if ((recombine_pair_to_ex(*i).info(inf)))
292 return inherited::info(inf);
295 int add::degree(const symbol & s) const
298 if (!overall_coeff.is_equal(_ex0())) {
302 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
303 cur_deg=(*cit).rest.degree(s);
304 if (cur_deg>deg) deg=cur_deg;
309 int add::ldegree(const symbol & s) const
312 if (!overall_coeff.is_equal(_ex0())) {
316 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
317 cur_deg = (*cit).rest.ldegree(s);
318 if (cur_deg<deg) deg=cur_deg;
323 ex add::coeff(const symbol & s, int n) const
326 coeffseq.reserve(seq.size());
328 epvector::const_iterator it=seq.begin();
329 while (it!=seq.end()) {
330 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
335 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
337 return (new add(coeffseq))->setflag(status_flags::dynallocated);
340 ex add::eval(int level) const
342 // simplifications: +(;c) -> c
345 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
347 epvector * evaled_seqp=evalchildren(level);
348 if (evaled_seqp!=0) {
349 // do more evaluation later
350 return (new add(evaled_seqp,overall_coeff))->
351 setflag(status_flags::dynallocated);
354 #ifdef DO_GINAC_ASSERT
355 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
356 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
357 if (is_ex_exactly_of_type((*cit).rest,numeric)) {
360 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
362 #endif // def DO_GINAC_ASSERT
364 if (flags & status_flags::evaluated) {
365 GINAC_ASSERT(seq.size()>0);
366 GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(_ex0()));
370 int seq_size=seq.size();
373 return overall_coeff;
374 } else if ((seq_size==1)&&overall_coeff.is_equal(_ex0())) {
376 return recombine_pair_to_ex(*(seq.begin()));
381 exvector add::get_indices(void) const
383 // FIXME: all terms in the sum should have the same indices (compatible
384 // tensors) however this is not checked, since there is no function yet
385 // which compares indices (idxvector can be unsorted)
389 return (seq.begin())->rest.get_indices();
392 ex add::simplify_ncmul(const exvector & v) const
395 return inherited::simplify_ncmul(v);
397 return (*seq.begin()).rest.simplify_ncmul(v);
402 /** Implementation of ex::diff() for a sum. It differentiates each term.
404 ex add::derivative(const symbol & s) const
406 // D(a+b+c)=D(a)+D(b)+D(c)
407 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
410 int add::compare_same_type(const basic & other) const
412 return inherited::compare_same_type(other);
415 bool add::is_equal_same_type(const basic & other) const
417 return inherited::is_equal_same_type(other);
420 unsigned add::return_type(void) const
423 return return_types::commutative;
425 return (*seq.begin()).rest.return_type();
428 unsigned add::return_type_tinfo(void) const
433 return (*seq.begin()).rest.return_type_tinfo();
436 ex add::thisexpairseq(const epvector & v, const ex & oc) const
438 return (new add(v,oc))->setflag(status_flags::dynallocated);
441 ex add::thisexpairseq(epvector * vp, const ex & oc) const
443 return (new add(vp,oc))->setflag(status_flags::dynallocated);
446 expair add::split_ex_to_pair(const ex & e) const
448 if (is_ex_exactly_of_type(e,mul)) {
449 const mul & mulref=ex_to_mul(e);
450 ex numfactor=mulref.overall_coeff;
451 // mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
452 mul * mulcopyp=new mul(mulref);
453 mulcopyp->overall_coeff=_ex1();
454 mulcopyp->clearflag(status_flags::evaluated);
455 mulcopyp->clearflag(status_flags::hash_calculated);
456 return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
458 return expair(e,_ex1());
461 expair add::combine_ex_with_coeff_to_pair(const ex & e,
464 GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
466 if (is_ex_exactly_of_type(e, mul)) {
467 const mul &mulref = ex_to_mul(e);
468 ex numfactor = mulref.overall_coeff;
469 mul *mulcopyp = new mul(mulref);
470 mulcopyp->overall_coeff = one;
471 mulcopyp->clearflag(status_flags::evaluated);
472 mulcopyp->clearflag(status_flags::hash_calculated);
473 mulcopyp->setflag(status_flags::dynallocated);
474 if (are_ex_trivially_equal(c, one)) {
475 return expair(*mulcopyp, numfactor);
476 } else if (are_ex_trivially_equal(numfactor, one)) {
477 return expair(*mulcopyp, c);
479 return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
480 } else if (is_ex_exactly_of_type(e, numeric)) {
481 if (are_ex_trivially_equal(c, one)) {
482 return expair(e, one);
484 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), one);
489 expair add::combine_pair_with_coeff_to_pair(const expair & p,
492 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
493 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
495 if (is_ex_exactly_of_type(p.rest,numeric)) {
496 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
497 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
500 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
503 ex add::recombine_pair_to_ex(const expair & p) const
505 if (ex_to_numeric(p.coeff).is_equal(_num1()))
508 return p.rest*p.coeff;
511 ex add::expand(unsigned options) const
513 if (flags & status_flags::expanded)
516 epvector * vp = expandchildren(options);
520 return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
524 // static member variables
529 unsigned add::precedence = 40;
531 #ifndef NO_NAMESPACE_GINAC
533 #endif // ndef NO_NAMESPACE_GINAC