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
34 GINAC_IMPLEMENT_REGISTERED_CLASS(add, expairseq)
37 // default constructor, destructor, copy constructor assignment operator and helpers
42 debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
43 tinfo_key = TINFO_add;
55 add::add(const ex & lh, const ex & rh)
57 debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
58 tinfo_key = TINFO_add;
59 overall_coeff = _ex0();
60 construct_from_2_ex(lh,rh);
61 GINAC_ASSERT(is_canonical());
64 add::add(const exvector & v)
66 debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
67 tinfo_key = TINFO_add;
68 overall_coeff = _ex0();
69 construct_from_exvector(v);
70 GINAC_ASSERT(is_canonical());
73 add::add(const epvector & v)
75 debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
76 tinfo_key = TINFO_add;
77 overall_coeff = _ex0();
78 construct_from_epvector(v);
79 GINAC_ASSERT(is_canonical());
82 add::add(const epvector & v, const ex & oc)
84 debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
85 tinfo_key = TINFO_add;
87 construct_from_epvector(v);
88 GINAC_ASSERT(is_canonical());
91 add::add(epvector * vp, const ex & oc)
93 debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
94 tinfo_key = TINFO_add;
97 construct_from_epvector(*vp);
99 GINAC_ASSERT(is_canonical());
106 DEFAULT_ARCHIVING(add)
109 // functions overriding virtual functions from bases classes
114 void add::print(std::ostream & os, unsigned upper_precedence) const
116 debugmsg("add print",LOGLEVEL_PRINT);
117 if (precedence<=upper_precedence) os << "(";
120 // first print the overall numeric coefficient, if present:
121 if (!overall_coeff.is_zero()) {
125 // then proceed with the remaining factors:
126 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
127 coeff = ex_to_numeric(cit->coeff);
129 if (coeff.csgn()==-1) os << '-'; else os << '+';
131 if (coeff.csgn()==-1) os << '-';
134 if (!coeff.is_equal(_num1()) &&
135 !coeff.is_equal(_num_1())) {
136 if (coeff.is_rational()) {
137 if (coeff.is_negative())
142 if (coeff.csgn()==-1)
143 (-coeff).print(os, precedence);
145 coeff.print(os, precedence);
149 cit->rest.print(os, precedence);
151 if (precedence<=upper_precedence) os << ")";
154 void add::printraw(std::ostream & os) const
156 debugmsg("add printraw",LOGLEVEL_PRINT);
159 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
161 (*it).rest.bp->printraw(os);
163 (*it).coeff.bp->printraw(os);
166 os << ",hash=" << hashvalue << ",flags=" << flags;
170 void add::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
172 debugmsg("add print csrc", LOGLEVEL_PRINT);
173 if (precedence <= upper_precedence)
176 // Print arguments, separated by "+"
177 epvector::const_iterator it = seq.begin();
178 epvector::const_iterator itend = seq.end();
179 while (it != itend) {
181 // If the coefficient is -1, it is replaced by a single minus sign
182 if (it->coeff.compare(_num1()) == 0) {
183 it->rest.bp->printcsrc(os, type, precedence);
184 } else if (it->coeff.compare(_num_1()) == 0) {
186 it->rest.bp->printcsrc(os, type, precedence);
187 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
188 it->rest.bp->printcsrc(os, type, precedence);
190 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
191 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
193 it->rest.bp->printcsrc(os, type, precedence);
195 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
197 it->coeff.bp->printcsrc(os, type, precedence);
199 it->rest.bp->printcsrc(os, type, precedence);
202 // Separator is "+", except if the following expression would have a leading minus sign
204 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)))
208 if (!overall_coeff.is_zero()) {
209 if (overall_coeff.info(info_flags::positive)) os << '+';
210 overall_coeff.bp->printcsrc(os,type,precedence);
213 if (precedence <= upper_precedence)
217 bool add::info(unsigned inf) const
220 case info_flags::polynomial:
221 case info_flags::integer_polynomial:
222 case info_flags::cinteger_polynomial:
223 case info_flags::rational_polynomial:
224 case info_flags::crational_polynomial:
225 case info_flags::rational_function: {
226 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
227 if (!(recombine_pair_to_ex(*i).info(inf)))
230 return overall_coeff.info(inf);
232 case info_flags::algebraic: {
233 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
234 if ((recombine_pair_to_ex(*i).info(inf)))
240 return inherited::info(inf);
243 int add::degree(const ex & s) const
246 if (!overall_coeff.is_equal(_ex0()))
250 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
251 cur_deg = (*cit).rest.degree(s);
258 int add::ldegree(const ex & s) const
261 if (!overall_coeff.is_equal(_ex0()))
265 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
266 cur_deg = (*cit).rest.ldegree(s);
267 if (cur_deg<deg) deg=cur_deg;
272 ex add::coeff(const ex & s, int n) const
275 coeffseq.reserve(seq.size());
277 epvector::const_iterator it=seq.begin();
278 while (it!=seq.end()) {
279 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
284 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
286 return (new add(coeffseq))->setflag(status_flags::dynallocated);
289 ex add::eval(int level) const
291 // simplifications: +(;c) -> c
294 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
296 epvector * evaled_seqp = evalchildren(level);
297 if (evaled_seqp!=0) {
298 // do more evaluation later
299 return (new add(evaled_seqp,overall_coeff))->
300 setflag(status_flags::dynallocated);
303 #ifdef DO_GINAC_ASSERT
304 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
305 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
306 if (is_ex_exactly_of_type((*cit).rest,numeric))
308 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
310 #endif // def DO_GINAC_ASSERT
312 if (flags & status_flags::evaluated) {
313 GINAC_ASSERT(seq.size()>0);
314 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
318 int seq_size = seq.size();
321 return overall_coeff;
322 } else if ((seq_size==1) && overall_coeff.is_equal(_ex0())) {
324 return recombine_pair_to_ex(*(seq.begin()));
329 ex add::simplify_ncmul(const exvector & v) const
332 return inherited::simplify_ncmul(v);
334 return (*seq.begin()).rest.simplify_ncmul(v);
339 /** Implementation of ex::diff() for a sum. It differentiates each term.
341 ex add::derivative(const symbol & s) const
343 // D(a+b+c)=D(a)+D(b)+D(c)
344 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
347 int add::compare_same_type(const basic & other) const
349 return inherited::compare_same_type(other);
352 bool add::is_equal_same_type(const basic & other) const
354 return inherited::is_equal_same_type(other);
357 unsigned add::return_type(void) const
360 return return_types::commutative;
362 return (*seq.begin()).rest.return_type();
365 unsigned add::return_type_tinfo(void) const
370 return (*seq.begin()).rest.return_type_tinfo();
373 ex add::thisexpairseq(const epvector & v, const ex & oc) const
375 return (new add(v,oc))->setflag(status_flags::dynallocated);
378 ex add::thisexpairseq(epvector * vp, const ex & oc) const
380 return (new add(vp,oc))->setflag(status_flags::dynallocated);
383 expair add::split_ex_to_pair(const ex & e) const
385 if (is_ex_exactly_of_type(e,mul)) {
386 const mul &mulref = ex_to_mul(e);
387 ex numfactor = mulref.overall_coeff;
388 mul *mulcopyp = new mul(mulref);
389 mulcopyp->overall_coeff = _ex1();
390 mulcopyp->clearflag(status_flags::evaluated);
391 mulcopyp->clearflag(status_flags::hash_calculated);
392 mulcopyp->setflag(status_flags::dynallocated);
393 return expair(*mulcopyp,numfactor);
395 return expair(e,_ex1());
398 expair add::combine_ex_with_coeff_to_pair(const ex & e,
401 GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
402 if (is_ex_exactly_of_type(e, mul)) {
403 const mul &mulref = ex_to_mul(e);
404 ex numfactor = mulref.overall_coeff;
405 mul *mulcopyp = new mul(mulref);
406 mulcopyp->overall_coeff = _ex1();
407 mulcopyp->clearflag(status_flags::evaluated);
408 mulcopyp->clearflag(status_flags::hash_calculated);
409 mulcopyp->setflag(status_flags::dynallocated);
410 if (are_ex_trivially_equal(c, _ex1()))
411 return expair(*mulcopyp, numfactor);
412 else if (are_ex_trivially_equal(numfactor, _ex1()))
413 return expair(*mulcopyp, c);
415 return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
416 } else if (is_ex_exactly_of_type(e, numeric)) {
417 if (are_ex_trivially_equal(c, _ex1()))
418 return expair(e, _ex1());
419 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
424 expair add::combine_pair_with_coeff_to_pair(const expair & p,
427 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
428 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
430 if (is_ex_exactly_of_type(p.rest,numeric)) {
431 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
432 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
435 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
438 ex add::recombine_pair_to_ex(const expair & p) const
440 if (ex_to_numeric(p.coeff).is_equal(_num1()))
443 return p.rest*p.coeff;
446 ex add::expand(unsigned options) const
448 if (flags & status_flags::expanded)
451 epvector * vp = expandchildren(options);
453 // the terms have not changed, so it is safe to declare this expanded
454 setflag(status_flags::expanded);
458 return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
462 // static member variables
467 unsigned add::precedence = 40;