3 * Interface to GiNaC's light-weight expression handles. */
6 * GiNaC Copyright (C) 1999-2003 Johannes Gutenberg University Mainz, Germany
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.
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.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #ifndef __GINAC_EX_H__
24 #define __GINAC_EX_H__
36 /** Helper class to initialize the library. There must be one static object
37 * of this class in every object file that makes use of our flyweights in
38 * order to guarantee proper initialization. Hence we put it into this
39 * file which is included by every relevant file anyways. This is modeled
40 * after section 27.4.2.1.6 of the C++ standard, where cout and friends are
51 /** For construction of flyweights, etc. */
52 static library_init library_initializer;
55 class scalar_products;
58 /** Lightweight wrapper for GiNaC's symbolic objects. Basically all it does is
59 * to hold a pointer to the other objects, manage the reference counting and
60 * provide methods for manipulation of these objects. (Some people call such
61 * a thing a proxy class.) */
64 friend class archive_node;
65 friend inline bool are_ex_trivially_equal(const ex &, const ex &);
66 template<class T> friend inline const T &ex_to(const ex &);
67 template<class T> friend inline bool is_a(const ex &);
68 template<class T> friend inline bool is_exactly_a(const ex &);
70 // default constructor, copy constructor and assignment operator
73 #ifdef OBSCURE_CINT_HACK
75 ex & operator=(const ex & other);
80 ex(const basic & other);
87 /** Construct ex from string and a list of symbols. The input grammar is
88 * similar to the GiNaC output format. All symbols and indices to be used
89 * in the expression must be specified in a lst in the second argument.
90 * Undefined symbols and other parser errors will throw an exception. */
91 ex(const std::string &s, const ex &l);
95 class const_iterator : public std::iterator<std::random_access_iterator_tag, ex, ptrdiff_t, const ex *, const ex &>
101 const_iterator(const basic *bp_, size_t i_) : bp(bp_), i(i_) {}
103 bool operator==(const const_iterator &other) const
105 return bp == other.bp && i == other.i;
108 bool operator!=(const const_iterator &other) const
110 return !(*this == other);
113 bool operator<(const const_iterator &other) const
118 bool operator>(const const_iterator &other) const
120 return other < *this;
123 bool operator<=(const const_iterator &other) const
125 return !(other < *this);
128 bool operator>=(const const_iterator &other) const
130 return !(*this < other);
133 // This should return an ex&, but that would be a reference to a
141 // How do we make this work in the context of the "reference to
142 // temporary" problem? Return an auto_ptr?
143 pointer operator->() const
145 return &(operator*());
149 const_iterator &operator++()
155 const_iterator operator++(int)
157 const_iterator tmp = *this;
162 const_iterator &operator+=(difference_type n)
168 const_iterator operator+(difference_type n) const
170 return const_iterator(bp, i + n);
173 inline friend const_iterator operator+(difference_type n, const const_iterator &it)
175 return const_iterator(it.bp, it.i + n);
178 const_iterator &operator--()
184 const_iterator operator--(int)
186 const_iterator tmp = *this;
191 const_iterator &operator-=(difference_type n)
197 const_iterator operator-(difference_type n) const
199 return const_iterator(bp, i - n);
202 inline friend difference_type operator-(const const_iterator &lhs, const const_iterator &rhs)
204 return lhs.i - rhs.i;
207 reference operator[](difference_type n) const
216 const_iterator begin() const { return const_iterator(get_pointer(bp), 0); }
217 const_iterator end() const { return const_iterator(get_pointer(bp), bp->nops()); }
220 // This doesn't work because of the "reference to temporary" problem
222 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
223 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
224 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
227 // non-virtual functions in this class
229 /** Efficiently swap the contents of two expressions. */
230 void swap(ex & other)
232 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
233 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
238 ex eval(int level = 0) const { return bp->eval(level); }
239 ex evalf(int level = 0) const { return bp->evalf(level); }
240 ex evalm() const { return bp->evalm(); }
241 ex eval_ncmul(const exvector & v) const { return bp->eval_ncmul(v); }
244 void print(const print_context & c, unsigned level = 0) const;
245 void dbgprint() const;
246 void dbgprinttree() const;
249 bool info(unsigned inf) const { return bp->info(inf); }
252 size_t nops() const { return bp->nops(); }
253 ex op(size_t i) const { return bp->op(i); }
254 ex operator[](const ex & index) const { return (*bp)[index]; }
255 ex operator[](size_t i) const { return (*bp)[i]; }
256 ex & let_op(size_t i);
257 ex & operator[](const ex & index);
258 ex & operator[](size_t i);
263 bool has(const ex & pattern) const { return bp->has(pattern); }
264 bool find(const ex & pattern, lst & found) const;
265 bool match(const ex & pattern) const;
266 bool match(const ex & pattern, lst & repl_lst) const { return bp->match(pattern, repl_lst); }
269 ex subs(const exmap & m, unsigned options = 0) const;
270 ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
271 ex subs(const ex & e, unsigned options = 0) const;
274 ex map(map_function & f) const { return bp->map(f); }
275 ex map(ex (*f)(const ex & e)) const;
277 // visitors and tree traversal
278 void accept(visitor & v) const { bp->accept(v); }
279 void traverse_preorder(visitor & v) const;
280 void traverse_postorder(visitor & v) const;
281 void traverse(visitor & v) const { traverse_preorder(v); }
284 int degree(const ex & s) const { return bp->degree(s); }
285 int ldegree(const ex & s) const { return bp->ldegree(s); }
286 ex coeff(const ex & s, int n = 1) const { return bp->coeff(s, n); }
287 ex lcoeff(const ex & s) const { return coeff(s, degree(s)); }
288 ex tcoeff(const ex & s) const { return coeff(s, ldegree(s)); }
291 ex expand(unsigned options=0) const;
292 ex collect(const ex & s, bool distributed = false) const { return bp->collect(s, distributed); }
294 // differentiation and series expansion
295 ex diff(const symbol & s, unsigned nth = 1) const;
296 ex series(const ex & r, int order, unsigned options = 0) const;
298 // rational functions
299 ex normal(int level = 0) const;
300 ex to_rational(lst &repl_lst) const;
301 ex to_polynomial(lst &repl_lst) const;
304 ex numer_denom() const;
306 // polynomial algorithms
307 ex unit(const ex &x) const;
308 ex content(const ex &x) const;
309 numeric integer_content() const;
310 ex primpart(const ex &x) const;
311 ex primpart(const ex &x, const ex &cont) const;
312 ex smod(const numeric &xi) const { return bp->smod(xi); }
313 numeric max_coefficient() const;
316 exvector get_free_indices() const { return bp->get_free_indices(); }
317 ex simplify_indexed() const;
318 ex simplify_indexed(const scalar_products & sp) const;
321 int compare(const ex & other) const;
322 bool is_equal(const ex & other) const;
323 bool is_zero() const { extern const ex _ex0; return is_equal(_ex0); }
326 ex symmetrize() const;
327 ex symmetrize(const lst & l) const;
328 ex antisymmetrize() const;
329 ex antisymmetrize(const lst & l) const;
330 ex symmetrize_cyclic() const;
331 ex symmetrize_cyclic(const lst & l) const;
334 unsigned return_type() const { return bp->return_type(); }
335 unsigned return_type_tinfo() const { return bp->return_type_tinfo(); }
337 unsigned gethash() const { return bp->gethash(); }
340 static ptr<basic> construct_from_basic(const basic & other);
341 static basic & construct_from_int(int i);
342 static basic & construct_from_uint(unsigned int i);
343 static basic & construct_from_long(long i);
344 static basic & construct_from_ulong(unsigned long i);
345 static basic & construct_from_double(double d);
346 static ptr<basic> construct_from_string_and_lst(const std::string &s, const ex &l);
347 void makewriteable();
348 void share(const ex & other) const;
350 #ifdef OBSCURE_CINT_HACK
352 static bool last_created_or_assigned_bp_can_be_converted_to_ex()
354 if (last_created_or_assigned_bp==0) return false;
355 if ((last_created_or_assigned_bp->flags &
356 status_flags::dynallocated)==0) return false;
357 if ((last_created_or_assigned_bp->flags &
358 status_flags::evaluated)==0) return false;
362 void update_last_created_or_assigned_bp()
364 last_created_or_assigned_bp = bp;
365 last_created_or_assigned_exp = (long)(void *)(this);
367 #endif // def OBSCURE_CINT_HACK
372 mutable ptr<basic> bp; ///< pointer to basic object managed by this
374 #ifdef OBSCURE_CINT_HACK
376 static ptr<basic> last_created_or_assigned_bp;
377 static basic * dummy_bp;
378 static long last_created_or_assigned_exp;
379 #endif // def OBSCURE_CINT_HACK
383 // performance-critical inlined method implementations
385 // This needs to be a basic* because we don't know that numeric is derived
386 // from basic and we need a basic& for the ex default constructor
387 extern const basic *_num0_bp;
390 ex::ex() : bp(*const_cast<basic *>(_num0_bp))
392 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
393 #ifdef OBSCURE_CINT_HACK
394 update_last_created_or_assigned_bp();
395 #endif // def OBSCURE_CINT_HACK
398 #ifdef OBSCURE_CINT_HACK
400 ex::ex(const ex & other) : bp(other.bp)
402 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
403 update_last_created_or_assigned_bp();
407 ex & ex::operator=(const ex & other)
409 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
410 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
412 update_last_created_or_assigned_bp();
415 #endif // def OBSCURE_CINT_HACK
418 ex::ex(const basic & other) : bp(construct_from_basic(other))
420 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
421 #ifdef OBSCURE_CINT_HACK
422 update_last_created_or_assigned_bp();
423 #endif // def OBSCURE_CINT_HACK
427 ex::ex(int i) : bp(construct_from_int(i))
429 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
430 #ifdef OBSCURE_CINT_HACK
431 update_last_created_or_assigned_bp();
432 #endif // def OBSCURE_CINT_HACK
436 ex::ex(unsigned int i) : bp(construct_from_uint(i))
438 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
439 #ifdef OBSCURE_CINT_HACK
440 update_last_created_or_assigned_bp();
441 #endif // def OBSCURE_CINT_HACK
445 ex::ex(long i) : bp(construct_from_long(i))
447 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
448 #ifdef OBSCURE_CINT_HACK
449 update_last_created_or_assigned_bp();
450 #endif // def OBSCURE_CINT_HACK
454 ex::ex(unsigned long i) : bp(construct_from_ulong(i))
456 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
457 #ifdef OBSCURE_CINT_HACK
458 update_last_created_or_assigned_bp();
459 #endif // def OBSCURE_CINT_HACK
463 ex::ex(double const d) : bp(construct_from_double(d))
465 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
466 #ifdef OBSCURE_CINT_HACK
467 update_last_created_or_assigned_bp();
468 #endif // def OBSCURE_CINT_HACK
472 ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
474 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
475 #ifdef OBSCURE_CINT_HACK
476 update_last_created_or_assigned_bp();
477 #endif // def OBSCURE_CINT_HACK
481 int ex::compare(const ex & other) const
483 if (bp == other.bp) // trivial case: both expressions point to same basic
485 const int cmpval = bp->compare(*other.bp);
487 // Expressions point to different, but equal, trees: conserve
488 // memory and make subsequent compare() operations faster by
489 // making both expression point to the same tree.
496 bool ex::is_equal(const ex & other) const
498 if (bp == other.bp) // trivial case: both expressions point to same basic
500 return bp->is_equal(*other.bp);
506 /** Compare two objects of class quickly without doing a deep tree traversal.
507 * @return "true" if they are equal
508 * "false" if equality cannot be established quickly (e1 and e2 may
509 * still be equal, in this case. */
510 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
512 return e1.bp == e2.bp;
515 // wrapper functions around member functions
516 inline size_t nops(const ex & thisex)
517 { return thisex.nops(); }
519 inline ex expand(const ex & thisex, unsigned options = 0)
520 { return thisex.expand(options); }
522 inline bool has(const ex & thisex, const ex & pattern)
523 { return thisex.has(pattern); }
525 inline bool find(const ex & thisex, const ex & pattern, lst & found)
526 { return thisex.find(pattern, found); }
528 inline int degree(const ex & thisex, const ex & s)
529 { return thisex.degree(s); }
531 inline int ldegree(const ex & thisex, const ex & s)
532 { return thisex.ldegree(s); }
534 inline ex coeff(const ex & thisex, const ex & s, int n=1)
535 { return thisex.coeff(s, n); }
537 inline ex numer(const ex & thisex)
538 { return thisex.numer(); }
540 inline ex denom(const ex & thisex)
541 { return thisex.denom(); }
543 inline ex numer_denom(const ex & thisex)
544 { return thisex.numer_denom(); }
546 inline ex normal(const ex & thisex, int level=0)
547 { return thisex.normal(level); }
549 inline ex to_rational(const ex & thisex, lst & repl_lst)
550 { return thisex.to_rational(repl_lst); }
552 inline ex to_polynomial(const ex & thisex, lst & repl_lst)
553 { return thisex.to_polynomial(repl_lst); }
555 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
556 { return thisex.collect(s, distributed); }
558 inline ex eval(const ex & thisex, int level = 0)
559 { return thisex.eval(level); }
561 inline ex evalf(const ex & thisex, int level = 0)
562 { return thisex.evalf(level); }
564 inline ex evalm(const ex & thisex)
565 { return thisex.evalm(); }
567 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
568 { return thisex.diff(s, nth); }
570 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
571 { return thisex.series(r, order, options); }
573 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
574 { return thisex.match(pattern, repl_lst); }
576 inline ex simplify_indexed(const ex & thisex)
577 { return thisex.simplify_indexed(); }
579 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
580 { return thisex.simplify_indexed(sp); }
582 inline ex symmetrize(const ex & thisex)
583 { return thisex.symmetrize(); }
585 inline ex symmetrize(const ex & thisex, const lst & l)
586 { return thisex.symmetrize(l); }
588 inline ex antisymmetrize(const ex & thisex)
589 { return thisex.antisymmetrize(); }
591 inline ex antisymmetrize(const ex & thisex, const lst & l)
592 { return thisex.antisymmetrize(l); }
594 inline ex symmetrize_cyclic(const ex & thisex)
595 { return thisex.symmetrize_cyclic(); }
597 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
598 { return thisex.symmetrize_cyclic(l); }
600 inline ex op(const ex & thisex, size_t i)
601 { return thisex.op(i); }
603 inline ex lhs(const ex & thisex)
604 { return thisex.lhs(); }
606 inline ex rhs(const ex & thisex)
607 { return thisex.rhs(); }
609 inline bool is_zero(const ex & thisex)
610 { return thisex.is_zero(); }
612 inline void swap(ex & e1, ex & e2)
615 /* Function objects for STL sort() etc. */
616 struct ex_is_less : public std::binary_function<ex, ex, bool> {
617 bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
620 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
621 bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
624 struct op0_is_equal : public std::binary_function<ex, ex, bool> {
625 bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
628 struct ex_swap : public std::binary_function<ex, ex, void> {
629 void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
632 inline ex ex::subs(const exmap & m, unsigned options) const
634 return bp->subs(m, options);
637 inline ex subs(const ex & thisex, const exmap & m, unsigned options = 0)
638 { return thisex.subs(m, options); }
640 inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
641 { return thisex.subs(ls, lr, options); }
643 inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
644 { return thisex.subs(e, options); }
647 /* Convert function pointer to function object suitable for map(). */
648 class pointer_to_map_function : public map_function {
650 ex (*ptr)(const ex &);
652 explicit pointer_to_map_function(ex x(const ex &)) : ptr(x) {}
653 ex operator()(const ex & e) { return ptr(e); }
657 class pointer_to_map_function_1arg : public map_function {
659 ex (*ptr)(const ex &, T1);
662 explicit pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
663 ex operator()(const ex & e) { return ptr(e, arg1); }
666 template<class T1, class T2>
667 class pointer_to_map_function_2args : public map_function {
669 ex (*ptr)(const ex &, T1, T2);
673 explicit pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
674 ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
677 template<class T1, class T2, class T3>
678 class pointer_to_map_function_3args : public map_function {
680 ex (*ptr)(const ex &, T1, T2, T3);
685 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) {}
686 ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
689 inline ex ex::map(ex f(const ex &)) const
691 pointer_to_map_function fcn(f);
695 // convenience type checker template functions
697 /** Check if ex is a handle to a T, including base classes. */
699 inline bool is_a(const ex &obj)
701 return is_a<T>(*obj.bp);
704 /** Check if ex is a handle to a T, not including base classes. */
706 inline bool is_exactly_a(const ex &obj)
708 return is_exactly_a<T>(*obj.bp);
711 /** Return a reference to the basic-derived class T object embedded in an
712 * expression. This is fast but unsafe: the result is undefined if the
713 * expression does not contain a T object at its top level. Hence, you
714 * should generally check the type of e first.
716 * @param e expression
717 * @return reference to object of class T
718 * @see is_exactly_a<class T>() */
720 inline const T &ex_to(const ex &e)
722 GINAC_ASSERT(is_a<T>(e));
723 return static_cast<const T &>(*e.bp);
729 // Specializations of Standard Library algorithms
732 /** Specialization of std::swap() for ex objects. */
734 inline void swap(GiNaC::ex &a, GiNaC::ex &b)
739 /** Specialization of std::iter_swap() for vector<ex> iterators. */
741 inline void iter_swap(vector<GiNaC::ex>::iterator i1, vector<GiNaC::ex>::iterator i2)
746 /** Specialization of std::iter_swap() for list<ex> iterators. */
748 inline void iter_swap(list<GiNaC::ex>::iterator i1, list<GiNaC::ex>::iterator i2)
755 #endif // ndef __GINAC_EX_H__