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
35 GINAC_IMPLEMENT_REGISTERED_CLASS(add, expairseq)
38 // default constructor, destructor, copy constructor assignment operator and helpers
43 debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
44 tinfo_key = TINFO_add;
56 add::add(const ex & lh, const ex & rh)
58 debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
59 tinfo_key = TINFO_add;
60 overall_coeff = _ex0();
61 construct_from_2_ex(lh,rh);
62 GINAC_ASSERT(is_canonical());
65 add::add(const exvector & v)
67 debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
68 tinfo_key = TINFO_add;
69 overall_coeff = _ex0();
70 construct_from_exvector(v);
71 GINAC_ASSERT(is_canonical());
74 add::add(const epvector & v)
76 debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
77 tinfo_key = TINFO_add;
78 overall_coeff = _ex0();
79 construct_from_epvector(v);
80 GINAC_ASSERT(is_canonical());
83 add::add(const epvector & v, const ex & oc)
85 debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
86 tinfo_key = TINFO_add;
88 construct_from_epvector(v);
89 GINAC_ASSERT(is_canonical());
92 add::add(epvector * vp, const ex & oc)
94 debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
95 tinfo_key = TINFO_add;
98 construct_from_epvector(*vp);
100 GINAC_ASSERT(is_canonical());
107 DEFAULT_ARCHIVING(add)
110 // functions overriding virtual functions from bases classes
115 void add::print(const print_context & c, unsigned level) const
117 debugmsg("add print", LOGLEVEL_PRINT);
119 if (is_of_type(c, print_tree)) {
121 inherited::print(c, level);
123 } else if (is_of_type(c, print_csrc)) {
125 if (precedence() <= level)
128 // Print arguments, separated by "+"
129 epvector::const_iterator it = seq.begin(), itend = seq.end();
130 while (it != itend) {
132 // If the coefficient is -1, it is replaced by a single minus sign
133 if (it->coeff.compare(_num1()) == 0) {
134 it->rest.bp->print(c, precedence());
135 } else if (it->coeff.compare(_num_1()) == 0) {
137 it->rest.bp->print(c, precedence());
138 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
139 it->rest.bp->print(c, precedence());
141 ex_to_numeric(it->coeff).denom().print(c, precedence());
142 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
144 it->rest.bp->print(c, precedence());
146 ex_to_numeric(it->coeff).denom().print(c, precedence());
148 it->coeff.bp->print(c, precedence());
150 it->rest.bp->print(c, precedence());
153 // Separator is "+", except if the following expression would have a leading minus sign
155 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)))
159 if (!overall_coeff.is_zero()) {
160 if (overall_coeff.info(info_flags::positive))
162 overall_coeff.bp->print(c, precedence());
165 if (precedence() <= level)
170 if (precedence() <= level) {
171 if (is_of_type(c, print_latex))
180 // First print the overall numeric coefficient, if present
181 if (!overall_coeff.is_zero()) {
182 if (!is_of_type(c, print_tree))
183 overall_coeff.print(c, 0);
185 overall_coeff.print(c, precedence());
189 // Then proceed with the remaining factors
190 epvector::const_iterator it = seq.begin(), itend = seq.end();
191 while (it != itend) {
192 coeff = ex_to_numeric(it->coeff);
194 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
196 if (coeff.csgn() == -1) c.s << '-';
199 if (!coeff.is_equal(_num1()) &&
200 !coeff.is_equal(_num_1())) {
201 if (coeff.is_rational()) {
202 if (coeff.is_negative())
207 if (coeff.csgn() == -1)
208 (-coeff).print(c, precedence());
210 coeff.print(c, precedence());
212 if (is_of_type(c, print_latex))
217 it->rest.print(c, precedence());
221 if (precedence() <= level) {
222 if (is_of_type(c, print_latex))
230 bool add::info(unsigned inf) const
233 case info_flags::polynomial:
234 case info_flags::integer_polynomial:
235 case info_flags::cinteger_polynomial:
236 case info_flags::rational_polynomial:
237 case info_flags::crational_polynomial:
238 case info_flags::rational_function: {
239 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
240 if (!(recombine_pair_to_ex(*i).info(inf)))
243 return overall_coeff.info(inf);
245 case info_flags::algebraic: {
246 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
247 if ((recombine_pair_to_ex(*i).info(inf)))
253 return inherited::info(inf);
256 int add::degree(const ex & s) const
259 if (!overall_coeff.is_equal(_ex0()))
263 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
264 cur_deg = (*cit).rest.degree(s);
271 int add::ldegree(const ex & s) const
274 if (!overall_coeff.is_equal(_ex0()))
278 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
279 cur_deg = (*cit).rest.ldegree(s);
280 if (cur_deg<deg) deg=cur_deg;
285 ex add::coeff(const ex & s, int n) const
289 epvector::const_iterator it=seq.begin();
290 while (it!=seq.end()) {
291 ex restcoeff = it->rest.coeff(s,n);
292 if (!restcoeff.is_zero())
293 coeffseq.push_back(combine_ex_with_coeff_to_pair(restcoeff,it->coeff));
297 return (new add(coeffseq, n==0 ? overall_coeff : default_overall_coeff()))->setflag(status_flags::dynallocated);
300 ex add::eval(int level) const
302 // simplifications: +(;c) -> c
305 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
307 epvector * evaled_seqp = evalchildren(level);
308 if (evaled_seqp!=0) {
309 // do more evaluation later
310 return (new add(evaled_seqp,overall_coeff))->
311 setflag(status_flags::dynallocated);
314 #ifdef DO_GINAC_ASSERT
315 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
316 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
317 if (is_ex_exactly_of_type((*cit).rest,numeric))
319 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
321 #endif // def DO_GINAC_ASSERT
323 if (flags & status_flags::evaluated) {
324 GINAC_ASSERT(seq.size()>0);
325 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
329 int seq_size = seq.size();
332 return overall_coeff;
333 } else if ((seq_size==1) && overall_coeff.is_equal(_ex0())) {
335 return recombine_pair_to_ex(*(seq.begin()));
340 ex add::evalm(void) const
342 // Evaluate children first and add up all matrices. Stop if there's one
343 // term that is not a matrix.
344 epvector *s = new epvector;
345 s->reserve(seq.size());
347 bool all_matrices = true;
348 bool first_term = true;
351 epvector::const_iterator it = seq.begin(), itend = seq.end();
352 while (it != itend) {
353 const ex &m = recombine_pair_to_ex(*it).evalm();
354 s->push_back(split_ex_to_pair(m));
355 if (is_ex_of_type(m, matrix)) {
357 sum = ex_to_matrix(m);
360 sum = sum.add(ex_to_matrix(m));
362 all_matrices = false;
367 return sum + overall_coeff;
369 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
372 ex add::simplify_ncmul(const exvector & v) const
375 return inherited::simplify_ncmul(v);
377 return (*seq.begin()).rest.simplify_ncmul(v);
382 /** Implementation of ex::diff() for a sum. It differentiates each term.
384 ex add::derivative(const symbol & s) const
386 // D(a+b+c)=D(a)+D(b)+D(c)
387 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
390 int add::compare_same_type(const basic & other) const
392 return inherited::compare_same_type(other);
395 bool add::is_equal_same_type(const basic & other) const
397 return inherited::is_equal_same_type(other);
400 unsigned add::return_type(void) const
403 return return_types::commutative;
405 return (*seq.begin()).rest.return_type();
408 unsigned add::return_type_tinfo(void) const
413 return (*seq.begin()).rest.return_type_tinfo();
416 ex add::thisexpairseq(const epvector & v, const ex & oc) const
418 return (new add(v,oc))->setflag(status_flags::dynallocated);
421 ex add::thisexpairseq(epvector * vp, const ex & oc) const
423 return (new add(vp,oc))->setflag(status_flags::dynallocated);
426 expair add::split_ex_to_pair(const ex & e) const
428 if (is_ex_exactly_of_type(e,mul)) {
429 const mul &mulref = ex_to_mul(e);
430 ex numfactor = mulref.overall_coeff;
431 mul *mulcopyp = new mul(mulref);
432 mulcopyp->overall_coeff = _ex1();
433 mulcopyp->clearflag(status_flags::evaluated);
434 mulcopyp->clearflag(status_flags::hash_calculated);
435 mulcopyp->setflag(status_flags::dynallocated);
436 return expair(*mulcopyp,numfactor);
438 return expair(e,_ex1());
441 expair add::combine_ex_with_coeff_to_pair(const ex & e,
444 GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
445 if (is_ex_exactly_of_type(e, mul)) {
446 const mul &mulref = ex_to_mul(e);
447 ex numfactor = mulref.overall_coeff;
448 mul *mulcopyp = new mul(mulref);
449 mulcopyp->overall_coeff = _ex1();
450 mulcopyp->clearflag(status_flags::evaluated);
451 mulcopyp->clearflag(status_flags::hash_calculated);
452 mulcopyp->setflag(status_flags::dynallocated);
453 if (are_ex_trivially_equal(c, _ex1()))
454 return expair(*mulcopyp, numfactor);
455 else if (are_ex_trivially_equal(numfactor, _ex1()))
456 return expair(*mulcopyp, c);
458 return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
459 } else if (is_ex_exactly_of_type(e, numeric)) {
460 if (are_ex_trivially_equal(c, _ex1()))
461 return expair(e, _ex1());
462 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
467 expair add::combine_pair_with_coeff_to_pair(const expair & p,
470 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
471 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
473 if (is_ex_exactly_of_type(p.rest,numeric)) {
474 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
475 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
478 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
481 ex add::recombine_pair_to_ex(const expair & p) const
483 if (ex_to_numeric(p.coeff).is_equal(_num1()))
486 return p.rest*p.coeff;
489 ex add::expand(unsigned options) const
491 if (flags & status_flags::expanded)
494 epvector * vp = expandchildren(options);
496 // the terms have not changed, so it is safe to declare this expanded
497 setflag(status_flags::expanded);
501 return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);