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(const print_context & c, unsigned level) const
116 debugmsg("add print", LOGLEVEL_PRINT);
118 if (is_of_type(c, print_tree)) {
120 inherited::print(c, level);
122 } else if (is_of_type(c, print_csrc)) {
124 if (precedence <= level)
127 // Print arguments, separated by "+"
128 epvector::const_iterator it = seq.begin(), itend = seq.end();
129 while (it != itend) {
131 // If the coefficient is -1, it is replaced by a single minus sign
132 if (it->coeff.compare(_num1()) == 0) {
133 it->rest.bp->print(c, precedence);
134 } else if (it->coeff.compare(_num_1()) == 0) {
136 it->rest.bp->print(c, precedence);
137 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
138 it->rest.bp->print(c, precedence);
140 ex_to_numeric(it->coeff).denom().print(c, precedence);
141 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
143 it->rest.bp->print(c, precedence);
145 ex_to_numeric(it->coeff).denom().print(c, precedence);
147 it->coeff.bp->print(c, precedence);
149 it->rest.bp->print(c, precedence);
152 // Separator is "+", except if the following expression would have a leading minus sign
154 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)))
158 if (!overall_coeff.is_zero()) {
159 if (overall_coeff.info(info_flags::positive))
161 overall_coeff.bp->print(c, precedence);
164 if (precedence <= level)
169 if (precedence <= level) {
170 if (is_of_type(c, print_latex))
179 // First print the overall numeric coefficient, if present
180 if (!overall_coeff.is_zero()) {
181 if (!is_of_type(c, print_tree))
182 overall_coeff.print(c, 0);
184 overall_coeff.print(c, precedence);
188 // Then proceed with the remaining factors
189 epvector::const_iterator it = seq.begin(), itend = seq.end();
190 while (it != itend) {
191 coeff = ex_to_numeric(it->coeff);
193 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
195 if (coeff.csgn() == -1) c.s << '-';
198 if (!coeff.is_equal(_num1()) &&
199 !coeff.is_equal(_num_1())) {
200 if (coeff.is_rational()) {
201 if (coeff.is_negative())
206 if (coeff.csgn() == -1)
207 (-coeff).print(c, precedence);
209 coeff.print(c, precedence);
211 if (is_of_type(c, print_latex))
216 it->rest.print(c, precedence);
220 if (precedence <= level) {
221 if (is_of_type(c, print_latex))
229 bool add::info(unsigned inf) const
232 case info_flags::polynomial:
233 case info_flags::integer_polynomial:
234 case info_flags::cinteger_polynomial:
235 case info_flags::rational_polynomial:
236 case info_flags::crational_polynomial:
237 case info_flags::rational_function: {
238 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
239 if (!(recombine_pair_to_ex(*i).info(inf)))
242 return overall_coeff.info(inf);
244 case info_flags::algebraic: {
245 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
246 if ((recombine_pair_to_ex(*i).info(inf)))
252 return inherited::info(inf);
255 int add::degree(const ex & s) const
258 if (!overall_coeff.is_equal(_ex0()))
262 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
263 cur_deg = (*cit).rest.degree(s);
270 int add::ldegree(const ex & s) const
273 if (!overall_coeff.is_equal(_ex0()))
277 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
278 cur_deg = (*cit).rest.ldegree(s);
279 if (cur_deg<deg) deg=cur_deg;
284 ex add::coeff(const ex & s, int n) const
287 coeffseq.reserve(seq.size());
289 epvector::const_iterator it=seq.begin();
290 while (it!=seq.end()) {
291 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
296 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
298 return (new add(coeffseq))->setflag(status_flags::dynallocated);
301 ex add::eval(int level) const
303 // simplifications: +(;c) -> c
306 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
308 epvector * evaled_seqp = evalchildren(level);
309 if (evaled_seqp!=0) {
310 // do more evaluation later
311 return (new add(evaled_seqp,overall_coeff))->
312 setflag(status_flags::dynallocated);
315 #ifdef DO_GINAC_ASSERT
316 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
317 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
318 if (is_ex_exactly_of_type((*cit).rest,numeric))
320 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
322 #endif // def DO_GINAC_ASSERT
324 if (flags & status_flags::evaluated) {
325 GINAC_ASSERT(seq.size()>0);
326 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
330 int seq_size = seq.size();
333 return overall_coeff;
334 } else if ((seq_size==1) && overall_coeff.is_equal(_ex0())) {
336 return recombine_pair_to_ex(*(seq.begin()));
341 ex add::simplify_ncmul(const exvector & v) const
344 return inherited::simplify_ncmul(v);
346 return (*seq.begin()).rest.simplify_ncmul(v);
351 /** Implementation of ex::diff() for a sum. It differentiates each term.
353 ex add::derivative(const symbol & s) const
355 // D(a+b+c)=D(a)+D(b)+D(c)
356 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
359 int add::compare_same_type(const basic & other) const
361 return inherited::compare_same_type(other);
364 bool add::is_equal_same_type(const basic & other) const
366 return inherited::is_equal_same_type(other);
369 unsigned add::return_type(void) const
372 return return_types::commutative;
374 return (*seq.begin()).rest.return_type();
377 unsigned add::return_type_tinfo(void) const
382 return (*seq.begin()).rest.return_type_tinfo();
385 ex add::thisexpairseq(const epvector & v, const ex & oc) const
387 return (new add(v,oc))->setflag(status_flags::dynallocated);
390 ex add::thisexpairseq(epvector * vp, const ex & oc) const
392 return (new add(vp,oc))->setflag(status_flags::dynallocated);
395 expair add::split_ex_to_pair(const ex & e) const
397 if (is_ex_exactly_of_type(e,mul)) {
398 const mul &mulref = ex_to_mul(e);
399 ex numfactor = mulref.overall_coeff;
400 mul *mulcopyp = new mul(mulref);
401 mulcopyp->overall_coeff = _ex1();
402 mulcopyp->clearflag(status_flags::evaluated);
403 mulcopyp->clearflag(status_flags::hash_calculated);
404 mulcopyp->setflag(status_flags::dynallocated);
405 return expair(*mulcopyp,numfactor);
407 return expair(e,_ex1());
410 expair add::combine_ex_with_coeff_to_pair(const ex & e,
413 GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
414 if (is_ex_exactly_of_type(e, mul)) {
415 const mul &mulref = ex_to_mul(e);
416 ex numfactor = mulref.overall_coeff;
417 mul *mulcopyp = new mul(mulref);
418 mulcopyp->overall_coeff = _ex1();
419 mulcopyp->clearflag(status_flags::evaluated);
420 mulcopyp->clearflag(status_flags::hash_calculated);
421 mulcopyp->setflag(status_flags::dynallocated);
422 if (are_ex_trivially_equal(c, _ex1()))
423 return expair(*mulcopyp, numfactor);
424 else if (are_ex_trivially_equal(numfactor, _ex1()))
425 return expair(*mulcopyp, c);
427 return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
428 } else if (is_ex_exactly_of_type(e, numeric)) {
429 if (are_ex_trivially_equal(c, _ex1()))
430 return expair(e, _ex1());
431 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
436 expair add::combine_pair_with_coeff_to_pair(const expair & p,
439 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
440 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
442 if (is_ex_exactly_of_type(p.rest,numeric)) {
443 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
444 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
447 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
450 ex add::recombine_pair_to_ex(const expair & p) const
452 if (ex_to_numeric(p.coeff).is_equal(_num1()))
455 return p.rest*p.coeff;
458 ex add::expand(unsigned options) const
460 if (flags & status_flags::expanded)
463 epvector * vp = expandchildren(options);
465 // the terms have not changed, so it is safe to declare this expanded
466 setflag(status_flags::expanded);
470 return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
474 // static member variables
479 unsigned add::precedence = 40;