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