]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
ce0fe8c1686d30b33ae861c383ef739b438ca434
[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 symbol & s) const;
90         int ldegree(const symbol & s) const;
91         ex coeff(const symbol & s, int n=1) const;
92         ex lcoeff(const symbol & s) const { return coeff(s, degree(s)); }
93         ex tcoeff(const symbol & 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 symbol & 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         ex exncmul(const ex & rh) const;
133 private:
134         void construct_from_basic(const basic & other);
135         void construct_from_int(int i);
136         void construct_from_uint(unsigned int i);
137         void construct_from_long(long i);
138         void construct_from_ulong(unsigned long i);
139         void construct_from_double(double d);
140         void construct_from_string_and_lst(const std::string &s, const ex &l);
141         void makewriteable();
142
143 #ifdef OBSCURE_CINT_HACK
144 public:
145         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
146         {
147                 if (last_created_or_assigned_bp==0) return false;
148                 if ((last_created_or_assigned_bp->flags &
149                          status_flags::dynallocated)==0) return false;
150                 if ((last_created_or_assigned_bp->flags &
151                          status_flags::evaluated)==0) return false;
152                 return true;
153         }
154 protected:
155         void update_last_created_or_assigned_bp(void)
156         {
157                 if (last_created_or_assigned_bp!=0) {
158                         if (--last_created_or_assigned_bp->refcount == 0) {
159                                 delete last_created_or_assigned_bp;
160                         }
161                 }
162                 last_created_or_assigned_bp = bp;
163                 ++last_created_or_assigned_bp->refcount;
164                 last_created_or_assigned_exp = (long)(void *)(this);
165         }
166 #endif // def OBSCURE_CINT_HACK
167
168 // member variables
169
170 public:
171         basic *bp;      ///< pointer to basic object managed by this
172 #ifdef OBSCURE_CINT_HACK
173         static basic * last_created_or_assigned_bp;
174         static basic * dummy_bp;
175         static long last_created_or_assigned_exp;
176 #endif // def OBSCURE_CINT_HACK
177 };
178
179
180 // performance-critical inlined method implementations
181
182 inline
183 ex::ex() : bp(_ex0().bp)
184 {
185         /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
186         GINAC_ASSERT(_ex0().bp!=0);
187         GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
188         GINAC_ASSERT(bp!=0);
189         ++bp->refcount;
190 #ifdef OBSCURE_CINT_HACK
191         update_last_created_or_assigned_bp();
192 #endif // def OBSCURE_CINT_HACK
193 }
194
195 inline
196 ex::~ex()
197 {
198         /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
199         GINAC_ASSERT(bp!=0);
200         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
201         if (--bp->refcount == 0)
202                 delete bp;
203 }
204
205 inline
206 ex::ex(const ex & other) : bp(other.bp)
207 {
208         /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
209         GINAC_ASSERT(bp!=0);
210         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
211         ++bp->refcount;
212 #ifdef OBSCURE_CINT_HACK
213         update_last_created_or_assigned_bp();
214 #endif // def OBSCURE_CINT_HACK
215 }
216
217 inline
218 const ex & ex::operator=(const ex & other)
219 {
220         /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
221         GINAC_ASSERT(bp!=0);
222         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
223         GINAC_ASSERT(other.bp!=0);
224         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
225         if (--bp->refcount==0)
226                 delete bp;
227         ++other.bp->refcount;
228         bp = other.bp;
229 #ifdef OBSCURE_CINT_HACK
230         update_last_created_or_assigned_bp();
231 #endif // def OBSCURE_CINT_HACK
232         return *this;
233 }
234
235 inline
236 ex::ex(const basic & other)
237 {
238         /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
239         construct_from_basic(other);
240 #ifdef OBSCURE_CINT_HACK
241         update_last_created_or_assigned_bp();
242 #endif // def OBSCURE_CINT_HACK
243 }
244
245 inline
246 ex::ex(int i)
247 {
248         /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
249         construct_from_int(i);
250 #ifdef OBSCURE_CINT_HACK
251         update_last_created_or_assigned_bp();
252 #endif // def OBSCURE_CINT_HACK
253 }
254
255 inline
256 ex::ex(unsigned int i)
257 {
258         /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
259         construct_from_uint(i);
260 #ifdef OBSCURE_CINT_HACK
261         update_last_created_or_assigned_bp();
262 #endif // def OBSCURE_CINT_HACK
263 }
264
265 inline
266 ex::ex(long i)
267 {
268         /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
269         construct_from_long(i);
270 #ifdef OBSCURE_CINT_HACK
271         update_last_created_or_assigned_bp();
272 #endif // def OBSCURE_CINT_HACK
273 }
274
275 inline
276 ex::ex(unsigned long i)
277 {
278         /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
279         construct_from_ulong(i);
280 #ifdef OBSCURE_CINT_HACK
281         update_last_created_or_assigned_bp();
282 #endif // def OBSCURE_CINT_HACK
283 }
284
285 inline
286 ex::ex(double const d)
287 {
288         /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
289         construct_from_double(d);
290 #ifdef OBSCURE_CINT_HACK
291         update_last_created_or_assigned_bp();
292 #endif // def OBSCURE_CINT_HACK
293 }
294
295 inline
296 ex::ex(const std::string &s, const ex &l)
297 {
298         /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
299         construct_from_string_and_lst(s, l);
300 #ifdef OBSCURE_CINT_HACK
301         update_last_created_or_assigned_bp();
302 #endif // def OBSCURE_CINT_HACK
303 }
304
305 inline
306 int ex::compare(const ex & other) const
307 {
308         GINAC_ASSERT(bp!=0);
309         GINAC_ASSERT(other.bp!=0);
310         if (bp==other.bp)  // trivial case: both expressions point to same basic
311                 return 0;
312         return bp->compare(*other.bp);
313 }
314
315 inline
316 bool ex::is_equal(const ex & other) const
317 {
318         GINAC_ASSERT(bp!=0);
319         GINAC_ASSERT(other.bp!=0);
320         if (bp==other.bp)  // trivial case: both expressions point to same basic
321                 return true;
322         return bp->is_equal(*other.bp);
323 }
324
325
326 // utility functions
327 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
328 {
329         return e1.bp == e2.bp;
330 }
331
332 // wrapper functions around member functions
333 inline unsigned nops(const ex & thisex)
334 { return thisex.nops(); }
335
336 inline ex expand(const ex & thisex, unsigned options = 0)
337 { return thisex.expand(options); }
338
339 inline bool has(const ex & thisex, const ex & other)
340 { return thisex.has(other); }
341
342 inline int degree(const ex & thisex, const symbol & s)
343 { return thisex.degree(s); }
344
345 inline int ldegree(const ex & thisex, const symbol & s)
346 { return thisex.ldegree(s); }
347
348 inline ex coeff(const ex & thisex, const symbol & s, int n=1)
349 { return thisex.coeff(s, n); }
350
351 inline ex numer(const ex & thisex)
352 { return thisex.numer(); }
353
354 inline ex denom(const ex & thisex)
355 { return thisex.denom(); }
356
357 inline ex normal(const ex & thisex, int level=0)
358 { return thisex.normal(level); }
359
360 inline ex to_rational(const ex & thisex, lst & repl_lst)
361 { return thisex.to_rational(repl_lst); }
362
363 inline ex collect(const ex & thisex, const symbol & s)
364 { return thisex.collect(s); }
365
366 inline ex eval(const ex & thisex, int level = 0)
367 { return thisex.eval(level); }
368
369 inline ex evalf(const ex & thisex, int level = 0)
370 { return thisex.evalf(level); }
371
372 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
373 { return thisex.diff(s, nth); }
374
375 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
376 { return thisex.series(r, order, options); }
377
378 inline ex subs(const ex & thisex, const ex & e)
379 { return thisex.subs(e); }
380
381 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
382 { return thisex.subs(ls, lr); }
383
384 inline ex op(const ex & thisex, int i)
385 { return thisex.op(i); }
386
387 inline ex lhs(const ex & thisex)
388 { return thisex.lhs(); }
389
390 inline ex rhs(const ex & thisex)
391 { return thisex.rhs(); }
392
393 inline bool is_zero(const ex & thisex)
394 { return thisex.is_zero(); }
395
396 inline void swap(ex & e1, ex & e2)
397 { e1.swap(e2); }
398
399 } // namespace GiNaC
400
401 #endif // ndef __GINAC_EX_H__