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