3 * Implementation of GiNaC's sums of expressions. */
6 * GiNaC Copyright (C) 1999-2005 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
29 #include "operators.h"
37 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(add, expairseq,
38 print_func<print_context>(&add::do_print).
39 print_func<print_latex>(&add::do_print_latex).
40 print_func<print_csrc>(&add::do_print_csrc).
41 print_func<print_tree>(&add::do_print_tree).
42 print_func<print_python_repr>(&add::do_print_python_repr))
45 // default constructor
50 tinfo_key = TINFO_add;
59 add::add(const ex & lh, const ex & rh)
61 tinfo_key = TINFO_add;
63 construct_from_2_ex(lh,rh);
64 GINAC_ASSERT(is_canonical());
67 add::add(const exvector & v)
69 tinfo_key = TINFO_add;
71 construct_from_exvector(v);
72 GINAC_ASSERT(is_canonical());
75 add::add(const epvector & v)
77 tinfo_key = TINFO_add;
79 construct_from_epvector(v);
80 GINAC_ASSERT(is_canonical());
83 add::add(const epvector & v, const ex & oc)
85 tinfo_key = TINFO_add;
87 construct_from_epvector(v);
88 GINAC_ASSERT(is_canonical());
91 add::add(std::auto_ptr<epvector> vp, const ex & oc)
93 tinfo_key = TINFO_add;
94 GINAC_ASSERT(vp.get()!=0);
96 construct_from_epvector(*vp);
97 GINAC_ASSERT(is_canonical());
104 DEFAULT_ARCHIVING(add)
107 // functions overriding virtual functions from base classes
112 void add::print_add(const print_context & c, const char *openbrace, const char *closebrace, const char *mul_sym, unsigned level) const
114 if (precedence() <= level)
115 c.s << openbrace << '(';
120 // First print the overall numeric coefficient, if present
121 if (!overall_coeff.is_zero()) {
122 overall_coeff.print(c, 0);
126 // Then proceed with the remaining factors
127 epvector::const_iterator it = seq.begin(), itend = seq.end();
128 while (it != itend) {
129 coeff = ex_to<numeric>(it->coeff);
131 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
133 if (coeff.csgn() == -1) c.s << '-';
136 if (!coeff.is_equal(*_num1_p) &&
137 !coeff.is_equal(*_num_1_p)) {
138 if (coeff.is_rational()) {
139 if (coeff.is_negative())
144 if (coeff.csgn() == -1)
145 (-coeff).print(c, precedence());
147 coeff.print(c, precedence());
151 it->rest.print(c, precedence());
155 if (precedence() <= level)
156 c.s << ')' << closebrace;
159 void add::do_print(const print_context & c, unsigned level) const
161 print_add(c, "", "", "*", level);
164 void add::do_print_latex(const print_latex & c, unsigned level) const
166 print_add(c, "{", "}", " ", level);
169 void add::do_print_csrc(const print_csrc & c, unsigned level) const
171 if (precedence() <= level)
174 // Print arguments, separated by "+"
175 epvector::const_iterator it = seq.begin(), itend = seq.end();
176 while (it != itend) {
178 // If the coefficient is -1, it is replaced by a single minus sign
179 if (it->coeff.is_equal(_ex1)) {
180 it->rest.print(c, precedence());
181 } else if (it->coeff.is_equal(_ex_1)) {
183 it->rest.print(c, precedence());
184 } else if (ex_to<numeric>(it->coeff).numer().is_equal(*_num1_p)) {
185 it->rest.print(c, precedence());
187 ex_to<numeric>(it->coeff).denom().print(c, precedence());
188 } else if (ex_to<numeric>(it->coeff).numer().is_equal(*_num_1_p)) {
190 it->rest.print(c, precedence());
192 ex_to<numeric>(it->coeff).denom().print(c, precedence());
194 it->coeff.print(c, precedence());
196 it->rest.print(c, precedence());
199 // Separator is "+", except if the following expression would have a leading minus sign or the sign is sitting in parenthesis (as in a ctor)
202 && (is_a<print_csrc_cl_N>(c) || !it->coeff.info(info_flags::real) // sign inside ctor arguments
203 || !(it->coeff.info(info_flags::negative) || (it->coeff.is_equal(*_num1_p) && is_exactly_a<numeric>(it->rest) && it->rest.info(info_flags::negative)))))
207 if (!overall_coeff.is_zero()) {
208 if (overall_coeff.info(info_flags::positive)
209 || is_a<print_csrc_cl_N>(c) || !overall_coeff.info(info_flags::real)) // sign inside ctor argument
211 overall_coeff.print(c, precedence());
214 if (precedence() <= level)
218 void add::do_print_python_repr(const print_python_repr & c, unsigned level) const
220 c.s << class_name() << '(';
222 for (size_t i=1; i<nops(); ++i) {
229 bool add::info(unsigned inf) const
232 case info_flags::polynomial:
233 case info_flags::integer_polynomial:
234 case info_flags::cinteger_polynomial:
235 case info_flags::rational_polynomial:
236 case info_flags::crational_polynomial:
237 case info_flags::rational_function: {
238 epvector::const_iterator i = seq.begin(), end = seq.end();
240 if (!(recombine_pair_to_ex(*i).info(inf)))
244 return overall_coeff.info(inf);
246 case info_flags::algebraic: {
247 epvector::const_iterator i = seq.begin(), end = seq.end();
249 if ((recombine_pair_to_ex(*i).info(inf)))
256 return inherited::info(inf);
259 int add::degree(const ex & s) const
262 if (!overall_coeff.is_zero())
265 // Find maximum of degrees of individual terms
266 epvector::const_iterator i = seq.begin(), end = seq.end();
268 int cur_deg = i->rest.degree(s);
276 int add::ldegree(const ex & s) const
279 if (!overall_coeff.is_zero())
282 // Find minimum of degrees of individual terms
283 epvector::const_iterator i = seq.begin(), end = seq.end();
285 int cur_deg = i->rest.ldegree(s);
293 ex add::coeff(const ex & s, int n) const
295 std::auto_ptr<epvector> coeffseq(new epvector);
296 std::auto_ptr<epvector> coeffseq_cliff(new epvector);
297 char rl = clifford_max_label(s);
298 bool do_clifford = (rl != -1);
299 bool nonscalar = false;
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()) {
307 if (clifford_max_label(restcoeff) == -1) {
308 coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(ncmul(restcoeff, dirac_ONE(rl)), i->coeff));
310 coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
314 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
319 return (new add(nonscalar ? coeffseq_cliff : coeffseq,
320 n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
323 /** Perform automatic term rewriting rules in this class. In the following
324 * x stands for a symbolic variables of type ex and c stands for such
325 * an expression that contain a plain number.
329 * @param level cut-off in recursive evaluation */
330 ex add::eval(int level) const
332 std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
333 if (evaled_seqp.get()) {
334 // do more evaluation later
335 return (new add(evaled_seqp, overall_coeff))->
336 setflag(status_flags::dynallocated);
339 #ifdef DO_GINAC_ASSERT
340 epvector::const_iterator i = seq.begin(), end = seq.end();
342 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
343 if (is_exactly_a<numeric>(i->rest))
345 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
348 #endif // def DO_GINAC_ASSERT
350 if (flags & status_flags::evaluated) {
351 GINAC_ASSERT(seq.size()>0);
352 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
356 int seq_size = seq.size();
359 return overall_coeff;
360 } else if (seq_size == 1 && overall_coeff.is_zero()) {
362 return recombine_pair_to_ex(*(seq.begin()));
363 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
364 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
369 ex add::evalm() const
371 // Evaluate children first and add up all matrices. Stop if there's one
372 // term that is not a matrix.
373 std::auto_ptr<epvector> s(new epvector);
374 s->reserve(seq.size());
376 bool all_matrices = true;
377 bool first_term = true;
380 epvector::const_iterator it = seq.begin(), itend = seq.end();
381 while (it != itend) {
382 const ex &m = recombine_pair_to_ex(*it).evalm();
383 s->push_back(split_ex_to_pair(m));
384 if (is_a<matrix>(m)) {
386 sum = ex_to<matrix>(m);
389 sum = sum.add(ex_to<matrix>(m));
391 all_matrices = false;
396 return sum + overall_coeff;
398 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
401 ex add::conjugate() const
404 for (size_t i=0; i<nops(); ++i) {
406 v->push_back(op(i).conjugate());
410 ex ccterm = term.conjugate();
411 if (are_ex_trivially_equal(term, ccterm))
415 for (size_t j=0; j<i; ++j)
417 v->push_back(ccterm);
427 ex add::eval_ncmul(const exvector & v) const
430 return inherited::eval_ncmul(v);
432 return seq.begin()->rest.eval_ncmul(v);
437 /** Implementation of ex::diff() for a sum. It differentiates each term.
439 ex add::derivative(const symbol & y) const
441 std::auto_ptr<epvector> s(new epvector);
442 s->reserve(seq.size());
444 // Only differentiate the "rest" parts of the expairs. This is faster
445 // than the default implementation in basic::derivative() although
446 // if performs the same function (differentiate each term).
447 epvector::const_iterator i = seq.begin(), end = seq.end();
449 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
452 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
455 int add::compare_same_type(const basic & other) const
457 return inherited::compare_same_type(other);
460 unsigned add::return_type() const
463 return return_types::commutative;
465 return seq.begin()->rest.return_type();
468 unsigned add::return_type_tinfo() const
473 return seq.begin()->rest.return_type_tinfo();
476 ex add::thisexpairseq(const epvector & v, const ex & oc) const
478 return (new add(v,oc))->setflag(status_flags::dynallocated);
481 ex add::thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc) const
483 return (new add(vp,oc))->setflag(status_flags::dynallocated);
486 expair add::split_ex_to_pair(const ex & e) const
488 if (is_exactly_a<mul>(e)) {
489 const mul &mulref(ex_to<mul>(e));
490 const ex &numfactor = mulref.overall_coeff;
491 mul *mulcopyp = new mul(mulref);
492 mulcopyp->overall_coeff = _ex1;
493 mulcopyp->clearflag(status_flags::evaluated);
494 mulcopyp->clearflag(status_flags::hash_calculated);
495 mulcopyp->setflag(status_flags::dynallocated);
496 return expair(*mulcopyp,numfactor);
498 return expair(e,_ex1);
501 expair add::combine_ex_with_coeff_to_pair(const ex & e,
504 GINAC_ASSERT(is_exactly_a<numeric>(c));
505 if (is_exactly_a<mul>(e)) {
506 const mul &mulref(ex_to<mul>(e));
507 const ex &numfactor = mulref.overall_coeff;
508 mul *mulcopyp = new mul(mulref);
509 mulcopyp->overall_coeff = _ex1;
510 mulcopyp->clearflag(status_flags::evaluated);
511 mulcopyp->clearflag(status_flags::hash_calculated);
512 mulcopyp->setflag(status_flags::dynallocated);
513 if (c.is_equal(_ex1))
514 return expair(*mulcopyp, numfactor);
515 else if (numfactor.is_equal(_ex1))
516 return expair(*mulcopyp, c);
518 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
519 } else if (is_exactly_a<numeric>(e)) {
520 if (c.is_equal(_ex1))
521 return expair(e, _ex1);
522 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
527 expair add::combine_pair_with_coeff_to_pair(const expair & p,
530 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
531 GINAC_ASSERT(is_exactly_a<numeric>(c));
533 if (is_exactly_a<numeric>(p.rest)) {
534 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(*_num1_p)); // should be normalized
535 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
538 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
541 ex add::recombine_pair_to_ex(const expair & p) const
543 if (ex_to<numeric>(p.coeff).is_equal(*_num1_p))
546 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
549 ex add::expand(unsigned options) const
551 std::auto_ptr<epvector> vp = expandchildren(options);
553 // the terms have not changed, so it is safe to declare this expanded
554 return (options == 0) ? setflag(status_flags::expanded) : *this;
557 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));