GiNaC 1.8.7
ex.h
Go to the documentation of this file.
1
5/*
6 * GiNaC Copyright (C) 1999-2023 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
35namespace 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
51 static void init_unarchivers();
52public:
55private:
56 static int count;
57};
60
61class scalar_products;
62class const_iterator;
65
66
72class 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
80public:
81 ex() noexcept;
82
83 // other constructors
84public:
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(long long i);
91 ex(unsigned long long i);
92 ex(double const d);
93
98 ex(const std::string &s, const ex &l);
99
100public:
101 // non-virtual functions in this class
102public:
104 void swap(ex & other) noexcept
105 {
107 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
108 bp.swap(other.bp);
109 }
110
111 // iterators
112 const_iterator begin() const noexcept;
113 const_iterator end() const noexcept;
115 const_preorder_iterator preorder_end() const noexcept;
118
119 // evaluation
120 ex eval() const { return bp->eval(); }
121 ex evalf() const { return bp->evalf(); }
122 ex evalm() const { return bp->evalm(); }
123 ex eval_ncmul(const exvector & v) const { return bp->eval_ncmul(v); }
124 ex eval_integ() const { return bp->eval_integ(); }
125
126 // printing
127 void print(const print_context & c, unsigned level = 0) const;
128 void dbgprint() const;
129 void dbgprinttree() const;
130
131 // info
132 bool info(unsigned inf) const { return bp->info(inf); }
133
134 // operand access
135 size_t nops() const { return bp->nops(); }
136 ex op(size_t i) const { return bp->op(i); }
137 ex operator[](const ex & index) const { return (const_cast<const basic&>(*bp))[index]; }
138 ex operator[](size_t i) const { return (const_cast<const basic&>(*bp))[i]; }
139 ex & let_op(size_t i);
140 ex & operator[](const ex & index);
141 ex & operator[](size_t i);
142 ex lhs() const;
143 ex rhs() const;
144
145 // function for complex expressions
146 ex conjugate() const { return bp->conjugate(); }
147 ex real_part() const { return bp->real_part(); }
148 ex imag_part() const { return bp->imag_part(); }
149
150 // pattern matching
151 bool has(const ex & pattern, unsigned options = 0) const { return bp->has(pattern, options); }
152 bool find(const ex & pattern, exset& found) const;
153 bool match(const ex & pattern) const;
154 bool match(const ex & pattern, exmap & repls) const { return bp->match(pattern, repls); }
155
156 // substitutions
157 ex subs(const exmap & m, unsigned options = 0) const;
158 ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
159 ex subs(const ex & e, unsigned options = 0) const;
160
161 // function mapping
162 ex map(map_function & f) const { return bp->map(f); }
163 ex map(ex (*f)(const ex & e)) const;
164
165 // visitors and tree traversal
166 void accept(visitor & v) const { bp->accept(v); }
167 void traverse_preorder(visitor & v) const;
168 void traverse_postorder(visitor & v) const;
169 void traverse(visitor & v) const { traverse_preorder(v); }
170
171 // degree/coeff
172 bool is_polynomial(const ex & vars) const;
173 int degree(const ex & s) const { return bp->degree(s); }
174 int ldegree(const ex & s) const { return bp->ldegree(s); }
175 ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
176 ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
177 ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
178
179 // expand/collect
180 ex expand(unsigned options=0) const;
181 ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
182
183 // differentiation and series expansion
184 ex diff(const symbol & s, unsigned nth = 1) const;
185 ex series(const ex & r, int order, unsigned options = 0) const;
186
187 // rational functions
188 ex normal() const;
189 ex to_rational(exmap & repl) const;
190 ex to_polynomial(exmap & repl) const;
191 ex numer() const;
192 ex denom() const;
193 ex numer_denom() const;
194
195 // polynomial algorithms
196 ex unit(const ex &x) const;
197 ex content(const ex &x) const;
198 numeric integer_content() const;
199 ex primpart(const ex &x) const;
200 ex primpart(const ex &x, const ex &cont) const;
201 void unitcontprim(const ex &x, ex &u, ex &c, ex &p) const;
202 ex smod(const numeric &xi) const { return bp->smod(xi); }
203 numeric max_coefficient() const;
204
205 // indexed objects
206 exvector get_free_indices() const { return bp->get_free_indices(); }
207 ex simplify_indexed(unsigned options = 0) const;
208 ex simplify_indexed(const scalar_products & sp, unsigned options = 0) const;
209
210 // comparison
211 int compare(const ex & other) const;
212 bool is_equal(const ex & other) const;
213 bool is_zero() const {
214#ifndef _MSC_VER
215 extern const ex _ex0;
216#endif
217 return is_equal(_ex0);
218 }
219 bool is_zero_matrix() const;
220
221 // symmetry
222 ex symmetrize() const;
223 ex symmetrize(const lst & l) const;
224 ex antisymmetrize() const;
225 ex antisymmetrize(const lst & l) const;
226 ex symmetrize_cyclic() const;
227 ex symmetrize_cyclic(const lst & l) const;
228
229 // noncommutativity
230 unsigned return_type() const { return bp->return_type(); }
231 return_type_t return_type_tinfo() const { return bp->return_type_tinfo(); }
232
233 unsigned gethash() const { return bp->gethash(); }
234
235private:
236 static ptr<basic> construct_from_basic(const basic & other);
237 static basic & construct_from_int(int i);
238 static basic & construct_from_uint(unsigned int i);
239 static basic & construct_from_long(long i);
240 static basic & construct_from_ulong(unsigned long i);
241 static basic & construct_from_longlong(long long i);
242 static basic & construct_from_ulonglong(unsigned long long i);
243 static basic & construct_from_double(double d);
244 static ptr<basic> construct_from_string_and_lst(const std::string &s, const ex &l);
245 void makewriteable();
246 void share(const ex & other) const;
247
248// member variables
249
250private:
251 mutable ptr<basic> bp;
252};
253
254
255// performance-critical inlined method implementations
256
257// This needs to be a basic* because we don't know that numeric is derived
258// from basic and we need a basic& for the ex default constructor
259extern const basic *_num0_bp;
260
261inline
262ex::ex() noexcept : bp(*const_cast<basic *>(_num0_bp))
263{
265}
266
267inline
268ex::ex(const basic & other) : bp(construct_from_basic(other))
269{
271}
272
273inline
274ex::ex(int i) : bp(construct_from_int(i))
275{
277}
278
279inline
280ex::ex(unsigned int i) : bp(construct_from_uint(i))
281{
283}
284
285inline
286ex::ex(long i) : bp(construct_from_long(i))
287{
289}
290
291inline
292ex::ex(unsigned long i) : bp(construct_from_ulong(i))
293{
295}
296
297inline
298ex::ex(long long i) : bp(construct_from_longlong(i))
299{
301}
302
303inline
304ex::ex(unsigned long long i) : bp(construct_from_ulonglong(i))
305{
307}
308
309inline
310ex::ex(double const d) : bp(construct_from_double(d))
311{
313}
314
315inline
316ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
317{
319}
320
321inline
322int ex::compare(const ex & other) const
323{
324#ifdef GINAC_COMPARE_STATISTICS
325 compare_statistics.total_compares++;
326#endif
327 if (bp == other.bp) // trivial case: both expressions point to same basic
328 return 0;
329#ifdef GINAC_COMPARE_STATISTICS
330 compare_statistics.nontrivial_compares++;
331#endif
332 const int cmpval = bp->compare(*other.bp);
333#if 1
334 if (cmpval == 0) {
335 // Expressions point to different, but equal, trees: conserve
336 // memory and make subsequent compare() operations faster by
337 // making both expressions point to the same tree.
338 share(other);
339 }
340#endif
341 return cmpval;
342}
343
344inline
345bool ex::is_equal(const ex & other) const
346{
347#ifdef GINAC_COMPARE_STATISTICS
348 compare_statistics.total_is_equals++;
349#endif
350 if (bp == other.bp) // trivial case: both expressions point to same basic
351 return true;
352#ifdef GINAC_COMPARE_STATISTICS
353 compare_statistics.nontrivial_is_equals++;
354#endif
355 const bool equal = bp->is_equal(*other.bp);
356#if 0
357 if (equal) {
358 // Expressions point to different, but equal, trees: conserve
359 // memory and make subsequent compare() operations faster by
360 // making both expressions point to the same tree.
361 share(other);
362 }
363#endif
364 return equal;
365}
366
367
368// Iterators
369
371 friend class ex;
374public:
375 using iterator_category = std::random_access_iterator_tag;
376 using value_type = ex;
377 using difference_type = ptrdiff_t;
378 using pointer = const ex *;
379 using reference = const ex &;
380
381 const_iterator() noexcept {}
382
383private:
384 const_iterator(const ex &e_, size_t i_) noexcept : e(e_), i(i_) {}
385
386public:
387 // This should return an ex&, but that would be a reference to a
388 // temporary value
389 ex operator*() const
390 {
391 return e.op(i);
392 }
393
394 // This should return an ex*, but that would be a pointer to a
395 // temporary value
396 std::unique_ptr<ex> operator->() const
397 {
398 return std::unique_ptr<ex>(new ex(operator*()));
399 }
400
402 {
403 return e.op(i + n);
404 }
405
407 {
408 ++i;
409 return *this;
410 }
411
413 {
414 const_iterator tmp = *this;
415 ++i;
416 return tmp;
417 }
418
420 {
421 i += n;
422 return *this;
423 }
424
426 {
427 return const_iterator(e, i + n);
428 }
429
430 inline friend const_iterator operator+(difference_type n, const const_iterator &it) noexcept
431 {
432 return const_iterator(it.e, it.i + n);
433 }
434
436 {
437 --i;
438 return *this;
439 }
440
442 {
443 const_iterator tmp = *this;
444 --i;
445 return tmp;
446 }
447
449 {
450 i -= n;
451 return *this;
452 }
453
455 {
456 return const_iterator(e, i - n);
457 }
458
459 inline friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs) noexcept
460 {
461 return lhs.i - rhs.i;
462 }
463
464 bool operator==(const const_iterator &other) const noexcept
465 {
466 return are_ex_trivially_equal(e, other.e) && i == other.i;
467 }
468
469 bool operator!=(const const_iterator &other) const noexcept
470 {
471 return !(*this == other);
472 }
473
474 bool operator<(const const_iterator &other) const noexcept
475 {
476 return i < other.i;
477 }
478
479 bool operator>(const const_iterator &other) const noexcept
480 {
481 return other < *this;
482 }
483
484 bool operator<=(const const_iterator &other) const noexcept
485 {
486 return !(other < *this);
487 }
488
489 bool operator>=(const const_iterator &other) const noexcept
490 {
491 return !(*this < other);
492 }
493
494protected:
495 ex e; // this used to be a "const basic *", but in view of object fusion that wouldn't be safe
496 size_t i;
497};
498
499namespace internal {
500
501struct _iter_rep {
502 _iter_rep(const ex &e_, size_t i_, size_t i_end_) : e(e_), i(i_), i_end(i_end_) {}
503
504 bool operator==(const _iter_rep &other) const noexcept
505 {
506 return are_ex_trivially_equal(e, other.e) && i == other.i;
507 }
508
509 bool operator!=(const _iter_rep &other) const noexcept
510 {
511 return !(*this == other);
512 }
513
515 size_t i;
516 size_t i_end;
517};
518
519} // namespace internal
520
522public:
523 using iterator_category = std::forward_iterator_tag;
524 using value_type = ex;
525 using difference_type = ptrdiff_t;
526 using pointer = const ex *;
527 using reference = const ex &;
529
530 const_preorder_iterator(const ex &e, size_t n)
531 {
532 s.push(internal::_iter_rep(e, 0, n));
533 }
534
535public:
537 {
538 return s.top().e;
539 }
540
542 {
543 return &(s.top().e);
544 }
545
547 {
548 increment();
549 return *this;
550 }
551
553 {
554 const_preorder_iterator tmp = *this;
555 increment();
556 return tmp;
557 }
558
559 bool operator==(const const_preorder_iterator &other) const noexcept
560 {
561 return s == other.s;
562 }
563
564 bool operator!=(const const_preorder_iterator &other) const noexcept
565 {
566 return !(*this == other);
567 }
568
569private:
570 std::stack<internal::_iter_rep, std::vector<internal::_iter_rep>> s;
571
573 {
574 while (!s.empty() && s.top().i == s.top().i_end) {
575 s.pop();
576 if (s.empty())
577 return;
578 ++s.top().i;
579 }
580
581 internal::_iter_rep & current = s.top();
582
583 if (current.i != current.i_end) {
584 const ex & child = current.e.op(current.i);
585 s.push(internal::_iter_rep(child, 0, child.nops()));
586 }
587 }
588};
589
591public:
592 using iterator_category = std::forward_iterator_tag;
593 using value_type = ex;
594 using difference_type = ptrdiff_t;
595 using pointer = const ex *;
596 using reference = const ex &;
598
599 const_postorder_iterator(const ex &e, size_t n)
600 {
601 s.push(internal::_iter_rep(e, 0, n));
602 descend();
603 }
604
605public:
607 {
608 return s.top().e;
609 }
610
612 {
613 return &(s.top().e);
614 }
615
617 {
618 increment();
619 return *this;
620 }
621
623 {
624 const_postorder_iterator tmp = *this;
625 increment();
626 return tmp;
627 }
628
629 bool operator==(const const_postorder_iterator &other) const noexcept
630 {
631 return s == other.s;
632 }
633
634 bool operator!=(const const_postorder_iterator &other) const noexcept
635 {
636 return !(*this == other);
637 }
638
639private:
640 std::stack<internal::_iter_rep, std::vector<internal::_iter_rep>> s;
641
642 void descend()
643 {
644 while (s.top().i != s.top().i_end) {
645 internal::_iter_rep & current = s.top();
646 const ex & child = current.e.op(current.i);
647 s.push(internal::_iter_rep(child, 0, child.nops()));
648 }
649 }
650
652 {
653 if (s.top().i == s.top().i_end)
654 s.pop();
655 if (!s.empty()) {
656 ++s.top().i;
657 descend();
658 }
659 }
660};
661
662inline const_iterator ex::begin() const noexcept
663{
664 return const_iterator(*this, 0);
665}
666
667inline const_iterator ex::end() const noexcept
668{
669 return const_iterator(*this, nops());
670}
671
673{
674 return const_preorder_iterator(*this, nops());
675}
676
678{
680}
681
683{
684 return const_postorder_iterator(*this, nops());
685}
686
688{
690}
691
692
693// utility functions
694
699inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
700{
701 return e1.bp == e2.bp;
702}
703
704/* Function objects for STL sort() etc. */
706 bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
707};
708
710 bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
711};
712
714 bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
715};
716
717struct ex_swap {
718 void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
719};
720
721// Make it possible to print exvectors and exmaps
722std::ostream & operator<<(std::ostream & os, const exvector & e);
723std::ostream & operator<<(std::ostream & os, const exset & e);
724std::ostream & operator<<(std::ostream & os, const exmap & e);
725
726// wrapper functions around member functions
727inline size_t nops(const ex & thisex)
728{ return thisex.nops(); }
729
730inline ex expand(const ex & thisex, unsigned options = 0)
731{ return thisex.expand(options); }
732
733inline ex conjugate(const ex & thisex)
734{ return thisex.conjugate(); }
735
736inline ex real_part(const ex & thisex)
737{ return thisex.real_part(); }
738
739inline ex imag_part(const ex & thisex)
740{ return thisex.imag_part(); }
741
742inline bool has(const ex & thisex, const ex & pattern, unsigned options = 0)
743{ return thisex.has(pattern, options); }
744
745inline bool find(const ex & thisex, const ex & pattern, exset& found)
746{ return thisex.find(pattern, found); }
747
748inline bool is_polynomial(const ex & thisex, const ex & vars)
749{ return thisex.is_polynomial(vars); }
750
751inline int degree(const ex & thisex, const ex & s)
752{ return thisex.degree(s); }
753
754inline int ldegree(const ex & thisex, const ex & s)
755{ return thisex.ldegree(s); }
756
757inline ex coeff(const ex & thisex, const ex & s, int n=1)
758{ return thisex.coeff(s, n); }
759
760inline ex numer(const ex & thisex)
761{ return thisex.numer(); }
762
763inline ex denom(const ex & thisex)
764{ return thisex.denom(); }
765
766inline ex numer_denom(const ex & thisex)
767{ return thisex.numer_denom(); }
768
769inline ex normal(const ex & thisex)
770{ return thisex.normal(); }
771
772inline ex to_rational(const ex & thisex, exmap & repl)
773{ return thisex.to_rational(repl); }
774
775inline ex to_polynomial(const ex & thisex, exmap & repl)
776{ return thisex.to_polynomial(repl); }
777
778inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
779{ return thisex.collect(s, distributed); }
780
781inline ex eval(const ex & thisex)
782{ return thisex.eval(); }
783
784inline ex evalf(const ex & thisex)
785{ return thisex.evalf(); }
786
787inline ex evalm(const ex & thisex)
788{ return thisex.evalm(); }
789
790inline ex eval_integ(const ex & thisex)
791{ return thisex.eval_integ(); }
792
793inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
794{ return thisex.diff(s, nth); }
795
796inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
797{ return thisex.series(r, order, options); }
798
799inline bool match(const ex & thisex, const ex & pattern, exmap& repl_lst)
800{ return thisex.match(pattern, repl_lst); }
801
802inline ex simplify_indexed(const ex & thisex, unsigned options = 0)
803{ return thisex.simplify_indexed(options); }
804
805inline ex simplify_indexed(const ex & thisex, const scalar_products & sp, unsigned options = 0)
806{ return thisex.simplify_indexed(sp, options); }
807
808inline ex symmetrize(const ex & thisex)
809{ return thisex.symmetrize(); }
810
811inline ex symmetrize(const ex & thisex, const lst & l)
812{ return thisex.symmetrize(l); }
813
814inline ex antisymmetrize(const ex & thisex)
815{ return thisex.antisymmetrize(); }
816
817inline ex antisymmetrize(const ex & thisex, const lst & l)
818{ return thisex.antisymmetrize(l); }
819
820inline ex symmetrize_cyclic(const ex & thisex)
821{ return thisex.symmetrize_cyclic(); }
822
823inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
824{ return thisex.symmetrize_cyclic(l); }
825
826inline ex op(const ex & thisex, size_t i)
827{ return thisex.op(i); }
828
829inline ex lhs(const ex & thisex)
830{ return thisex.lhs(); }
831
832inline ex rhs(const ex & thisex)
833{ return thisex.rhs(); }
834
835inline bool is_zero(const ex & thisex)
836{ return thisex.is_zero(); }
837
838inline void swap(ex & e1, ex & e2)
839{ e1.swap(e2); }
840
841inline ex ex::subs(const exmap & m, unsigned options) const
842{
843 return bp->subs(m, options);
844}
845
846inline ex subs(const ex & thisex, const exmap & m, unsigned options = 0)
847{ return thisex.subs(m, options); }
848
849inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
850{ return thisex.subs(ls, lr, options); }
851
852inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
853{ return thisex.subs(e, options); }
854
855
856/* Convert function pointer to function object suitable for map(). */
858protected:
859 ex (*ptr)(const ex &);
860public:
861 explicit pointer_to_map_function(ex x(const ex &)) : ptr(x) {}
862 ex operator()(const ex & e) override { return ptr(e); }
863};
864
865template<class T1>
867protected:
868 ex (*ptr)(const ex &, T1);
870public:
871 explicit pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
872 ex operator()(const ex & e) override { return ptr(e, arg1); }
873};
874
875template<class T1, class T2>
877protected:
878 ex (*ptr)(const ex &, T1, T2);
881public:
882 explicit pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
883 ex operator()(const ex & e) override { return ptr(e, arg1, arg2); }
884};
885
886template<class T1, class T2, class T3>
888protected:
889 ex (*ptr)(const ex &, T1, T2, T3);
893public:
894 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) {}
895 ex operator()(const ex & e) override { return ptr(e, arg1, arg2, arg3); }
896};
897
898template<class C>
900protected:
901 ex (C::*ptr)(const ex &);
902 C &c;
903public:
904 explicit pointer_to_member_to_map_function(ex (C::*member)(const ex &), C &obj) : ptr(member), c(obj) {}
905 ex operator()(const ex & e) override { return (c.*ptr)(e); }
906};
907
908template<class C, class T1>
910protected:
911 ex (C::*ptr)(const ex &, T1);
912 C &c;
914public:
915 explicit pointer_to_member_to_map_function_1arg(ex (C::*member)(const ex &, T1), C &obj, T1 a1) : ptr(member), c(obj), arg1(a1) {}
916 ex operator()(const ex & e) override { return (c.*ptr)(e, arg1); }
917};
918
919template<class C, class T1, class T2>
921protected:
922 ex (C::*ptr)(const ex &, T1, T2);
923 C &c;
926public:
927 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) {}
928 ex operator()(const ex & e) override { return (c.*ptr)(e, arg1, arg2); }
929};
930
931template<class C, class T1, class T2, class T3>
933protected:
934 ex (C::*ptr)(const ex &, T1, T2, T3);
935 C &c;
939public:
940 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) {}
941 ex operator()(const ex & e) override { return (c.*ptr)(e, arg1, arg2, arg3); }
942};
943
944inline ex ex::map(ex f(const ex &)) const
945{
946 pointer_to_map_function fcn(f);
947 return bp->map(fcn);
948}
949
950// convenience type checker template functions
951
953template <class T>
954inline bool is_a(const ex &obj)
955{
956 return is_a<T>(*obj.bp);
957}
958
960template <class T>
961inline bool is_exactly_a(const ex &obj)
962{
963 return is_exactly_a<T>(*obj.bp);
964}
965
976template <class T>
977inline const T &ex_to(const ex &e)
978{
979 GINAC_ASSERT(is_a<T>(e));
980 return static_cast<const T &>(*e.bp);
981}
982
983} // namespace GiNaC
984
985
986// Specializations of Standard Library algorithms
987namespace std {
988
990template <>
991inline void swap(GiNaC::ex &a, GiNaC::ex &b)
992{
993 a.swap(b);
994}
995
997template<>
998struct hash<GiNaC::ex>
999{
1000 std::size_t operator()(const GiNaC::ex & e) const noexcept
1001 {
1002 return e.gethash();
1003 }
1004};
1005
1007template<>
1008struct equal_to<GiNaC::ex>
1009{
1010 bool operator()(const GiNaC::ex &e1, const GiNaC::ex &e2) const noexcept
1011 {
1012 return e1.is_equal(e2);
1013 }
1014};
1015
1016} // namespace std
1017
1018#endif // ndef GINAC_EX_H
#define GINAC_ASSERT(X)
Assertion macro for checking invariances.
Definition: assertion.h:33
Interface to GiNaC's ABC.
This class stores all properties needed to record/retrieve the state of one object of class basic (or...
Definition: archive.h:49
This class is the ABC (abstract base class) of GiNaC's class hierarchy.
Definition: basic.h:105
bool operator<(const const_iterator &other) const noexcept
Definition: ex.h:474
ptrdiff_t difference_type
Definition: ex.h:377
friend const_iterator operator+(difference_type n, const const_iterator &it) noexcept
Definition: ex.h:430
const_iterator operator+(difference_type n) const noexcept
Definition: ex.h:425
bool operator<=(const const_iterator &other) const noexcept
Definition: ex.h:484
ex operator*() const
Definition: ex.h:389
friend class ex
Definition: ex.h:371
bool operator>(const const_iterator &other) const noexcept
Definition: ex.h:479
bool operator>=(const const_iterator &other) const noexcept
Definition: ex.h:489
std::random_access_iterator_tag iterator_category
Definition: ex.h:375
std::unique_ptr< ex > operator->() const
Definition: ex.h:396
bool operator!=(const const_iterator &other) const noexcept
Definition: ex.h:469
friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs) noexcept
Definition: ex.h:459
const_iterator(const ex &e_, size_t i_) noexcept
Definition: ex.h:384
const_iterator operator-(difference_type n) const noexcept
Definition: ex.h:454
const_iterator operator++(int) noexcept
Definition: ex.h:412
const_iterator & operator--() noexcept
Definition: ex.h:435
bool operator==(const const_iterator &other) const noexcept
Definition: ex.h:464
const_iterator & operator+=(difference_type n) noexcept
Definition: ex.h:419
const_iterator & operator++() noexcept
Definition: ex.h:406
ex operator[](difference_type n) const
Definition: ex.h:401
const_iterator() noexcept
Definition: ex.h:381
const_iterator & operator-=(difference_type n) noexcept
Definition: ex.h:448
const_iterator operator--(int) noexcept
Definition: ex.h:441
std::forward_iterator_tag iterator_category
Definition: ex.h:592
const_postorder_iterator & operator++()
Definition: ex.h:616
bool operator!=(const const_postorder_iterator &other) const noexcept
Definition: ex.h:634
std::stack< internal::_iter_rep, std::vector< internal::_iter_rep > > s
Definition: ex.h:640
pointer operator->() const
Definition: ex.h:611
const_postorder_iterator operator++(int)
Definition: ex.h:622
reference operator*() const
Definition: ex.h:606
const_postorder_iterator() noexcept
Definition: ex.h:597
const_postorder_iterator(const ex &e, size_t n)
Definition: ex.h:599
bool operator==(const const_postorder_iterator &other) const noexcept
Definition: ex.h:629
const_preorder_iterator() noexcept
Definition: ex.h:528
pointer operator->() const
Definition: ex.h:541
const_preorder_iterator & operator++()
Definition: ex.h:546
const_preorder_iterator operator++(int)
Definition: ex.h:552
const_preorder_iterator(const ex &e, size_t n)
Definition: ex.h:530
reference operator*() const
Definition: ex.h:536
bool operator==(const const_preorder_iterator &other) const noexcept
Definition: ex.h:559
std::forward_iterator_tag iterator_category
Definition: ex.h:523
std::stack< internal::_iter_rep, std::vector< internal::_iter_rep > > s
Definition: ex.h:570
bool operator!=(const const_preorder_iterator &other) const noexcept
Definition: ex.h:564
ptrdiff_t difference_type
Definition: ex.h:525
Wrapper template for making GiNaC classes out of STL containers.
Definition: container.h:73
Lightweight wrapper for GiNaC's symbolic objects.
Definition: ex.h:72
static ptr< basic > construct_from_string_and_lst(const std::string &s, const ex &l)
static basic & construct_from_ulonglong(unsigned long long i)
Definition: ex.cpp:545
friend bool is_exactly_a(const ex &)
Check if ex is a handle to a T, not including base classes.
Definition: ex.h:961
ex to_rational(exmap &repl) const
Rationalization of non-rational functions.
Definition: normal.cpp:2627
ex unit(const ex &x) const
Compute unit part (= sign of leading coefficient) of a multivariate polynomial in Q[x].
Definition: normal.cpp:923
static basic & construct_from_longlong(long long i)
Definition: ex.cpp:536
ex map(map_function &f) const
Definition: ex.h:162
void traverse_preorder(visitor &v) const
Traverse expression tree with given visitor, preorder traversal.
Definition: ex.cpp:188
ex operator[](const ex &index) const
Definition: ex.h:137
static basic & construct_from_int(int i)
Definition: ex.cpp:352
numeric integer_content() const
Compute the integer content (= GCD of all numeric coefficients) of an expanded polynomial.
Definition: normal.cpp:318
ex primpart(const ex &x) const
Compute primitive part of a multivariate polynomial in Q[x].
Definition: normal.cpp:981
ex lcoeff(const ex &s) const
Definition: ex.h:176
static basic & construct_from_uint(unsigned int i)
Definition: ex.cpp:410
void traverse(visitor &v) const
Definition: ex.h:169
friend bool are_ex_trivially_equal(const ex &, const ex &)
Compare two objects of class quickly without doing a deep tree traversal.
Definition: ex.h:699
bool match(const ex &pattern) const
Check whether expression matches a specified pattern.
Definition: ex.cpp:97
ex map(ex(*f)(const ex &e)) const
bool match(const ex &pattern, exmap &repls) const
Definition: ex.h:154
bool is_polynomial(const ex &vars) const
Check whether expression is a polynomial.
Definition: ex.cpp:243
const_preorder_iterator preorder_end() const noexcept
Definition: ex.h:677
ex operator[](size_t i) const
Definition: ex.h:138
unsigned gethash() const
Definition: ex.h:233
const_iterator begin() const noexcept
Definition: ex.h:662
exvector get_free_indices() const
Definition: ex.h:206
static ptr< basic > construct_from_basic(const basic &other)
Helper function for the ex-from-basic constructor.
Definition: ex.cpp:294
ex eval_integ() const
Definition: ex.h:124
bool find(const ex &pattern, exset &found) const
Find all occurrences of a pattern.
Definition: ex.cpp:107
void accept(visitor &v) const
Definition: ex.h:166
ex diff(const symbol &s, unsigned nth=1) const
Compute partial derivative of an expression.
Definition: ex.cpp:88
ex expand(unsigned options=0) const
Expand an expression.
Definition: ex.cpp:75
ex tcoeff(const ex &s) const
Definition: ex.h:177
ex numer_denom() const
Get numerator and denominator of an expression.
Definition: normal.cpp:2594
bool is_equal(const ex &other) const
Definition: ex.h:345
static basic & construct_from_double(double d)
Definition: ex.cpp:554
ex normal() const
Normalization of rational functions.
Definition: normal.cpp:2519
int degree(const ex &s) const
Definition: ex.h:173
friend const T & ex_to(const ex &)
Return a reference to the basic-derived class T object embedded in an expression.
Definition: ex.h:977
ptr< basic > bp
pointer to basic object managed by this
Definition: ex.h:251
bool has(const ex &pattern, unsigned options=0) const
Definition: ex.h:151
ex & let_op(size_t i)
Return modifiable operand/member at position i.
Definition: ex.cpp:208
ex evalf() const
Definition: ex.h:121
numeric max_coefficient() const
Return maximum (absolute value) coefficient of a polynomial.
Definition: normal.cpp:1154
ex eval_ncmul(const exvector &v) const
Definition: ex.h:123
ex conjugate() const
Definition: ex.h:146
ex simplify_indexed(unsigned options=0) const
Simplify/canonicalize expression containing indexed objects.
Definition: indexed.cpp:1256
bool is_zero_matrix() const
Check whether expression is zero or zero matrix.
Definition: ex.cpp:257
ex to_polynomial(exmap &repl) const
Definition: normal.cpp:2632
static basic & construct_from_ulong(unsigned long i)
Definition: ex.cpp:502
const_postorder_iterator postorder_end() const noexcept
Definition: ex.h:687
const_preorder_iterator preorder_begin() const
Definition: ex.h:672
ex symmetrize_cyclic() const
Symmetrize expression by cyclic permutation over its free indices.
Definition: indexed.cpp:1290
unsigned return_type() const
Definition: ex.h:230
void unitcontprim(const ex &x, ex &u, ex &c, ex &p) const
Compute unit part, content part, and primitive part of a multivariate polynomial in Q[x].
Definition: normal.cpp:1022
return_type_t return_type_tinfo() const
Definition: ex.h:231
void share(const ex &other) const
Share equal objects between expressions.
Definition: ex.cpp:280
void swap(ex &other) noexcept
Efficiently swap the contents of two expressions.
Definition: ex.h:104
const_iterator end() const noexcept
Definition: ex.h:667
size_t nops() const
Definition: ex.h:135
static basic & construct_from_long(long i)
Definition: ex.cpp:444
ex smod(const numeric &xi) const
Definition: ex.h:202
ex series(const ex &r, int order, unsigned options=0) const
Compute the truncated series expansion of an expression.
Definition: pseries.cpp:1272
ex imag_part() const
Definition: ex.h:148
ex subs(const exmap &m, unsigned options=0) const
Definition: ex.h:841
bool info(unsigned inf) const
Definition: ex.h:132
int compare(const ex &other) const
Definition: ex.h:322
ex symmetrize() const
Symmetrize expression over its free indices.
Definition: indexed.cpp:1278
friend bool is_a(const ex &)
Check if ex is a handle to a T, including base classes.
Definition: ex.h:954
ex lhs() const
Left hand side of relational expression.
Definition: ex.cpp:227
ex denom() const
Get denominator of an expression.
Definition: normal.cpp:2569
bool is_zero() const
Definition: ex.h:213
void print(const print_context &c, unsigned level=0) const
Print expression to stream.
Definition: ex.cpp:56
ex collect(const ex &s, bool distributed=false) const
Definition: ex.h:181
void dbgprinttree() const
Little wrapper arount printtree to be called within a debugger.
Definition: ex.cpp:68
ex op(size_t i) const
Definition: ex.h:136
ex content(const ex &x) const
Compute content part (= unit normal GCD of all coefficients) of a multivariate polynomial in Q[x].
Definition: normal.cpp:945
int ldegree(const ex &s) const
Definition: ex.h:174
ex rhs() const
Right hand side of relational expression.
Definition: ex.cpp:235
ex numer() const
Get numerator of an expression.
Definition: normal.cpp:2544
ex eval() const
Definition: ex.h:120
ex real_part() const
Definition: ex.h:147
ex evalm() const
Definition: ex.h:122
ex coeff(const ex &s, int n=1) const
Definition: ex.h:175
void dbgprint() const
Little wrapper arount print to be called within a debugger.
Definition: ex.cpp:62
void traverse_postorder(visitor &v) const
Traverse expression tree with given visitor, postorder traversal.
Definition: ex.cpp:198
const_postorder_iterator postorder_begin() const
Definition: ex.h:682
void makewriteable()
Make this ex writable (if more than one ex handle the same basic) by unlinking the object and creatin...
Definition: ex.cpp:271
ex antisymmetrize() const
Antisymmetrize expression over its free indices.
Definition: indexed.cpp:1284
ex() noexcept
Definition: ex.h:262
Helper class to initialize the library.
Definition: ex.h:50
library_init()
Ctor of static initialization helpers.
Definition: utils.cpp:77
static int count
How many static objects were created? Only the first one must create the static flyweights on the hea...
Definition: ex.h:56
static void init_unarchivers()
Definition: utils.cpp:260
~library_init()
Dtor of static initialization helpers.
Definition: utils.cpp:201
This class is a wrapper around CLN-numbers within the GiNaC class hierarchy.
Definition: numeric.h:82
ex(* ptr)(const ex &, T1)
Definition: ex.h:868
ex operator()(const ex &e) override
Definition: ex.h:872
pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1)
Definition: ex.h:871
ex operator()(const ex &e) override
Definition: ex.h:883
pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2)
Definition: ex.h:882
ex(* ptr)(const ex &, T1, T2)
Definition: ex.h:878
pointer_to_map_function_3args(ex x(const ex &, T1, T2, T3), T1 a1, T2 a2, T3 a3)
Definition: ex.h:894
ex operator()(const ex &e) override
Definition: ex.h:895
ex(* ptr)(const ex &, T1, T2, T3)
Definition: ex.h:889
ex operator()(const ex &e) override
Definition: ex.h:862
ex(* ptr)(const ex &)
Definition: ex.h:859
pointer_to_map_function(ex x(const ex &))
Definition: ex.h:861
ex operator()(const ex &e) override
Definition: ex.h:916
pointer_to_member_to_map_function_1arg(ex(C::*member)(const ex &, T1), C &obj, T1 a1)
Definition: ex.h:915
ex operator()(const ex &e) override
Definition: ex.h:928
pointer_to_member_to_map_function_2args(ex(C::*member)(const ex &, T1, T2), C &obj, T1 a1, T2 a2)
Definition: ex.h:927
pointer_to_member_to_map_function_3args(ex(C::*member)(const ex &, T1, T2, T3), C &obj, T1 a1, T2 a2, T3 a3)
Definition: ex.h:940
ex operator()(const ex &e) override
Definition: ex.h:941
pointer_to_member_to_map_function(ex(C::*member)(const ex &), C &obj)
Definition: ex.h:904
ex operator()(const ex &e) override
Definition: ex.h:905
Base class for print_contexts.
Definition: print.h:103
Class of (intrusively) reference-counted pointers that support copy-on-write semantics.
Definition: ptr.h:56
Helper class for storing information about known scalar products which are to be automatically replac...
Definition: indexed.h:217
@ dynallocated
heap-allocated (i.e. created by new if we want to be clever and bypass the stack,
Definition: flags.h:202
Basic CAS symbol.
Definition: symbol.h:39
Degenerate base class for visitors.
Definition: basic.h:97
unsigned options
Definition: factor.cpp:2475
size_t n
Definition: factor.cpp:1432
size_t c
Definition: factor.cpp:757
ex x
Definition: factor.cpp:1610
size_t r
Definition: factor.cpp:757
umodpoly lr[2]
Definition: factor.cpp:1428
mvec m
Definition: factor.cpp:758
ex cont
Definition: factor.cpp:2191
int order
Definition: add.cpp:38
bool is_zero(const ex &thisex)
Definition: ex.h:835
ex real_part(const ex &thisex)
Definition: ex.h:736
std::ostream & operator<<(std::ostream &os, const archive_node &n)
Write archive_node to binary data stream.
Definition: archive.cpp:200
ex denom(const ex &thisex)
Definition: ex.h:763
bool is_polynomial(const ex &thisex, const ex &vars)
Definition: ex.h:748
ex to_rational(const ex &thisex, exmap &repl)
Definition: ex.h:772
std::map< ex, ex, ex_is_less > exmap
Definition: basic.h:50
const T & ex_to(const ex &e)
Return a reference to the basic-derived class T object embedded in an expression.
Definition: ex.h:977
std::set< ex, ex_is_less > exset
Definition: basic.h:49
ex symmetrize(const ex &thisex)
Definition: ex.h:808
bool are_ex_trivially_equal(const ex &e1, const ex &e2)
Compare two objects of class quickly without doing a deep tree traversal.
Definition: ex.h:699
ex rhs(const ex &thisex)
Definition: ex.h:832
ex series(const ex &thisex, const ex &r, int order, unsigned options=0)
Definition: ex.h:796
ex conjugate(const ex &thisex)
Definition: ex.h:733
ex diff(const ex &thisex, const symbol &s, unsigned nth=1)
Definition: ex.h:793
ex simplify_indexed(const ex &thisex, unsigned options=0)
Definition: ex.h:802
ex subs(const ex &thisex, const exmap &m, unsigned options=0)
Definition: ex.h:846
ex eval(const ex &thisex)
Definition: ex.h:781
ex lhs(const ex &thisex)
Definition: ex.h:829
int degree(const ex &thisex, const ex &s)
Definition: ex.h:751
bool match(const ex &thisex, const ex &pattern, exmap &repl_lst)
Definition: ex.h:799
int ldegree(const ex &thisex, const ex &s)
Definition: ex.h:754
const basic * _num0_bp
Definition: utils.cpp:368
bool is_a(const basic &obj)
Check if obj is a T, including base classes.
Definition: basic.h:313
ex evalf(const ex &thisex)
Definition: ex.h:784
ex normal(const ex &thisex)
Definition: ex.h:769
ex antisymmetrize(const ex &thisex)
Definition: ex.h:814
ex op(const ex &thisex, size_t i)
Definition: ex.h:826
ex coeff(const ex &thisex, const ex &s, int n=1)
Definition: ex.h:757
ex numer(const ex &thisex)
Definition: ex.h:760
ex collect(const ex &thisex, const ex &s, bool distributed=false)
Definition: ex.h:778
ex eval_integ(const ex &thisex)
Definition: ex.h:790
void swap(ex &e1, ex &e2)
Definition: ex.h:838
bool find(const ex &thisex, const ex &pattern, exset &found)
Definition: ex.h:745
ex evalm(const ex &thisex)
Definition: ex.h:787
bool is_exactly_a(const basic &obj)
Check if obj is a T, not including base classes.
Definition: basic.h:320
ex symmetrize_cyclic(const ex &thisex)
Definition: ex.h:820
bool has(const ex &thisex, const ex &pattern, unsigned options=0)
Definition: ex.h:742
const ex _ex0
Definition: utils.cpp:369
std::vector< ex > exvector
Definition: basic.h:48
ex numer_denom(const ex &thisex)
Definition: ex.h:766
size_t nops(const ex &thisex)
Definition: ex.h:727
ex imag_part(const ex &thisex)
Definition: ex.h:739
ex to_polynomial(const ex &thisex, exmap &repl)
Definition: ex.h:775
static library_init library_initializer
For construction of flyweights, etc.
Definition: ex.h:59
ex expand(const ex &thisex, unsigned options=0)
Definition: ex.h:730
Definition: ex.h:987
void swap(GiNaC::ex &a, GiNaC::ex &b)
Specialization of std::swap() for ex objects.
Definition: ex.h:991
Reference-counted pointer template.
bool operator()(const ex &lh, const ex &rh) const
Definition: ex.h:710
bool operator()(const ex &lh, const ex &rh) const
Definition: ex.h:706
void operator()(ex &lh, ex &rh) const
Definition: ex.h:718
bool operator==(const _iter_rep &other) const noexcept
Definition: ex.h:504
bool operator!=(const _iter_rep &other) const noexcept
Definition: ex.h:509
_iter_rep(const ex &e_, size_t i_, size_t i_end_)
Definition: ex.h:502
Function object for map().
Definition: basic.h:85
bool operator()(const ex &lh, const ex &rh) const
Definition: ex.h:714
To distinguish between different kinds of non-commutative objects.
Definition: registrar.h:44
bool operator()(const GiNaC::ex &e1, const GiNaC::ex &e2) const noexcept
Definition: ex.h:1010
std::size_t operator()(const GiNaC::ex &e) const noexcept
Definition: ex.h:1000

This page is part of the GiNaC developer's reference. It was generated automatically by doxygen. For an introduction, see the tutorial.