]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
- ginac-config.1: removed (duplicate).
[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         // non-virtual functions in this class
74 public:
75         void swap(ex & other);
76         void print(const print_context & c, unsigned level = 0) const;
77         void printtree(std::ostream & os) const;
78         void dbgprint(void) const;
79         void dbgprinttree(void) const;
80         bool info(unsigned inf) const { return bp->info(inf); }
81         unsigned nops() const { return bp->nops(); }
82         ex expand(unsigned options=0) const;
83         bool has(const ex & pattern) const { return bp->has(pattern); }
84         ex map(map_function & f) const { return bp->map(f); }
85         ex map(ex (*f)(const ex & e)) const;
86         bool find(const ex & pattern, lst & found) const;
87         int degree(const ex & s) const { return bp->degree(s); }
88         int ldegree(const ex & s) const { return bp->ldegree(s); }
89         ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
90         ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
91         ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
92         ex numer(void) const;
93         ex denom(void) const;
94         ex numer_denom(void) const;
95         ex unit(const symbol &x) const;
96         ex content(const symbol &x) const;
97         numeric integer_content(void) const;
98         ex primpart(const symbol &x) const;
99         ex primpart(const symbol &x, const ex &cont) const;
100         ex normal(int level = 0) const;
101         ex to_rational(lst &repl_lst) const;
102         ex smod(const numeric &xi) const;
103         numeric max_coefficient(void) const;
104         ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
105         ex eval(int level = 0) const { return bp->eval(level); }
106         ex evalf(int level = 0) const { return bp->evalf(level); }
107         ex evalm(void) const { return bp->evalm(); }
108         ex diff(const symbol & s, unsigned nth = 1) const;
109         ex series(const ex & r, int order, unsigned options = 0) const;
110         bool match(const ex & pattern) const;
111         bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
112         ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const { return bp->subs(ls, lr, no_pattern); }
113         ex subs(const ex & e, bool no_pattern = false) const { return bp->subs(e, no_pattern); }
114         exvector get_free_indices(void) const { return bp->get_free_indices(); }
115         ex simplify_indexed(void) const;
116         ex simplify_indexed(const scalar_products & sp) const;
117         ex symmetrize(void) const;
118         ex symmetrize(const lst & l) const;
119         ex antisymmetrize(void) const;
120         ex antisymmetrize(const lst & l) const;
121         ex symmetrize_cyclic(void) const;
122         ex symmetrize_cyclic(const lst & l) const;
123         ex simplify_ncmul(const exvector & v) const { return bp->simplify_ncmul(v); }
124         ex operator[](const ex & index) const;
125         ex operator[](int i) const;
126         ex op(int i) const { return bp->op(i); }
127         ex & let_op(int i);
128         ex lhs(void) const;
129         ex rhs(void) const;
130         int compare(const ex & other) const;
131         bool is_equal(const ex & other) const;
132         bool is_zero(void) const { return is_equal(_ex0()); }
133         
134         unsigned return_type(void) const { return bp->return_type(); }
135         unsigned return_type_tinfo(void) const { return bp->return_type_tinfo(); }
136         unsigned gethash(void) const { return bp->gethash(); }
137 private:
138         void construct_from_basic(const basic & other);
139         void construct_from_int(int i);
140         void construct_from_uint(unsigned int i);
141         void construct_from_long(long i);
142         void construct_from_ulong(unsigned long i);
143         void construct_from_double(double d);
144         void construct_from_string_and_lst(const std::string &s, const ex &l);
145         void makewriteable();
146
147 #ifdef OBSCURE_CINT_HACK
148 public:
149         static bool last_created_or_assigned_bp_can_be_converted_to_ex(void)
150         {
151                 if (last_created_or_assigned_bp==0) return false;
152                 if ((last_created_or_assigned_bp->flags &
153                          status_flags::dynallocated)==0) return false;
154                 if ((last_created_or_assigned_bp->flags &
155                          status_flags::evaluated)==0) return false;
156                 return true;
157         }
158 protected:
159         void update_last_created_or_assigned_bp(void)
160         {
161                 if (last_created_or_assigned_bp!=0) {
162                         if (--last_created_or_assigned_bp->refcount == 0) {
163                                 delete last_created_or_assigned_bp;
164                         }
165                 }
166                 last_created_or_assigned_bp = bp;
167                 ++last_created_or_assigned_bp->refcount;
168                 last_created_or_assigned_exp = (long)(void *)(this);
169         }
170 #endif // def OBSCURE_CINT_HACK
171
172 // member variables
173
174 public:
175         basic *bp;      ///< pointer to basic object managed by this
176 #ifdef OBSCURE_CINT_HACK
177         static basic * last_created_or_assigned_bp;
178         static basic * dummy_bp;
179         static long last_created_or_assigned_exp;
180 #endif // def OBSCURE_CINT_HACK
181 };
182
183
184 // performance-critical inlined method implementations
185
186 inline
187 ex::ex() : bp(_ex0().bp)
188 {
189         /*debugmsg("ex default ctor",LOGLEVEL_CONSTRUCT);*/
190         GINAC_ASSERT(_ex0().bp!=0);
191         GINAC_ASSERT(_ex0().bp->flags & status_flags::dynallocated);
192         GINAC_ASSERT(bp!=0);
193         ++bp->refcount;
194 #ifdef OBSCURE_CINT_HACK
195         update_last_created_or_assigned_bp();
196 #endif // def OBSCURE_CINT_HACK
197 }
198
199 inline
200 ex::~ex()
201 {
202         /*debugmsg("ex dtor",LOGLEVEL_DESTRUCT);*/
203         GINAC_ASSERT(bp!=0);
204         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
205         if (--bp->refcount == 0)
206                 delete bp;
207 }
208
209 inline
210 ex::ex(const ex & other) : bp(other.bp)
211 {
212         /*debugmsg("ex copy ctor",LOGLEVEL_CONSTRUCT);*/
213         GINAC_ASSERT(bp!=0);
214         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
215         ++bp->refcount;
216 #ifdef OBSCURE_CINT_HACK
217         update_last_created_or_assigned_bp();
218 #endif // def OBSCURE_CINT_HACK
219 }
220
221 inline
222 ex & ex::operator=(const ex & other)
223 {
224         /*debugmsg("ex operator=",LOGLEVEL_ASSIGNMENT);*/
225         GINAC_ASSERT(bp!=0);
226         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
227         GINAC_ASSERT(other.bp!=0);
228         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
229         // NB: must first increment other.bp->refcount, since other might be *this.
230         ++other.bp->refcount;
231         if (--bp->refcount==0)
232                 delete bp;
233         bp = other.bp;
234 #ifdef OBSCURE_CINT_HACK
235         update_last_created_or_assigned_bp();
236 #endif // def OBSCURE_CINT_HACK
237         return *this;
238 }
239
240 inline
241 ex::ex(const basic & other)
242 {
243         /*debugmsg("ex ctor from basic",LOGLEVEL_CONSTRUCT);*/
244         construct_from_basic(other);
245 #ifdef OBSCURE_CINT_HACK
246         update_last_created_or_assigned_bp();
247 #endif // def OBSCURE_CINT_HACK
248 }
249
250 inline
251 ex::ex(int i)
252 {
253         /*debugmsg("ex ctor from int",LOGLEVEL_CONSTRUCT);*/
254         construct_from_int(i);
255 #ifdef OBSCURE_CINT_HACK
256         update_last_created_or_assigned_bp();
257 #endif // def OBSCURE_CINT_HACK
258 }
259
260 inline
261 ex::ex(unsigned int i)
262 {
263         /*debugmsg("ex ctor from unsigned int",LOGLEVEL_CONSTRUCT);*/
264         construct_from_uint(i);
265 #ifdef OBSCURE_CINT_HACK
266         update_last_created_or_assigned_bp();
267 #endif // def OBSCURE_CINT_HACK
268 }
269
270 inline
271 ex::ex(long i)
272 {
273         /*debugmsg("ex ctor from long",LOGLEVEL_CONSTRUCT);*/
274         construct_from_long(i);
275 #ifdef OBSCURE_CINT_HACK
276         update_last_created_or_assigned_bp();
277 #endif // def OBSCURE_CINT_HACK
278 }
279
280 inline
281 ex::ex(unsigned long i)
282 {
283         /*debugmsg("ex ctor from unsigned long",LOGLEVEL_CONSTRUCT);*/
284         construct_from_ulong(i);
285 #ifdef OBSCURE_CINT_HACK
286         update_last_created_or_assigned_bp();
287 #endif // def OBSCURE_CINT_HACK
288 }
289
290 inline
291 ex::ex(double const d)
292 {
293         /*debugmsg("ex ctor from double",LOGLEVEL_CONSTRUCT);*/
294         construct_from_double(d);
295 #ifdef OBSCURE_CINT_HACK
296         update_last_created_or_assigned_bp();
297 #endif // def OBSCURE_CINT_HACK
298 }
299
300 inline
301 ex::ex(const std::string &s, const ex &l)
302 {
303         /*debugmsg("ex ctor from string,lst",LOGLEVEL_CONSTRUCT);*/
304         construct_from_string_and_lst(s, l);
305 #ifdef OBSCURE_CINT_HACK
306         update_last_created_or_assigned_bp();
307 #endif // def OBSCURE_CINT_HACK
308 }
309
310 inline
311 int ex::compare(const ex & other) const
312 {
313         GINAC_ASSERT(bp!=0);
314         GINAC_ASSERT(other.bp!=0);
315         if (bp==other.bp)  // trivial case: both expressions point to same basic
316                 return 0;
317         return bp->compare(*other.bp);
318 }
319
320 inline
321 bool ex::is_equal(const ex & other) const
322 {
323         GINAC_ASSERT(bp!=0);
324         GINAC_ASSERT(other.bp!=0);
325         if (bp==other.bp)  // trivial case: both expressions point to same basic
326                 return true;
327         return bp->is_equal(*other.bp);
328 }
329
330
331 // utility functions
332 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
333 {
334         return e1.bp == e2.bp;
335 }
336
337 // wrapper functions around member functions
338 inline unsigned nops(const ex & thisex)
339 { return thisex.nops(); }
340
341 inline ex expand(const ex & thisex, unsigned options = 0)
342 { return thisex.expand(options); }
343
344 inline bool has(const ex & thisex, const ex & pattern)
345 { return thisex.has(pattern); }
346
347 inline bool find(const ex & thisex, const ex & pattern, lst & found)
348 { return thisex.find(pattern, found); }
349
350 inline int degree(const ex & thisex, const ex & s)
351 { return thisex.degree(s); }
352
353 inline int ldegree(const ex & thisex, const ex & s)
354 { return thisex.ldegree(s); }
355
356 inline ex coeff(const ex & thisex, const ex & s, int n=1)
357 { return thisex.coeff(s, n); }
358
359 inline ex numer(const ex & thisex)
360 { return thisex.numer(); }
361
362 inline ex denom(const ex & thisex)
363 { return thisex.denom(); }
364
365 inline ex numer_denom(const ex & thisex)
366 { return thisex.numer_denom(); }
367
368 inline ex normal(const ex & thisex, int level=0)
369 { return thisex.normal(level); }
370
371 inline ex to_rational(const ex & thisex, lst & repl_lst)
372 { return thisex.to_rational(repl_lst); }
373
374 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
375 { return thisex.collect(s, distributed); }
376
377 inline ex eval(const ex & thisex, int level = 0)
378 { return thisex.eval(level); }
379
380 inline ex evalf(const ex & thisex, int level = 0)
381 { return thisex.evalf(level); }
382
383 inline ex evalm(const ex & thisex)
384 { return thisex.evalm(); }
385
386 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
387 { return thisex.diff(s, nth); }
388
389 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
390 { return thisex.series(r, order, options); }
391
392 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
393 { return thisex.match(pattern, repl_lst); }
394
395 inline ex subs(const ex & thisex, const ex & e)
396 { return thisex.subs(e); }
397
398 inline ex subs(const ex & thisex, const lst & ls, const lst & lr)
399 { return thisex.subs(ls, lr); }
400
401 inline ex simplify_indexed(const ex & thisex)
402 { return thisex.simplify_indexed(); }
403
404 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
405 { return thisex.simplify_indexed(sp); }
406
407 inline ex symmetrize(const ex & thisex)
408 { return thisex.symmetrize(); }
409
410 inline ex symmetrize(const ex & thisex, const lst & l)
411 { return thisex.symmetrize(l); }
412
413 inline ex antisymmetrize(const ex & thisex)
414 { return thisex.antisymmetrize(); }
415
416 inline ex antisymmetrize(const ex & thisex, const lst & l)
417 { return thisex.antisymmetrize(l); }
418
419 inline ex symmetrize_cyclic(const ex & thisex)
420 { return thisex.symmetrize_cyclic(); }
421
422 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
423 { return thisex.symmetrize_cyclic(l); }
424
425 inline ex op(const ex & thisex, int i)
426 { return thisex.op(i); }
427
428 inline ex lhs(const ex & thisex)
429 { return thisex.lhs(); }
430
431 inline ex rhs(const ex & thisex)
432 { return thisex.rhs(); }
433
434 inline bool is_zero(const ex & thisex)
435 { return thisex.is_zero(); }
436
437 inline void swap(ex & e1, ex & e2)
438 { e1.swap(e2); }
439
440
441 /* Function objects for STL sort() etc. */
442 struct ex_is_less : public std::binary_function<ex, ex, bool> {
443         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
444 };
445
446 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
447         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
448 };
449
450 struct ex_swap : public std::binary_function<ex, ex, void> {
451         void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
452 };
453
454
455 /* Convert function pointer to function object suitable for map(). */
456 class pointer_to_map_function : public map_function {
457 protected:
458         ex (*ptr)(const ex &);
459 public:
460         explicit pointer_to_map_function(ex (*x)(const ex &)) : ptr(x) {}
461         ex operator()(const ex & e) { return ptr(e); }
462 };
463
464 template<class T1>
465 class pointer_to_map_function_1arg : public map_function {
466 protected:
467         ex (*ptr)(const ex &, T1);
468         T1 arg1;
469 public:
470         explicit pointer_to_map_function_1arg(ex (*x)(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
471         ex operator()(const ex & e) { return ptr(e, arg1); }
472 };
473
474 template<class T1, class T2>
475 class pointer_to_map_function_2args : public map_function {
476 protected:
477         ex (*ptr)(const ex &, T1, T2);
478         T1 arg1;
479         T2 arg2;
480 public:
481         explicit pointer_to_map_function_2args(ex (*x)(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
482         ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
483 };
484
485 template<class T1, class T2, class T3>
486 class pointer_to_map_function_3args : public map_function {
487 protected:
488         ex (*ptr)(const ex &, T1, T2, T3);
489         T1 arg1;
490         T2 arg2;
491         T3 arg3;
492 public:
493         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) {}
494         ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
495 };
496
497 inline ex ex::map(ex (*f)(const ex & e)) const
498 {
499         pointer_to_map_function fcn(f);
500         return bp->map(fcn);
501 }
502
503
504 } // namespace GiNaC
505
506 #endif // ndef __GINAC_EX_H__