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