]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
- More drastic performance boost on matrix stuff.
[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 #ifdef CINT_CONVERSION_WORKAROUND
127     // workaround to fix the missing automatic derived->basic->ex conversion
128     const ex & operator=(const basic & other)
129         {
130             return *this=ex(other);
131         }
132 #endif // def CINT_CONVERSION_WORKAROUND 
133
134     // other constructors
135 public:
136     ex(const basic & other)
137 #ifdef INLINE_EX_CONSTRUCTORS
138         {
139             construct_from_basic(other);
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(int i)
149 #ifdef INLINE_EX_CONSTRUCTORS
150         {
151             construct_from_int(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(unsigned int i)
161 #ifdef INLINE_EX_CONSTRUCTORS
162         {
163             construct_from_uint(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(long i)
173 #ifdef INLINE_EX_CONSTRUCTORS
174         {
175             construct_from_long(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(unsigned long i)
185 #ifdef INLINE_EX_CONSTRUCTORS
186         {
187             construct_from_ulong(i);
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     ex(double const d)
197 #ifdef INLINE_EX_CONSTRUCTORS
198         {
199             construct_from_double(d);
200 #ifdef OBSCURE_CINT_HACK
201             update_last_created_or_assigned_bp();
202 #endif // def OBSCURE_CINT_HACK
203         }
204 #else
205 ;
206 #endif // def INLINE_EX_CONSTRUCTORS
207
208     
209     // functions overriding virtual functions from bases classes
210     // none
211     
212     // new virtual functions which can be overridden by derived classes
213     // none
214
215     // non-virtual functions in this class
216 public:
217     void swap(ex & other);
218     void printraw(ostream & os) const;
219     void printtree(ostream & os, unsigned indent=0) const;
220     void print(ostream & os, unsigned upper_precedence=0) const;
221     void printcsrc(ostream & os, unsigned type, const char *var_name) const;
222     void dbgprint(void) const;
223     void dbgprinttree(void) const;
224     bool info(unsigned inf) const;
225     unsigned nops() const;
226     ex expand(unsigned options=0) const;
227     bool has(const ex & other) const;
228 #ifdef CINT_CONVERSION_WORKAROUND
229     bool has(basic const & other) const { return has(ex(other)); }
230 #endif // def CINT_CONVERSION_WORKAROUND
231     int degree(const symbol & s) const;
232     int ldegree(const symbol & s) const;
233     ex coeff(const symbol & s, int n=1) const;
234     ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
235     ex tcoeff(const symbol & s) const { return coeff(s, ldegree(s)); }
236     ex numer(void) const;
237     ex denom(void) const;
238     ex unit(const symbol &x) const;
239     ex content(const symbol &x) const;
240     numeric integer_content(void) const;
241     ex primpart(const symbol &x) const;
242     ex primpart(const symbol &x, const ex &cont) const;
243 #ifdef CINT_CONVERSION_WORKAROUND
244     ex primpart(const symbol &x, const basic &cont) const { return primpart(x,ex(cont)); }
245 #endif // def CINT_CONVERSION_WORKAROUND
246     ex normal(int level = 0) const;
247     ex smod(const numeric &xi) const;
248     numeric max_coefficient(void) const;
249     ex collect(const symbol & s) const;
250     ex eval(int level = 0) const;
251     ex evalf(int level = 0) const;
252     ex diff(const symbol & s, unsigned nth = 1) const;
253     ex series(const symbol & s, const ex & point, int order = 6) const;
254 #ifdef CINT_CONVERSION_WORKAROUND
255     ex series(const symbol & s, const basic & point, int order = 6) const { return series(s,ex(point),order); }
256 #endif // def CINT_CONVERSION_WORKAROUND
257     ex subs(const lst & ls, const lst & lr) const;
258     ex subs(const ex & e) const;
259 #ifdef CINT_CONVERSION_WORKAROUND
260     ex subs(const basic & e) const { return subs(ex(e)); }
261 #endif // def CINT_CONVERSION_WORKAROUND
262     exvector get_indices(void) const;
263     ex simplify_ncmul(const exvector & v) const;
264     ex operator[](const ex & index) const;
265 #ifdef CINT_CONVERSION_WORKAROUND
266     ex operator[](const basic & index) const { return operator[](ex(index)); }
267 #endif // def CINT_CONVERSION_WORKAROUND
268     ex operator[](int i) const;
269     ex op(int i) const;
270     ex & let_op(int i);
271     ex lhs(void) const;
272     ex rhs(void) const;
273     int compare(const ex & other) const
274 #ifdef INLINE_EX_CONSTRUCTORS
275         {
276             GINAC_ASSERT(bp!=0);
277             GINAC_ASSERT(other.bp!=0);
278             if (bp==other.bp) {
279                 // special case: both expression point to same basic, trivially equal
280                 return 0; 
281             }
282             return bp->compare(*other.bp);
283         }
284 #else
285 ;
286 #endif // def INLINE_EX_CONSTRUCTORS
287 #ifdef CINT_CONVERSION_WORKAROUND
288     int compare(const basic & other) const { return compare(ex(other)); }
289 #endif // def CINT_CONVERSION_WORKAROUND
290     bool is_equal(const ex & other) const
291 #ifdef INLINE_EX_CONSTRUCTORS
292         {
293             GINAC_ASSERT(bp!=0);
294             GINAC_ASSERT(other.bp!=0);
295             if (bp==other.bp) {
296                 // special case: both expression point to same basic, trivially equal
297                 return true; 
298             }
299             return bp->is_equal(*other.bp);
300         }
301 #else
302 ;
303 #endif // def INLINE_EX_CONSTRUCTORS
304 #ifdef CINT_CONVERSION_WORKAROUND
305     bool is_equal(const basic & other) const { return is_equal(ex(other)); }
306 #endif // def CINT_CONVERSION_WORKAROUND
307     bool is_zero(void) const {return compare(_ex0())==0;};
308         
309     unsigned return_type(void) const;
310     unsigned return_type_tinfo(void) const;
311     unsigned gethash(void) const;
312
313     ex exadd(const ex & rh) const;
314 #ifdef CINT_CONVERSION_WORKAROUND
315     ex exadd(const basic & rh) const { return exadd(ex(rh)); }
316 #endif // def CINT_CONVERSION_WORKAROUND
317     ex exmul(const ex & rh) const;
318 #ifdef CINT_CONVERSION_WORKAROUND
319     ex exmul(const basic & rh) const { return exmul(ex(rh)); }
320 #endif // def CINT_CONVERSION_WORKAROUND
321     ex exncmul(const ex & rh) const;
322 #ifdef CINT_CONVERSION_WORKAROUND
323     ex exncmul(const basic & rh) const { return exncmul(ex(rh)); }
324 #endif // def CINT_CONVERSION_WORKAROUND
325 private:
326     void construct_from_basic(const basic & other);
327     void construct_from_int(int i);
328     void construct_from_uint(unsigned int i);
329     void construct_from_long(long i);
330     void construct_from_ulong(unsigned long i);
331     void construct_from_double(double d);
332     void makewriteable();
333
334 #ifdef OBSCURE_CINT_HACK
335 public:
336     static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
337         {
338             if (last_created_or_assigned_bp==0) return false;
339             if ((last_created_or_assigned_bp->flags &
340                  status_flags::dynallocated)==0) return false;
341             if ((last_created_or_assigned_bp->flags &
342                  status_flags::evaluated)==0) return false;
343             return true;
344         }
345 protected:
346     void update_last_created_or_assigned_bp(void)
347         {
348             if (last_created_or_assigned_bp!=0) {
349                 if (--last_created_or_assigned_bp->refcount == 0) {
350                     delete last_created_or_assigned_bp;
351                 }
352             }
353             last_created_or_assigned_bp=bp;
354             ++last_created_or_assigned_bp->refcount;
355             last_created_or_assigned_exp=(long)(void *)(this);
356         }
357 #endif // def OBSCURE_CINT_HACK
358
359 // member variables
360
361 public:
362     basic *bp;
363 #ifdef OBSCURE_CINT_HACK
364     static basic * last_created_or_assigned_bp;
365     static basic * dummy_bp;
366     static long last_created_or_assigned_exp;
367 #endif // def OBSCURE_CINT_HACK
368 };
369
370 // utility functions
371 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
372 {
373         return e1.bp == e2.bp;
374 }
375
376 // wrapper functions around member functions
377 inline unsigned nops(const ex & thisex)
378 { return thisex.nops(); }
379
380 inline ex expand(const ex & thisex, unsigned options = 0)
381 { return thisex.expand(options); }
382
383 inline bool has(const ex & thisex, const ex & other)
384 { return thisex.has(other); }
385
386 inline int degree(const ex & thisex, const symbol & s)
387 { return thisex.degree(s); }
388
389 inline int ldegree(const ex & thisex, const symbol & s)
390 { return thisex.ldegree(s); }
391
392 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
393 { return thisex.coeff(s, n); }
394
395 inline ex numer(const ex & thisex)
396 { return thisex.numer(); }
397
398 inline ex denom(const ex & thisex)
399 { return thisex.denom(); }
400
401 inline ex normal(const ex & thisex, int level=0)
402 { return thisex.normal(level); }
403
404 inline ex collect(const ex & thisex, const symbol & s)
405 { return thisex.collect(s); }
406
407 inline ex eval(const ex & thisex, int level = 0)
408 { return thisex.eval(level); }
409
410 inline ex evalf(const ex & thisex, int level = 0)
411 { return thisex.evalf(level); }
412
413 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
414 { return thisex.diff(s, nth); }
415
416 inline ex series(const ex & thisex, const symbol & s, const ex & point, int order = 6)
417 { return thisex.series(s, point, order); }
418
419 inline ex subs(const ex & thisex, const ex & e)
420 { return thisex.subs(e); }
421
422 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
423 { return thisex.subs(ls, lr); }
424
425 inline ex op(const ex & thisex, int i)
426 { return thisex.op(i); }
427
428 inline ex lhs(const ex & thisex)
429 { return thisex.lhs(); }
430
431 inline ex rhs(const ex & thisex)
432 { return thisex.rhs(); }
433
434 inline bool is_zero(const ex & thisex)
435 { return thisex.is_zero(); }
436
437 inline void swap(ex & e1, ex & e2)
438 { e1.swap(e2); }
439
440 #ifndef NO_NAMESPACE_GINAC
441 } // namespace GiNaC
442 #endif // ndef NO_NAMESPACE_GINAC
443
444 #endif // ndef __GINAC_EX_H__
445