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