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