3 * Implementation of GiNaC's sums of expressions. */
6 * GiNaC Copyright (C) 1999-2010 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
26 #include "operators.h"
39 GINAC_IMPLEMENT_REGISTERED_CLASS_OPT(add, expairseq,
40 print_func<print_context>(&add::do_print).
41 print_func<print_latex>(&add::do_print_latex).
42 print_func<print_csrc>(&add::do_print_csrc).
43 print_func<print_tree>(&add::do_print_tree).
44 print_func<print_python_repr>(&add::do_print_python_repr))
47 // default constructor
60 add::add(const ex & lh, const ex & rh)
63 construct_from_2_ex(lh,rh);
64 GINAC_ASSERT(is_canonical());
67 add::add(const exvector & v)
70 construct_from_exvector(v);
71 GINAC_ASSERT(is_canonical());
74 add::add(const epvector & v)
77 construct_from_epvector(v);
78 GINAC_ASSERT(is_canonical());
81 add::add(const epvector & v, const ex & oc)
84 construct_from_epvector(v);
85 GINAC_ASSERT(is_canonical());
88 add::add(std::auto_ptr<epvector> vp, const ex & oc)
90 GINAC_ASSERT(vp.get()!=0);
92 construct_from_epvector(*vp);
93 GINAC_ASSERT(is_canonical());
100 GINAC_BIND_UNARCHIVER(add);
103 // functions overriding virtual functions from base classes
108 void add::print_add(const print_context & c, const char *openbrace, const char *closebrace, const char *mul_sym, unsigned level) const
110 if (precedence() <= level)
111 c.s << openbrace << '(';
116 // First print the overall numeric coefficient, if present
117 if (!overall_coeff.is_zero()) {
118 overall_coeff.print(c, 0);
122 // Then proceed with the remaining factors
123 epvector::const_iterator it = seq.begin(), itend = seq.end();
124 while (it != itend) {
125 coeff = ex_to<numeric>(it->coeff);
127 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
129 if (coeff.csgn() == -1) c.s << '-';
132 if (!coeff.is_equal(*_num1_p) &&
133 !coeff.is_equal(*_num_1_p)) {
134 if (coeff.is_rational()) {
135 if (coeff.is_negative())
140 if (coeff.csgn() == -1)
141 (-coeff).print(c, precedence());
143 coeff.print(c, precedence());
147 it->rest.print(c, precedence());
151 if (precedence() <= level)
152 c.s << ')' << closebrace;
155 void add::do_print(const print_context & c, unsigned level) const
157 print_add(c, "", "", "*", level);
160 void add::do_print_latex(const print_latex & c, unsigned level) const
162 print_add(c, "{", "}", " ", level);
165 void add::do_print_csrc(const print_csrc & c, unsigned level) const
167 if (precedence() <= level)
170 // Print arguments, separated by "+" or "-"
171 epvector::const_iterator it = seq.begin(), itend = seq.end();
172 char separator = ' ';
173 while (it != itend) {
175 // If the coefficient is negative, separator is "-"
176 if (it->coeff.is_equal(_ex_1) ||
177 ex_to<numeric>(it->coeff).numer().is_equal(*_num_1_p))
180 if (it->coeff.is_equal(_ex1) || it->coeff.is_equal(_ex_1)) {
181 it->rest.print(c, precedence());
182 } else if (ex_to<numeric>(it->coeff).numer().is_equal(*_num1_p) ||
183 ex_to<numeric>(it->coeff).numer().is_equal(*_num_1_p))
185 it->rest.print(c, precedence());
187 ex_to<numeric>(it->coeff).denom().print(c, precedence());
189 it->coeff.print(c, precedence());
191 it->rest.print(c, precedence());
198 if (!overall_coeff.is_zero()) {
199 if (overall_coeff.info(info_flags::positive)
200 || is_a<print_csrc_cl_N>(c) || !overall_coeff.info(info_flags::real)) // sign inside ctor argument
202 overall_coeff.print(c, precedence());
205 if (precedence() <= level)
209 void add::do_print_python_repr(const print_python_repr & c, unsigned level) const
211 c.s << class_name() << '(';
213 for (size_t i=1; i<nops(); ++i) {
220 bool add::info(unsigned inf) const
223 case info_flags::polynomial:
224 case info_flags::integer_polynomial:
225 case info_flags::cinteger_polynomial:
226 case info_flags::rational_polynomial:
227 case info_flags::real:
228 case info_flags::rational:
229 case info_flags::integer:
230 case info_flags::crational:
231 case info_flags::cinteger:
232 case info_flags::positive:
233 case info_flags::nonnegative:
234 case info_flags::posint:
235 case info_flags::nonnegint:
236 case info_flags::even:
237 case info_flags::crational_polynomial:
238 case info_flags::rational_function: {
239 epvector::const_iterator i = seq.begin(), end = seq.end();
241 if (!(recombine_pair_to_ex(*i).info(inf)))
245 if (overall_coeff.is_zero() && (inf == info_flags::positive || inf == info_flags::posint))
247 return overall_coeff.info(inf);
249 case info_flags::algebraic: {
250 epvector::const_iterator i = seq.begin(), end = seq.end();
252 if ((recombine_pair_to_ex(*i).info(inf)))
259 return inherited::info(inf);
262 int add::degree(const ex & s) const
264 int deg = std::numeric_limits<int>::min();
265 if (!overall_coeff.is_zero())
268 // Find maximum of degrees of individual terms
269 epvector::const_iterator i = seq.begin(), end = seq.end();
271 int cur_deg = i->rest.degree(s);
279 int add::ldegree(const ex & s) const
281 int deg = std::numeric_limits<int>::max();
282 if (!overall_coeff.is_zero())
285 // Find minimum of degrees of individual terms
286 epvector::const_iterator i = seq.begin(), end = seq.end();
288 int cur_deg = i->rest.ldegree(s);
296 ex add::coeff(const ex & s, int n) const
298 std::auto_ptr<epvector> coeffseq(new epvector);
299 std::auto_ptr<epvector> coeffseq_cliff(new epvector);
300 char rl = clifford_max_label(s);
301 bool do_clifford = (rl != -1);
302 bool nonscalar = false;
304 // Calculate sum of coefficients in each term
305 epvector::const_iterator i = seq.begin(), end = seq.end();
307 ex restcoeff = i->rest.coeff(s, n);
308 if (!restcoeff.is_zero()) {
310 if (clifford_max_label(restcoeff) == -1) {
311 coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(ncmul(restcoeff, dirac_ONE(rl)), i->coeff));
313 coeffseq_cliff->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
317 coeffseq->push_back(combine_ex_with_coeff_to_pair(restcoeff, i->coeff));
322 return (new add(nonscalar ? coeffseq_cliff : coeffseq,
323 n==0 ? overall_coeff : _ex0))->setflag(status_flags::dynallocated);
326 /** Perform automatic term rewriting rules in this class. In the following
327 * x stands for a symbolic variables of type ex and c stands for such
328 * an expression that contain a plain number.
332 * @param level cut-off in recursive evaluation */
333 ex add::eval(int level) const
335 std::auto_ptr<epvector> evaled_seqp = evalchildren(level);
336 if (evaled_seqp.get()) {
337 // do more evaluation later
338 return (new add(evaled_seqp, overall_coeff))->
339 setflag(status_flags::dynallocated);
342 #ifdef DO_GINAC_ASSERT
343 epvector::const_iterator i = seq.begin(), end = seq.end();
345 GINAC_ASSERT(!is_exactly_a<add>(i->rest));
346 if (is_exactly_a<numeric>(i->rest))
348 GINAC_ASSERT(!is_exactly_a<numeric>(i->rest));
351 #endif // def DO_GINAC_ASSERT
353 if (flags & status_flags::evaluated) {
354 GINAC_ASSERT(seq.size()>0);
355 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
359 int seq_size = seq.size();
362 return overall_coeff;
363 } else if (seq_size == 1 && overall_coeff.is_zero()) {
365 return recombine_pair_to_ex(*(seq.begin()));
366 } else if (!overall_coeff.is_zero() && seq[0].rest.return_type() != return_types::commutative) {
367 throw (std::logic_error("add::eval(): sum of non-commutative objects has non-zero numeric term"));
372 ex add::evalm() const
374 // Evaluate children first and add up all matrices. Stop if there's one
375 // term that is not a matrix.
376 std::auto_ptr<epvector> s(new epvector);
377 s->reserve(seq.size());
379 bool all_matrices = true;
380 bool first_term = true;
383 epvector::const_iterator it = seq.begin(), itend = seq.end();
384 while (it != itend) {
385 const ex &m = recombine_pair_to_ex(*it).evalm();
386 s->push_back(split_ex_to_pair(m));
387 if (is_a<matrix>(m)) {
389 sum = ex_to<matrix>(m);
392 sum = sum.add(ex_to<matrix>(m));
394 all_matrices = false;
399 return sum + overall_coeff;
401 return (new add(s, overall_coeff))->setflag(status_flags::dynallocated);
404 ex add::conjugate() const
407 for (size_t i=0; i<nops(); ++i) {
409 v->push_back(op(i).conjugate());
413 ex ccterm = term.conjugate();
414 if (are_ex_trivially_equal(term, ccterm))
418 for (size_t j=0; j<i; ++j)
420 v->push_back(ccterm);
430 ex add::real_part() const
433 v.reserve(seq.size());
434 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i)
435 if ((i->coeff).info(info_flags::real)) {
436 ex rp = (i->rest).real_part();
438 v.push_back(expair(rp, i->coeff));
440 ex rp=recombine_pair_to_ex(*i).real_part();
442 v.push_back(split_ex_to_pair(rp));
444 return (new add(v, overall_coeff.real_part()))
445 -> setflag(status_flags::dynallocated);
448 ex add::imag_part() const
451 v.reserve(seq.size());
452 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i)
453 if ((i->coeff).info(info_flags::real)) {
454 ex ip = (i->rest).imag_part();
456 v.push_back(expair(ip, i->coeff));
458 ex ip=recombine_pair_to_ex(*i).imag_part();
460 v.push_back(split_ex_to_pair(ip));
462 return (new add(v, overall_coeff.imag_part()))
463 -> setflag(status_flags::dynallocated);
466 ex add::eval_ncmul(const exvector & v) const
469 return inherited::eval_ncmul(v);
471 return seq.begin()->rest.eval_ncmul(v);
476 /** Implementation of ex::diff() for a sum. It differentiates each term.
478 ex add::derivative(const symbol & y) const
480 std::auto_ptr<epvector> s(new epvector);
481 s->reserve(seq.size());
483 // Only differentiate the "rest" parts of the expairs. This is faster
484 // than the default implementation in basic::derivative() although
485 // if performs the same function (differentiate each term).
486 epvector::const_iterator i = seq.begin(), end = seq.end();
488 s->push_back(combine_ex_with_coeff_to_pair(i->rest.diff(y), i->coeff));
491 return (new add(s, _ex0))->setflag(status_flags::dynallocated);
494 int add::compare_same_type(const basic & other) const
496 return inherited::compare_same_type(other);
499 unsigned add::return_type() const
502 return return_types::commutative;
504 return seq.begin()->rest.return_type();
507 return_type_t add::return_type_tinfo() const
510 return make_return_type_t<add>();
512 return seq.begin()->rest.return_type_tinfo();
515 // Note: do_index_renaming is ignored because it makes no sense for an add.
516 ex add::thisexpairseq(const epvector & v, const ex & oc, bool do_index_renaming) const
518 return (new add(v,oc))->setflag(status_flags::dynallocated);
521 // Note: do_index_renaming is ignored because it makes no sense for an add.
522 ex add::thisexpairseq(std::auto_ptr<epvector> vp, const ex & oc, bool do_index_renaming) const
524 return (new add(vp,oc))->setflag(status_flags::dynallocated);
527 expair add::split_ex_to_pair(const ex & e) const
529 if (is_exactly_a<mul>(e)) {
530 const mul &mulref(ex_to<mul>(e));
531 const ex &numfactor = mulref.overall_coeff;
532 mul *mulcopyp = new mul(mulref);
533 mulcopyp->overall_coeff = _ex1;
534 mulcopyp->clearflag(status_flags::evaluated);
535 mulcopyp->clearflag(status_flags::hash_calculated);
536 mulcopyp->setflag(status_flags::dynallocated);
537 return expair(*mulcopyp,numfactor);
539 return expair(e,_ex1);
542 expair add::combine_ex_with_coeff_to_pair(const ex & e,
545 GINAC_ASSERT(is_exactly_a<numeric>(c));
546 if (is_exactly_a<mul>(e)) {
547 const mul &mulref(ex_to<mul>(e));
548 const ex &numfactor = mulref.overall_coeff;
549 mul *mulcopyp = new mul(mulref);
550 mulcopyp->overall_coeff = _ex1;
551 mulcopyp->clearflag(status_flags::evaluated);
552 mulcopyp->clearflag(status_flags::hash_calculated);
553 mulcopyp->setflag(status_flags::dynallocated);
554 if (c.is_equal(_ex1))
555 return expair(*mulcopyp, numfactor);
556 else if (numfactor.is_equal(_ex1))
557 return expair(*mulcopyp, c);
559 return expair(*mulcopyp, ex_to<numeric>(numfactor).mul_dyn(ex_to<numeric>(c)));
560 } else if (is_exactly_a<numeric>(e)) {
561 if (c.is_equal(_ex1))
562 return expair(e, _ex1);
563 return expair(ex_to<numeric>(e).mul_dyn(ex_to<numeric>(c)), _ex1);
568 expair add::combine_pair_with_coeff_to_pair(const expair & p,
571 GINAC_ASSERT(is_exactly_a<numeric>(p.coeff));
572 GINAC_ASSERT(is_exactly_a<numeric>(c));
574 if (is_exactly_a<numeric>(p.rest)) {
575 GINAC_ASSERT(ex_to<numeric>(p.coeff).is_equal(*_num1_p)); // should be normalized
576 return expair(ex_to<numeric>(p.rest).mul_dyn(ex_to<numeric>(c)),_ex1);
579 return expair(p.rest,ex_to<numeric>(p.coeff).mul_dyn(ex_to<numeric>(c)));
582 ex add::recombine_pair_to_ex(const expair & p) const
584 if (ex_to<numeric>(p.coeff).is_equal(*_num1_p))
587 return (new mul(p.rest,p.coeff))->setflag(status_flags::dynallocated);
590 ex add::expand(unsigned options) const
592 std::auto_ptr<epvector> vp = expandchildren(options);
594 // the terms have not changed, so it is safe to declare this expanded
595 return (options == 0) ? setflag(status_flags::expanded) : *this;
598 return (new add(vp, overall_coeff))->setflag(status_flags::dynallocated | (options == 0 ? status_flags::expanded : 0));