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