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