]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
const_preorder_iterator and const_postorder_iterator and associated methods
[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         ex smod(const numeric &xi) const { return bp->smod(xi); }
196         numeric max_coefficient() const;
197
198         // indexed objects
199         exvector get_free_indices() const { return bp->get_free_indices(); }
200         ex simplify_indexed(unsigned options = 0) const;
201         ex simplify_indexed(const scalar_products & sp, unsigned options = 0) const;
202
203         // comparison
204         int compare(const ex & other) const;
205         bool is_equal(const ex & other) const;
206         bool is_zero() const { extern const ex _ex0; return is_equal(_ex0); }
207         
208         // symmetry
209         ex symmetrize() const;
210         ex symmetrize(const lst & l) const;
211         ex antisymmetrize() const;
212         ex antisymmetrize(const lst & l) const;
213         ex symmetrize_cyclic() const;
214         ex symmetrize_cyclic(const lst & l) const;
215
216         // noncommutativity
217         unsigned return_type() const { return bp->return_type(); }
218         unsigned return_type_tinfo() const { return bp->return_type_tinfo(); }
219
220         unsigned gethash() const { return bp->gethash(); }
221
222 private:
223         static ptr<basic> construct_from_basic(const basic & other);
224         static basic & construct_from_int(int i);
225         static basic & construct_from_uint(unsigned int i);
226         static basic & construct_from_long(long i);
227         static basic & construct_from_ulong(unsigned long i);
228         static basic & construct_from_double(double d);
229         static ptr<basic> construct_from_string_and_lst(const std::string &s, const ex &l);
230         void makewriteable();
231         void share(const ex & other) const;
232
233 #ifdef OBSCURE_CINT_HACK
234 public:
235         static bool last_created_or_assigned_bp_can_be_converted_to_ex()
236         {
237                 if (last_created_or_assigned_bp==0) return false;
238                 if ((last_created_or_assigned_bp->flags &
239                          status_flags::dynallocated)==0) return false;
240                 if ((last_created_or_assigned_bp->flags &
241                          status_flags::evaluated)==0) return false;
242                 return true;
243         }
244 protected:
245         void update_last_created_or_assigned_bp()
246         {
247                 last_created_or_assigned_bp = bp;
248                 last_created_or_assigned_exp = (long)(void *)(this);
249         }
250 #endif // def OBSCURE_CINT_HACK
251
252 // member variables
253
254 private:
255         mutable ptr<basic> bp;  ///< pointer to basic object managed by this
256
257 #ifdef OBSCURE_CINT_HACK
258 public:
259         static ptr<basic> last_created_or_assigned_bp;
260         static basic * dummy_bp;
261         static long last_created_or_assigned_exp;
262 #endif // def OBSCURE_CINT_HACK
263 };
264
265
266 // performance-critical inlined method implementations
267
268 // This needs to be a basic* because we don't know that numeric is derived
269 // from basic and we need a basic& for the ex default constructor
270 extern const basic *_num0_bp;
271
272 inline
273 ex::ex() throw() : bp(*const_cast<basic *>(_num0_bp))
274 {
275         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
276 #ifdef OBSCURE_CINT_HACK
277         update_last_created_or_assigned_bp();
278 #endif // def OBSCURE_CINT_HACK
279 }
280
281 #ifdef OBSCURE_CINT_HACK
282 inline
283 ex::ex(const ex & other) : bp(other.bp)
284 {
285         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
286         update_last_created_or_assigned_bp();
287 }
288
289 inline
290 ex & ex::operator=(const ex & other)
291 {
292         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
293         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
294         bp = other.bp;
295         update_last_created_or_assigned_bp();
296         return *this;
297 }
298 #endif // def OBSCURE_CINT_HACK
299
300 inline
301 ex::ex(const basic & other) : bp(construct_from_basic(other))
302 {
303         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
304 #ifdef OBSCURE_CINT_HACK
305         update_last_created_or_assigned_bp();
306 #endif // def OBSCURE_CINT_HACK
307 }
308
309 inline
310 ex::ex(int i) : bp(construct_from_int(i))
311 {
312         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
313 #ifdef OBSCURE_CINT_HACK
314         update_last_created_or_assigned_bp();
315 #endif // def OBSCURE_CINT_HACK
316 }
317
318 inline
319 ex::ex(unsigned int i) : bp(construct_from_uint(i))
320 {
321         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
322 #ifdef OBSCURE_CINT_HACK
323         update_last_created_or_assigned_bp();
324 #endif // def OBSCURE_CINT_HACK
325 }
326
327 inline
328 ex::ex(long i) : bp(construct_from_long(i))
329 {
330         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
331 #ifdef OBSCURE_CINT_HACK
332         update_last_created_or_assigned_bp();
333 #endif // def OBSCURE_CINT_HACK
334 }
335
336 inline
337 ex::ex(unsigned long i) : bp(construct_from_ulong(i))
338 {
339         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
340 #ifdef OBSCURE_CINT_HACK
341         update_last_created_or_assigned_bp();
342 #endif // def OBSCURE_CINT_HACK
343 }
344
345 inline
346 ex::ex(double const d) : bp(construct_from_double(d))
347 {
348         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
349 #ifdef OBSCURE_CINT_HACK
350         update_last_created_or_assigned_bp();
351 #endif // def OBSCURE_CINT_HACK
352 }
353
354 inline
355 ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
356 {
357         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
358 #ifdef OBSCURE_CINT_HACK
359         update_last_created_or_assigned_bp();
360 #endif // def OBSCURE_CINT_HACK
361 }
362
363 inline
364 int ex::compare(const ex & other) const
365 {
366 #ifdef GINAC_COMPARE_STATISTICS
367         compare_statistics.total_compares++;
368 #endif
369         if (bp == other.bp)  // trivial case: both expressions point to same basic
370                 return 0;
371 #ifdef GINAC_COMPARE_STATISTICS
372         compare_statistics.nontrivial_compares++;
373 #endif
374         const int cmpval = bp->compare(*other.bp);
375 #if 1
376         if (cmpval == 0) {
377                 // Expressions point to different, but equal, trees: conserve
378                 // memory and make subsequent compare() operations faster by
379                 // making both expressions point to the same tree.
380                 share(other);
381         }
382 #endif
383         return cmpval;
384 }
385
386 inline
387 bool ex::is_equal(const ex & other) const
388 {
389 #ifdef GINAC_COMPARE_STATISTICS
390         compare_statistics.total_is_equals++;
391 #endif
392         if (bp == other.bp)  // trivial case: both expressions point to same basic
393                 return true;
394 #ifdef GINAC_COMPARE_STATISTICS
395         compare_statistics.nontrivial_is_equals++;
396 #endif
397     const bool equal = bp->is_equal(*other.bp);
398 #if 0
399         if (equal) {
400                 // Expressions point to different, but equal, trees: conserve
401                 // memory and make subsequent compare() operations faster by
402                 // making both expressions point to the same tree.
403                 share(other);
404         }
405 #endif
406         return equal;
407 }
408
409
410 // Iterators
411
412 class const_iterator : public std::iterator<std::random_access_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
413         friend class ex;
414         friend class const_preorder_iterator;
415         friend class const_postorder_iterator;
416
417 public:
418         const_iterator() throw() {}
419
420 private:
421         const_iterator(const ex &e_, size_t i_) throw() : e(e_), i(i_) {}
422
423 public:
424         // This should return an ex&, but that would be a reference to a
425         // temporary value
426         ex operator*() const
427         {
428                 return e.op(i);
429         }
430
431         // This should return an ex*, but that would be a pointer to a
432         // temporary value
433         std::auto_ptr<ex> operator->() const
434         {
435                 return std::auto_ptr<ex>(new ex(operator*()));
436         }
437
438         ex operator[](difference_type n) const
439         {
440                 return e.op(i + n);
441         }
442
443         const_iterator &operator++() throw()
444         {
445                 ++i;
446                 return *this;
447         }
448
449         const_iterator operator++(int) throw()
450         {
451                 const_iterator tmp = *this;
452                 ++i;
453                 return tmp;
454         }
455
456         const_iterator &operator+=(difference_type n) throw()
457         {
458                 i += n;
459                 return *this;
460         }
461
462         const_iterator operator+(difference_type n) const throw()
463         {
464                 return const_iterator(e, i + n);
465         }
466
467         inline friend const_iterator operator+(difference_type n, const const_iterator &it) throw()
468         {
469                 return const_iterator(it.e, it.i + n);
470         }
471
472         const_iterator &operator--() throw()
473         {
474                 --i;
475                 return *this;
476         }
477
478         const_iterator operator--(int) throw()
479         {
480                 const_iterator tmp = *this;
481                 --i;
482                 return tmp;
483         }
484
485         const_iterator &operator-=(difference_type n) throw()
486         {
487                 i -= n;
488                 return *this;
489         }
490
491         const_iterator operator-(difference_type n) const throw()
492         {
493                 return const_iterator(e, i - n);
494         }
495
496         inline friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs) throw()
497         {
498                 return lhs.i - rhs.i;
499         }
500
501         bool operator==(const const_iterator &other) const throw()
502         {
503                 return are_ex_trivially_equal(e, other.e) && i == other.i;
504         }
505
506         bool operator!=(const const_iterator &other) const throw()
507         {
508                 return !(*this == other);
509         }
510
511         bool operator<(const const_iterator &other) const throw()
512         {
513                 return i < other.i;
514         }
515
516         bool operator>(const const_iterator &other) const throw()
517         {
518                 return other < *this;
519         }
520
521         bool operator<=(const const_iterator &other) const throw()
522         {
523                 return !(other < *this);
524         }
525
526         bool operator>=(const const_iterator &other) const throw()
527         {
528                 return !(*this < other);
529         }
530
531 protected:
532         ex e; // this used to be a "const basic *", but in view of object fusion that wouldn't be safe
533         size_t i;
534 };
535
536 namespace internal {
537
538 struct _iter_rep {
539         _iter_rep(const ex &e_, size_t i_, size_t i_end_) : e(e_), i(i_), i_end(i_end_) {}
540
541         bool operator==(const _iter_rep &other) const throw()
542         {
543                 return are_ex_trivially_equal(e, other.e) && i == other.i;
544         }
545
546         bool operator!=(const _iter_rep &other) const throw()
547         {
548                 return !(*this == other);
549         }
550
551         ex e;
552         size_t i;
553         size_t i_end;
554 };
555
556 } // namespace internal
557
558 class const_preorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
559 public:
560         const_preorder_iterator() throw() {}
561
562         const_preorder_iterator(const ex &e, size_t n)
563         {
564                 s.push(internal::_iter_rep(e, 0, n));
565         }
566
567 public:
568         reference operator*() const
569         {
570                 return s.top().e;
571         }
572
573         pointer operator->() const
574         {
575                 return &(s.top().e);
576         }
577
578         const_preorder_iterator &operator++()
579         {
580                 increment();
581                 return *this;
582         }
583
584         const_preorder_iterator operator++(int)
585         {
586                 const_preorder_iterator tmp = *this;
587                 increment();
588                 return tmp;
589         }
590
591         bool operator==(const const_preorder_iterator &other) const throw()
592         {
593                 return s == other.s;
594         }
595
596         bool operator!=(const const_preorder_iterator &other) const throw()
597         {
598                 return !(*this == other);
599         }
600
601 private:
602         std::stack<internal::_iter_rep, std::vector<internal::_iter_rep> > s;
603
604         void increment()
605         {
606                 while (!s.empty() && s.top().i == s.top().i_end) {
607                         s.pop();
608                         if (s.empty())
609                                 return;
610                         ++s.top().i;
611                 }
612
613                 internal::_iter_rep & current = s.top();
614
615                 if (current.i != current.i_end) {
616                         const ex & child = current.e.op(current.i);
617                         s.push(internal::_iter_rep(child, 0, child.nops()));
618                 }
619         }
620 };
621
622 class const_postorder_iterator : public std::iterator<std::forward_iterator_tag, ex, ptrdiff_t, const ex *, const ex &> {
623 public:
624         const_postorder_iterator() throw() {}
625
626         const_postorder_iterator(const ex &e, size_t n)
627         {
628                 s.push(internal::_iter_rep(e, 0, n));
629                 descend();
630         }
631
632 public:
633         reference operator*() const
634         {
635                 return s.top().e;
636         }
637
638         pointer operator->() const
639         {
640                 return &(s.top().e);
641         }
642
643         const_postorder_iterator &operator++()
644         {
645                 increment();
646                 return *this;
647         }
648
649         const_postorder_iterator operator++(int)
650         {
651                 const_postorder_iterator tmp = *this;
652                 increment();
653                 return tmp;
654         }
655
656         bool operator==(const const_postorder_iterator &other) const throw()
657         {
658                 return s == other.s;
659         }
660
661         bool operator!=(const const_postorder_iterator &other) const throw()
662         {
663                 return !(*this == other);
664         }
665
666 private:
667         std::stack<internal::_iter_rep, std::vector<internal::_iter_rep> > s;
668
669         void descend()
670         {
671                 while (s.top().i != s.top().i_end) {
672                         internal::_iter_rep & current = s.top();
673                         const ex & child = current.e.op(current.i);
674                         s.push(internal::_iter_rep(child, 0, child.nops()));
675                 }
676         }
677
678         void increment()
679         {
680                 if (s.top().i == s.top().i_end)
681                         s.pop();
682                 if (!s.empty()) {
683                         ++s.top().i;
684                         descend();
685                 }
686         }
687 };
688
689 inline const_iterator ex::begin() const throw()
690 {
691         return const_iterator(*this, 0);
692 }
693
694 inline const_iterator ex::end() const throw()
695 {
696         return const_iterator(*this, nops());
697 }
698
699 inline const_preorder_iterator ex::preorder_begin() const
700 {
701         return const_preorder_iterator(*this, nops());
702 }
703
704 inline const_preorder_iterator ex::preorder_end() const throw()
705 {
706         return const_preorder_iterator();
707 }
708
709 inline const_postorder_iterator ex::postorder_begin() const
710 {
711         return const_postorder_iterator(*this, nops());
712 }
713
714 inline const_postorder_iterator ex::postorder_end() const throw()
715 {
716         return const_postorder_iterator();
717 }
718
719
720 // utility functions
721
722 /** Compare two objects of class quickly without doing a deep tree traversal.
723  *  @return "true" if they are equal
724  *          "false" if equality cannot be established quickly (e1 and e2 may
725  *          still be equal, in this case. */
726 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
727 {
728         return e1.bp == e2.bp;
729 }
730
731 /* Function objects for STL sort() etc. */
732 struct ex_is_less : public std::binary_function<ex, ex, bool> {
733         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
734 };
735
736 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
737         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
738 };
739
740 struct op0_is_equal : public std::binary_function<ex, ex, bool> {
741         bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
742 };
743
744 struct ex_swap : public std::binary_function<ex, ex, void> {
745         void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
746 };
747
748 // wrapper functions around member functions
749 inline size_t nops(const ex & thisex)
750 { return thisex.nops(); }
751
752 inline ex expand(const ex & thisex, unsigned options = 0)
753 { return thisex.expand(options); }
754
755 inline bool has(const ex & thisex, const ex & pattern)
756 { return thisex.has(pattern); }
757
758 inline bool find(const ex & thisex, const ex & pattern, lst & found)
759 { return thisex.find(pattern, found); }
760
761 inline int degree(const ex & thisex, const ex & s)
762 { return thisex.degree(s); }
763
764 inline int ldegree(const ex & thisex, const ex & s)
765 { return thisex.ldegree(s); }
766
767 inline ex coeff(const ex & thisex, const ex & s, int n=1)
768 { return thisex.coeff(s, n); }
769
770 inline ex numer(const ex & thisex)
771 { return thisex.numer(); }
772
773 inline ex denom(const ex & thisex)
774 { return thisex.denom(); }
775
776 inline ex numer_denom(const ex & thisex)
777 { return thisex.numer_denom(); }
778
779 inline ex normal(const ex & thisex, int level=0)
780 { return thisex.normal(level); }
781
782 inline ex to_rational(const ex & thisex, lst & repl_lst)
783 { return thisex.to_rational(repl_lst); }
784
785 inline ex to_rational(const ex & thisex, exmap & repl)
786 { return thisex.to_rational(repl); }
787
788 inline ex to_polynomial(const ex & thisex, exmap & repl)
789 { return thisex.to_polynomial(repl); }
790
791 inline ex to_polynomial(const ex & thisex, lst & repl_lst)
792 { return thisex.to_polynomial(repl_lst); }
793
794 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
795 { return thisex.collect(s, distributed); }
796
797 inline ex eval(const ex & thisex, int level = 0)
798 { return thisex.eval(level); }
799
800 inline ex evalf(const ex & thisex, int level = 0)
801 { return thisex.evalf(level); }
802
803 inline ex evalm(const ex & thisex)
804 { return thisex.evalm(); }
805
806 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
807 { return thisex.diff(s, nth); }
808
809 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
810 { return thisex.series(r, order, options); }
811
812 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
813 { return thisex.match(pattern, repl_lst); }
814
815 inline ex simplify_indexed(const ex & thisex, unsigned options = 0)
816 { return thisex.simplify_indexed(options); }
817
818 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp, unsigned options = 0)
819 { return thisex.simplify_indexed(sp, options); }
820
821 inline ex symmetrize(const ex & thisex)
822 { return thisex.symmetrize(); }
823
824 inline ex symmetrize(const ex & thisex, const lst & l)
825 { return thisex.symmetrize(l); }
826
827 inline ex antisymmetrize(const ex & thisex)
828 { return thisex.antisymmetrize(); }
829
830 inline ex antisymmetrize(const ex & thisex, const lst & l)
831 { return thisex.antisymmetrize(l); }
832
833 inline ex symmetrize_cyclic(const ex & thisex)
834 { return thisex.symmetrize_cyclic(); }
835
836 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
837 { return thisex.symmetrize_cyclic(l); }
838
839 inline ex op(const ex & thisex, size_t i)
840 { return thisex.op(i); }
841
842 inline ex lhs(const ex & thisex)
843 { return thisex.lhs(); }
844
845 inline ex rhs(const ex & thisex)
846 { return thisex.rhs(); }
847
848 inline bool is_zero(const ex & thisex)
849 { return thisex.is_zero(); }
850
851 inline void swap(ex & e1, ex & e2)
852 { e1.swap(e2); }
853
854 inline ex ex::subs(const exmap & m, unsigned options) const
855 {
856         return bp->subs(m, options);
857 }
858
859 inline ex subs(const ex & thisex, const exmap & m, unsigned options = 0)
860 { return thisex.subs(m, options); }
861
862 inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
863 { return thisex.subs(ls, lr, options); }
864
865 inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
866 { return thisex.subs(e, options); }
867
868
869 /* Convert function pointer to function object suitable for map(). */
870 class pointer_to_map_function : public map_function {
871 protected:
872         ex (*ptr)(const ex &);
873 public:
874         explicit pointer_to_map_function(ex x(const ex &)) : ptr(x) {}
875         ex operator()(const ex & e) { return ptr(e); }
876 };
877
878 template<class T1>
879 class pointer_to_map_function_1arg : public map_function {
880 protected:
881         ex (*ptr)(const ex &, T1);
882         T1 arg1;
883 public:
884         explicit pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
885         ex operator()(const ex & e) { return ptr(e, arg1); }
886 };
887
888 template<class T1, class T2>
889 class pointer_to_map_function_2args : public map_function {
890 protected:
891         ex (*ptr)(const ex &, T1, T2);
892         T1 arg1;
893         T2 arg2;
894 public:
895         explicit pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
896         ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
897 };
898
899 template<class T1, class T2, class T3>
900 class pointer_to_map_function_3args : public map_function {
901 protected:
902         ex (*ptr)(const ex &, T1, T2, T3);
903         T1 arg1;
904         T2 arg2;
905         T3 arg3;
906 public:
907         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) {}
908         ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
909 };
910
911 inline ex ex::map(ex f(const ex &)) const
912 {
913         pointer_to_map_function fcn(f);
914         return bp->map(fcn);
915 }
916
917 // convenience type checker template functions
918
919 /** Check if ex is a handle to a T, including base classes. */
920 template <class T>
921 inline bool is_a(const ex &obj)
922 {
923         return is_a<T>(*obj.bp);
924 }
925
926 /** Check if ex is a handle to a T, not including base classes. */
927 template <class T>
928 inline bool is_exactly_a(const ex &obj)
929 {
930         return is_exactly_a<T>(*obj.bp);
931 }
932
933 /** Return a reference to the basic-derived class T object embedded in an
934  *  expression.  This is fast but unsafe: the result is undefined if the
935  *  expression does not contain a T object at its top level.  Hence, you
936  *  should generally check the type of e first.  Also, you shouldn't cache
937  *  the returned reference because GiNaC's garbage collector may destroy
938  *  the referenced object any time it's used in another expression.
939  *
940  *  @param e expression
941  *  @return reference to object of class T
942  *  @see is_exactly_a<class T>() */
943 template <class T>
944 inline const T &ex_to(const ex &e)
945 {
946         GINAC_ASSERT(is_a<T>(e));
947         return static_cast<const T &>(*e.bp);
948 }
949
950 } // namespace GiNaC
951
952
953 // Specializations of Standard Library algorithms
954 namespace std {
955
956 /** Specialization of std::swap() for ex objects. */
957 template <>
958 inline void swap(GiNaC::ex &a, GiNaC::ex &b)
959 {
960         a.swap(b);
961 }
962
963 /** Specialization of std::iter_swap() for vector<ex> iterators. */
964 template <>
965 inline void iter_swap(vector<GiNaC::ex>::iterator i1, vector<GiNaC::ex>::iterator i2)
966 {
967         i1->swap(*i2);
968 }
969
970 /** Specialization of std::iter_swap() for list<ex> iterators. */
971 template <>
972 inline void iter_swap(list<GiNaC::ex>::iterator i1, list<GiNaC::ex>::iterator i2)
973 {
974         i1->swap(*i2);
975 }
976
977 } // namespace std
978
979 #endif // ndef __GINAC_EX_H__