]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
log(-<realnumber>) now returns a real number (correct CVS check-in)
[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-2004 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 <iterator>
28 #include <functional>
29 #include <stack>
30
31 #include "basic.h"
32 #include "ptr.h"
33
34 namespace GiNaC {
35
36
37 /** Helper class to initialize the library.  There must be one static object
38  *  of this class in every object file that makes use of our flyweights in
39  *  order to guarantee proper initialization.  Hence we put it into this
40  *  file which is included by every relevant file anyways.  This is modeled
41  *  after section 27.4.2.1.6 of the C++ standard, where cout and friends are
42  *  set up.
43  *
44  *  @see utils.cpp */
45 class library_init {
46 public:
47         library_init();
48         ~library_init();
49 private:
50         static int count;
51 };
52 /** For construction of flyweights, etc. */
53 static library_init library_initializer;
54
55
56 class scalar_products;
57 class const_iterator;
58 class const_preorder_iterator;
59 class const_postorder_iterator;
60
61
62 /** Lightweight wrapper for GiNaC's symbolic objects.  Basically all it does is
63  *  to hold a pointer to the other objects, manage the reference counting and
64  *  provide methods for manipulation of these objects.  (Some people call such
65  *  a thing a proxy class.) */
66 class ex {
67         friend class archive_node;
68         friend inline bool are_ex_trivially_equal(const ex &, const ex &);
69         template<class T> friend inline const T &ex_to(const ex &);
70         template<class T> friend inline bool is_a(const ex &);
71         template<class T> friend inline bool is_exactly_a(const ex &);
72         
73         // default constructor, copy constructor and assignment operator
74 public:
75         ex() throw();
76 #ifdef OBSCURE_CINT_HACK
77         ex(const ex & other);
78         ex & operator=(const ex & other);
79 #endif
80
81         // other constructors
82 public:
83         ex(const basic & other);
84         ex(int i);
85         ex(unsigned int i);
86         ex(long i);
87         ex(unsigned long i);
88         ex(double const d);
89
90         /** Construct ex from string and a list of symbols. The input grammar is
91          *  similar to the GiNaC output format. All symbols and indices to be used
92          *  in the expression must be specified in a lst in the second argument.
93          *  Undefined symbols and other parser errors will throw an exception. */
94         ex(const std::string &s, const ex &l);
95         
96 public:
97         // non-virtual functions in this class
98 public:
99         /** Efficiently swap the contents of two expressions. */
100         void swap(ex & other) throw()
101         {
102                 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
103                 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
104                 bp.swap(other.bp);
105         }
106
107         // iterators
108         const_iterator begin() const throw();
109         const_iterator end() const throw();
110         const_preorder_iterator preorder_begin() const;
111         const_preorder_iterator preorder_end() const throw();
112         const_postorder_iterator postorder_begin() const;
113         const_postorder_iterator postorder_end() const throw();
114
115         // evaluation
116         ex eval(int level = 0) const { return bp->eval(level); }
117         ex evalf(int level = 0) const { return bp->evalf(level); }
118         ex evalm() const { return bp->evalm(); }
119         ex eval_ncmul(const exvector & v) const { return bp->eval_ncmul(v); }
120
121         // printing
122         void print(const print_context & c, unsigned level = 0) const;
123         void dbgprint() const;
124         void dbgprinttree() const;
125
126         // info
127         bool info(unsigned inf) const { return bp->info(inf); }
128
129         // operand access
130         size_t nops() const { return bp->nops(); }
131         ex op(size_t i) const { return bp->op(i); }
132         ex operator[](const ex & index) const { return (*bp)[index]; }
133         ex operator[](size_t i) const { return (*bp)[i]; }
134         ex & let_op(size_t i);
135         ex & operator[](const ex & index);
136         ex & operator[](size_t i);
137         ex lhs() const;
138         ex rhs() const;
139
140         // complex conjugation
141         ex conjugate() const { return bp->conjugate(); }
142
143         // pattern matching
144         bool has(const ex & pattern) const { return bp->has(pattern); }
145         bool find(const ex & pattern, lst & found) const;
146         bool match(const ex & pattern) const;
147         bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
148
149         // substitutions
150         ex subs(const exmap & m, unsigned options = 0) const;
151         ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
152         ex subs(const ex & e, unsigned options = 0) const;
153
154         // function mapping
155         ex map(map_function & f) const { return bp->map(f); }
156         ex map(ex (*f)(const ex & e)) const;
157
158         // visitors and tree traversal
159         void accept(visitor & v) const { bp->accept(v); }
160         void traverse_preorder(visitor & v) const;
161         void traverse_postorder(visitor & v) const;
162         void traverse(visitor & v) const { traverse_preorder(v); }
163
164         // degree/coeff
165         int degree(const ex & s) const { return bp->degree(s); }
166         int ldegree(const ex & s) const { return bp->ldegree(s); }
167         ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
168         ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
169         ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
170
171         // expand/collect
172         ex expand(unsigned options=0) const;
173         ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
174
175         // differentiation and series expansion
176         ex diff(const symbol & s, unsigned nth = 1) const;
177         ex series(const ex & r, int order, unsigned options = 0) const;
178
179         // rational functions
180         ex normal(int level = 0) const;
181         ex to_rational(exmap & repl) const;
182         ex to_rational(lst & repl_lst) const;
183         ex to_polynomial(exmap & repl) const;
184         ex to_polynomial(lst & repl_lst) const;
185         ex numer() const;
186         ex denom() const;
187         ex numer_denom() const;
188
189         // polynomial algorithms
190         ex unit(const ex &x) const;
191         ex content(const ex &x) const;
192         numeric integer_content() const;
193         ex primpart(const ex &x) const;
194         ex primpart(const ex &x, const ex &cont) const;
195         void unitcontprim(const ex &x, ex &u, ex &c, ex &p) const;
196         ex smod(const numeric &xi) const { return bp->smod(xi); }
197         numeric max_coefficient() const;
198
199         // indexed objects
200         exvector get_free_indices() const { return bp->get_free_indices(); }
201         ex simplify_indexed(unsigned options = 0) const;
202         ex simplify_indexed(const scalar_products & sp, unsigned options = 0) const;
203
204         // comparison
205         int compare(const ex & other) const;
206         bool is_equal(const ex & other) const;
207         bool is_zero() const { extern const ex _ex0; return is_equal(_ex0); }
208         
209         // symmetry
210         ex symmetrize() const;
211         ex symmetrize(const lst & l) const;
212         ex antisymmetrize() const;
213         ex antisymmetrize(const lst & l) const;
214         ex symmetrize_cyclic() const;
215         ex symmetrize_cyclic(const lst & l) const;
216
217         // noncommutativity
218         unsigned return_type() const { return bp->return_type(); }
219         unsigned return_type_tinfo() const { return bp->return_type_tinfo(); }
220
221         unsigned gethash() const { return bp->gethash(); }
222
223 private:
224         static ptr<basic> construct_from_basic(const basic & other);
225         static basic & construct_from_int(int i);
226         static basic & construct_from_uint(unsigned int i);
227         static basic & construct_from_long(long i);
228         static basic & construct_from_ulong(unsigned long i);
229         static basic & construct_from_double(double d);
230         static ptr<basic> construct_from_string_and_lst(const std::string &s, const ex &l);
231         void makewriteable();
232         void share(const ex & other) const;
233
234 #ifdef OBSCURE_CINT_HACK
235 public:
236         static bool last_created_or_assigned_bp_can_be_converted_to_ex()
237         {
238                 if (last_created_or_assigned_bp==0) return false;
239                 if ((last_created_or_assigned_bp->flags &
240                          status_flags::dynallocated)==0) return false;
241                 if ((last_created_or_assigned_bp->flags &
242                          status_flags::evaluated)==0) return false;
243                 return true;
244         }
245 protected:
246         void update_last_created_or_assigned_bp()
247         {
248                 last_created_or_assigned_bp = bp;
249                 last_created_or_assigned_exp = (long)(void *)(this);
250         }
251 #endif // def OBSCURE_CINT_HACK
252
253 // member variables
254
255 private:
256         mutable ptr<basic> bp;  ///< pointer to basic object managed by this
257
258 #ifdef OBSCURE_CINT_HACK
259 public:
260         static ptr<basic> last_created_or_assigned_bp;
261         static basic * dummy_bp;
262         static long last_created_or_assigned_exp;
263 #endif // def OBSCURE_CINT_HACK
264 };
265
266
267 // performance-critical inlined method implementations
268
269 // This needs to be a basic* because we don't know that numeric is derived
270 // from basic and we need a basic& for the ex default constructor
271 extern const basic *_num0_bp;
272
273 inline
274 ex::ex() throw() : bp(*const_cast<basic *>(_num0_bp))
275 {
276         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
277 #ifdef OBSCURE_CINT_HACK
278         update_last_created_or_assigned_bp();
279 #endif // def OBSCURE_CINT_HACK
280 }
281
282 #ifdef OBSCURE_CINT_HACK
283 inline
284 ex::ex(const ex & other) : bp(other.bp)
285 {
286         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
287         update_last_created_or_assigned_bp();
288 }
289
290 inline
291 ex & ex::operator=(const ex & other)
292 {
293         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
294         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
295         bp = other.bp;
296         update_last_created_or_assigned_bp();
297         return *this;
298 }
299 #endif // def OBSCURE_CINT_HACK
300
301 inline
302 ex::ex(const basic & other) : bp(construct_from_basic(other))
303 {
304         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
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(int i) : bp(construct_from_int(i))
312 {
313         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
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(unsigned int i) : bp(construct_from_uint(i))
321 {
322         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
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(long i) : bp(construct_from_long(i))
330 {
331         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
332 #ifdef OBSCURE_CINT_HACK
333         update_last_created_or_assigned_bp();
334 #endif // def OBSCURE_CINT_HACK
335 }
336
337 inline
338 ex::ex(unsigned long i) : bp(construct_from_ulong(i))
339 {
340         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
341 #ifdef OBSCURE_CINT_HACK
342         update_last_created_or_assigned_bp();
343 #endif // def OBSCURE_CINT_HACK
344 }
345
346 inline
347 ex::ex(double const d) : bp(construct_from_double(d))
348 {
349         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
350 #ifdef OBSCURE_CINT_HACK
351         update_last_created_or_assigned_bp();
352 #endif // def OBSCURE_CINT_HACK
353 }
354
355 inline
356 ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
357 {
358         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
359 #ifdef OBSCURE_CINT_HACK
360         update_last_created_or_assigned_bp();
361 #endif // def OBSCURE_CINT_HACK
362 }
363
364 inline
365 int ex::compare(const ex & other) const
366 {
367 #ifdef GINAC_COMPARE_STATISTICS
368         compare_statistics.total_compares++;
369 #endif
370         if (bp == other.bp)  // trivial case: both expressions point to same basic
371                 return 0;
372 #ifdef GINAC_COMPARE_STATISTICS
373         compare_statistics.nontrivial_compares++;
374 #endif
375         const int cmpval = bp->compare(*other.bp);
376 #if 1
377         if (cmpval == 0) {
378                 // Expressions point to different, but equal, trees: conserve
379                 // memory and make subsequent compare() operations faster by
380                 // making both expressions point to the same tree.
381                 share(other);
382         }
383 #endif
384         return cmpval;
385 }
386
387 inline
388 bool ex::is_equal(const ex & other) const
389 {
390 #ifdef GINAC_COMPARE_STATISTICS
391         compare_statistics.total_is_equals++;
392 #endif
393         if (bp == other.bp)  // trivial case: both expressions point to same basic
394                 return true;
395 #ifdef GINAC_COMPARE_STATISTICS
396         compare_statistics.nontrivial_is_equals++;
397 #endif
398     const bool equal = bp->is_equal(*other.bp);
399 #if 0
400         if (equal) {
401                 // Expressions point to different, but equal, trees: conserve
402                 // memory and make subsequent compare() operations faster by
403                 // making both expressions point to the same tree.
404                 share(other);
405         }
406 #endif
407         return equal;
408 }
409
410
411 // Iterators
412
413 class const_iterator : public std::iterator<std::random_access_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
414         friend class ex;
415         friend class const_preorder_iterator;
416         friend class const_postorder_iterator;
417
418 public:
419         const_iterator() throw() {}
420
421 private:
422         const_iterator(const ex &e_, size_t i_) throw() : e(e_), i(i_) {}
423
424 public:
425         // This should return an ex&, but that would be a reference to a
426         // temporary value
427         ex operator*() const
428         {
429                 return e.op(i);
430         }
431
432         // This should return an ex*, but that would be a pointer to a
433         // temporary value
434         std::auto_ptr<ex> operator->() const
435         {
436                 return std::auto_ptr<ex>(new ex(operator*()));
437         }
438
439         ex operator[](difference_type n) const
440         {
441                 return e.op(i + n);
442         }
443
444         const_iterator &operator++() throw()
445         {
446                 ++i;
447                 return *this;
448         }
449
450         const_iterator operator++(int) throw()
451         {
452                 const_iterator tmp = *this;
453                 ++i;
454                 return tmp;
455         }
456
457         const_iterator &operator+=(difference_type n) throw()
458         {
459                 i += n;
460                 return *this;
461         }
462
463         const_iterator operator+(difference_type n) const throw()
464         {
465                 return const_iterator(e, i + n);
466         }
467
468         inline friend const_iterator operator+(difference_type n, const const_iterator &it) throw()
469         {
470                 return const_iterator(it.e, it.i + n);
471         }
472
473         const_iterator &operator--() throw()
474         {
475                 --i;
476                 return *this;
477         }
478
479         const_iterator operator--(int) throw()
480         {
481                 const_iterator tmp = *this;
482                 --i;
483                 return tmp;
484         }
485
486         const_iterator &operator-=(difference_type n) throw()
487         {
488                 i -= n;
489                 return *this;
490         }
491
492         const_iterator operator-(difference_type n) const throw()
493         {
494                 return const_iterator(e, i - n);
495         }
496
497         inline friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs) throw()
498         {
499                 return lhs.i - rhs.i;
500         }
501
502         bool operator==(const const_iterator &other) const throw()
503         {
504                 return are_ex_trivially_equal(e, other.e) && i == other.i;
505         }
506
507         bool operator!=(const const_iterator &other) const throw()
508         {
509                 return !(*this == other);
510         }
511
512         bool operator<(const const_iterator &other) const throw()
513         {
514                 return i < other.i;
515         }
516
517         bool operator>(const const_iterator &other) const throw()
518         {
519                 return other < *this;
520         }
521
522         bool operator<=(const const_iterator &other) const throw()
523         {
524                 return !(other < *this);
525         }
526
527         bool operator>=(const const_iterator &other) const throw()
528         {
529                 return !(*this < other);
530         }
531
532 protected:
533         ex e; // this used to be a "const basic *", but in view of object fusion that wouldn't be safe
534         size_t i;
535 };
536
537 namespace internal {
538
539 struct _iter_rep {
540         _iter_rep(const ex &e_, size_t i_, size_t i_end_) : e(e_), i(i_), i_end(i_end_) {}
541
542         bool operator==(const _iter_rep &other) const throw()
543         {
544                 return are_ex_trivially_equal(e, other.e) && i == other.i;
545         }
546
547         bool operator!=(const _iter_rep &other) const throw()
548         {
549                 return !(*this == other);
550         }
551
552         ex e;
553         size_t i;
554         size_t i_end;
555 };
556
557 } // namespace internal
558
559 class const_preorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
560 public:
561         const_preorder_iterator() throw() {}
562
563         const_preorder_iterator(const ex &e, size_t n)
564         {
565                 s.push(internal::_iter_rep(e, 0, n));
566         }
567
568 public:
569         reference operator*() const
570         {
571                 return s.top().e;
572         }
573
574         pointer operator->() const
575         {
576                 return &(s.top().e);
577         }
578
579         const_preorder_iterator &operator++()
580         {
581                 increment();
582                 return *this;
583         }
584
585         const_preorder_iterator operator++(int)
586         {
587                 const_preorder_iterator tmp = *this;
588                 increment();
589                 return tmp;
590         }
591
592         bool operator==(const const_preorder_iterator &other) const throw()
593         {
594                 return s == other.s;
595         }
596
597         bool operator!=(const const_preorder_iterator &other) const throw()
598         {
599                 return !(*this == other);
600         }
601
602 private:
603         std::stack<internal::_iter_rep, std::vector<internal::_iter_rep> > s;
604
605         void increment()
606         {
607                 while (!s.empty() && s.top().i == s.top().i_end) {
608                         s.pop();
609                         if (s.empty())
610                                 return;
611                         ++s.top().i;
612                 }
613
614                 internal::_iter_rep & current = s.top();
615
616                 if (current.i != current.i_end) {
617                         const ex & child = current.e.op(current.i);
618                         s.push(internal::_iter_rep(child, 0, child.nops()));
619                 }
620         }
621 };
622
623 class const_postorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
624 public:
625         const_postorder_iterator() throw() {}
626
627         const_postorder_iterator(const ex &e, size_t n)
628         {
629                 s.push(internal::_iter_rep(e, 0, n));
630                 descend();
631         }
632
633 public:
634         reference operator*() const
635         {
636                 return s.top().e;
637         }
638
639         pointer operator->() const
640         {
641                 return &(s.top().e);
642         }
643
644         const_postorder_iterator &operator++()
645         {
646                 increment();
647                 return *this;
648         }
649
650         const_postorder_iterator operator++(int)
651         {
652                 const_postorder_iterator tmp = *this;
653                 increment();
654                 return tmp;
655         }
656
657         bool operator==(const const_postorder_iterator &other) const throw()
658         {
659                 return s == other.s;
660         }
661
662         bool operator!=(const const_postorder_iterator &other) const throw()
663         {
664                 return !(*this == other);
665         }
666
667 private:
668         std::stack<internal::_iter_rep, std::vector<internal::_iter_rep> > s;
669
670         void descend()
671         {
672                 while (s.top().i != s.top().i_end) {
673                         internal::_iter_rep & current = s.top();
674                         const ex & child = current.e.op(current.i);
675                         s.push(internal::_iter_rep(child, 0, child.nops()));
676                 }
677         }
678
679         void increment()
680         {
681                 if (s.top().i == s.top().i_end)
682                         s.pop();
683                 if (!s.empty()) {
684                         ++s.top().i;
685                         descend();
686                 }
687         }
688 };
689
690 inline const_iterator ex::begin() const throw()
691 {
692         return const_iterator(*this, 0);
693 }
694
695 inline const_iterator ex::end() const throw()
696 {
697         return const_iterator(*this, nops());
698 }
699
700 inline const_preorder_iterator ex::preorder_begin() const
701 {
702         return const_preorder_iterator(*this, nops());
703 }
704
705 inline const_preorder_iterator ex::preorder_end() const throw()
706 {
707         return const_preorder_iterator();
708 }
709
710 inline const_postorder_iterator ex::postorder_begin() const
711 {
712         return const_postorder_iterator(*this, nops());
713 }
714
715 inline const_postorder_iterator ex::postorder_end() const throw()
716 {
717         return const_postorder_iterator();
718 }
719
720
721 // utility functions
722
723 /** Compare two objects of class quickly without doing a deep tree traversal.
724  *  @return "true" if they are equal
725  *          "false" if equality cannot be established quickly (e1 and e2 may
726  *          still be equal, in this case. */
727 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
728 {
729         return e1.bp == e2.bp;
730 }
731
732 /* Function objects for STL sort() etc. */
733 struct ex_is_less : public std::binary_function<ex, ex, bool> {
734         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
735 };
736
737 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
738         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
739 };
740
741 struct op0_is_equal : public std::binary_function<ex, ex, bool> {
742         bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
743 };
744
745 struct ex_swap : public std::binary_function<ex, ex, void> {
746         void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
747 };
748
749 // wrapper functions around member functions
750 inline size_t nops(const ex & thisex)
751 { return thisex.nops(); }
752
753 inline ex expand(const ex & thisex, unsigned options = 0)
754 { return thisex.expand(options); }
755
756 inline ex conjugate(const ex & thisex)
757 { return thisex.conjugate(); }
758
759 inline bool has(const ex & thisex, const ex & pattern)
760 { return thisex.has(pattern); }
761
762 inline bool find(const ex & thisex, const ex & pattern, lst & found)
763 { return thisex.find(pattern, found); }
764
765 inline int degree(const ex & thisex, const ex & s)
766 { return thisex.degree(s); }
767
768 inline int ldegree(const ex & thisex, const ex & s)
769 { return thisex.ldegree(s); }
770
771 inline ex coeff(const ex & thisex, const ex & s, int n=1)
772 { return thisex.coeff(s, n); }
773
774 inline ex numer(const ex & thisex)
775 { return thisex.numer(); }
776
777 inline ex denom(const ex & thisex)
778 { return thisex.denom(); }
779
780 inline ex numer_denom(const ex & thisex)
781 { return thisex.numer_denom(); }
782
783 inline ex normal(const ex & thisex, int level=0)
784 { return thisex.normal(level); }
785
786 inline ex to_rational(const ex & thisex, lst & repl_lst)
787 { return thisex.to_rational(repl_lst); }
788
789 inline ex to_rational(const ex & thisex, exmap & repl)
790 { return thisex.to_rational(repl); }
791
792 inline ex to_polynomial(const ex & thisex, exmap & repl)
793 { return thisex.to_polynomial(repl); }
794
795 inline ex to_polynomial(const ex & thisex, lst & repl_lst)
796 { return thisex.to_polynomial(repl_lst); }
797
798 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
799 { return thisex.collect(s, distributed); }
800
801 inline ex eval(const ex & thisex, int level = 0)
802 { return thisex.eval(level); }
803
804 inline ex evalf(const ex & thisex, int level = 0)
805 { return thisex.evalf(level); }
806
807 inline ex evalm(const ex & thisex)
808 { return thisex.evalm(); }
809
810 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
811 { return thisex.diff(s, nth); }
812
813 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
814 { return thisex.series(r, order, options); }
815
816 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
817 { return thisex.match(pattern, repl_lst); }
818
819 inline ex simplify_indexed(const ex & thisex, unsigned options = 0)
820 { return thisex.simplify_indexed(options); }
821
822 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp, unsigned options = 0)
823 { return thisex.simplify_indexed(sp, options); }
824
825 inline ex symmetrize(const ex & thisex)
826 { return thisex.symmetrize(); }
827
828 inline ex symmetrize(const ex & thisex, const lst & l)
829 { return thisex.symmetrize(l); }
830
831 inline ex antisymmetrize(const ex & thisex)
832 { return thisex.antisymmetrize(); }
833
834 inline ex antisymmetrize(const ex & thisex, const lst & l)
835 { return thisex.antisymmetrize(l); }
836
837 inline ex symmetrize_cyclic(const ex & thisex)
838 { return thisex.symmetrize_cyclic(); }
839
840 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
841 { return thisex.symmetrize_cyclic(l); }
842
843 inline ex op(const ex & thisex, size_t i)
844 { return thisex.op(i); }
845
846 inline ex lhs(const ex & thisex)
847 { return thisex.lhs(); }
848
849 inline ex rhs(const ex & thisex)
850 { return thisex.rhs(); }
851
852 inline bool is_zero(const ex & thisex)
853 { return thisex.is_zero(); }
854
855 inline void swap(ex & e1, ex & e2)
856 { e1.swap(e2); }
857
858 inline ex ex::subs(const exmap & m, unsigned options) const
859 {
860         return bp->subs(m, options);
861 }
862
863 inline ex subs(const ex & thisex, const exmap & m, unsigned options = 0)
864 { return thisex.subs(m, options); }
865
866 inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
867 { return thisex.subs(ls, lr, options); }
868
869 inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
870 { return thisex.subs(e, options); }
871
872
873 /* Convert function pointer to function object suitable for map(). */
874 class pointer_to_map_function : public map_function {
875 protected:
876         ex (*ptr)(const ex &);
877 public:
878         explicit pointer_to_map_function(ex x(const ex &)) : ptr(x) {}
879         ex operator()(const ex & e) { return ptr(e); }
880 };
881
882 template<class T1>
883 class pointer_to_map_function_1arg : public map_function {
884 protected:
885         ex (*ptr)(const ex &, T1);
886         T1 arg1;
887 public:
888         explicit pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
889         ex operator()(const ex & e) { return ptr(e, arg1); }
890 };
891
892 template<class T1, class T2>
893 class pointer_to_map_function_2args : public map_function {
894 protected:
895         ex (*ptr)(const ex &, T1, T2);
896         T1 arg1;
897         T2 arg2;
898 public:
899         explicit pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
900         ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
901 };
902
903 template<class T1, class T2, class T3>
904 class pointer_to_map_function_3args : public map_function {
905 protected:
906         ex (*ptr)(const ex &, T1, T2, T3);
907         T1 arg1;
908         T2 arg2;
909         T3 arg3;
910 public:
911         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) {}
912         ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
913 };
914
915 inline ex ex::map(ex f(const ex &)) const
916 {
917         pointer_to_map_function fcn(f);
918         return bp->map(fcn);
919 }
920
921 // convenience type checker template functions
922
923 /** Check if ex is a handle to a T, including base classes. */
924 template <class T>
925 inline bool is_a(const ex &obj)
926 {
927         return is_a<T>(*obj.bp);
928 }
929
930 /** Check if ex is a handle to a T, not including base classes. */
931 template <class T>
932 inline bool is_exactly_a(const ex &obj)
933 {
934         return is_exactly_a<T>(*obj.bp);
935 }
936
937 /** Return a reference to the basic-derived class T object embedded in an
938  *  expression.  This is fast but unsafe: the result is undefined if the
939  *  expression does not contain a T object at its top level.  Hence, you
940  *  should generally check the type of e first.  Also, you shouldn't cache
941  *  the returned reference because GiNaC's garbage collector may destroy
942  *  the referenced object any time it's used in another expression.
943  *
944  *  @param e expression
945  *  @return reference to object of class T
946  *  @see is_exactly_a<class T>() */
947 template <class T>
948 inline const T &ex_to(const ex &e)
949 {
950         GINAC_ASSERT(is_a<T>(e));
951         return static_cast<const T &>(*e.bp);
952 }
953
954 } // namespace GiNaC
955
956
957 // Specializations of Standard Library algorithms
958 namespace std {
959
960 /** Specialization of std::swap() for ex objects. */
961 template <>
962 inline void swap(GiNaC::ex &a, GiNaC::ex &b)
963 {
964         a.swap(b);
965 }
966
967 /** Specialization of std::iter_swap() for vector<ex> iterators. */
968 template <>
969 inline void iter_swap(vector<GiNaC::ex>::iterator i1, vector<GiNaC::ex>::iterator i2)
970 {
971         i1->swap(*i2);
972 }
973
974 /** Specialization of std::iter_swap() for list<ex> iterators. */
975 template <>
976 inline void iter_swap(list<GiNaC::ex>::iterator i1, list<GiNaC::ex>::iterator i2)
977 {
978         i1->swap(*i2);
979 }
980
981 } // namespace std
982
983 #endif // ndef __GINAC_EX_H__