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