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