]> www.ginac.de Git - ginac.git/blob - ginac/add.cpp
* INSTALL: adjusted Cint status.
[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                         c.s << "(";
171
172                 numeric coeff;
173                 bool first = true;
174
175                 // First print the overall numeric coefficient, if present
176                 if (!overall_coeff.is_zero()) {
177                         if (!is_of_type(c, print_tree))
178                                 overall_coeff.print(c, 0);
179                         else
180                                 overall_coeff.print(c, precedence);
181                         first = false;
182                 }
183
184                 // Then proceed with the remaining factors
185                 epvector::const_iterator it = seq.begin(), itend = seq.end();
186                 while (it != itend) {
187                         coeff = ex_to_numeric(it->coeff);
188                         if (!first) {
189                                 if (coeff.csgn() == -1) c.s << '-'; else c.s << '+';
190                         } else {
191                                 if (coeff.csgn() == -1) c.s << '-';
192                                 first = false;
193                         }
194                         if (!coeff.is_equal(_num1()) &&
195                             !coeff.is_equal(_num_1())) {
196                                 if (coeff.is_rational()) {
197                                         if (coeff.is_negative())
198                                                 (-coeff).print(c);
199                                         else
200                                                 coeff.print(c);
201                                 } else {
202                                         if (coeff.csgn() == -1)
203                                                 (-coeff).print(c, precedence);
204                                         else
205                                                 coeff.print(c, precedence);
206                                 }
207                                 c.s << '*';
208                         }
209                         it->rest.print(c, precedence);
210                         it++;
211                 }
212
213                 if (precedence <= level)
214                         c.s << ")";
215         }
216 }
217
218 bool add::info(unsigned inf) const
219 {
220         switch (inf) {
221                 case info_flags::polynomial:
222                 case info_flags::integer_polynomial:
223                 case info_flags::cinteger_polynomial:
224                 case info_flags::rational_polynomial:
225                 case info_flags::crational_polynomial:
226                 case info_flags::rational_function: {
227                         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
228                                 if (!(recombine_pair_to_ex(*i).info(inf)))
229                                         return false;
230                         }
231                         return overall_coeff.info(inf);
232                 }
233                 case info_flags::algebraic: {
234                         for (epvector::const_iterator i=seq.begin(); i!=seq.end(); ++i) {
235                                 if ((recombine_pair_to_ex(*i).info(inf)))
236                                         return true;
237                         }
238                         return false;
239                 }
240         }
241         return inherited::info(inf);
242 }
243
244 int add::degree(const ex & s) const
245 {
246         int deg = INT_MIN;
247         if (!overall_coeff.is_equal(_ex0()))
248                 deg = 0;
249         
250         int cur_deg;
251         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
252                 cur_deg = (*cit).rest.degree(s);
253                 if (cur_deg>deg)
254                         deg = cur_deg;
255         }
256         return deg;
257 }
258
259 int add::ldegree(const ex & s) const
260 {
261         int deg = INT_MAX;
262         if (!overall_coeff.is_equal(_ex0()))
263                 deg = 0;
264         
265         int cur_deg;
266         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
267                 cur_deg = (*cit).rest.ldegree(s);
268                 if (cur_deg<deg) deg=cur_deg;
269         }
270         return deg;
271 }
272
273 ex add::coeff(const ex & s, int n) const
274 {
275         epvector coeffseq;
276         coeffseq.reserve(seq.size());
277
278         epvector::const_iterator it=seq.begin();
279         while (it!=seq.end()) {
280                 coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
281                                                                  (*it).coeff));
282                 ++it;
283         }
284         if (n==0) {
285                 return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
286         }
287         return (new add(coeffseq))->setflag(status_flags::dynallocated);
288 }
289
290 ex add::eval(int level) const
291 {
292         // simplifications: +(;c) -> c
293         //                  +(x;1) -> x
294         
295         debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
296         
297         epvector * evaled_seqp = evalchildren(level);
298         if (evaled_seqp!=0) {
299                 // do more evaluation later
300                 return (new add(evaled_seqp,overall_coeff))->
301                        setflag(status_flags::dynallocated);
302         }
303         
304 #ifdef DO_GINAC_ASSERT
305         for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
306                 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
307                 if (is_ex_exactly_of_type((*cit).rest,numeric))
308                         dbgprint();
309                 GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
310         }
311 #endif // def DO_GINAC_ASSERT
312         
313         if (flags & status_flags::evaluated) {
314                 GINAC_ASSERT(seq.size()>0);
315                 GINAC_ASSERT(seq.size()>1 || !overall_coeff.is_zero());
316                 return *this;
317         }
318         
319         int seq_size = seq.size();
320         if (seq_size==0) {
321                 // +(;c) -> c
322                 return overall_coeff;
323         } else if ((seq_size==1) && overall_coeff.is_equal(_ex0())) {
324                 // +(x;0) -> x
325                 return recombine_pair_to_ex(*(seq.begin()));
326         }
327         return this->hold();
328 }
329
330 ex add::simplify_ncmul(const exvector & v) const
331 {
332         if (seq.size()==0) {
333                 return inherited::simplify_ncmul(v);
334         }
335         return (*seq.begin()).rest.simplify_ncmul(v);
336 }    
337
338 // protected
339
340 /** Implementation of ex::diff() for a sum. It differentiates each term.
341  *  @see ex::diff */
342 ex add::derivative(const symbol & s) const
343 {
344         // D(a+b+c)=D(a)+D(b)+D(c)
345         return (new add(diffchildren(s)))->setflag(status_flags::dynallocated);
346 }
347
348 int add::compare_same_type(const basic & other) const
349 {
350         return inherited::compare_same_type(other);
351 }
352
353 bool add::is_equal_same_type(const basic & other) const
354 {
355         return inherited::is_equal_same_type(other);
356 }
357
358 unsigned add::return_type(void) const
359 {
360         if (seq.size()==0) {
361                 return return_types::commutative;
362         }
363         return (*seq.begin()).rest.return_type();
364 }
365    
366 unsigned add::return_type_tinfo(void) const
367 {
368         if (seq.size()==0) {
369                 return tinfo_key;
370         }
371         return (*seq.begin()).rest.return_type_tinfo();
372 }
373
374 ex add::thisexpairseq(const epvector & v, const ex & oc) const
375 {
376         return (new add(v,oc))->setflag(status_flags::dynallocated);
377 }
378
379 ex add::thisexpairseq(epvector * vp, const ex & oc) const
380 {
381         return (new add(vp,oc))->setflag(status_flags::dynallocated);
382 }
383
384 expair add::split_ex_to_pair(const ex & e) const
385 {
386         if (is_ex_exactly_of_type(e,mul)) {
387                 const mul &mulref = ex_to_mul(e);
388                 ex numfactor = mulref.overall_coeff;
389                 mul *mulcopyp = new mul(mulref);
390                 mulcopyp->overall_coeff = _ex1();
391                 mulcopyp->clearflag(status_flags::evaluated);
392                 mulcopyp->clearflag(status_flags::hash_calculated);
393                 mulcopyp->setflag(status_flags::dynallocated);
394                 return expair(*mulcopyp,numfactor);
395         }
396         return expair(e,_ex1());
397 }
398
399 expair add::combine_ex_with_coeff_to_pair(const ex & e,
400                                                                                   const ex & c) const
401 {
402         GINAC_ASSERT(is_ex_exactly_of_type(c, numeric));
403         if (is_ex_exactly_of_type(e, mul)) {
404                 const mul &mulref = ex_to_mul(e);
405                 ex numfactor = mulref.overall_coeff;
406                 mul *mulcopyp = new mul(mulref);
407                 mulcopyp->overall_coeff = _ex1();
408                 mulcopyp->clearflag(status_flags::evaluated);
409                 mulcopyp->clearflag(status_flags::hash_calculated);
410                 mulcopyp->setflag(status_flags::dynallocated);
411                 if (are_ex_trivially_equal(c, _ex1()))
412                         return expair(*mulcopyp, numfactor);
413                 else if (are_ex_trivially_equal(numfactor, _ex1()))
414                         return expair(*mulcopyp, c);
415                 else
416                         return expair(*mulcopyp, ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
417         } else if (is_ex_exactly_of_type(e, numeric)) {
418                 if (are_ex_trivially_equal(c, _ex1()))
419                         return expair(e, _ex1());
420                 return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)), _ex1());
421         }
422         return expair(e, c);
423 }
424
425 expair add::combine_pair_with_coeff_to_pair(const expair & p,
426                                                                                         const ex & c) const
427 {
428         GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
429         GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
430
431         if (is_ex_exactly_of_type(p.rest,numeric)) {
432                 GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(_num1())); // should be normalized
433                 return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),_ex1());
434         }
435
436         return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
437 }
438         
439 ex add::recombine_pair_to_ex(const expair & p) const
440 {
441         if (ex_to_numeric(p.coeff).is_equal(_num1()))
442                 return p.rest;
443         else
444                 return p.rest*p.coeff;
445 }
446
447 ex add::expand(unsigned options) const
448 {
449         if (flags & status_flags::expanded)
450                 return *this;
451         
452         epvector * vp = expandchildren(options);
453         if (vp==0) {
454                 // the terms have not changed, so it is safe to declare this expanded
455                 setflag(status_flags::expanded);
456                 return *this;
457         }
458         
459         return (new add(vp,overall_coeff))->setflag(status_flags::expanded | status_flags::dynallocated);
460 }
461
462 //////////
463 // static member variables
464 //////////
465
466 // protected
467
468 unsigned add::precedence = 40;
469
470 } // namespace GiNaC