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 ctor, dtor, copy ctor, assignment operator and helpers
42 tinfo_key = TINFO_add;
54 add::add(const ex & lh, const ex & rh)
56 tinfo_key = TINFO_add;
58 construct_from_2_ex(lh,rh);
59 GINAC_ASSERT(is_canonical());
62 add::add(const exvector & v)
64 tinfo_key = TINFO_add;
66 construct_from_exvector(v);
67 GINAC_ASSERT(is_canonical());
70 add::add(const epvector & v)
72 tinfo_key = TINFO_add;
74 construct_from_epvector(v);
75 GINAC_ASSERT(is_canonical());
78 add::add(const epvector & v, const ex & oc)
80 tinfo_key = TINFO_add;
82 construct_from_epvector(v);
83 GINAC_ASSERT(is_canonical());
86 add::add(epvector * vp, const ex & oc)
88 tinfo_key = TINFO_add;
91 construct_from_epvector(*vp);
93 GINAC_ASSERT(is_canonical());
100 DEFAULT_ARCHIVING(add)
103 // functions overriding virtual functions from base classes
108 void add::print(const print_context & c, unsigned level) const
110 if (is_a<print_tree>(c)) {
112 inherited::print(c, level);
114 } else if (is_a<print_csrc>(c)) {
116 if (precedence() <= level)
119 // Print arguments, separated by "+"
120 epvector::const_iterator it = seq.begin(), itend = seq.end();
121 while (it != itend) {
123 // If the coefficient is -1, it is replaced by a single minus sign
124 if (it->coeff.compare(_num1) == 0) {
125 it->rest.print(c, precedence());
126 } else if (it->coeff.compare(_num_1) == 0) {
128 it->rest.print(c, precedence());
129 } else if (ex_to<numeric>(it->coeff).numer().compare(_num1) == 0) {
130 it->rest.print(c, precedence());
132 ex_to<numeric>(it->coeff).denom().print(c, precedence());
133 } else if (ex_to<numeric>(it->coeff).numer().compare(_num_1) == 0) {
135 it->rest.print(c, precedence());
137 ex_to<numeric>(it->coeff).denom().print(c, precedence());
139 it->coeff.print(c, precedence());
141 it->rest.print(c, precedence());
144 // Separator is "+", except if the following expression would have a leading minus sign
146 if (it != itend && !(it->coeff.compare(_num0) < 0 || (it->coeff.compare(_num1) == 0 && is_exactly_a<numeric>(it->rest) && it->rest.compare(_num0) < 0)))
150 if (!overall_coeff.is_zero()) {
151 if (overall_coeff.info(info_flags::positive))
153 overall_coeff.print(c, precedence());
156 if (precedence() <= level)
159 } else if (is_a<print_python_repr>(c)) {
161 c.s << class_name() << '(';
162 unsigned end = nops();
165 for (unsigned i=1; i<end; ++i) {
173 if (precedence() <= level) {
174 if (is_a<print_latex>(c))
183 // First print the overall numeric coefficient, if present
184 if (!overall_coeff.is_zero()) {
185 if (!is_a<print_tree>(c))
186 overall_coeff.print(c, 0);
188 overall_coeff.print(c, precedence());
192 // Then proceed with the remaining factors
193 epvector::const_iterator it = seq.begin(), itend = seq.end();
194 while (it != itend) {
195 coeff = ex_to<numeric>(it->coeff);
197 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
199 if (coeff.csgn() == -1) c.s << '-';
202 if (!coeff.is_equal(_num1) &&
203 !coeff.is_equal(_num_1)) {
204 if (coeff.is_rational()) {
205 if (coeff.is_negative())
210 if (coeff.csgn() == -1)
211 (-coeff).print(c, precedence());
213 coeff.print(c, precedence());
215 if (is_a<print_latex>(c))
220 it->rest.print(c, precedence());
224 if (precedence() <= level) {
225 if (is_a<print_latex>(c))
233 bool add::info(unsigned inf) const
236 case info_flags::polynomial:
237 case info_flags::integer_polynomial:
238 case info_flags::cinteger_polynomial:
239 case info_flags::rational_polynomial:
240 case info_flags::crational_polynomial:
241 case info_flags::rational_function: {
242 epvector::const_iterator i = seq.begin(), end = seq.end();
244 if (!(recombine_pair_to_ex(*i).info(inf)))
248 return overall_coeff.info(inf);
250 case info_flags::algebraic: {
251 epvector::const_iterator i = seq.begin(), end = seq.end();
253 if ((recombine_pair_to_ex(*i).info(inf)))
260 return inherited::info(inf);
263 int add::degree(const ex & s) const
266 if (!overall_coeff.is_zero())
269 // Find maximum of degrees of individual terms
270 epvector::const_iterator i = seq.begin(), end = seq.end();
272 int cur_deg = i->rest.degree(s);
280 int add::ldegree(const ex & s) const
283 if (!overall_coeff.is_zero())
286 // Find minimum of degrees of individual terms
287 epvector::const_iterator i = seq.begin(), end = seq.end();
289 int cur_deg = i->rest.ldegree(s);
297 ex add::coeff(const ex & s, int n) const
299 epvector *coeffseq = new epvector();
301 // Calculate sum of coefficients in each term
302 epvector::const_iterator i = seq.begin(), end = seq.end();
304 ex restcoeff = i->rest.coeff(s, n);
305 if (!restcoeff.is_zero())
306 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
310 return (new add(coeffseq, n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
313 /** Perform automatic term rewriting rules in this class. In the following
314 * x stands for a symbolic variables of type ex and c stands for such
315 * an expression that contain a plain number.
319 * @param level cut-off in recursive evaluation */
320 ex add::eval(int level) const
322 epvector *evaled_seqp = evalchildren(level);
324 // do more evaluation later
325 return (new add(evaled_seqp, overall_coeff))->
326 setflag(status_flags::dynallocated);
329 #ifdef DO_GINAC_ASSERT
330 epvector::const_iterator i = seq.begin(), end = seq.end();
332 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
333 if (is_ex_exactly_of_type(i->rest,numeric))
335 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
338 #endif // def DO_GINAC_ASSERT
340 if (flags & status_flags::evaluated) {
341 GINAC_ASSERT(seq.size()>0);
342 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
346 int seq_size = seq.size();
349 return overall_coeff;
350 } else if (seq_size == 1 && overall_coeff.is_zero()) {
352 return recombine_pair_to_ex(*(seq.begin()));
353 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
354 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
359 ex add::evalm(void) const
361 // Evaluate children first and add up all matrices. Stop if there's one
362 // term that is not a matrix.
363 epvector *s = new epvector;
364 s->reserve(seq.size());
366 bool all_matrices = true;
367 bool first_term = true;
370 epvector::const_iterator it = seq.begin(), itend = seq.end();
371 while (it != itend) {
372 const ex &m = recombine_pair_to_ex(*it).evalm();
373 s->push_back(split_ex_to_pair(m));
374 if (is_ex_of_type(m, matrix)) {
376 sum = ex_to<matrix>(m);
379 sum = sum.add(ex_to<matrix>(m));
381 all_matrices = false;
387 return sum + overall_coeff;
389 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
392 ex add::simplify_ncmul(const exvector & v) const
395 return inherited::simplify_ncmul(v);
397 return seq.begin()->rest.simplify_ncmul(v);
402 /** Implementation of ex::diff() for a sum. It differentiates each term.
404 ex add::derivative(const symbol & y) const
406 epvector *s = new epvector();
407 s->reserve(seq.size());
409 // Only differentiate the "rest" parts of the expairs. This is faster
410 // than the default implementation in basic::derivative() although
411 // if performs the same function (differentiate each term).
412 epvector::const_iterator i = seq.begin(), end = seq.end();
414 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
417 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
420 int add::compare_same_type(const basic & other) const
422 return inherited::compare_same_type(other);
425 bool add::is_equal_same_type(const basic & other) const
427 return inherited::is_equal_same_type(other);
430 unsigned add::return_type(void) const
433 return return_types::commutative;
435 return seq.begin()->rest.return_type();
438 unsigned add::return_type_tinfo(void) const
443 return seq.begin()->rest.return_type_tinfo();
446 ex add::thisexpairseq(const epvector & v, const ex & oc) const
448 return (new add(v,oc))->setflag(status_flags::dynallocated);
451 ex add::thisexpairseq(epvector * vp, const ex & oc) const
453 return (new add(vp,oc))->setflag(status_flags::dynallocated);
456 expair add::split_ex_to_pair(const ex & e) const
458 if (is_ex_exactly_of_type(e,mul)) {
459 const mul &mulref(ex_to<mul>(e));
460 const ex &numfactor = mulref.overall_coeff;
461 mul *mulcopyp = new mul(mulref);
462 mulcopyp->overall_coeff = _ex1;
463 mulcopyp->clearflag(status_flags::evaluated);
464 mulcopyp->clearflag(status_flags::hash_calculated);
465 mulcopyp->setflag(status_flags::dynallocated);
466 return expair(*mulcopyp,numfactor);
468 return expair(e,_ex1);
471 expair add::combine_ex_with_coeff_to_pair(const ex & e,
474 GINAC_ASSERT(is_exactly_a<numeric>(c));
475 if (is_ex_exactly_of_type(e, mul)) {
476 const mul &mulref(ex_to<mul>(e));
477 const ex &numfactor = mulref.overall_coeff;
478 mul *mulcopyp = new mul(mulref);
479 mulcopyp->overall_coeff = _ex1;
480 mulcopyp->clearflag(status_flags::evaluated);
481 mulcopyp->clearflag(status_flags::hash_calculated);
482 mulcopyp->setflag(status_flags::dynallocated);
483 if (are_ex_trivially_equal(c, _ex1))
484 return expair(*mulcopyp, numfactor);
485 else if (are_ex_trivially_equal(numfactor, _ex1))
486 return expair(*mulcopyp, c);
488 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
489 } else if (is_ex_exactly_of_type(e, numeric)) {
490 if (are_ex_trivially_equal(c, _ex1))
491 return expair(e, _ex1);
492 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
497 expair add::combine_pair_with_coeff_to_pair(const expair & p,
500 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
501 GINAC_ASSERT(is_exactly_a<numeric>(c));
503 if (is_ex_exactly_of_type(p.rest,numeric)) {
504 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(_num1)); // should be normalized
505 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
508 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
511 ex add::recombine_pair_to_ex(const expair & p) const
513 if (ex_to<numeric>(p.coeff).is_equal(_num1))
516 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
519 ex add::expand(unsigned options) const
521 epvector *vp = expandchildren(options);
523 // the terms have not changed, so it is safe to declare this expanded
524 return (options == 0) ? setflag(status_flags::expanded) : *this;
527 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));