]> www.ginac.de Git - ginac.git/blob - ginac/add.cpp
- changed behaviour of numeric::is_rational() and added numeric::is_cinteger()
[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 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 "debugmsg.h"
29
30 #ifndef NO_GINAC_NAMESPACE
31 namespace GiNaC {
32 #endif // ndef NO_GINAC_NAMESPACE
33
34 //////////
35 // default constructor, destructor, copy constructor assignment operator and helpers
36 //////////
37
38 // public
39
40 add::add()
41 {
42     debugmsg("add default constructor",LOGLEVEL_CONSTRUCT);
43     tinfo_key = TINFO_add;
44 }
45
46 add::~add()
47 {
48     debugmsg("add destructor",LOGLEVEL_DESTRUCT);
49     destroy(0);
50 }
51
52 add::add(add const & other)
53 {
54     debugmsg("add copy constructor",LOGLEVEL_CONSTRUCT);
55     copy(other);
56 }
57
58 add const & add::operator=(add const & other)
59 {
60     debugmsg("add operator=",LOGLEVEL_ASSIGNMENT);
61     if (this != &other) {
62         destroy(1);
63         copy(other);
64     }
65     return *this;
66 }
67
68 // protected
69
70 void add::copy(add const & other)
71 {
72     expairseq::copy(other);
73 }
74
75 void add::destroy(bool call_parent)
76 {
77     if (call_parent) expairseq::destroy(call_parent);
78 }
79
80 //////////
81 // other constructors
82 //////////
83
84 // public
85
86 add::add(ex const & lh, ex const & rh)
87 {
88     debugmsg("add constructor from ex,ex",LOGLEVEL_CONSTRUCT);
89     tinfo_key = TINFO_add;
90     overall_coeff=exZERO();
91     construct_from_2_ex(lh,rh);
92     GINAC_ASSERT(is_canonical());
93 }
94
95 add::add(exvector const & v)
96 {
97     debugmsg("add constructor from exvector",LOGLEVEL_CONSTRUCT);
98     tinfo_key = TINFO_add;
99     overall_coeff=exZERO();
100     construct_from_exvector(v);
101     GINAC_ASSERT(is_canonical());
102 }
103
104 /*
105 add::add(epvector const & v, bool do_not_canonicalize)
106 {
107     debugmsg("add constructor from epvector,bool",LOGLEVEL_CONSTRUCT);
108     tinfo_key = TINFO_add;
109     if (do_not_canonicalize) {
110         seq=v;
111 #ifdef EXPAIRSEQ_USE_HASHTAB
112         combine_same_terms(); // to build hashtab
113 #endif // def EXPAIRSEQ_USE_HASHTAB
114     } else {
115         construct_from_epvector(v);
116     }
117     GINAC_ASSERT(is_canonical());
118 }
119 */
120
121 add::add(epvector const & v)
122 {
123     debugmsg("add constructor from epvector",LOGLEVEL_CONSTRUCT);
124     tinfo_key = TINFO_add;
125     overall_coeff=exZERO();
126     construct_from_epvector(v);
127     GINAC_ASSERT(is_canonical());
128 }
129
130 add::add(epvector const & v, ex const & oc)
131 {
132     debugmsg("add constructor from epvector,ex",LOGLEVEL_CONSTRUCT);
133     tinfo_key = TINFO_add;
134     overall_coeff=oc;
135     construct_from_epvector(v);
136     GINAC_ASSERT(is_canonical());
137 }
138
139 add::add(epvector * vp, ex const & oc)
140 {
141     debugmsg("add constructor from epvector *,ex",LOGLEVEL_CONSTRUCT);
142     tinfo_key = TINFO_add;
143     GINAC_ASSERT(vp!=0);
144     overall_coeff=oc;
145     construct_from_epvector(*vp);
146     delete vp;
147     GINAC_ASSERT(is_canonical());
148 }
149
150 //////////
151 // functions overriding virtual functions from bases classes
152 //////////
153
154 // public
155
156 basic * add::duplicate() const
157 {
158     debugmsg("add duplicate",LOGLEVEL_DUPLICATE);
159     return new add(*this);
160 }
161
162 bool add::info(unsigned inf) const
163 {
164     // TODO: optimize
165     if (inf==info_flags::polynomial || inf==info_flags::integer_polynomial || inf==info_flags::rational_polynomial || inf==info_flags::rational_function) {
166         for (epvector::const_iterator it=seq.begin(); it!=seq.end(); ++it) {
167             if (!(recombine_pair_to_ex(*it).info(inf)))
168                 return false;
169         }
170         return true;
171     } else {
172         return expairseq::info(inf);
173     }
174 }
175
176 int add::degree(symbol const & s) const
177 {
178     int deg=INT_MIN;
179     if (!overall_coeff.is_equal(exZERO())) {
180         deg=0;
181     }
182     int cur_deg;
183     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
184         cur_deg=(*cit).rest.degree(s);
185         if (cur_deg>deg) deg=cur_deg;
186     }
187     return deg;
188 }
189
190 int add::ldegree(symbol const & s) const
191 {
192     int deg=INT_MAX;
193     if (!overall_coeff.is_equal(exZERO())) {
194         deg=0;
195     }
196     int cur_deg;
197     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
198         cur_deg=(*cit).rest.ldegree(s);
199         if (cur_deg<deg) deg=cur_deg;
200     }
201     return deg;
202 }
203
204 ex add::coeff(symbol const & s, int const n) const
205 {
206     epvector coeffseq;
207     coeffseq.reserve(seq.size());
208
209     epvector::const_iterator it=seq.begin();
210     while (it!=seq.end()) {
211         coeffseq.push_back(combine_ex_with_coeff_to_pair((*it).rest.coeff(s,n),
212                                                          (*it).coeff));
213         ++it;
214     }
215     if (n==0) {
216         return (new add(coeffseq,overall_coeff))->setflag(status_flags::dynallocated);
217     }
218     return (new add(coeffseq))->setflag(status_flags::dynallocated);
219 }
220
221 ex add::eval(int level) const
222 {
223     // simplifications: +(;c) -> c
224     //                  +(x;1) -> x
225
226     debugmsg("add eval",LOGLEVEL_MEMBER_FUNCTION);
227
228     epvector * evaled_seqp=evalchildren(level);
229     if (evaled_seqp!=0) {
230         // do more evaluation later
231         return (new add(evaled_seqp,overall_coeff))->
232                    setflag(status_flags::dynallocated);
233     }
234
235 #ifdef DO_GINAC_ASSERT
236     for (epvector::const_iterator cit=seq.begin(); cit!=seq.end(); ++cit) {
237         GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,add));
238         if (is_ex_exactly_of_type((*cit).rest,numeric)) {
239             dbgprint();
240         }
241         GINAC_ASSERT(!is_ex_exactly_of_type((*cit).rest,numeric));
242     }
243 #endif // def DO_GINAC_ASSERT
244
245     if (flags & status_flags::evaluated) {
246         GINAC_ASSERT(seq.size()>0);
247         GINAC_ASSERT((seq.size()>1)||!overall_coeff.is_equal(exZERO()));
248         return *this;
249     }
250
251     int seq_size=seq.size();
252     if (seq_size==0) {
253         // +(;c) -> c
254         return overall_coeff;
255     } else if ((seq_size==1)&&overall_coeff.is_equal(exZERO())) {
256         // +(x;0) -> x
257         return recombine_pair_to_ex(*(seq.begin()));
258     }
259     return this->hold();
260 }
261
262 exvector add::get_indices(void) const
263 {
264     // FIXME: all terms in the sum should have the same indices (compatible
265     // tensors) however this is not checked, since there is no function yet
266     // which compares indices (idxvector can be unsorted)
267     if (seq.size()==0) {
268         return exvector();
269     }
270     return (seq.begin())->rest.get_indices();
271 }    
272
273 ex add::simplify_ncmul(exvector const & v) const
274 {
275     if (seq.size()==0) {
276         return expairseq::simplify_ncmul(v);
277     }
278     return (*seq.begin()).rest.simplify_ncmul(v);
279 }    
280
281 // protected
282
283 int add::compare_same_type(basic const & other) const
284 {
285     return expairseq::compare_same_type(other);
286 }
287
288 bool add::is_equal_same_type(basic const & other) const
289 {
290     return expairseq::is_equal_same_type(other);
291 }
292
293 unsigned add::return_type(void) const
294 {
295     if (seq.size()==0) {
296         return return_types::commutative;
297     }
298     return (*seq.begin()).rest.return_type();
299 }
300    
301 unsigned add::return_type_tinfo(void) const
302 {
303     if (seq.size()==0) {
304         return tinfo_key;
305     }
306     return (*seq.begin()).rest.return_type_tinfo();
307 }
308
309 ex add::thisexpairseq(epvector const & v, ex const & oc) const
310 {
311     return (new add(v,oc))->setflag(status_flags::dynallocated);
312 }
313
314 ex add::thisexpairseq(epvector * vp, ex const & oc) const
315 {
316     return (new add(vp,oc))->setflag(status_flags::dynallocated);
317 }
318
319 expair add::split_ex_to_pair(ex const & e) const
320 {
321     if (is_ex_exactly_of_type(e,mul)) {
322         mul const & mulref=ex_to_mul(e);
323         ex numfactor=mulref.overall_coeff;
324         // mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
325         mul * mulcopyp=new mul(mulref);
326         mulcopyp->overall_coeff=exONE();
327         mulcopyp->clearflag(status_flags::evaluated);
328         mulcopyp->clearflag(status_flags::hash_calculated);
329         return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
330     }
331     return expair(e,exONE());
332 }
333
334 expair add::combine_ex_with_coeff_to_pair(ex const & e,
335                                           ex const & c) const
336 {
337     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
338     if (is_ex_exactly_of_type(e,mul)) {
339         mul const & mulref=ex_to_mul(e);
340         ex numfactor=mulref.overall_coeff;
341         //mul * mulcopyp=static_cast<mul *>(mulref.duplicate());
342         mul * mulcopyp=new mul(mulref);
343         mulcopyp->overall_coeff=exONE();
344         mulcopyp->clearflag(status_flags::evaluated);
345         mulcopyp->clearflag(status_flags::hash_calculated);
346         if (are_ex_trivially_equal(c,exONE())) {
347             return expair(mulcopyp->setflag(status_flags::dynallocated),numfactor);
348         } else if (are_ex_trivially_equal(numfactor,exONE())) {
349             return expair(mulcopyp->setflag(status_flags::dynallocated),c);
350         }
351         return expair(mulcopyp->setflag(status_flags::dynallocated),
352                           ex_to_numeric(numfactor).mul_dyn(ex_to_numeric(c)));
353     } else if (is_ex_exactly_of_type(e,numeric)) {
354         if (are_ex_trivially_equal(c,exONE())) {
355             return expair(e,exONE());
356         }
357         return expair(ex_to_numeric(e).mul_dyn(ex_to_numeric(c)),exONE());
358     }
359     return expair(e,c);
360 }
361     
362 expair add::combine_pair_with_coeff_to_pair(expair const & p,
363                                             ex const & c) const
364 {
365     GINAC_ASSERT(is_ex_exactly_of_type(p.coeff,numeric));
366     GINAC_ASSERT(is_ex_exactly_of_type(c,numeric));
367
368     if (is_ex_exactly_of_type(p.rest,numeric)) {
369         GINAC_ASSERT(ex_to_numeric(p.coeff).is_equal(numONE())); // should be normalized
370         return expair(ex_to_numeric(p.rest).mul_dyn(ex_to_numeric(c)),exONE());
371     }
372
373     return expair(p.rest,ex_to_numeric(p.coeff).mul_dyn(ex_to_numeric(c)));
374 }
375     
376 ex add::recombine_pair_to_ex(expair const & p) const
377 {
378     //if (p.coeff.compare(exONE())==0) {
379     //if (are_ex_trivially_equal(p.coeff,exONE())) {
380     if (ex_to_numeric(p.coeff).is_equal(numONE())) {
381         return p.rest;
382     } else {
383         return p.rest*p.coeff;
384     }
385 }
386
387 ex add::expand(unsigned options) const
388 {
389     epvector * vp=expandchildren(options);
390     if (vp==0) {
391         return *this;
392     }
393     return (new add(vp,overall_coeff))->setflag(status_flags::expanded    |
394                                                 status_flags::dynallocated );
395 }
396
397 //////////
398 // new virtual functions which can be overridden by derived classes
399 //////////
400
401 // none
402
403 //////////
404 // non-virtual functions in this class
405 //////////
406
407 // none
408
409 //////////
410 // static member variables
411 //////////
412
413 // protected
414
415 unsigned add::precedence=40;
416
417 //////////
418 // global constants
419 //////////
420
421 const add some_add;
422 type_info const & typeid_add=typeid(some_add);
423
424 #ifndef NO_GINAC_NAMESPACE
425 } // namespace GiNaC
426 #endif // ndef NO_GINAC_NAMESPACE