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