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