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_OPT(add, expairseq,
36 print_func<print_context>(&add::do_print).
37 print_func<print_latex>(&add::do_print_latex).
38 print_func<print_csrc>(&add::do_print_csrc).
39 print_func<print_tree>(&inherited::do_print_tree).
40 print_func<print_python_repr>(&add::do_print_python_repr))
43 // default constructor
48 tinfo_key = TINFO_add;
57 add::add(const ex & lh, const ex & rh)
59 tinfo_key = TINFO_add;
61 construct_from_2_ex(lh,rh);
62 GINAC_ASSERT(is_canonical());
65 add::add(const exvector & v)
67 tinfo_key = TINFO_add;
69 construct_from_exvector(v);
70 GINAC_ASSERT(is_canonical());
73 add::add(const epvector & v)
75 tinfo_key = TINFO_add;
77 construct_from_epvector(v);
78 GINAC_ASSERT(is_canonical());
81 add::add(const epvector & v, const ex & oc)
83 tinfo_key = TINFO_add;
85 construct_from_epvector(v);
86 GINAC_ASSERT(is_canonical());
89 add::add(epvector * vp, const ex & oc)
91 tinfo_key = TINFO_add;
94 construct_from_epvector(*vp);
96 GINAC_ASSERT(is_canonical());
103 DEFAULT_ARCHIVING(add)
106 // functions overriding virtual functions from base classes
111 void add::print_add(const print_context & c, const char *openbrace, const char *closebrace, const char *mul_sym, unsigned level) const
113 if (precedence() <= level)
114 c.s << openbrace << '(';
119 // First print the overall numeric coefficient, if present
120 if (!overall_coeff.is_zero()) {
121 overall_coeff.print(c, 0);
125 // Then proceed with the remaining factors
126 epvector::const_iterator it = seq.begin(), itend = seq.end();
127 while (it != itend) {
128 coeff = ex_to<numeric>(it->coeff);
130 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
132 if (coeff.csgn() == -1) c.s << '-';
135 if (!coeff.is_equal(_num1) &&
136 !coeff.is_equal(_num_1)) {
137 if (coeff.is_rational()) {
138 if (coeff.is_negative())
143 if (coeff.csgn() == -1)
144 (-coeff).print(c, precedence());
146 coeff.print(c, precedence());
150 it->rest.print(c, precedence());
154 if (precedence() <= level)
155 c.s << ')' << closebrace;
158 void add::do_print(const print_context & c, unsigned level) const
160 print_add(c, "", "", "*", level);
163 void add::do_print_latex(const print_latex & c, unsigned level) const
165 print_add(c, "{", "}", " ", level);
168 void add::do_print_csrc(const print_csrc & c, unsigned level) const
170 if (precedence() <= level)
173 // Print arguments, separated by "+"
174 epvector::const_iterator it = seq.begin(), itend = seq.end();
175 while (it != itend) {
177 // If the coefficient is -1, it is replaced by a single minus sign
178 if (it->coeff.is_equal(_ex1)) {
179 it->rest.print(c, precedence());
180 } else if (it->coeff.is_equal(_ex_1)) {
182 it->rest.print(c, precedence());
183 } else if (ex_to<numeric>(it->coeff).numer().is_equal(_num1)) {
184 it->rest.print(c, precedence());
186 ex_to<numeric>(it->coeff).denom().print(c, precedence());
187 } else if (ex_to<numeric>(it->coeff).numer().is_equal(_num_1)) {
189 it->rest.print(c, precedence());
191 ex_to<numeric>(it->coeff).denom().print(c, precedence());
193 it->coeff.print(c, precedence());
195 it->rest.print(c, precedence());
198 // Separator is "+", except if the following expression would have a leading minus sign or the sign is sitting in parenthesis (as in a ctor)
201 && (is_a<print_csrc_cl_N>(c) || !it->coeff.info(info_flags::real) // sign inside ctor arguments
202 || !(it->coeff.info(info_flags::negative) || (it->coeff.is_equal(_num1) && is_exactly_a<numeric>(it->rest) && it->rest.info(info_flags::negative)))))
206 if (!overall_coeff.is_zero()) {
207 if (overall_coeff.info(info_flags::positive)
208 || is_a<print_csrc_cl_N>(c) || !overall_coeff.info(info_flags::real)) // sign inside ctor argument
210 overall_coeff.print(c, precedence());
213 if (precedence() <= level)
217 void add::do_print_python_repr(const print_python_repr & c, unsigned level) const
219 c.s << class_name() << '(';
221 for (size_t i=1; i<nops(); ++i) {
228 bool add::info(unsigned inf) const
231 case info_flags::polynomial:
232 case info_flags::integer_polynomial:
233 case info_flags::cinteger_polynomial:
234 case info_flags::rational_polynomial:
235 case info_flags::crational_polynomial:
236 case info_flags::rational_function: {
237 epvector::const_iterator i = seq.begin(), end = seq.end();
239 if (!(recombine_pair_to_ex(*i).info(inf)))
243 return overall_coeff.info(inf);
245 case info_flags::algebraic: {
246 epvector::const_iterator i = seq.begin(), end = seq.end();
248 if ((recombine_pair_to_ex(*i).info(inf)))
255 return inherited::info(inf);
258 int add::degree(const ex & s) const
261 if (!overall_coeff.is_zero())
264 // Find maximum of degrees of individual terms
265 epvector::const_iterator i = seq.begin(), end = seq.end();
267 int cur_deg = i->rest.degree(s);
275 int add::ldegree(const ex & s) const
278 if (!overall_coeff.is_zero())
281 // Find minimum of degrees of individual terms
282 epvector::const_iterator i = seq.begin(), end = seq.end();
284 int cur_deg = i->rest.ldegree(s);
292 ex add::coeff(const ex & s, int n) const
294 epvector *coeffseq = new epvector();
296 // Calculate sum of coefficients in each term
297 epvector::const_iterator i = seq.begin(), end = seq.end();
299 ex restcoeff = i->rest.coeff(s, n);
300 if (!restcoeff.is_zero())
301 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
305 return (new add(coeffseq, n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
308 /** Perform automatic term rewriting rules in this class. In the following
309 * x stands for a symbolic variables of type ex and c stands for such
310 * an expression that contain a plain number.
314 * @param level cut-off in recursive evaluation */
315 ex add::eval(int level) const
317 epvector *evaled_seqp = evalchildren(level);
319 // do more evaluation later
320 return (new add(evaled_seqp, overall_coeff))->
321 setflag(status_flags::dynallocated);
324 #ifdef DO_GINAC_ASSERT
325 epvector::const_iterator i = seq.begin(), end = seq.end();
327 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
328 if (is_exactly_a<numeric>(i->rest))
330 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
333 #endif // def DO_GINAC_ASSERT
335 if (flags & status_flags::evaluated) {
336 GINAC_ASSERT(seq.size()>0);
337 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
341 int seq_size = seq.size();
344 return overall_coeff;
345 } else if (seq_size == 1 && overall_coeff.is_zero()) {
347 return recombine_pair_to_ex(*(seq.begin()));
348 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
349 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
354 ex add::evalm() const
356 // Evaluate children first and add up all matrices. Stop if there's one
357 // term that is not a matrix.
358 epvector *s = new epvector;
359 s->reserve(seq.size());
361 bool all_matrices = true;
362 bool first_term = true;
365 epvector::const_iterator it = seq.begin(), itend = seq.end();
366 while (it != itend) {
367 const ex &m = recombine_pair_to_ex(*it).evalm();
368 s->push_back(split_ex_to_pair(m));
369 if (is_a<matrix>(m)) {
371 sum = ex_to<matrix>(m);
374 sum = sum.add(ex_to<matrix>(m));
376 all_matrices = false;
382 return sum + overall_coeff;
384 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
387 ex add::eval_ncmul(const exvector & v) const
390 return inherited::eval_ncmul(v);
392 return seq.begin()->rest.eval_ncmul(v);
397 /** Implementation of ex::diff() for a sum. It differentiates each term.
399 ex add::derivative(const symbol & y) const
401 epvector *s = new epvector();
402 s->reserve(seq.size());
404 // Only differentiate the "rest" parts of the expairs. This is faster
405 // than the default implementation in basic::derivative() although
406 // if performs the same function (differentiate each term).
407 epvector::const_iterator i = seq.begin(), end = seq.end();
409 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
412 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
415 int add::compare_same_type(const basic & other) const
417 return inherited::compare_same_type(other);
420 unsigned add::return_type() const
423 return return_types::commutative;
425 return seq.begin()->rest.return_type();
428 unsigned add::return_type_tinfo() const
433 return seq.begin()->rest.return_type_tinfo();
436 ex add::thisexpairseq(const epvector & v, const ex & oc) const
438 return (new add(v,oc))->setflag(status_flags::dynallocated);
441 ex add::thisexpairseq(epvector * vp, const ex & oc) const
443 return (new add(vp,oc))->setflag(status_flags::dynallocated);
446 expair add::split_ex_to_pair(const ex & e) const
448 if (is_exactly_a<mul>(e)) {
449 const mul &mulref(ex_to<mul>(e));
450 const ex &numfactor = mulref.overall_coeff;
451 mul *mulcopyp = new mul(mulref);
452 mulcopyp->overall_coeff = _ex1;
453 mulcopyp->clearflag(status_flags::evaluated);
454 mulcopyp->clearflag(status_flags::hash_calculated);
455 mulcopyp->setflag(status_flags::dynallocated);
456 return expair(*mulcopyp,numfactor);
458 return expair(e,_ex1);
461 expair add::combine_ex_with_coeff_to_pair(const ex & e,
464 GINAC_ASSERT(is_exactly_a<numeric>(c));
465 if (is_exactly_a<mul>(e)) {
466 const mul &mulref(ex_to<mul>(e));
467 const ex &numfactor = mulref.overall_coeff;
468 mul *mulcopyp = new mul(mulref);
469 mulcopyp->overall_coeff = _ex1;
470 mulcopyp->clearflag(status_flags::evaluated);
471 mulcopyp->clearflag(status_flags::hash_calculated);
472 mulcopyp->setflag(status_flags::dynallocated);
473 if (c.is_equal(_ex1))
474 return expair(*mulcopyp, numfactor);
475 else if (numfactor.is_equal(_ex1))
476 return expair(*mulcopyp, c);
478 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
479 } else if (is_exactly_a<numeric>(e)) {
480 if (c.is_equal(_ex1))
481 return expair(e, _ex1);
482 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
487 expair add::combine_pair_with_coeff_to_pair(const expair & p,
490 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
491 GINAC_ASSERT(is_exactly_a<numeric>(c));
493 if (is_exactly_a<numeric>(p.rest)) {
494 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(_num1)); // should be normalized
495 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
498 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
501 ex add::recombine_pair_to_ex(const expair & p) const
503 if (ex_to<numeric>(p.coeff).is_equal(_num1))
506 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
509 ex add::expand(unsigned options) const
511 epvector *vp = expandchildren(options);
513 // the terms have not changed, so it is safe to declare this expanded
514 return (options == 0) ? setflag(status_flags::expanded) : *this;
517 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));