]> www.ginac.de Git - ginac.git/blob - ginac/ex.h
- added some (empty) exception specifications (reduces code size a little)
[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
30 #include "basic.h"
31 #include "ptr.h"
32
33 namespace GiNaC {
34
35
36 /** Helper class to initialize the library.  There must be one static object
37  *  of this class in every object file that makes use of our flyweights in
38  *  order to guarantee proper initialization.  Hence we put it into this
39  *  file which is included by every relevant file anyways.  This is modeled
40  *  after section 27.4.2.1.6 of the C++ standard, where cout and friends are
41  *  set up.
42  *
43  *  @see utils.cpp */
44 class library_init {
45 public:
46         library_init();
47         ~library_init();
48 private:
49         static int count;
50 };
51 /** For construction of flyweights, etc. */
52 static library_init library_initializer;
53
54
55 class scalar_products;
56
57
58 /** Lightweight wrapper for GiNaC's symbolic objects.  Basically all it does is
59  *  to hold a pointer to the other objects, manage the reference counting and
60  *  provide methods for manipulation of these objects.  (Some people call such
61  *  a thing a proxy class.) */
62 class ex
63 {
64         friend class archive_node;
65         friend inline bool are_ex_trivially_equal(const ex &, const ex &);
66         template<class T> friend inline const T &ex_to(const ex &);
67         template<class T> friend inline bool is_a(const ex &);
68         template<class T> friend inline bool is_exactly_a(const ex &);
69         
70         // default constructor, copy constructor and assignment operator
71 public:
72         ex() throw();
73 #ifdef OBSCURE_CINT_HACK
74         ex(const ex & other);
75         ex & operator=(const ex & other);
76 #endif
77
78         // other constructors
79 public:
80         ex(const basic & other);
81         ex(int i);
82         ex(unsigned int i);
83         ex(long i);
84         ex(unsigned long i);
85         ex(double const d);
86
87         /** Construct ex from string and a list of symbols. The input grammar is
88          *  similar to the GiNaC output format. All symbols and indices to be used
89          *  in the expression must be specified in a lst in the second argument.
90          *  Undefined symbols and other parser errors will throw an exception. */
91         ex(const std::string &s, const ex &l);
92         
93 public:
94         // Iterators
95         class const_iterator : public std::iterator<std::random_access_iterator_tag, ex, ptrdiff_t, const ex *, const ex &>
96         {
97                 friend class ex;
98
99         public:
100                 const_iterator() throw() {}
101                 const_iterator(const basic *bp_, size_t i_) throw() : bp(bp_), i(i_) {}
102
103                 bool operator==(const const_iterator &other) const throw()
104                 {
105                         return bp == other.bp && i == other.i;
106                 }
107
108                 bool operator!=(const const_iterator &other) const throw()
109                 {
110                         return !(*this == other);
111                 }
112
113                 bool operator<(const const_iterator &other) const throw()
114                 {
115                         return i < other.i;
116                 }
117
118                 bool operator>(const const_iterator &other) const throw()
119                 {
120                         return other < *this;
121                 }
122
123                 bool operator<=(const const_iterator &other) const throw()
124                 {
125                         return !(other < *this);
126                 }
127
128                 bool operator>=(const const_iterator &other) const throw()
129                 {
130                         return !(*this < other);
131                 }
132
133                 // This should return an ex&, but that would be a reference to a
134                 // temporary value
135                 ex operator*() const
136                 {
137                         return bp->op(i);
138                 }
139
140 #if 0
141                 // How do we make this work in the context of the "reference to
142                 // temporary" problem? Return an auto_ptr?
143                 pointer operator->() const
144                 {
145                         return &(operator*());
146                 }
147 #endif
148
149                 const_iterator &operator++() throw()
150                 {
151                         ++i;
152                         return *this;
153                 }
154
155                 const_iterator operator++(int) throw()
156                 {
157                         const_iterator tmp = *this;
158                         ++i;
159                         return tmp;
160                 }
161
162                 const_iterator &operator+=(difference_type n) throw()
163                 {
164                         i += n;
165                         return *this;
166                 }
167
168                 const_iterator operator+(difference_type n) const throw()
169                 {
170                         return const_iterator(bp, i + n);
171                 }
172
173                 inline friend const_iterator operator+(difference_type n, const const_iterator &it) throw()
174                 {
175                         return const_iterator(it.bp, it.i + n);
176                 }
177
178                 const_iterator &operator--() throw()
179                 {
180                         --i;
181                         return *this;
182                 }
183
184                 const_iterator operator--(int) throw()
185                 {
186                         const_iterator tmp = *this;
187                         --i;
188                         return tmp;
189                 }
190
191                 const_iterator &operator-=(difference_type n) throw()
192                 {
193                         i -= n;
194                         return *this;
195                 }
196
197                 const_iterator operator-(difference_type n) const throw()
198                 {
199                         return const_iterator(bp, i - n);
200                 }
201
202                 inline friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs) throw()
203                 {
204                         return lhs.i - rhs.i;
205                 }
206
207                 ex operator[](difference_type n) const
208                 {
209                         return bp->op(i + n);
210                 }
211
212         protected:
213                 const basic *bp;
214                 size_t i;
215         };
216
217         const_iterator begin() const throw() { return const_iterator(get_pointer(bp), 0); }
218         const_iterator end() const throw() { return const_iterator(get_pointer(bp), bp->nops()); }
219
220 #if 0
221         // This doesn't work because of the "reference to temporary" problem
222         // in operator*()
223         typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
224         const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
225         const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
226 #endif
227
228         // non-virtual functions in this class
229 public:
230         /** Efficiently swap the contents of two expressions. */
231         void swap(ex & other) throw()
232         {
233                 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
234                 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
235                 bp.swap(other.bp);
236         }
237
238         // evaluation
239         ex eval(int level = 0) const { return bp->eval(level); }
240         ex evalf(int level = 0) const { return bp->evalf(level); }
241         ex evalm() const { return bp->evalm(); }
242         ex eval_ncmul(const exvector & v) const { return bp->eval_ncmul(v); }
243
244         // printing
245         void print(const print_context & c, unsigned level = 0) const;
246         void dbgprint() const;
247         void dbgprinttree() const;
248
249         // info
250         bool info(unsigned inf) const { return bp->info(inf); }
251
252         // operand access
253         size_t nops() const { return bp->nops(); }
254         ex op(size_t i) const { return bp->op(i); }
255         ex operator[](const ex & index) const { return (*bp)[index]; }
256         ex operator[](size_t i) const { return (*bp)[i]; }
257         ex & let_op(size_t i);
258         ex & operator[](const ex & index);
259         ex & operator[](size_t i);
260         ex lhs() const;
261         ex rhs() const;
262
263         // pattern matching
264         bool has(const ex & pattern) const { return bp->has(pattern); }
265         bool find(const ex & pattern, lst & found) const;
266         bool match(const ex & pattern) const;
267         bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
268
269         // substitutions
270         ex subs(const exmap & m, unsigned options = 0) const;
271         ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
272         ex subs(const ex & e, unsigned options = 0) const;
273
274         // function mapping
275         ex map(map_function & f) const { return bp->map(f); }
276         ex map(ex (*f)(const ex & e)) const;
277
278         // visitors and tree traversal
279         void accept(visitor & v) const { bp->accept(v); }
280         void traverse_preorder(visitor & v) const;
281         void traverse_postorder(visitor & v) const;
282         void traverse(visitor & v) const { traverse_preorder(v); }
283
284         // degree/coeff
285         int degree(const ex & s) const { return bp->degree(s); }
286         int ldegree(const ex & s) const { return bp->ldegree(s); }
287         ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
288         ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
289         ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
290
291         // expand/collect
292         ex expand(unsigned options=0) const;
293         ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
294
295         // differentiation and series expansion
296         ex diff(const symbol & s, unsigned nth = 1) const;
297         ex series(const ex & r, int order, unsigned options = 0) const;
298
299         // rational functions
300         ex normal(int level = 0) const;
301         ex to_rational(lst &repl_lst) const;
302         ex to_polynomial(lst &repl_lst) const;
303         ex numer() const;
304         ex denom() const;
305         ex numer_denom() const;
306
307         // polynomial algorithms
308         ex unit(const ex &x) const;
309         ex content(const ex &x) const;
310         numeric integer_content() const;
311         ex primpart(const ex &x) const;
312         ex primpart(const ex &x, const ex &cont) const;
313         ex smod(const numeric &xi) const { return bp->smod(xi); }
314         numeric max_coefficient() const;
315
316         // indexed objects
317         exvector get_free_indices() const { return bp->get_free_indices(); }
318         ex simplify_indexed() const;
319         ex simplify_indexed(const scalar_products & sp) const;
320
321         // comparison
322         int compare(const ex & other) const;
323         bool is_equal(const ex & other) const;
324         bool is_zero() const { extern const ex _ex0; return is_equal(_ex0); }
325         
326         // symmetry
327         ex symmetrize() const;
328         ex symmetrize(const lst & l) const;
329         ex antisymmetrize() const;
330         ex antisymmetrize(const lst & l) const;
331         ex symmetrize_cyclic() const;
332         ex symmetrize_cyclic(const lst & l) const;
333
334         // noncommutativity
335         unsigned return_type() const { return bp->return_type(); }
336         unsigned return_type_tinfo() const { return bp->return_type_tinfo(); }
337
338         unsigned gethash() const { return bp->gethash(); }
339
340 private:
341         static ptr<basic> construct_from_basic(const basic & other);
342         static basic & construct_from_int(int i);
343         static basic & construct_from_uint(unsigned int i);
344         static basic & construct_from_long(long i);
345         static basic & construct_from_ulong(unsigned long i);
346         static basic & construct_from_double(double d);
347         static ptr<basic> construct_from_string_and_lst(const std::string &s, const ex &l);
348         void makewriteable();
349         void share(const ex & other) const;
350
351 #ifdef OBSCURE_CINT_HACK
352 public:
353         static bool last_created_or_assigned_bp_can_be_converted_to_ex()
354         {
355                 if (last_created_or_assigned_bp==0) return false;
356                 if ((last_created_or_assigned_bp->flags &
357                          status_flags::dynallocated)==0) return false;
358                 if ((last_created_or_assigned_bp->flags &
359                          status_flags::evaluated)==0) return false;
360                 return true;
361         }
362 protected:
363         void update_last_created_or_assigned_bp()
364         {
365                 last_created_or_assigned_bp = bp;
366                 last_created_or_assigned_exp = (long)(void *)(this);
367         }
368 #endif // def OBSCURE_CINT_HACK
369
370 // member variables
371
372 private:
373         mutable ptr<basic> bp;  ///< pointer to basic object managed by this
374
375 #ifdef OBSCURE_CINT_HACK
376 public:
377         static ptr<basic> last_created_or_assigned_bp;
378         static basic * dummy_bp;
379         static long last_created_or_assigned_exp;
380 #endif // def OBSCURE_CINT_HACK
381 };
382
383
384 // performance-critical inlined method implementations
385
386 // This needs to be a basic* because we don't know that numeric is derived
387 // from basic and we need a basic& for the ex default constructor
388 extern const basic *_num0_bp;
389
390 inline
391 ex::ex() throw() : bp(*const_cast<basic *>(_num0_bp))
392 {
393         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
394 #ifdef OBSCURE_CINT_HACK
395         update_last_created_or_assigned_bp();
396 #endif // def OBSCURE_CINT_HACK
397 }
398
399 #ifdef OBSCURE_CINT_HACK
400 inline
401 ex::ex(const ex & other) : bp(other.bp)
402 {
403         GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
404         update_last_created_or_assigned_bp();
405 }
406
407 inline
408 ex & ex::operator=(const ex & other)
409 {
410         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
411         GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
412         bp = other.bp;
413         update_last_created_or_assigned_bp();
414         return *this;
415 }
416 #endif // def OBSCURE_CINT_HACK
417
418 inline
419 ex::ex(const basic & other) : bp(construct_from_basic(other))
420 {
421         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
422 #ifdef OBSCURE_CINT_HACK
423         update_last_created_or_assigned_bp();
424 #endif // def OBSCURE_CINT_HACK
425 }
426
427 inline
428 ex::ex(int i) : bp(construct_from_int(i))
429 {
430         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
431 #ifdef OBSCURE_CINT_HACK
432         update_last_created_or_assigned_bp();
433 #endif // def OBSCURE_CINT_HACK
434 }
435
436 inline
437 ex::ex(unsigned int i) : bp(construct_from_uint(i))
438 {
439         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
440 #ifdef OBSCURE_CINT_HACK
441         update_last_created_or_assigned_bp();
442 #endif // def OBSCURE_CINT_HACK
443 }
444
445 inline
446 ex::ex(long i) : bp(construct_from_long(i))
447 {
448         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
449 #ifdef OBSCURE_CINT_HACK
450         update_last_created_or_assigned_bp();
451 #endif // def OBSCURE_CINT_HACK
452 }
453
454 inline
455 ex::ex(unsigned long i) : bp(construct_from_ulong(i))
456 {
457         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
458 #ifdef OBSCURE_CINT_HACK
459         update_last_created_or_assigned_bp();
460 #endif // def OBSCURE_CINT_HACK
461 }
462
463 inline
464 ex::ex(double const d) : bp(construct_from_double(d))
465 {
466         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
467 #ifdef OBSCURE_CINT_HACK
468         update_last_created_or_assigned_bp();
469 #endif // def OBSCURE_CINT_HACK
470 }
471
472 inline
473 ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
474 {
475         GINAC_ASSERT(bp->flags & status_flags::dynallocated);
476 #ifdef OBSCURE_CINT_HACK
477         update_last_created_or_assigned_bp();
478 #endif // def OBSCURE_CINT_HACK
479 }
480
481 inline
482 int ex::compare(const ex & other) const
483 {
484         if (bp == other.bp)  // trivial case: both expressions point to same basic
485                 return 0;
486         const int cmpval = bp->compare(*other.bp);
487         if (cmpval == 0) {
488                 // Expressions point to different, but equal, trees: conserve
489                 // memory and make subsequent compare() operations faster by
490                 // making both expression point to the same tree.
491                 share(other);
492         }
493         return cmpval;
494 }
495
496 inline
497 bool ex::is_equal(const ex & other) const
498 {
499         if (bp == other.bp)  // trivial case: both expressions point to same basic
500                 return true;
501         return bp->is_equal(*other.bp);
502 }
503
504
505 // utility functions
506
507 /** Compare two objects of class quickly without doing a deep tree traversal.
508  *  @return "true" if they are equal
509  *          "false" if equality cannot be established quickly (e1 and e2 may
510  *          still be equal, in this case. */
511 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
512 {
513         return e1.bp == e2.bp;
514 }
515
516 // wrapper functions around member functions
517 inline size_t nops(const ex & thisex)
518 { return thisex.nops(); }
519
520 inline ex expand(const ex & thisex, unsigned options = 0)
521 { return thisex.expand(options); }
522
523 inline bool has(const ex & thisex, const ex & pattern)
524 { return thisex.has(pattern); }
525
526 inline bool find(const ex & thisex, const ex & pattern, lst & found)
527 { return thisex.find(pattern, found); }
528
529 inline int degree(const ex & thisex, const ex & s)
530 { return thisex.degree(s); }
531
532 inline int ldegree(const ex & thisex, const ex & s)
533 { return thisex.ldegree(s); }
534
535 inline ex coeff(const ex & thisex, const ex & s, int n=1)
536 { return thisex.coeff(s, n); }
537
538 inline ex numer(const ex & thisex)
539 { return thisex.numer(); }
540
541 inline ex denom(const ex & thisex)
542 { return thisex.denom(); }
543
544 inline ex numer_denom(const ex & thisex)
545 { return thisex.numer_denom(); }
546
547 inline ex normal(const ex & thisex, int level=0)
548 { return thisex.normal(level); }
549
550 inline ex to_rational(const ex & thisex, lst & repl_lst)
551 { return thisex.to_rational(repl_lst); }
552
553 inline ex to_polynomial(const ex & thisex, lst & repl_lst)
554 { return thisex.to_polynomial(repl_lst); }
555
556 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
557 { return thisex.collect(s, distributed); }
558
559 inline ex eval(const ex & thisex, int level = 0)
560 { return thisex.eval(level); }
561
562 inline ex evalf(const ex & thisex, int level = 0)
563 { return thisex.evalf(level); }
564
565 inline ex evalm(const ex & thisex)
566 { return thisex.evalm(); }
567
568 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
569 { return thisex.diff(s, nth); }
570
571 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
572 { return thisex.series(r, order, options); }
573
574 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
575 { return thisex.match(pattern, repl_lst); }
576
577 inline ex simplify_indexed(const ex & thisex)
578 { return thisex.simplify_indexed(); }
579
580 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
581 { return thisex.simplify_indexed(sp); }
582
583 inline ex symmetrize(const ex & thisex)
584 { return thisex.symmetrize(); }
585
586 inline ex symmetrize(const ex & thisex, const lst & l)
587 { return thisex.symmetrize(l); }
588
589 inline ex antisymmetrize(const ex & thisex)
590 { return thisex.antisymmetrize(); }
591
592 inline ex antisymmetrize(const ex & thisex, const lst & l)
593 { return thisex.antisymmetrize(l); }
594
595 inline ex symmetrize_cyclic(const ex & thisex)
596 { return thisex.symmetrize_cyclic(); }
597
598 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
599 { return thisex.symmetrize_cyclic(l); }
600
601 inline ex op(const ex & thisex, size_t i)
602 { return thisex.op(i); }
603
604 inline ex lhs(const ex & thisex)
605 { return thisex.lhs(); }
606
607 inline ex rhs(const ex & thisex)
608 { return thisex.rhs(); }
609
610 inline bool is_zero(const ex & thisex)
611 { return thisex.is_zero(); }
612
613 inline void swap(ex & e1, ex & e2)
614 { e1.swap(e2); }
615
616 /* Function objects for STL sort() etc. */
617 struct ex_is_less : public std::binary_function<ex, ex, bool> {
618         bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
619 };
620
621 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
622         bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
623 };
624
625 struct op0_is_equal : public std::binary_function<ex, ex, bool> {
626         bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
627 };
628
629 struct ex_swap : public std::binary_function<ex, ex, void> {
630         void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
631 };
632
633 inline ex ex::subs(const exmap & m, unsigned options) const
634 {
635         return bp->subs(m, options);
636 }
637
638 inline ex subs(const ex & thisex, const exmap & m, unsigned options = 0)
639 { return thisex.subs(m, options); }
640
641 inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
642 { return thisex.subs(ls, lr, options); }
643
644 inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
645 { return thisex.subs(e, options); }
646
647
648 /* Convert function pointer to function object suitable for map(). */
649 class pointer_to_map_function : public map_function {
650 protected:
651         ex (*ptr)(const ex &);
652 public:
653         explicit pointer_to_map_function(ex x(const ex &)) : ptr(x) {}
654         ex operator()(const ex & e) { return ptr(e); }
655 };
656
657 template<class T1>
658 class pointer_to_map_function_1arg : public map_function {
659 protected:
660         ex (*ptr)(const ex &, T1);
661         T1 arg1;
662 public:
663         explicit pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
664         ex operator()(const ex & e) { return ptr(e, arg1); }
665 };
666
667 template<class T1, class T2>
668 class pointer_to_map_function_2args : public map_function {
669 protected:
670         ex (*ptr)(const ex &, T1, T2);
671         T1 arg1;
672         T2 arg2;
673 public:
674         explicit pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
675         ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
676 };
677
678 template<class T1, class T2, class T3>
679 class pointer_to_map_function_3args : public map_function {
680 protected:
681         ex (*ptr)(const ex &, T1, T2, T3);
682         T1 arg1;
683         T2 arg2;
684         T3 arg3;
685 public:
686         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) {}
687         ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
688 };
689
690 inline ex ex::map(ex f(const ex &)) const
691 {
692         pointer_to_map_function fcn(f);
693         return bp->map(fcn);
694 }
695
696 // convenience type checker template functions
697
698 /** Check if ex is a handle to a T, including base classes. */
699 template <class T>
700 inline bool is_a(const ex &obj)
701 {
702         return is_a<T>(*obj.bp);
703 }
704
705 /** Check if ex is a handle to a T, not including base classes. */
706 template <class T>
707 inline bool is_exactly_a(const ex &obj)
708 {
709         return is_exactly_a<T>(*obj.bp);
710 }
711
712 /** Return a reference to the basic-derived class T object embedded in an
713  *  expression.  This is fast but unsafe: the result is undefined if the
714  *  expression does not contain a T object at its top level.  Hence, you
715  *  should generally check the type of e first.
716  *
717  *  @param e expression
718  *  @return reference to object of class T
719  *  @see is_exactly_a<class T>() */
720 template <class T>
721 inline const T &ex_to(const ex &e)
722 {
723         GINAC_ASSERT(is_a<T>(e));
724         return static_cast<const T &>(*e.bp);
725 }
726
727 } // namespace GiNaC
728
729
730 // Specializations of Standard Library algorithms
731 namespace std {
732
733 /** Specialization of std::swap() for ex objects. */
734 template <>
735 inline void swap(GiNaC::ex &a, GiNaC::ex &b)
736 {
737         a.swap(b);
738 }
739
740 /** Specialization of std::iter_swap() for vector<ex> iterators. */
741 template <>
742 inline void iter_swap(vector<GiNaC::ex>::iterator i1, vector<GiNaC::ex>::iterator i2)
743 {
744         i1->swap(*i2);
745 }
746
747 /** Specialization of std::iter_swap() for list<ex> iterators. */
748 template <>
749 inline void iter_swap(list<GiNaC::ex>::iterator i1, list<GiNaC::ex>::iterator i2)
750 {
751         i1->swap(*i2);
752 }
753
754 } // namespace std
755
756 #endif // ndef __GINAC_EX_H__