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