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