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