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