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