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