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