replaced "precedence" static member variable by virtual precedence() function
[ginac.git] / ginac / add.cpp
1 /** @file add.cpp
2  *
3  *  Implementation of GiNaC's sums of expressions. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 Johannes Gutenberg University Mainz, Germany
7  *
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.
12  *
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.
17  *
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
21  */
22
23 #include <iostream>
24 #include <stdexcept>
25
26 #include "add.h"
27 #include "mul.h"
28 #include "archive.h"
29 #include "debugmsg.h"
30 #include "utils.h"
31
32 namespace GiNaC {
33
34 GINAC_IMPLEMENT_REGISTERED_CLASS(add, expairseq)
35
36 //////////
37 // default constructor, destructor, copy constructor assignment operator and helpers
38 //////////
39
40 add::add()
41 {
42         debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
43         tinfo_key = TINFO_add;
44 }
45
46 DEFAULT_COPY(add)
47 DEFAULT_DESTROY(add)
48
49 //////////
50 // other constructors
51 //////////
52
53 // public
54
55 add::add(const ex & lh, const ex & rh)
56 {
57         debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
58         tinfo_key = TINFO_add;
59         overall_coeff = _ex0();
60         construct_from_2_ex(lh,rh);
61         GINAC_ASSERT(is_canonical());
62 }
63
64 add::add(const exvector & v)
65 {
66         debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
67         tinfo_key = TINFO_add;
68         overall_coeff = _ex0();
69         construct_from_exvector(v);
70         GINAC_ASSERT(is_canonical());
71 }
72
73 add::add(const epvector & v)
74 {
75         debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
76         tinfo_key = TINFO_add;
77         overall_coeff = _ex0();
78         construct_from_epvector(v);
79         GINAC_ASSERT(is_canonical());
80 }
81
82 add::add(const epvector & v, const ex & oc)
83 {
84         debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
85         tinfo_key = TINFO_add;
86         overall_coeff = oc;
87         construct_from_epvector(v);
88         GINAC_ASSERT(is_canonical());
89 }
90
91 add::add(epvector * vp, const ex & oc)
92 {
93         debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
94         tinfo_key = TINFO_add;
95         GINAC_ASSERT(vp!=0);
96         overall_coeff = oc;
97         construct_from_epvector(*vp);
98         delete vp;
99         GINAC_ASSERT(is_canonical());
100 }
101
102 //////////
103 // archiving
104 //////////
105
106 DEFAULT_ARCHIVING(add)
107
108 //////////
109 // functions overriding virtual functions from bases classes
110 //////////
111
112 // public
113
114 void add::print(const print_context & c, unsigned level) const
115 {
116         debugmsg("add print", LOGLEVEL_PRINT);
117
118         if (is_of_type(c, print_tree)) {
119
120                 inherited::print(c, level);
121
122         } else if (is_of_type(c, print_csrc)) {
123
124                 if (precedence() <= level)
125                         c.s << "(";
126         
127                 // Print arguments, separated by "+"
128                 epvector::const_iterator it = seq.begin(), itend = seq.end();
129                 while (it != itend) {
130                 
131                         // If the coefficient is -1, it is replaced by a single minus sign
132                         if (it->coeff.compare(_num1()) == 0) {
133                                 it->rest.bp->print(c, precedence());
134                         } else if (it->coeff.compare(_num_1()) == 0) {
135                                 c.s << "-";
136                                 it->rest.bp->print(c, precedence());
137                         } else if (ex_to_numeric(it->coeff).numer().compare(_num1()) == 0) {
138                                 it->rest.bp->print(c, precedence());
139                                 c.s << "/";
140                                 ex_to_numeric(it->coeff).denom().print(c, precedence());
141                         } else if (ex_to_numeric(it->coeff).numer().compare(_num_1()) == 0) {
142                                 c.s << "-";
143                                 it->rest.bp->print(c, precedence());
144                                 c.s << "/";
145                                 ex_to_numeric(it->coeff).denom().print(c, precedence());
146                         } else {
147                                 it->coeff.bp->print(c, precedence());
148                                 c.s << "*";
149                                 it->rest.bp->print(c, precedence());
150                         }
151                 
152                         // Separator is "+", except if the following expression would have a leading minus sign
153                         it++;
154                         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)))
155                                 c.s << "+";
156                 }
157         
158                 if (!overall_coeff.is_zero()) {
159                         if (overall_coeff.info(info_flags::positive))
160                                 c.s << '+';
161                         overall_coeff.bp->print(c, precedence());
162                 }
163         
164                 if (precedence() <= level)
165                         c.s << ")";
166
167         } else {
168
169                 if (precedence() <= level) {
170                         if (is_of_type(c, print_latex))
171                                 c.s << "{(";
172                         else
173                                 c.s << "(";
174                 }
175
176                 numeric coeff;
177                 bool first = true;
178
179                 // First print the overall numeric coefficient, if present
180                 if (!overall_coeff.is_zero()) {
181                         if (!is_of_type(c, print_tree))
182                                 overall_coeff.print(c, 0);
183                         else
184                                 overall_coeff.print(c, precedence());
185                         first = false;
186                 }
187
188                 // Then proceed with the remaining factors
189                 epvector::const_iterator it = seq.begin(), itend = seq.end();
190                 while (it != itend) {
191                         coeff = ex_to_numeric(it->coeff);
192                         if (!first) {
193                                 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
194                         } else {
195                                 if (coeff.csgn() == -1) c.s << '-';
196                                 first = false;
197                         }
198                         if (!coeff.is_equal(_num1()) &&
199                             !coeff.is_equal(_num_1())) {
200                                 if (coeff.is_rational()) {
201                                         if (coeff.is_negative())
202                                                 (-coeff).print(c);
203                                         else
204                                                 coeff.print(c);
205                                 } else {
206                                         if (coeff.csgn() == -1)
207                                                 (-coeff).print(c, precedence());
208                                         else
209                                                 coeff.print(c, precedence());
210                                 }
211                                 if (is_of_type(c, print_latex))
212                                         c.s << ' ';
213                                 else
214                                         c.s << '*';
215                         }
216                         it->rest.print(c, precedence());
217                         it++;
218                 }
219
220                 if (precedence() <= level) {
221                         if (is_of_type(c, print_latex))
222                                 c.s << ")}";
223                         else
224                                 c.s << ")";
225                 }
226         }
227 }
228
229 bool add::info(unsigned inf) const
230 {
231         switch (inf) {
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                         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
239                                 if (!(recombine_pair_to_ex(*i).info(inf)))
240                                         return false;
241                         }
242                         return overall_coeff.info(inf);
243                 }
244                 case info_flags::algebraic: {
245                         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
246                                 if ((recombine_pair_to_ex(*i).info(inf)))
247                                         return true;
248                         }
249                         return false;
250                 }
251         }
252         return inherited::info(inf);
253 }
254
255 int add::degree(const ex & s) const
256 {
257         int deg = INT_MIN;
258         if (!overall_coeff.is_equal(_ex0()))
259                 deg = 0;
260         
261         int cur_deg;
262         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
263                 cur_deg = (*cit).rest.degree(s);
264                 if (cur_deg>deg)
265                         deg = cur_deg;
266         }
267         return deg;
268 }
269
270 int add::ldegree(const ex & s) const
271 {
272         int deg = INT_MAX;
273         if (!overall_coeff.is_equal(_ex0()))
274                 deg = 0;
275         
276         int cur_deg;
277         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
278                 cur_deg = (*cit).rest.ldegree(s);
279                 if (cur_deg<deg) deg=cur_deg;
280         }
281         return deg;
282 }
283
284 ex add::coeff(const ex & s, int n) const
285 {
286         epvector coeffseq;
287         
288         epvector::const_iterator it=seq.begin();
289         while (it!=seq.end()) {
290                 ex restcoeff = it->rest.coeff(s,n);
291                 if (!restcoeff.is_zero())
292                         coeffseq.push_back(combine_ex_with_coeff_to_pair(restcoeff,it->coeff));
293                 ++it;
294         }
295         
296         return (new add(coeffseq, n==0 ? overall_coeff : default_overall_coeff()))->setflag(status_flags::dynallocated);
297 }
298
299 ex add::eval(int level) const
300 {
301         // simplifications: +(;c) -> c
302         //                  +(x;1) -> x
303         
304         debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
305         
306         epvector * evaled_seqp = evalchildren(level);
307         if (evaled_seqp!=0) {
308                 // do more evaluation later
309                 return (new add(evaled_seqp,overall_coeff))->
310                        setflag(status_flags::dynallocated);
311         }
312         
313 #ifdef DO_GINAC_ASSERT
314         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
315                 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
316                 if (is_ex_exactly_of_type((*cit).rest,numeric))
317                         dbgprint();
318                 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
319         }
320 #endif // def DO_GINAC_ASSERT
321         
322         if (flags & status_flags::evaluated) {
323                 GINAC_ASSERT(seq.size()>0);
324                 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
325                 return *this;
326         }
327         
328         int seq_size = seq.size();
329         if (seq_size==0) {
330                 // +(;c) -> c
331                 return overall_coeff;
332         } else if ((seq_size==1) && overall_coeff.is_equal(_ex0())) {
333                 // +(x;0) -> x
334                 return recombine_pair_to_ex(*(seq.begin()));
335         }
336         return this->hold();
337 }
338
339 ex add::simplify_ncmul(const exvector & v) const
340 {
341         if (seq.size()==0) {
342                 return inherited::simplify_ncmul(v);
343         }
344         return (*seq.begin()).rest.simplify_ncmul(v);
345 }    
346
347 // protected
348
349 /** Implementation of ex::diff() for a sum. It differentiates each term.
350  *  @see ex::diff */
351 ex add::derivative(const symbol & s) const
352 {
353         // D(a+b+c)=D(a)+D(b)+D(c)
354         return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
355 }
356
357 int add::compare_same_type(const basic & other) const
358 {
359         return inherited::compare_same_type(other);
360 }
361
362 bool add::is_equal_same_type(const basic & other) const
363 {
364         return inherited::is_equal_same_type(other);
365 }
366
367 unsigned add::return_type(void) const
368 {
369         if (seq.size()==0) {
370                 return return_types::commutative;
371         }
372         return (*seq.begin()).rest.return_type();
373 }
374    
375 unsigned add::return_type_tinfo(void) const
376 {
377         if (seq.size()==0) {
378                 return tinfo_key;
379         }
380         return (*seq.begin()).rest.return_type_tinfo();
381 }
382
383 ex add::thisexpairseq(const epvector & v, const ex & oc) const
384 {
385         return (new add(v,oc))->setflag(status_flags::dynallocated);
386 }
387
388 ex add::thisexpairseq(epvector * vp, const ex & oc) const
389 {
390         return (new add(vp,oc))->setflag(status_flags::dynallocated);
391 }
392
393 expair add::split_ex_to_pair(const ex & e) const
394 {
395         if (is_ex_exactly_of_type(e,mul)) {
396                 const mul &mulref = ex_to_mul(e);
397                 ex numfactor = mulref.overall_coeff;
398                 mul *mulcopyp = new mul(mulref);
399                 mulcopyp->overall_coeff = _ex1();
400                 mulcopyp->clearflag(status_flags::evaluated);
401                 mulcopyp->clearflag(status_flags::hash_calculated);
402                 mulcopyp->setflag(status_flags::dynallocated);
403                 return expair(*mulcopyp,numfactor);
404         }
405         return expair(e,_ex1());
406 }
407
408 expair add::combine_ex_with_coeff_to_pair(const ex & e,
409                                                                                   const ex & c) const
410 {
411         GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
412         if (is_ex_exactly_of_type(e, mul)) {
413                 const mul &mulref = ex_to_mul(e);
414                 ex numfactor = mulref.overall_coeff;
415                 mul *mulcopyp = new mul(mulref);
416                 mulcopyp->overall_coeff = _ex1();
417                 mulcopyp->clearflag(status_flags::evaluated);
418                 mulcopyp->clearflag(status_flags::hash_calculated);
419                 mulcopyp->setflag(status_flags::dynallocated);
420                 if (are_ex_trivially_equal(c, _ex1()))
421                         return expair(*mulcopyp, numfactor);
422                 else if (are_ex_trivially_equal(numfactor, _ex1()))
423                         return expair(*mulcopyp, c);
424                 else
425                         return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
426         } else if (is_ex_exactly_of_type(e, numeric)) {
427                 if (are_ex_trivially_equal(c, _ex1()))
428                         return expair(e, _ex1());
429                 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
430         }
431         return expair(e, c);
432 }
433
434 expair add::combine_pair_with_coeff_to_pair(const expair & p,
435                                                                                         const ex & c) const
436 {
437         GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
438         GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
439
440         if (is_ex_exactly_of_type(p.rest,numeric)) {
441                 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
442                 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
443         }
444
445         return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
446 }
447         
448 ex add::recombine_pair_to_ex(const expair & p) const
449 {
450         if (ex_to_numeric(p.coeff).is_equal(_num1()))
451                 return p.rest;
452         else
453                 return p.rest*p.coeff;
454 }
455
456 ex add::expand(unsigned options) const
457 {
458         if (flags & status_flags::expanded)
459                 return *this;
460         
461         epvector * vp = expandchildren(options);
462         if (vp==0) {
463                 // the terms have not changed, so it is safe to declare this expanded
464                 setflag(status_flags::expanded);
465                 return *this;
466         }
467         
468         return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
469 }
470
471 } // namespace GiNaC