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