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