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