3 * Implementation of GiNaC's sums of expressions. */
6 * GiNaC Copyright (C) 1999-2002 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) // 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))
156 overall_coeff.print(c, precedence());
159 if (precedence() <= level)
162 } else if (is_a<print_python_repr>(c)) {
164 c.s << class_name() << '(';
166 for (unsigned i=1; i<nops(); ++i) {
174 if (precedence() <= level) {
175 if (is_a<print_latex>(c))
184 // First print the overall numeric coefficient, if present
185 if (!overall_coeff.is_zero()) {
186 if (!is_a<print_tree>(c))
187 overall_coeff.print(c, 0);
189 overall_coeff.print(c, precedence());
193 // Then proceed with the remaining factors
194 epvector::const_iterator it = seq.begin(), itend = seq.end();
195 while (it != itend) {
196 coeff = ex_to<numeric>(it->coeff);
198 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
200 if (coeff.csgn() == -1) c.s << '-';
203 if (!coeff.is_equal(_num1) &&
204 !coeff.is_equal(_num_1)) {
205 if (coeff.is_rational()) {
206 if (coeff.is_negative())
211 if (coeff.csgn() == -1)
212 (-coeff).print(c, precedence());
214 coeff.print(c, precedence());
216 if (is_a<print_latex>(c))
221 it->rest.print(c, precedence());
225 if (precedence() <= level) {
226 if (is_a<print_latex>(c))
234 bool add::info(unsigned inf) const
237 case info_flags::polynomial:
238 case info_flags::integer_polynomial:
239 case info_flags::cinteger_polynomial:
240 case info_flags::rational_polynomial:
241 case info_flags::crational_polynomial:
242 case info_flags::rational_function: {
243 epvector::const_iterator i = seq.begin(), end = seq.end();
245 if (!(recombine_pair_to_ex(*i).info(inf)))
249 return overall_coeff.info(inf);
251 case info_flags::algebraic: {
252 epvector::const_iterator i = seq.begin(), end = seq.end();
254 if ((recombine_pair_to_ex(*i).info(inf)))
261 return inherited::info(inf);
264 int add::degree(const ex & s) const
267 if (!overall_coeff.is_zero())
270 // Find maximum of degrees of individual terms
271 epvector::const_iterator i = seq.begin(), end = seq.end();
273 int cur_deg = i->rest.degree(s);
281 int add::ldegree(const ex & s) const
284 if (!overall_coeff.is_zero())
287 // Find minimum of degrees of individual terms
288 epvector::const_iterator i = seq.begin(), end = seq.end();
290 int cur_deg = i->rest.ldegree(s);
298 ex add::coeff(const ex & s, int n) const
300 epvector *coeffseq = new epvector();
302 // Calculate sum of coefficients in each term
303 epvector::const_iterator i = seq.begin(), end = seq.end();
305 ex restcoeff = i->rest.coeff(s, n);
306 if (!restcoeff.is_zero())
307 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
311 return (new add(coeffseq, n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
314 /** Perform automatic term rewriting rules in this class. In the following
315 * x stands for a symbolic variables of type ex and c stands for such
316 * an expression that contain a plain number.
320 * @param level cut-off in recursive evaluation */
321 ex add::eval(int level) const
323 epvector *evaled_seqp = evalchildren(level);
325 // do more evaluation later
326 return (new add(evaled_seqp, overall_coeff))->
327 setflag(status_flags::dynallocated);
330 #ifdef DO_GINAC_ASSERT
331 epvector::const_iterator i = seq.begin(), end = seq.end();
333 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
334 if (is_exactly_a<numeric>(i->rest))
336 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
339 #endif // def DO_GINAC_ASSERT
341 if (flags & status_flags::evaluated) {
342 GINAC_ASSERT(seq.size()>0);
343 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
347 int seq_size = seq.size();
350 return overall_coeff;
351 } else if (seq_size == 1 && overall_coeff.is_zero()) {
353 return recombine_pair_to_ex(*(seq.begin()));
354 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
355 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
360 ex add::evalm(void) const
362 // Evaluate children first and add up all matrices. Stop if there's one
363 // term that is not a matrix.
364 epvector *s = new epvector;
365 s->reserve(seq.size());
367 bool all_matrices = true;
368 bool first_term = true;
371 epvector::const_iterator it = seq.begin(), itend = seq.end();
372 while (it != itend) {
373 const ex &m = recombine_pair_to_ex(*it).evalm();
374 s->push_back(split_ex_to_pair(m));
375 if (is_a<matrix>(m)) {
377 sum = ex_to<matrix>(m);
380 sum = sum.add(ex_to<matrix>(m));
382 all_matrices = false;
388 return sum + overall_coeff;
390 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
393 ex add::simplify_ncmul(const exvector & v) const
396 return inherited::simplify_ncmul(v);
398 return seq.begin()->rest.simplify_ncmul(v);
403 /** Implementation of ex::diff() for a sum. It differentiates each term.
405 ex add::derivative(const symbol & y) const
407 epvector *s = new epvector();
408 s->reserve(seq.size());
410 // Only differentiate the "rest" parts of the expairs. This is faster
411 // than the default implementation in basic::derivative() although
412 // if performs the same function (differentiate each term).
413 epvector::const_iterator i = seq.begin(), end = seq.end();
415 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
418 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
421 int add::compare_same_type(const basic & other) const
423 return inherited::compare_same_type(other);
426 unsigned add::return_type(void) const
429 return return_types::commutative;
431 return seq.begin()->rest.return_type();
434 unsigned add::return_type_tinfo(void) const
439 return seq.begin()->rest.return_type_tinfo();
442 ex add::thisexpairseq(const epvector & v, const ex & oc) const
444 return (new add(v,oc))->setflag(status_flags::dynallocated);
447 ex add::thisexpairseq(epvector * vp, const ex & oc) const
449 return (new add(vp,oc))->setflag(status_flags::dynallocated);
452 expair add::split_ex_to_pair(const ex & e) const
454 if (is_exactly_a<mul>(e)) {
455 const mul &mulref(ex_to<mul>(e));
456 const ex &numfactor = mulref.overall_coeff;
457 mul *mulcopyp = new mul(mulref);
458 mulcopyp->overall_coeff = _ex1;
459 mulcopyp->clearflag(status_flags::evaluated);
460 mulcopyp->clearflag(status_flags::hash_calculated);
461 mulcopyp->setflag(status_flags::dynallocated);
462 return expair(*mulcopyp,numfactor);
464 return expair(e,_ex1);
467 expair add::combine_ex_with_coeff_to_pair(const ex & e,
470 GINAC_ASSERT(is_exactly_a<numeric>(c));
471 if (is_exactly_a<mul>(e)) {
472 const mul &mulref(ex_to<mul>(e));
473 const ex &numfactor = mulref.overall_coeff;
474 mul *mulcopyp = new mul(mulref);
475 mulcopyp->overall_coeff = _ex1;
476 mulcopyp->clearflag(status_flags::evaluated);
477 mulcopyp->clearflag(status_flags::hash_calculated);
478 mulcopyp->setflag(status_flags::dynallocated);
479 if (c.is_equal(_ex1))
480 return expair(*mulcopyp, numfactor);
481 else if (numfactor.is_equal(_ex1))
482 return expair(*mulcopyp, c);
484 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
485 } else if (is_exactly_a<numeric>(e)) {
486 if (c.is_equal(_ex1))
487 return expair(e, _ex1);
488 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
493 expair add::combine_pair_with_coeff_to_pair(const expair & p,
496 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
497 GINAC_ASSERT(is_exactly_a<numeric>(c));
499 if (is_exactly_a<numeric>(p.rest)) {
500 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(_num1)); // should be normalized
501 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
504 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
507 ex add::recombine_pair_to_ex(const expair & p) const
509 if (ex_to<numeric>(p.coeff).is_equal(_num1))
512 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
515 ex add::expand(unsigned options) const
517 epvector *vp = expandchildren(options);
519 // the terms have not changed, so it is safe to declare this expanded
520 return (options == 0) ? setflag(status_flags::expanded) : *this;
523 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));