]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
- do something about the mad cast disease.
[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 #include <functional>
30
31 namespace GiNaC {
32
33 // Sorry, this is the only constant to pollute the global scope, the other ones
34 // are defined in utils.h and not visible from outside.
35 class ex;
36 extern const ex & _ex0(void);     ///<  single ex(numeric(0))
37
38 class symbol;
39 class lst;
40 class scalar_products;
41
42
43 /** Lightweight wrapper for GiNaC's symbolic objects.  Basically all it does is
44  *  to hold a pointer to the other objects, manage the reference counting and
45  *  provide methods for manipulation of these objects.  (Some people call such
46  *  a thing a proxy class.) */
47 class ex
48 {
49         friend class basic;
50         
51 // member functions
52         
53         // default ctor, dtor, copy ctor assignment operator and helpers
54 public:
55         ex();
56         ~ex();
57         ex(const ex & other);
58         ex & operator=(const ex & other);
59         // other ctors
60 public:
61         ex(const basic & other);
62         ex(int i);
63         ex(unsigned int i);
64         ex(long i);
65         ex(unsigned long i);
66         ex(double const d);
67         /** Construct ex from string and a list of symbols. The input grammar is
68          *  similar to the GiNaC output format. All symbols to be used in the
69          *  expression must be specified in a lst in the second argument. Undefined
70          *  symbols and other parser errors will throw an exception. */
71         ex(const std::string &s, const ex &l);
72         
73         // functions overriding virtual functions from bases classes
74         // none
75         
76         // new virtual functions which can be overridden by derived classes
77         // none
78
79         // non-virtual functions in this class
80 public:
81         void swap(ex & other);
82         void print(const print_context & c, unsigned level = 0) const;
83         void printtree(std::ostream & os) const;
84         void dbgprint(void) const;
85         void dbgprinttree(void) const;
86         bool info(unsigned inf) const { return bp->info(inf); }
87         unsigned nops() const { return bp->nops(); }
88         ex expand(unsigned options=0) const;
89         bool has(const ex & other) const { return bp->has(other); }
90         ex map(map_function & f) const { return bp->map(f); }
91         ex map(ex (*f)(const ex & e)) const;
92         int degree(const ex & s) const { return bp->degree(s); }
93         int ldegree(const ex & s) const { return bp->ldegree(s); }
94         ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
95         ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
96         ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
97         ex numer(void) const;
98         ex denom(void) const;
99         ex numer_denom(void) const;
100         ex unit(const symbol &x) const;
101         ex content(const symbol &x) const;
102         numeric integer_content(void) const;
103         ex primpart(const symbol &x) const;
104         ex primpart(const symbol &x, const ex &cont) const;
105         ex normal(int level = 0) const;
106         ex to_rational(lst &repl_lst) const;
107         ex smod(const numeric &xi) const;
108         numeric max_coefficient(void) const;
109         ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
110         ex eval(int level = 0) const { return bp->eval(level); }
111         ex evalf(int level = 0) const { return bp->evalf(level); }
112         ex evalm(void) const { return bp->evalm(); }
113         ex diff(const symbol & s, unsigned nth = 1) const;
114         ex series(const ex & r, int order, unsigned options = 0) const;
115         bool match(const ex & pattern) const;
116         bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
117         ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const { return bp->subs(ls, lr, no_pattern); }
118         ex subs(const ex & e, bool no_pattern = false) const { return bp->subs(e, no_pattern); }
119         exvector get_free_indices(void) const { return bp->get_free_indices(); }
120         ex simplify_indexed(void) const;
121         ex simplify_indexed(const scalar_products & sp) const;
122         ex symmetrize(void) const;
123         ex symmetrize(const lst & l) const;
124         ex antisymmetrize(void) const;
125         ex antisymmetrize(const lst & l) const;
126         ex symmetrize_cyclic(void) const;
127         ex symmetrize_cyclic(const lst & l) const;
128         ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
129         ex operator[](const ex & index) const;
130         ex operator[](int i) const;
131         ex op(int i) const { return bp->op(i); }
132         ex & let_op(int i);
133         ex lhs(void) const;
134         ex rhs(void) const;
135         int compare(const ex & other) const;
136         bool is_equal(const ex & other) const;
137         bool is_zero(void) const { return is_equal(_ex0()); }
138         
139         unsigned return_type(void) const { return bp->return_type(); }
140         unsigned return_type_tinfo(void) const { return bp->return_type_tinfo(); }
141         unsigned gethash(void) const { return bp->gethash(); }
142 private:
143         void construct_from_basic(const basic & other);
144         void construct_from_int(int i);
145         void construct_from_uint(unsigned int i);
146         void construct_from_long(long i);
147         void construct_from_ulong(unsigned long i);
148         void construct_from_double(double d);
149         void construct_from_string_and_lst(const std::string &s, const ex &l);
150         void makewriteable();
151
152 #ifdef OBSCURE_CINT_HACK
153 public:
154         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
155         {
156                 if (last_created_or_assigned_bp==0) return false;
157                 if ((last_created_or_assigned_bp->flags &
158                          status_flags::dynallocated)==0) return false;
159                 if ((last_created_or_assigned_bp->flags &
160                          status_flags::evaluated)==0) return false;
161                 return true;
162         }
163 protected:
164         void update_last_created_or_assigned_bp(void)
165         {
166                 if (last_created_or_assigned_bp!=0) {
167                         if (--last_created_or_assigned_bp->refcount == 0) {
168                                 delete last_created_or_assigned_bp;
169                         }
170                 }
171                 last_created_or_assigned_bp = bp;
172                 ++last_created_or_assigned_bp->refcount;
173                 last_created_or_assigned_exp = (long)(void *)(this);
174         }
175 #endif // def OBSCURE_CINT_HACK
176
177 // member variables
178
179 public:
180         basic *bp;      ///< pointer to basic object managed by this
181 #ifdef OBSCURE_CINT_HACK
182         static basic * last_created_or_assigned_bp;
183         static basic * dummy_bp;
184         static long last_created_or_assigned_exp;
185 #endif // def OBSCURE_CINT_HACK
186 };
187
188
189 // performance-critical inlined method implementations
190
191 inline
192 ex::ex() : bp(_ex0().bp)
193 {
194         /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
195         GINAC_ASSERT(_ex0().bp!=0);
196         GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
197         GINAC_ASSERT(bp!=0);
198         ++bp->refcount;
199 #ifdef OBSCURE_CINT_HACK
200         update_last_created_or_assigned_bp();
201 #endif // def OBSCURE_CINT_HACK
202 }
203
204 inline
205 ex::~ex()
206 {
207         /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
208         GINAC_ASSERT(bp!=0);
209         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
210         if (--bp->refcount == 0)
211                 delete bp;
212 }
213
214 inline
215 ex::ex(const ex & other) : bp(other.bp)
216 {
217         /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
218         GINAC_ASSERT(bp!=0);
219         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
220         ++bp->refcount;
221 #ifdef OBSCURE_CINT_HACK
222         update_last_created_or_assigned_bp();
223 #endif // def OBSCURE_CINT_HACK
224 }
225
226 inline
227 ex & ex::operator=(const ex & other)
228 {
229         /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
230         GINAC_ASSERT(bp!=0);
231         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
232         GINAC_ASSERT(other.bp!=0);
233         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
234         // NB: must first increment other.bp->refcount, since other might be *this.
235         ++other.bp->refcount;
236         if (--bp->refcount==0)
237                 delete bp;
238         bp = other.bp;
239 #ifdef OBSCURE_CINT_HACK
240         update_last_created_or_assigned_bp();
241 #endif // def OBSCURE_CINT_HACK
242         return *this;
243 }
244
245 inline
246 ex::ex(const basic & other)
247 {
248         /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
249         construct_from_basic(other);
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(int i)
257 {
258         /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
259         construct_from_int(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(unsigned int i)
267 {
268         /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
269         construct_from_uint(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(long i)
277 {
278         /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
279         construct_from_long(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(unsigned long i)
287 {
288         /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
289         construct_from_ulong(i);
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(double const d)
297 {
298         /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
299         construct_from_double(d);
300 #ifdef OBSCURE_CINT_HACK
301         update_last_created_or_assigned_bp();
302 #endif // def OBSCURE_CINT_HACK
303 }
304
305 inline
306 ex::ex(const std::string &s, const ex &l)
307 {
308         /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
309         construct_from_string_and_lst(s, l);
310 #ifdef OBSCURE_CINT_HACK
311         update_last_created_or_assigned_bp();
312 #endif // def OBSCURE_CINT_HACK
313 }
314
315 inline
316 int ex::compare(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 0;
322         return bp->compare(*other.bp);
323 }
324
325 inline
326 bool ex::is_equal(const ex & other) const
327 {
328         GINAC_ASSERT(bp!=0);
329         GINAC_ASSERT(other.bp!=0);
330         if (bp==other.bp)  // trivial case: both expressions point to same basic
331                 return true;
332         return bp->is_equal(*other.bp);
333 }
334
335
336 // utility functions
337 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
338 {
339         return e1.bp == e2.bp;
340 }
341
342 // wrapper functions around member functions
343 inline unsigned nops(const ex & thisex)
344 { return thisex.nops(); }
345
346 inline ex expand(const ex & thisex, unsigned options = 0)
347 { return thisex.expand(options); }
348
349 inline bool has(const ex & thisex, const ex & other)
350 { return thisex.has(other); }
351
352 inline int degree(const ex & thisex, const ex & s)
353 { return thisex.degree(s); }
354
355 inline int ldegree(const ex & thisex, const ex & s)
356 { return thisex.ldegree(s); }
357
358 inline ex coeff(const ex & thisex, const ex & s, int n=1)
359 { return thisex.coeff(s, n); }
360
361 inline ex numer(const ex & thisex)
362 { return thisex.numer(); }
363
364 inline ex denom(const ex & thisex)
365 { return thisex.denom(); }
366
367 inline ex numer_denom(const ex & thisex)
368 { return thisex.numer_denom(); }
369
370 inline ex normal(const ex & thisex, int level=0)
371 { return thisex.normal(level); }
372
373 inline ex to_rational(const ex & thisex, lst & repl_lst)
374 { return thisex.to_rational(repl_lst); }
375
376 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
377 { return thisex.collect(s, distributed); }
378
379 inline ex eval(const ex & thisex, int level = 0)
380 { return thisex.eval(level); }
381
382 inline ex evalf(const ex & thisex, int level = 0)
383 { return thisex.evalf(level); }
384
385 inline ex evalm(const ex & thisex)
386 { return thisex.evalm(); }
387
388 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
389 { return thisex.diff(s, nth); }
390
391 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
392 { return thisex.series(r, order, options); }
393
394 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
395 { return thisex.match(pattern, repl_lst); }
396
397 inline ex subs(const ex & thisex, const ex & e)
398 { return thisex.subs(e); }
399
400 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
401 { return thisex.subs(ls, lr); }
402
403 inline ex simplify_indexed(const ex & thisex)
404 { return thisex.simplify_indexed(); }
405
406 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
407 { return thisex.simplify_indexed(sp); }
408
409 inline ex symmetrize(const ex & thisex)
410 { return thisex.symmetrize(); }
411
412 inline ex symmetrize(const ex & thisex, const lst & l)
413 { return thisex.symmetrize(l); }
414
415 inline ex antisymmetrize(const ex & thisex)
416 { return thisex.antisymmetrize(); }
417
418 inline ex antisymmetrize(const ex & thisex, const lst & l)
419 { return thisex.antisymmetrize(l); }
420
421 inline ex symmetrize_cyclic(const ex & thisex)
422 { return thisex.symmetrize_cyclic(); }
423
424 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
425 { return thisex.symmetrize_cyclic(l); }
426
427 inline ex op(const ex & thisex, int i)
428 { return thisex.op(i); }
429
430 inline ex lhs(const ex & thisex)
431 { return thisex.lhs(); }
432
433 inline ex rhs(const ex & thisex)
434 { return thisex.rhs(); }
435
436 inline bool is_zero(const ex & thisex)
437 { return thisex.is_zero(); }
438
439 inline void swap(ex & e1, ex & e2)
440 { e1.swap(e2); }
441
442
443 /* Function objects for STL sort() etc. */
444 struct ex_is_less : public std::binary_function<ex, ex, bool> {
445         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
446 };
447
448 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
449         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
450 };
451
452 struct ex_swap : public std::binary_function<ex, ex, void> {
453         void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
454 };
455
456
457 /* Convert function pointer to function object suitable for map(). */
458 class pointer_to_map_function : public map_function {
459 protected:
460         ex (*ptr)(const ex &);
461 public:
462         explicit pointer_to_map_function(ex (*x)(const ex &)) : ptr(x) {}
463         ex operator()(const ex & e) { return ptr(e); }
464 };
465
466 template<class T1>
467 class pointer_to_map_function_1arg : public map_function {
468 protected:
469         ex (*ptr)(const ex &, T1);
470         T1 arg1;
471 public:
472         explicit pointer_to_map_function_1arg(ex (*x)(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
473         ex operator()(const ex & e) { return ptr(e, arg1); }
474 };
475
476 template<class T1, class T2>
477 class pointer_to_map_function_2args : public map_function {
478 protected:
479         ex (*ptr)(const ex &, T1, T2);
480         T1 arg1;
481         T2 arg2;
482 public:
483         explicit pointer_to_map_function_2args(ex (*x)(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
484         ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
485 };
486
487 template<class T1, class T2, class T3>
488 class pointer_to_map_function_3args : public map_function {
489 protected:
490         ex (*ptr)(const ex &, T1, T2, T3);
491         T1 arg1;
492         T2 arg2;
493         T3 arg3;
494 public:
495         explicit pointer_to_map_function_3args(ex (*x)(const ex &, T1, T2, T3), T1 a1, T2 a2, T3 a3) : ptr(x), arg1(a1), arg2(a2), arg3(a3) {}
496         ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
497 };
498
499 inline ex ex::map(ex (*f)(const ex & e)) const
500 {
501         pointer_to_map_function fcn(f);
502         return bp->map(fcn);
503 }
504
505
506 } // namespace GiNaC
507
508 #endif // ndef __GINAC_EX_H__