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 constructor, destructor, copy constructor assignment operator and helpers
44 debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
45 tinfo_key = TINFO_add;
50 /** For use by copy ctor and assignment operator. */
51 void add::copy(const add & other)
53 inherited::copy(other);
56 void add::destroy(bool call_parent)
58 if (call_parent) inherited::destroy(call_parent);
67 add::add(const ex & lh, const ex & rh)
69 debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
70 tinfo_key = TINFO_add;
71 overall_coeff = _ex0();
72 construct_from_2_ex(lh,rh);
73 GINAC_ASSERT(is_canonical());
76 add::add(const exvector & v)
78 debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
79 tinfo_key = TINFO_add;
80 overall_coeff = _ex0();
81 construct_from_exvector(v);
82 GINAC_ASSERT(is_canonical());
85 add::add(const epvector & v)
87 debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
88 tinfo_key = TINFO_add;
89 overall_coeff = _ex0();
90 construct_from_epvector(v);
91 GINAC_ASSERT(is_canonical());
94 add::add(const epvector & v, const ex & oc)
96 debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
97 tinfo_key = TINFO_add;
99 construct_from_epvector(v);
100 GINAC_ASSERT(is_canonical());
103 add::add(epvector * vp, const ex & oc)
105 debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
106 tinfo_key = TINFO_add;
109 construct_from_epvector(*vp);
111 GINAC_ASSERT(is_canonical());
118 /** Construct object from archive_node. */
119 add::add(const archive_node &n, const lst &sym_lst) : inherited(n, sym_lst)
121 debugmsg("add constructor from archive_node", LOGLEVEL_CONSTRUCT);
124 /** Unarchive the object. */
125 ex add::unarchive(const archive_node &n, const lst &sym_lst)
127 return (new add(n, sym_lst))->setflag(status_flags::dynallocated);
130 /** Archive the object. */
131 void add::archive(archive_node &n) const
133 inherited::archive(n);
137 // functions overriding virtual functions from bases classes
142 void add::print(std::ostream & os, unsigned upper_precedence) const
144 debugmsg("add print",LOGLEVEL_PRINT);
145 if (precedence<=upper_precedence) os << "(";
148 // first print the overall numeric coefficient, if present:
149 if (!overall_coeff.is_zero()) {
153 // then proceed with the remaining factors:
154 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
155 coeff = ex_to_numeric(cit->coeff);
157 if (coeff.csgn()==-1) os << '-'; else os << '+';
159 if (coeff.csgn()==-1) os << '-';
162 if (!coeff.is_equal(_num1()) &&
163 !coeff.is_equal(_num_1())) {
164 if (coeff.is_rational()) {
165 if (coeff.is_negative())
170 if (coeff.csgn()==-1)
171 (-coeff).print(os, precedence);
173 coeff.print(os, precedence);
177 cit->rest.print(os, precedence);
179 if (precedence<=upper_precedence) os << ")";
182 void add::printraw(std::ostream & os) const
184 debugmsg("add printraw",LOGLEVEL_PRINT);
187 for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
189 (*it).rest.bp->printraw(os);
191 (*it).coeff.bp->printraw(os);
194 os << ",hash=" << hashvalue << ",flags=" << flags;
198 void add::printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence) const
200 debugmsg("add print csrc", LOGLEVEL_PRINT);
201 if (precedence <= upper_precedence)
204 // Print arguments, separated by "+"
205 epvector::const_iterator it = seq.begin();
206 epvector::const_iterator itend = seq.end();
207 while (it != itend) {
209 // If the coefficient is -1, it is replaced by a single minus sign
210 if (it->coeff.compare(_num1()) == 0) {
211 it->rest.bp->printcsrc(os, type, precedence);
212 } else if (it->coeff.compare(_num_1()) == 0) {
214 it->rest.bp->printcsrc(os, type, precedence);
215 } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
216 it->rest.bp->printcsrc(os, type, precedence);
218 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
219 } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
221 it->rest.bp->printcsrc(os, type, precedence);
223 ex_to_numeric(it->coeff).denom().printcsrc(os, type, precedence);
225 it->coeff.bp->printcsrc(os, type, precedence);
227 it->rest.bp->printcsrc(os, type, precedence);
230 // Separator is "+", except if the following expression would have a leading minus sign
232 if (it != itend && !(it->coeff.compare(_num0()) < 0 || (it->coeff.compare(_num1()) == 0 && is_ex_exactly_of_type(it->rest, numeric) && it->rest.compare(_num0()) < 0)))
236 if (!overall_coeff.is_zero()) {
237 if (overall_coeff.info(info_flags::positive)) os << '+';
238 overall_coeff.bp->printcsrc(os,type,precedence);
241 if (precedence <= upper_precedence)
245 bool add::info(unsigned inf) const
248 case info_flags::polynomial:
249 case info_flags::integer_polynomial:
250 case info_flags::cinteger_polynomial:
251 case info_flags::rational_polynomial:
252 case info_flags::crational_polynomial:
253 case info_flags::rational_function: {
254 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
255 if (!(recombine_pair_to_ex(*i).info(inf)))
258 return overall_coeff.info(inf);
260 case info_flags::algebraic: {
261 for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
262 if ((recombine_pair_to_ex(*i).info(inf)))
268 return inherited::info(inf);
271 int add::degree(const symbol & s) const
274 if (!overall_coeff.is_equal(_ex0()))
278 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
279 cur_deg = (*cit).rest.degree(s);
286 int add::ldegree(const symbol & s) const
289 if (!overall_coeff.is_equal(_ex0()))
293 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
294 cur_deg = (*cit).rest.ldegree(s);
295 if (cur_deg<deg) deg=cur_deg;
300 ex add::coeff(const symbol & s, int n) const
303 coeffseq.reserve(seq.size());
305 epvector::const_iterator it=seq.begin();
306 while (it!=seq.end()) {
307 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
312 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
314 return (new add(coeffseq))->setflag(status_flags::dynallocated);
317 ex add::eval(int level) const
319 // simplifications: +(;c) -> c
322 debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
324 epvector * evaled_seqp = evalchildren(level);
325 if (evaled_seqp!=0) {
326 // do more evaluation later
327 return (new add(evaled_seqp,overall_coeff))->
328 setflag(status_flags::dynallocated);
331 #ifdef DO_GINAC_ASSERT
332 for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
333 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
334 if (is_ex_exactly_of_type((*cit).rest,numeric))
336 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
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_equal(_ex0())) {
352 return recombine_pair_to_ex(*(seq.begin()));
357 exvector add::get_indices(void) const
359 // FIXME: all terms in the sum should have the same indices (compatible
360 // tensors) however this is not checked, since there is no function yet
361 // which compares indices (idxvector can be unsorted)
365 return (seq.begin())->rest.get_indices();
368 ex add::simplify_ncmul(const exvector & v) const
371 return inherited::simplify_ncmul(v);
373 return (*seq.begin()).rest.simplify_ncmul(v);
378 /** Implementation of ex::diff() for a sum. It differentiates each term.
380 ex add::derivative(const symbol & s) const
382 // D(a+b+c)=D(a)+D(b)+D(c)
383 return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
386 int add::compare_same_type(const basic & other) const
388 return inherited::compare_same_type(other);
391 bool add::is_equal_same_type(const basic & other) const
393 return inherited::is_equal_same_type(other);
396 unsigned add::return_type(void) const
399 return return_types::commutative;
401 return (*seq.begin()).rest.return_type();
404 unsigned add::return_type_tinfo(void) const
409 return (*seq.begin()).rest.return_type_tinfo();
412 ex add::thisexpairseq(const epvector & v, const ex & oc) const
414 return (new add(v,oc))->setflag(status_flags::dynallocated);
417 ex add::thisexpairseq(epvector * vp, const ex & oc) const
419 return (new add(vp,oc))->setflag(status_flags::dynallocated);
422 expair add::split_ex_to_pair(const ex & e) const
424 if (is_ex_exactly_of_type(e,mul)) {
425 const mul &mulref = ex_to_mul(e);
426 ex numfactor = mulref.overall_coeff;
427 mul *mulcopyp = new mul(mulref);
428 mulcopyp->overall_coeff = _ex1();
429 mulcopyp->clearflag(status_flags::evaluated);
430 mulcopyp->clearflag(status_flags::hash_calculated);
431 mulcopyp->setflag(status_flags::dynallocated);
432 return expair(*mulcopyp,numfactor);
434 return expair(e,_ex1());
437 expair add::combine_ex_with_coeff_to_pair(const ex & e,
440 GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
441 if (is_ex_exactly_of_type(e, mul)) {
442 const mul &mulref = ex_to_mul(e);
443 ex numfactor = mulref.overall_coeff;
444 mul *mulcopyp = new mul(mulref);
445 mulcopyp->overall_coeff = _ex1();
446 mulcopyp->clearflag(status_flags::evaluated);
447 mulcopyp->clearflag(status_flags::hash_calculated);
448 mulcopyp->setflag(status_flags::dynallocated);
449 if (are_ex_trivially_equal(c, _ex1()))
450 return expair(*mulcopyp, numfactor);
451 else if (are_ex_trivially_equal(numfactor, _ex1()))
452 return expair(*mulcopyp, c);
454 return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
455 } else if (is_ex_exactly_of_type(e, numeric)) {
456 if (are_ex_trivially_equal(c, _ex1()))
457 return expair(e, _ex1());
458 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
463 expair add::combine_pair_with_coeff_to_pair(const expair & p,
466 GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
467 GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
469 if (is_ex_exactly_of_type(p.rest,numeric)) {
470 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
471 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
474 return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
477 ex add::recombine_pair_to_ex(const expair & p) const
479 if (ex_to_numeric(p.coeff).is_equal(_num1()))
482 return p.rest*p.coeff;
485 ex add::expand(unsigned options) const
487 if (flags & status_flags::expanded)
490 epvector * vp = expandchildren(options);
492 // the terms have not changed, so it is safe to declare this expanded
493 setflag(status_flags::expanded);
497 return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
501 // static member variables
506 unsigned add::precedence = 40;