3 * Implementation of GiNaC's sums of expressions. */
6 * GiNaC Copyright (C) 1999-2003 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
29 #include "operators.h"
35 GINAC_IMPLEMENT_REGISTERED_CLASS(add, expairseq)
38 // default ctor, dtor, copy ctor, assignment operator and helpers
43 tinfo_key = TINFO_add;
55 add::add(const ex & lh, const ex & rh)
57 tinfo_key = TINFO_add;
59 construct_from_2_ex(lh,rh);
60 GINAC_ASSERT(is_canonical());
63 add::add(const exvector & v)
65 tinfo_key = TINFO_add;
67 construct_from_exvector(v);
68 GINAC_ASSERT(is_canonical());
71 add::add(const epvector & v)
73 tinfo_key = TINFO_add;
75 construct_from_epvector(v);
76 GINAC_ASSERT(is_canonical());
79 add::add(const epvector & v, const ex & oc)
81 tinfo_key = TINFO_add;
83 construct_from_epvector(v);
84 GINAC_ASSERT(is_canonical());
87 add::add(epvector * vp, const ex & oc)
89 tinfo_key = TINFO_add;
92 construct_from_epvector(*vp);
94 GINAC_ASSERT(is_canonical());
101 DEFAULT_ARCHIVING(add)
104 // functions overriding virtual functions from base classes
109 void add::print(const print_context & c, unsigned level) const
111 if (is_a<print_tree>(c)) {
113 inherited::print(c, level);
115 } else if (is_a<print_csrc>(c)) {
117 if (precedence() <= level)
120 // Print arguments, separated by "+"
121 epvector::const_iterator it = seq.begin(), itend = seq.end();
122 while (it != itend) {
124 // If the coefficient is -1, it is replaced by a single minus sign
125 if (it->coeff.is_equal(_ex1)) {
126 it->rest.print(c, precedence());
127 } else if (it->coeff.is_equal(_ex_1)) {
129 it->rest.print(c, precedence());
130 } else if (ex_to<numeric>(it->coeff).numer().is_equal(_num1)) {
131 it->rest.print(c, precedence());
133 ex_to<numeric>(it->coeff).denom().print(c, precedence());
134 } else if (ex_to<numeric>(it->coeff).numer().is_equal(_num_1)) {
136 it->rest.print(c, precedence());
138 ex_to<numeric>(it->coeff).denom().print(c, precedence());
140 it->coeff.print(c, precedence());
142 it->rest.print(c, precedence());
145 // Separator is "+", except if the following expression would have a leading minus sign or the sign is sitting in parenthesis (as in a ctor)
148 && (is_a<print_csrc_cl_N>(c) || !it->coeff.info(info_flags::real) // sign inside ctor arguments
149 || !(it->coeff.info(info_flags::negative) || (it->coeff.is_equal(_num1) && is_exactly_a<numeric>(it->rest) && it->rest.info(info_flags::negative)))))
153 if (!overall_coeff.is_zero()) {
154 if (overall_coeff.info(info_flags::positive)
155 || is_a<print_csrc_cl_N>(c) || !overall_coeff.info(info_flags::real)) // sign inside ctor argument
157 overall_coeff.print(c, precedence());
160 if (precedence() <= level)
163 } else if (is_a<print_python_repr>(c)) {
165 c.s << class_name() << '(';
167 for (size_t i=1; i<nops(); ++i) {
175 if (precedence() <= level) {
176 if (is_a<print_latex>(c))
185 // First print the overall numeric coefficient, if present
186 if (!overall_coeff.is_zero()) {
187 if (!is_a<print_tree>(c))
188 overall_coeff.print(c, 0);
190 overall_coeff.print(c, precedence());
194 // Then proceed with the remaining factors
195 epvector::const_iterator it = seq.begin(), itend = seq.end();
196 while (it != itend) {
197 coeff = ex_to<numeric>(it->coeff);
199 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
201 if (coeff.csgn() == -1) c.s << '-';
204 if (!coeff.is_equal(_num1) &&
205 !coeff.is_equal(_num_1)) {
206 if (coeff.is_rational()) {
207 if (coeff.is_negative())
212 if (coeff.csgn() == -1)
213 (-coeff).print(c, precedence());
215 coeff.print(c, precedence());
217 if (is_a<print_latex>(c))
222 it->rest.print(c, precedence());
226 if (precedence() <= level) {
227 if (is_a<print_latex>(c))
235 bool add::info(unsigned inf) const
238 case info_flags::polynomial:
239 case info_flags::integer_polynomial:
240 case info_flags::cinteger_polynomial:
241 case info_flags::rational_polynomial:
242 case info_flags::crational_polynomial:
243 case info_flags::rational_function: {
244 epvector::const_iterator i = seq.begin(), end = seq.end();
246 if (!(recombine_pair_to_ex(*i).info(inf)))
250 return overall_coeff.info(inf);
252 case info_flags::algebraic: {
253 epvector::const_iterator i = seq.begin(), end = seq.end();
255 if ((recombine_pair_to_ex(*i).info(inf)))
262 return inherited::info(inf);
265 int add::degree(const ex & s) const
268 if (!overall_coeff.is_zero())
271 // Find maximum of degrees of individual terms
272 epvector::const_iterator i = seq.begin(), end = seq.end();
274 int cur_deg = i->rest.degree(s);
282 int add::ldegree(const ex & s) const
285 if (!overall_coeff.is_zero())
288 // Find minimum of degrees of individual terms
289 epvector::const_iterator i = seq.begin(), end = seq.end();
291 int cur_deg = i->rest.ldegree(s);
299 ex add::coeff(const ex & s, int n) const
301 epvector *coeffseq = new epvector();
303 // Calculate sum of coefficients in each term
304 epvector::const_iterator i = seq.begin(), end = seq.end();
306 ex restcoeff = i->rest.coeff(s, n);
307 if (!restcoeff.is_zero())
308 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
312 return (new add(coeffseq, n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
315 /** Perform automatic term rewriting rules in this class. In the following
316 * x stands for a symbolic variables of type ex and c stands for such
317 * an expression that contain a plain number.
321 * @param level cut-off in recursive evaluation */
322 ex add::eval(int level) const
324 epvector *evaled_seqp = evalchildren(level);
326 // do more evaluation later
327 return (new add(evaled_seqp, overall_coeff))->
328 setflag(status_flags::dynallocated);
331 #ifdef DO_GINAC_ASSERT
332 epvector::const_iterator i = seq.begin(), end = seq.end();
334 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
335 if (is_exactly_a<numeric>(i->rest))
337 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
340 #endif // def DO_GINAC_ASSERT
342 if (flags & status_flags::evaluated) {
343 GINAC_ASSERT(seq.size()>0);
344 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
348 int seq_size = seq.size();
351 return overall_coeff;
352 } else if (seq_size == 1 && overall_coeff.is_zero()) {
354 return recombine_pair_to_ex(*(seq.begin()));
355 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
356 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
361 ex add::evalm(void) const
363 // Evaluate children first and add up all matrices. Stop if there's one
364 // term that is not a matrix.
365 epvector *s = new epvector;
366 s->reserve(seq.size());
368 bool all_matrices = true;
369 bool first_term = true;
372 epvector::const_iterator it = seq.begin(), itend = seq.end();
373 while (it != itend) {
374 const ex &m = recombine_pair_to_ex(*it).evalm();
375 s->push_back(split_ex_to_pair(m));
376 if (is_a<matrix>(m)) {
378 sum = ex_to<matrix>(m);
381 sum = sum.add(ex_to<matrix>(m));
383 all_matrices = false;
389 return sum + overall_coeff;
391 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
394 ex add::eval_ncmul(const exvector & v) const
397 return inherited::eval_ncmul(v);
399 return seq.begin()->rest.eval_ncmul(v);
404 /** Implementation of ex::diff() for a sum. It differentiates each term.
406 ex add::derivative(const symbol & y) const
408 epvector *s = new epvector();
409 s->reserve(seq.size());
411 // Only differentiate the "rest" parts of the expairs. This is faster
412 // than the default implementation in basic::derivative() although
413 // if performs the same function (differentiate each term).
414 epvector::const_iterator i = seq.begin(), end = seq.end();
416 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
419 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
422 int add::compare_same_type(const basic & other) const
424 return inherited::compare_same_type(other);
427 unsigned add::return_type(void) const
430 return return_types::commutative;
432 return seq.begin()->rest.return_type();
435 unsigned add::return_type_tinfo(void) const
440 return seq.begin()->rest.return_type_tinfo();
443 ex add::thisexpairseq(const epvector & v, const ex & oc) const
445 return (new add(v,oc))->setflag(status_flags::dynallocated);
448 ex add::thisexpairseq(epvector * vp, const ex & oc) const
450 return (new add(vp,oc))->setflag(status_flags::dynallocated);
453 expair add::split_ex_to_pair(const ex & e) const
455 if (is_exactly_a<mul>(e)) {
456 const mul &mulref(ex_to<mul>(e));
457 const ex &numfactor = mulref.overall_coeff;
458 mul *mulcopyp = new mul(mulref);
459 mulcopyp->overall_coeff = _ex1;
460 mulcopyp->clearflag(status_flags::evaluated);
461 mulcopyp->clearflag(status_flags::hash_calculated);
462 mulcopyp->setflag(status_flags::dynallocated);
463 return expair(*mulcopyp,numfactor);
465 return expair(e,_ex1);
468 expair add::combine_ex_with_coeff_to_pair(const ex & e,
471 GINAC_ASSERT(is_exactly_a<numeric>(c));
472 if (is_exactly_a<mul>(e)) {
473 const mul &mulref(ex_to<mul>(e));
474 const ex &numfactor = mulref.overall_coeff;
475 mul *mulcopyp = new mul(mulref);
476 mulcopyp->overall_coeff = _ex1;
477 mulcopyp->clearflag(status_flags::evaluated);
478 mulcopyp->clearflag(status_flags::hash_calculated);
479 mulcopyp->setflag(status_flags::dynallocated);
480 if (c.is_equal(_ex1))
481 return expair(*mulcopyp, numfactor);
482 else if (numfactor.is_equal(_ex1))
483 return expair(*mulcopyp, c);
485 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
486 } else if (is_exactly_a<numeric>(e)) {
487 if (c.is_equal(_ex1))
488 return expair(e, _ex1);
489 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
494 expair add::combine_pair_with_coeff_to_pair(const expair & p,
497 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
498 GINAC_ASSERT(is_exactly_a<numeric>(c));
500 if (is_exactly_a<numeric>(p.rest)) {
501 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(_num1)); // should be normalized
502 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
505 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
508 ex add::recombine_pair_to_ex(const expair & p) const
510 if (ex_to<numeric>(p.coeff).is_equal(_num1))
513 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
516 ex add::expand(unsigned options) const
518 epvector *vp = expandchildren(options);
520 // the terms have not changed, so it is safe to declare this expanded
521 return (options == 0) ? setflag(status_flags::expanded) : *this;
524 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));