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