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