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