]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
pseries::expand(): do not generate zero terms.
[ginac.git] / ginac / ex.h
1 /** @file ex.h
2  *
3  *  Interface to GiNaC's light-weight expression handles. */
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 #ifndef __GINAC_EX_H__
24 #define __GINAC_EX_H__
25
26 #include <iostream>
27 #include "basic.h"
28 #include "operators.h"
29
30 #ifndef NO_NAMESPACE_GINAC
31 namespace GiNaC {
32 #endif // ndef NO_NAMESPACE_GINAC
33
34 class ex;
35 class symbol;
36 class lst;
37
38 // Sorry, this is the only constant to pollute the global scope, the other ones
39 // are defined in utils.h and not visible from outside.
40 extern const ex & _ex0(void);     //  single ex(numeric(0))
41
42 #define INLINE_EX_CONSTRUCTORS
43
44 /** Lightweight wrapper for GiNaC's symbolic objects.  Basically all it does is
45  *  to hold a pointer to the other objects, manage the reference counting and
46  *  provide methods for manipulation of these objects. */
47 class ex
48 {
49         friend class basic;
50
51 // member functions
52
53         // default constructor, destructor, copy constructor assignment operator and helpers
54 public:
55         ex()
56 #ifdef INLINE_EX_CONSTRUCTORS
57              : bp(_ex0().bp)
58         {
59                 GINAC_ASSERT(_ex0().bp!=0);
60                 GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
61                 GINAC_ASSERT(bp!=0);
62                 ++bp->refcount;
63 #ifdef OBSCURE_CINT_HACK
64                 update_last_created_or_assigned_bp();
65 #endif // def OBSCURE_CINT_HACK
66         }
67 #else
68 ;
69 #endif // def INLINE_EX_CONSTRUCTORS
70
71         ~ex()
72 #ifdef INLINE_EX_CONSTRUCTORS
73         {
74                 GINAC_ASSERT(bp!=0);
75                 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
76                 if (--bp->refcount == 0) {
77                         delete bp;
78                 }
79         }
80 #else
81 ;
82 #endif // def INLINE_EX_CONSTRUCTORS
83                 
84         ex(const ex & other)
85 #ifdef INLINE_EX_CONSTRUCTORS
86                              : bp(other.bp)
87         {
88                 GINAC_ASSERT(bp!=0);
89                 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
90                 ++bp->refcount;
91 #ifdef OBSCURE_CINT_HACK
92                 update_last_created_or_assigned_bp();
93 #endif // def OBSCURE_CINT_HACK
94         }
95 #else
96 ;
97 #endif // def INLINE_EX_CONSTRUCTORS
98                 
99         const ex & operator=(const ex & other)
100 #ifdef INLINE_EX_CONSTRUCTORS
101         {
102                 GINAC_ASSERT(bp!=0);
103                 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
104                 GINAC_ASSERT(other.bp!=0);
105                 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
106                 ++other.bp->refcount;
107                 basic * tmpbp=other.bp;
108                 if (--bp->refcount==0) {
109                         delete bp;
110                 }
111                 bp=tmpbp;
112 #ifdef OBSCURE_CINT_HACK
113                 update_last_created_or_assigned_bp();
114 #endif // def OBSCURE_CINT_HACK
115                 return *this;
116         }
117 #else
118 ;
119 #endif // def INLINE_EX_CONSTRUCTORS
120
121
122         // other constructors
123 public:
124         ex(const basic & other)
125 #ifdef INLINE_EX_CONSTRUCTORS
126         {
127                 construct_from_basic(other);
128 #ifdef OBSCURE_CINT_HACK
129                 update_last_created_or_assigned_bp();
130 #endif // def OBSCURE_CINT_HACK
131         }
132 #else
133 ;
134 #endif // def INLINE_EX_CONSTRUCTORS
135         
136         ex(int i)
137 #ifdef INLINE_EX_CONSTRUCTORS
138         {
139                 construct_from_int(i);
140 #ifdef OBSCURE_CINT_HACK
141                 update_last_created_or_assigned_bp();
142 #endif // def OBSCURE_CINT_HACK
143         }
144 #else
145 ;
146 #endif // def INLINE_EX_CONSTRUCTORS
147
148         ex(unsigned int i)
149 #ifdef INLINE_EX_CONSTRUCTORS
150         {
151                 construct_from_uint(i);
152 #ifdef OBSCURE_CINT_HACK
153                 update_last_created_or_assigned_bp();
154 #endif // def OBSCURE_CINT_HACK
155         }
156 #else
157 ;
158 #endif // def INLINE_EX_CONSTRUCTORS
159         
160         ex(long i)
161 #ifdef INLINE_EX_CONSTRUCTORS
162         {
163                 construct_from_long(i);
164 #ifdef OBSCURE_CINT_HACK
165                 update_last_created_or_assigned_bp();
166 #endif // def OBSCURE_CINT_HACK
167         }
168 #else
169 ;
170 #endif // def INLINE_EX_CONSTRUCTORS
171
172         ex(unsigned long i)
173 #ifdef INLINE_EX_CONSTRUCTORS
174         {
175                 construct_from_ulong(i);
176 #ifdef OBSCURE_CINT_HACK
177                 update_last_created_or_assigned_bp();
178 #endif // def OBSCURE_CINT_HACK
179         }
180 #else
181 ;
182 #endif // def INLINE_EX_CONSTRUCTORS
183         
184         ex(double const d)
185 #ifdef INLINE_EX_CONSTRUCTORS
186         {
187                 construct_from_double(d);
188 #ifdef OBSCURE_CINT_HACK
189                 update_last_created_or_assigned_bp();
190 #endif // def OBSCURE_CINT_HACK
191         }
192 #else
193 ;
194 #endif // def INLINE_EX_CONSTRUCTORS
195
196         /** Construct ex from string and a list of symbols. The input grammar is
197          *  similar to the GiNaC output format. All symbols to be used in the
198          *  expression must be specified in a lst in the second argument. Undefined
199          *  symbols and other parser errors will throw an exception. */
200         ex(const std::string &s, const ex &l)
201 #ifdef INLINE_EX_CONSTRUCTORS
202         {
203                 construct_from_string_and_lst(s, l);
204 #ifdef OBSCURE_CINT_HACK
205                 update_last_created_or_assigned_bp();
206 #endif // def OBSCURE_CINT_HACK
207         }
208 #else
209 ;
210 #endif // def INLINE_EX_CONSTRUCTORS
211
212         
213         // functions overriding virtual functions from bases classes
214         // none
215         
216         // new virtual functions which can be overridden by derived classes
217         // none
218
219         // non-virtual functions in this class
220 public:
221         void swap(ex & other);
222         void printraw(std::ostream & os) const;
223         void printtree(std::ostream & os, unsigned indent=0) const;
224         void print(std::ostream & os, unsigned upper_precedence=0) const;
225         void printcsrc(std::ostream & os, unsigned type, const char *var_name) const;
226         void dbgprint(void) const;
227         void dbgprinttree(void) const;
228         bool info(unsigned inf) const;
229         unsigned nops() const;
230         ex expand(unsigned options=0) const;
231         bool has(const ex & other) const;
232         int degree(const symbol & s) const;
233         int ldegree(const symbol & s) const;
234         ex coeff(const symbol & s, int n=1) const;
235         ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
236         ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
237         ex numer(void) const;
238         ex denom(void) const;
239         ex unit(const symbol &x) const;
240         ex content(const symbol &x) const;
241         numeric integer_content(void) const;
242         ex primpart(const symbol &x) const;
243         ex primpart(const symbol &x, const ex &cont) const;
244         ex normal(int level = 0) const;
245         ex to_rational(lst &repl_lst) const;
246         ex smod(const numeric &xi) const;
247         numeric max_coefficient(void) const;
248         ex collect(const symbol & s) const;
249         ex eval(int level = 0) const;
250         ex evalf(int level = 0) const;
251         ex diff(const symbol & s, unsigned nth = 1) const;
252         ex series(const ex & r, int order, unsigned options = 0) const;
253         ex subs(const lst & ls, const lst & lr) const;
254         ex subs(const ex & e) const;
255         exvector get_indices(void) const;
256         ex simplify_ncmul(const exvector & v) const;
257         ex operator[](const ex & index) const;
258         ex operator[](int i) const;
259         ex op(int i) const;
260         ex & let_op(int i);
261         ex lhs(void) const;
262         ex rhs(void) const;
263         int compare(const ex & other) const
264 #ifdef INLINE_EX_CONSTRUCTORS
265         {
266                 GINAC_ASSERT(bp!=0);
267                 GINAC_ASSERT(other.bp!=0);
268                 if (bp==other.bp) {
269                         // special case: both expression point to same basic, trivially equal
270                         return 0; 
271                 }
272                 return bp->compare(*other.bp);
273         }
274 #else
275 ;
276 #endif // def INLINE_EX_CONSTRUCTORS
277         bool is_equal(const ex & other) const
278 #ifdef INLINE_EX_CONSTRUCTORS
279         {
280                 GINAC_ASSERT(bp!=0);
281                 GINAC_ASSERT(other.bp!=0);
282                 if (bp==other.bp) {
283                         // special case: both expression point to same basic, trivially equal
284                         return true; 
285                 }
286                 return bp->is_equal(*other.bp);
287         }
288 #else
289 ;
290 #endif // def INLINE_EX_CONSTRUCTORS
291         bool is_zero(void) const {return compare(_ex0())==0;};
292                 
293         unsigned return_type(void) const;
294         unsigned return_type_tinfo(void) const;
295         unsigned gethash(void) const;
296
297         ex exadd(const ex & rh) const;
298         ex exmul(const ex & rh) const;
299         ex exncmul(const ex & rh) const;
300 private:
301         void construct_from_basic(const basic & other);
302         void construct_from_int(int i);
303         void construct_from_uint(unsigned int i);
304         void construct_from_long(long i);
305         void construct_from_ulong(unsigned long i);
306         void construct_from_double(double d);
307         void construct_from_string_and_lst(const std::string &s, const ex &l);
308         void makewriteable();
309
310 #ifdef OBSCURE_CINT_HACK
311 public:
312         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
313         {
314                 if (last_created_or_assigned_bp==0) return false;
315                 if ((last_created_or_assigned_bp->flags &
316                          status_flags::dynallocated)==0) return false;
317                 if ((last_created_or_assigned_bp->flags &
318                          status_flags::evaluated)==0) return false;
319                 return true;
320         }
321 protected:
322         void update_last_created_or_assigned_bp(void)
323         {
324                 if (last_created_or_assigned_bp!=0) {
325                         if (--last_created_or_assigned_bp->refcount == 0) {
326                                 delete last_created_or_assigned_bp;
327                         }
328                 }
329                 last_created_or_assigned_bp=bp;
330                 ++last_created_or_assigned_bp->refcount;
331                 last_created_or_assigned_exp=(long)(void *)(this);
332         }
333 #endif // def OBSCURE_CINT_HACK
334
335 // member variables
336
337 public:
338         basic *bp;
339 #ifdef OBSCURE_CINT_HACK
340         static basic * last_created_or_assigned_bp;
341         static basic * dummy_bp;
342         static long last_created_or_assigned_exp;
343 #endif // def OBSCURE_CINT_HACK
344 };
345
346 // utility functions
347 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
348 {
349         return e1.bp == e2.bp;
350 }
351
352 // wrapper functions around member functions
353 inline unsigned nops(const ex & thisex)
354 { return thisex.nops(); }
355
356 inline ex expand(const ex & thisex, unsigned options = 0)
357 { return thisex.expand(options); }
358
359 inline bool has(const ex & thisex, const ex & other)
360 { return thisex.has(other); }
361
362 inline int degree(const ex & thisex, const symbol & s)
363 { return thisex.degree(s); }
364
365 inline int ldegree(const ex & thisex, const symbol & s)
366 { return thisex.ldegree(s); }
367
368 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
369 { return thisex.coeff(s, n); }
370
371 inline ex numer(const ex & thisex)
372 { return thisex.numer(); }
373
374 inline ex denom(const ex & thisex)
375 { return thisex.denom(); }
376
377 inline ex normal(const ex & thisex, int level=0)
378 { return thisex.normal(level); }
379
380 inline ex to_rational(const ex & thisex, lst & repl_lst)
381 { return thisex.to_rational(repl_lst); }
382
383 inline ex collect(const ex & thisex, const symbol & s)
384 { return thisex.collect(s); }
385
386 inline ex eval(const ex & thisex, int level = 0)
387 { return thisex.eval(level); }
388
389 inline ex evalf(const ex & thisex, int level = 0)
390 { return thisex.evalf(level); }
391
392 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
393 { return thisex.diff(s, nth); }
394
395 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
396 { return thisex.series(r, order, options); }
397
398 inline ex subs(const ex & thisex, const ex & e)
399 { return thisex.subs(e); }
400
401 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
402 { return thisex.subs(ls, lr); }
403
404 inline ex op(const ex & thisex, int i)
405 { return thisex.op(i); }
406
407 inline ex lhs(const ex & thisex)
408 { return thisex.lhs(); }
409
410 inline ex rhs(const ex & thisex)
411 { return thisex.rhs(); }
412
413 inline bool is_zero(const ex & thisex)
414 { return thisex.is_zero(); }
415
416 inline void swap(ex & e1, ex & e2)
417 { e1.swap(e2); }
418
419 #ifndef NO_NAMESPACE_GINAC
420 } // namespace GiNaC
421 #endif // ndef NO_NAMESPACE_GINAC
422
423 #endif // ndef __GINAC_EX_H__
424