3 * Implementation of GiNaC's sums of expressions. */
6 * GiNaC Copyright (C) 1999-2004 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>(&add::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(std::auto_ptr<epvector> vp, const ex & oc)
91 tinfo_key = TINFO_add;
92 GINAC_ASSERT(vp.get()!=0);
94 construct_from_epvector(*vp);
95 GINAC_ASSERT(is_canonical());
102 DEFAULT_ARCHIVING(add)
105 // functions overriding virtual functions from base classes
110 void add::print_add(const print_context & c, const char *openbrace, const char *closebrace, const char *mul_sym, unsigned level) const
112 if (precedence() <= level)
113 c.s << openbrace << '(';
118 // First print the overall numeric coefficient, if present
119 if (!overall_coeff.is_zero()) {
120 overall_coeff.print(c, 0);
124 // Then proceed with the remaining factors
125 epvector::const_iterator it = seq.begin(), itend = seq.end();
126 while (it != itend) {
127 coeff = ex_to<numeric>(it->coeff);
129 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
131 if (coeff.csgn() == -1) c.s << '-';
134 if (!coeff.is_equal(_num1) &&
135 !coeff.is_equal(_num_1)) {
136 if (coeff.is_rational()) {
137 if (coeff.is_negative())
142 if (coeff.csgn() == -1)
143 (-coeff).print(c, precedence());
145 coeff.print(c, precedence());
149 it->rest.print(c, precedence());
153 if (precedence() <= level)
154 c.s << ')' << closebrace;
157 void add::do_print(const print_context & c, unsigned level) const
159 print_add(c, "", "", "*", level);
162 void add::do_print_latex(const print_latex & c, unsigned level) const
164 print_add(c, "{", "}", " ", level);
167 void add::do_print_csrc(const print_csrc & c, unsigned level) const
169 if (precedence() <= level)
172 // Print arguments, separated by "+"
173 epvector::const_iterator it = seq.begin(), itend = seq.end();
174 while (it != itend) {
176 // If the coefficient is -1, it is replaced by a single minus sign
177 if (it->coeff.is_equal(_ex1)) {
178 it->rest.print(c, precedence());
179 } else if (it->coeff.is_equal(_ex_1)) {
181 it->rest.print(c, precedence());
182 } else if (ex_to<numeric>(it->coeff).numer().is_equal(_num1)) {
183 it->rest.print(c, precedence());
185 ex_to<numeric>(it->coeff).denom().print(c, precedence());
186 } else if (ex_to<numeric>(it->coeff).numer().is_equal(_num_1)) {
188 it->rest.print(c, precedence());
190 ex_to<numeric>(it->coeff).denom().print(c, precedence());
192 it->coeff.print(c, precedence());
194 it->rest.print(c, precedence());
197 // Separator is "+", except if the following expression would have a leading minus sign or the sign is sitting in parenthesis (as in a ctor)
200 && (is_a<print_csrc_cl_N>(c) || !it->coeff.info(info_flags::real) // sign inside ctor arguments
201 || !(it->coeff.info(info_flags::negative) || (it->coeff.is_equal(_num1) && is_exactly_a<numeric>(it->rest) && it->rest.info(info_flags::negative)))))
205 if (!overall_coeff.is_zero()) {
206 if (overall_coeff.info(info_flags::positive)
207 || is_a<print_csrc_cl_N>(c) || !overall_coeff.info(info_flags::real)) // sign inside ctor argument
209 overall_coeff.print(c, precedence());
212 if (precedence() <= level)
216 void add::do_print_python_repr(const print_python_repr & c, unsigned level) const
218 c.s << class_name() << '(';
220 for (size_t i=1; i<nops(); ++i) {
227 bool add::info(unsigned inf) const
230 case info_flags::polynomial:
231 case info_flags::integer_polynomial:
232 case info_flags::cinteger_polynomial:
233 case info_flags::rational_polynomial:
234 case info_flags::crational_polynomial:
235 case info_flags::rational_function: {
236 epvector::const_iterator i = seq.begin(), end = seq.end();
238 if (!(recombine_pair_to_ex(*i).info(inf)))
242 return overall_coeff.info(inf);
244 case info_flags::algebraic: {
245 epvector::const_iterator i = seq.begin(), end = seq.end();
247 if ((recombine_pair_to_ex(*i).info(inf)))
254 return inherited::info(inf);
257 int add::degree(const ex & s) const
260 if (!overall_coeff.is_zero())
263 // Find maximum of degrees of individual terms
264 epvector::const_iterator i = seq.begin(), end = seq.end();
266 int cur_deg = i->rest.degree(s);
274 int add::ldegree(const ex & s) const
277 if (!overall_coeff.is_zero())
280 // Find minimum of degrees of individual terms
281 epvector::const_iterator i = seq.begin(), end = seq.end();
283 int cur_deg = i->rest.ldegree(s);
291 ex add::coeff(const ex & s, int n) const
293 std::auto_ptr<epvector> coeffseq(new epvector);
295 // Calculate sum of coefficients in each term
296 epvector::const_iterator i = seq.begin(), end = seq.end();
298 ex restcoeff = i->rest.coeff(s, n);
299 if (!restcoeff.is_zero())
300 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
304 return (new add(coeffseq, n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
307 /** Perform automatic term rewriting rules in this class. In the following
308 * x stands for a symbolic variables of type ex and c stands for such
309 * an expression that contain a plain number.
313 * @param level cut-off in recursive evaluation */
314 ex add::eval(int level) const
316 std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
317 if (evaled_seqp.get()) {
318 // do more evaluation later
319 return (new add(evaled_seqp, overall_coeff))->
320 setflag(status_flags::dynallocated);
323 #ifdef DO_GINAC_ASSERT
324 epvector::const_iterator i = seq.begin(), end = seq.end();
326 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
327 if (is_exactly_a<numeric>(i->rest))
329 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
332 #endif // def DO_GINAC_ASSERT
334 if (flags & status_flags::evaluated) {
335 GINAC_ASSERT(seq.size()>0);
336 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
340 int seq_size = seq.size();
343 return overall_coeff;
344 } else if (seq_size == 1 && overall_coeff.is_zero()) {
346 return recombine_pair_to_ex(*(seq.begin()));
347 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
348 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
353 ex add::evalm() const
355 // Evaluate children first and add up all matrices. Stop if there's one
356 // term that is not a matrix.
357 std::auto_ptr<epvector> s(new epvector);
358 s->reserve(seq.size());
360 bool all_matrices = true;
361 bool first_term = true;
364 epvector::const_iterator it = seq.begin(), itend = seq.end();
365 while (it != itend) {
366 const ex &m = recombine_pair_to_ex(*it).evalm();
367 s->push_back(split_ex_to_pair(m));
368 if (is_a<matrix>(m)) {
370 sum = ex_to<matrix>(m);
373 sum = sum.add(ex_to<matrix>(m));
375 all_matrices = false;
380 return sum + overall_coeff;
382 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
385 ex add::conjugate() const
388 for (int i=0; i<nops(); ++i) {
390 v->push_back(op(i).conjugate());
394 ex ccterm = term.conjugate();
395 if (are_ex_trivially_equal(term, ccterm))
399 for (int j=0; j<i; ++j)
401 v->push_back(ccterm);
411 ex add::eval_ncmul(const exvector & v) const
414 return inherited::eval_ncmul(v);
416 return seq.begin()->rest.eval_ncmul(v);
421 /** Implementation of ex::diff() for a sum. It differentiates each term.
423 ex add::derivative(const symbol & y) const
425 std::auto_ptr<epvector> s(new epvector);
426 s->reserve(seq.size());
428 // Only differentiate the "rest" parts of the expairs. This is faster
429 // than the default implementation in basic::derivative() although
430 // if performs the same function (differentiate each term).
431 epvector::const_iterator i = seq.begin(), end = seq.end();
433 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
436 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
439 int add::compare_same_type(const basic & other) const
441 return inherited::compare_same_type(other);
444 unsigned add::return_type() const
447 return return_types::commutative;
449 return seq.begin()->rest.return_type();
452 unsigned add::return_type_tinfo() const
457 return seq.begin()->rest.return_type_tinfo();
460 ex add::thisexpairseq(const epvector & v, const ex & oc) const
462 return (new add(v,oc))->setflag(status_flags::dynallocated);
465 ex add::thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc) const
467 return (new add(vp,oc))->setflag(status_flags::dynallocated);
470 expair add::split_ex_to_pair(const ex & e) const
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 return expair(*mulcopyp,numfactor);
482 return expair(e,_ex1);
485 expair add::combine_ex_with_coeff_to_pair(const ex & e,
488 GINAC_ASSERT(is_exactly_a<numeric>(c));
489 if (is_exactly_a<mul>(e)) {
490 const mul &mulref(ex_to<mul>(e));
491 const ex &numfactor = mulref.overall_coeff;
492 mul *mulcopyp = new mul(mulref);
493 mulcopyp->overall_coeff = _ex1;
494 mulcopyp->clearflag(status_flags::evaluated);
495 mulcopyp->clearflag(status_flags::hash_calculated);
496 mulcopyp->setflag(status_flags::dynallocated);
497 if (c.is_equal(_ex1))
498 return expair(*mulcopyp, numfactor);
499 else if (numfactor.is_equal(_ex1))
500 return expair(*mulcopyp, c);
502 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
503 } else if (is_exactly_a<numeric>(e)) {
504 if (c.is_equal(_ex1))
505 return expair(e, _ex1);
506 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
511 expair add::combine_pair_with_coeff_to_pair(const expair & p,
514 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
515 GINAC_ASSERT(is_exactly_a<numeric>(c));
517 if (is_exactly_a<numeric>(p.rest)) {
518 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(_num1)); // should be normalized
519 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
522 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
525 ex add::recombine_pair_to_ex(const expair & p) const
527 if (ex_to<numeric>(p.coeff).is_equal(_num1))
530 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
533 ex add::expand(unsigned options) const
535 std::auto_ptr<epvector> vp = expandchildren(options);
537 // the terms have not changed, so it is safe to declare this expanded
538 return (options == 0) ? setflag(status_flags::expanded) : *this;
541 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));