]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
- config.*: updated from FSF.
[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         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 print(const print_context & c, unsigned level = 0) const;
80         void printtree(std::ostream & os) const;
81         void dbgprint(void) const;
82         void dbgprinttree(void) const;
83         bool info(unsigned inf) const { return bp->info(inf); }
84         unsigned nops() const { return bp->nops(); }
85         ex expand(unsigned options=0) const;
86         bool has(const ex & other) const { return bp->has(other); }
87         ex map(map_func f) const { return bp->map(f); }
88         int degree(const ex & s) const { return bp->degree(s); }
89         int ldegree(const ex & s) const { return bp->ldegree(s); }
90         ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
91         ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
92         ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
93         ex numer(void) const;
94         ex denom(void) const;
95         ex numer_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, bool distributed = false) const { return bp->collect(s, distributed); }
106         ex eval(int level = 0) const { return bp->eval(level); }
107         ex evalf(int level = 0) const { return bp->evalf(level); }
108         ex evalm(void) const { return bp->evalm(); }
109         ex diff(const symbol & s, unsigned nth = 1) const;
110         ex series(const ex & r, int order, unsigned options = 0) const;
111         bool match(const ex & pattern) const;
112         bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
113         ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const { return bp->subs(ls, lr, no_pattern); }
114         ex subs(const ex & e, bool no_pattern = false) const { return bp->subs(e, no_pattern); }
115         exvector get_free_indices(void) const { return bp->get_free_indices(); }
116         ex simplify_indexed(void) const;
117         ex simplify_indexed(const scalar_products & sp) const;
118         ex symmetrize(void) const;
119         ex symmetrize(const lst & l) const;
120         ex antisymmetrize(void) const;
121         ex antisymmetrize(const lst & l) const;
122         ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
123         ex operator[](const ex & index) const;
124         ex operator[](int i) const;
125         ex op(int i) const { return bp->op(i); }
126         ex & let_op(int i);
127         ex lhs(void) const;
128         ex rhs(void) const;
129         int compare(const ex & other) const;
130         bool is_equal(const ex & other) const;
131         bool is_zero(void) const { return is_equal(_ex0()); }
132         
133         unsigned return_type(void) const { return bp->return_type(); }
134         unsigned return_type_tinfo(void) const { return bp->return_type_tinfo(); }
135         unsigned gethash(void) const { return bp->gethash(); }
136 private:
137         void construct_from_basic(const basic & other);
138         void construct_from_int(int i);
139         void construct_from_uint(unsigned int i);
140         void construct_from_long(long i);
141         void construct_from_ulong(unsigned long i);
142         void construct_from_double(double d);
143         void construct_from_string_and_lst(const std::string &s, const ex &l);
144         void makewriteable();
145
146 #ifdef OBSCURE_CINT_HACK
147 public:
148         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
149         {
150                 if (last_created_or_assigned_bp==0) return false;
151                 if ((last_created_or_assigned_bp->flags &
152                          status_flags::dynallocated)==0) return false;
153                 if ((last_created_or_assigned_bp->flags &
154                          status_flags::evaluated)==0) return false;
155                 return true;
156         }
157 protected:
158         void update_last_created_or_assigned_bp(void)
159         {
160                 if (last_created_or_assigned_bp!=0) {
161                         if (--last_created_or_assigned_bp->refcount == 0) {
162                                 delete last_created_or_assigned_bp;
163                         }
164                 }
165                 last_created_or_assigned_bp = bp;
166                 ++last_created_or_assigned_bp->refcount;
167                 last_created_or_assigned_exp = (long)(void *)(this);
168         }
169 #endif // def OBSCURE_CINT_HACK
170
171 // member variables
172
173 public:
174         basic *bp;      ///< pointer to basic object managed by this
175 #ifdef OBSCURE_CINT_HACK
176         static basic * last_created_or_assigned_bp;
177         static basic * dummy_bp;
178         static long last_created_or_assigned_exp;
179 #endif // def OBSCURE_CINT_HACK
180 };
181
182
183 // performance-critical inlined method implementations
184
185 inline
186 ex::ex() : bp(_ex0().bp)
187 {
188         /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
189         GINAC_ASSERT(_ex0().bp!=0);
190         GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
191         GINAC_ASSERT(bp!=0);
192         ++bp->refcount;
193 #ifdef OBSCURE_CINT_HACK
194         update_last_created_or_assigned_bp();
195 #endif // def OBSCURE_CINT_HACK
196 }
197
198 inline
199 ex::~ex()
200 {
201         /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
202         GINAC_ASSERT(bp!=0);
203         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
204         if (--bp->refcount == 0)
205                 delete bp;
206 }
207
208 inline
209 ex::ex(const ex & other) : bp(other.bp)
210 {
211         /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
212         GINAC_ASSERT(bp!=0);
213         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
214         ++bp->refcount;
215 #ifdef OBSCURE_CINT_HACK
216         update_last_created_or_assigned_bp();
217 #endif // def OBSCURE_CINT_HACK
218 }
219
220 inline
221 ex & ex::operator=(const ex & other)
222 {
223         /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
224         GINAC_ASSERT(bp!=0);
225         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
226         GINAC_ASSERT(other.bp!=0);
227         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
228         // NB: must first increment other.bp->refcount, since other might be *this.
229         ++other.bp->refcount;
230         if (--bp->refcount==0)
231                 delete bp;
232         bp = other.bp;
233 #ifdef OBSCURE_CINT_HACK
234         update_last_created_or_assigned_bp();
235 #endif // def OBSCURE_CINT_HACK
236         return *this;
237 }
238
239 inline
240 ex::ex(const basic & other)
241 {
242         /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
243         construct_from_basic(other);
244 #ifdef OBSCURE_CINT_HACK
245         update_last_created_or_assigned_bp();
246 #endif // def OBSCURE_CINT_HACK
247 }
248
249 inline
250 ex::ex(int i)
251 {
252         /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
253         construct_from_int(i);
254 #ifdef OBSCURE_CINT_HACK
255         update_last_created_or_assigned_bp();
256 #endif // def OBSCURE_CINT_HACK
257 }
258
259 inline
260 ex::ex(unsigned int i)
261 {
262         /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
263         construct_from_uint(i);
264 #ifdef OBSCURE_CINT_HACK
265         update_last_created_or_assigned_bp();
266 #endif // def OBSCURE_CINT_HACK
267 }
268
269 inline
270 ex::ex(long i)
271 {
272         /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
273         construct_from_long(i);
274 #ifdef OBSCURE_CINT_HACK
275         update_last_created_or_assigned_bp();
276 #endif // def OBSCURE_CINT_HACK
277 }
278
279 inline
280 ex::ex(unsigned long i)
281 {
282         /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
283         construct_from_ulong(i);
284 #ifdef OBSCURE_CINT_HACK
285         update_last_created_or_assigned_bp();
286 #endif // def OBSCURE_CINT_HACK
287 }
288
289 inline
290 ex::ex(double const d)
291 {
292         /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
293         construct_from_double(d);
294 #ifdef OBSCURE_CINT_HACK
295         update_last_created_or_assigned_bp();
296 #endif // def OBSCURE_CINT_HACK
297 }
298
299 inline
300 ex::ex(const std::string &s, const ex &l)
301 {
302         /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
303         construct_from_string_and_lst(s, l);
304 #ifdef OBSCURE_CINT_HACK
305         update_last_created_or_assigned_bp();
306 #endif // def OBSCURE_CINT_HACK
307 }
308
309 inline
310 int ex::compare(const ex & other) const
311 {
312         GINAC_ASSERT(bp!=0);
313         GINAC_ASSERT(other.bp!=0);
314         if (bp==other.bp)  // trivial case: both expressions point to same basic
315                 return 0;
316         return bp->compare(*other.bp);
317 }
318
319 inline
320 bool ex::is_equal(const ex & other) const
321 {
322         GINAC_ASSERT(bp!=0);
323         GINAC_ASSERT(other.bp!=0);
324         if (bp==other.bp)  // trivial case: both expressions point to same basic
325                 return true;
326         return bp->is_equal(*other.bp);
327 }
328
329
330 // utility functions
331 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
332 {
333         return e1.bp == e2.bp;
334 }
335
336 // wrapper functions around member functions
337 inline unsigned nops(const ex & thisex)
338 { return thisex.nops(); }
339
340 inline ex expand(const ex & thisex, unsigned options = 0)
341 { return thisex.expand(options); }
342
343 inline bool has(const ex & thisex, const ex & other)
344 { return thisex.has(other); }
345
346 inline int degree(const ex & thisex, const ex & s)
347 { return thisex.degree(s); }
348
349 inline int ldegree(const ex & thisex, const ex & s)
350 { return thisex.ldegree(s); }
351
352 inline ex coeff(const ex & thisex, const ex & s, int n=1)
353 { return thisex.coeff(s, n); }
354
355 inline ex numer(const ex & thisex)
356 { return thisex.numer(); }
357
358 inline ex denom(const ex & thisex)
359 { return thisex.denom(); }
360
361 inline ex numer_denom(const ex & thisex)
362 { return thisex.numer_denom(); }
363
364 inline ex normal(const ex & thisex, int level=0)
365 { return thisex.normal(level); }
366
367 inline ex to_rational(const ex & thisex, lst & repl_lst)
368 { return thisex.to_rational(repl_lst); }
369
370 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
371 { return thisex.collect(s, distributed); }
372
373 inline ex eval(const ex & thisex, int level = 0)
374 { return thisex.eval(level); }
375
376 inline ex evalf(const ex & thisex, int level = 0)
377 { return thisex.evalf(level); }
378
379 inline ex evalm(const ex & thisex)
380 { return thisex.evalm(); }
381
382 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
383 { return thisex.diff(s, nth); }
384
385 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
386 { return thisex.series(r, order, options); }
387
388 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
389 { return thisex.match(pattern, repl_lst); }
390
391 inline ex subs(const ex & thisex, const ex & e)
392 { return thisex.subs(e); }
393
394 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
395 { return thisex.subs(ls, lr); }
396
397 inline ex simplify_indexed(const ex & thisex)
398 { return thisex.simplify_indexed(); }
399
400 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
401 { return thisex.simplify_indexed(sp); }
402
403 inline ex symmetrize(const ex & thisex)
404 { return thisex.symmetrize(); }
405
406 inline ex symmetrize(const ex & thisex, const lst & l)
407 { return thisex.symmetrize(l); }
408
409 inline ex antisymmetrize(const ex & thisex)
410 { return thisex.antisymmetrize(); }
411
412 inline ex antisymmetrize(const ex & thisex, const lst & l)
413 { return thisex.antisymmetrize(l); }
414
415 inline ex op(const ex & thisex, int i)
416 { return thisex.op(i); }
417
418 inline ex lhs(const ex & thisex)
419 { return thisex.lhs(); }
420
421 inline ex rhs(const ex & thisex)
422 { return thisex.rhs(); }
423
424 inline bool is_zero(const ex & thisex)
425 { return thisex.is_zero(); }
426
427 inline void swap(ex & e1, ex & e2)
428 { e1.swap(e2); }
429
430 } // namespace GiNaC
431
432 #endif // ndef __GINAC_EX_H__