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