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 bool are_ex_trivially_equal(const ex &, const ex &);
66 template<class T> friend const T &ex_to(const ex &);
67 template<class T> friend bool is_a(const ex &);
68 template<class T> friend 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 symbol &x) const;
308 ex content(const symbol &x) const;
309 numeric integer_content() const;
310 ex primpart(const symbol &x) const;
311 ex primpart(const symbol &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();
349 #ifdef OBSCURE_CINT_HACK
351 static bool last_created_or_assigned_bp_can_be_converted_to_ex()
353 if (last_created_or_assigned_bp==0) return false;
354 if ((last_created_or_assigned_bp->flags &
355 status_flags::dynallocated)==0) return false;
356 if ((last_created_or_assigned_bp->flags &
357 status_flags::evaluated)==0) return false;
361 void update_last_created_or_assigned_bp()
363 last_created_or_assigned_bp = bp;
364 last_created_or_assigned_exp = (long)(void *)(this);
366 #endif // def OBSCURE_CINT_HACK
371 ptr<basic> bp; ///< pointer to basic object managed by this
373 #ifdef OBSCURE_CINT_HACK
375 static ptr<basic> last_created_or_assigned_bp;
376 static basic * dummy_bp;
377 static long last_created_or_assigned_exp;
378 #endif // def OBSCURE_CINT_HACK
382 // performance-critical inlined method implementations
384 // This needs to be a basic* because we don't know that numeric is derived
385 // from basic and we need a basic& for the ex default constructor
386 extern const basic *_num0_bp;
389 ex::ex() : bp(*const_cast<basic *>(_num0_bp))
391 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
392 #ifdef OBSCURE_CINT_HACK
393 update_last_created_or_assigned_bp();
394 #endif // def OBSCURE_CINT_HACK
397 #ifdef OBSCURE_CINT_HACK
399 ex::ex(const ex & other) : bp(other.bp)
401 GINAC_ASSERT((bp->flags) & status_flags::dynallocated);
402 update_last_created_or_assigned_bp();
406 ex & ex::operator=(const ex & other)
408 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
409 GINAC_ASSERT(other.bp->flags & status_flags::dynallocated);
411 update_last_created_or_assigned_bp();
414 #endif // def OBSCURE_CINT_HACK
417 ex::ex(const basic & other) : bp(construct_from_basic(other))
419 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
420 #ifdef OBSCURE_CINT_HACK
421 update_last_created_or_assigned_bp();
422 #endif // def OBSCURE_CINT_HACK
426 ex::ex(int i) : bp(construct_from_int(i))
428 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
429 #ifdef OBSCURE_CINT_HACK
430 update_last_created_or_assigned_bp();
431 #endif // def OBSCURE_CINT_HACK
435 ex::ex(unsigned int i) : bp(construct_from_uint(i))
437 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
438 #ifdef OBSCURE_CINT_HACK
439 update_last_created_or_assigned_bp();
440 #endif // def OBSCURE_CINT_HACK
444 ex::ex(long i) : bp(construct_from_long(i))
446 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
447 #ifdef OBSCURE_CINT_HACK
448 update_last_created_or_assigned_bp();
449 #endif // def OBSCURE_CINT_HACK
453 ex::ex(unsigned long i) : bp(construct_from_ulong(i))
455 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
456 #ifdef OBSCURE_CINT_HACK
457 update_last_created_or_assigned_bp();
458 #endif // def OBSCURE_CINT_HACK
462 ex::ex(double const d) : bp(construct_from_double(d))
464 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
465 #ifdef OBSCURE_CINT_HACK
466 update_last_created_or_assigned_bp();
467 #endif // def OBSCURE_CINT_HACK
471 ex::ex(const std::string &s, const ex &l) : bp(construct_from_string_and_lst(s, l))
473 GINAC_ASSERT(bp->flags & status_flags::dynallocated);
474 #ifdef OBSCURE_CINT_HACK
475 update_last_created_or_assigned_bp();
476 #endif // def OBSCURE_CINT_HACK
480 int ex::compare(const ex & other) const
482 if (bp == other.bp) // trivial case: both expressions point to same basic
484 return bp->compare(*other.bp);
488 bool ex::is_equal(const ex & other) const
490 if (bp == other.bp) // trivial case: both expressions point to same basic
492 return bp->is_equal(*other.bp);
498 /** Compare two objects of class quickly without doing a deep tree traversal.
499 * @return "true" if they are equal
500 * "false" if equality cannot be established quickly (e1 and e2 may
501 * still be equal, in this case. */
502 inline bool are_ex_trivially_equal(const ex &e1, const ex &e2)
504 return e1.bp == e2.bp;
507 // wrapper functions around member functions
508 inline size_t nops(const ex & thisex)
509 { return thisex.nops(); }
511 inline ex expand(const ex & thisex, unsigned options = 0)
512 { return thisex.expand(options); }
514 inline bool has(const ex & thisex, const ex & pattern)
515 { return thisex.has(pattern); }
517 inline bool find(const ex & thisex, const ex & pattern, lst & found)
518 { return thisex.find(pattern, found); }
520 inline int degree(const ex & thisex, const ex & s)
521 { return thisex.degree(s); }
523 inline int ldegree(const ex & thisex, const ex & s)
524 { return thisex.ldegree(s); }
526 inline ex coeff(const ex & thisex, const ex & s, int n=1)
527 { return thisex.coeff(s, n); }
529 inline ex numer(const ex & thisex)
530 { return thisex.numer(); }
532 inline ex denom(const ex & thisex)
533 { return thisex.denom(); }
535 inline ex numer_denom(const ex & thisex)
536 { return thisex.numer_denom(); }
538 inline ex normal(const ex & thisex, int level=0)
539 { return thisex.normal(level); }
541 inline ex to_rational(const ex & thisex, lst & repl_lst)
542 { return thisex.to_rational(repl_lst); }
544 inline ex to_polynomial(const ex & thisex, lst & repl_lst)
545 { return thisex.to_polynomial(repl_lst); }
547 inline ex collect(const ex & thisex, const ex & s, bool distributed = false)
548 { return thisex.collect(s, distributed); }
550 inline ex eval(const ex & thisex, int level = 0)
551 { return thisex.eval(level); }
553 inline ex evalf(const ex & thisex, int level = 0)
554 { return thisex.evalf(level); }
556 inline ex evalm(const ex & thisex)
557 { return thisex.evalm(); }
559 inline ex diff(const ex & thisex, const symbol & s, unsigned nth = 1)
560 { return thisex.diff(s, nth); }
562 inline ex series(const ex & thisex, const ex & r, int order, unsigned options = 0)
563 { return thisex.series(r, order, options); }
565 inline bool match(const ex & thisex, const ex & pattern, lst & repl_lst)
566 { return thisex.match(pattern, repl_lst); }
568 inline ex simplify_indexed(const ex & thisex)
569 { return thisex.simplify_indexed(); }
571 inline ex simplify_indexed(const ex & thisex, const scalar_products & sp)
572 { return thisex.simplify_indexed(sp); }
574 inline ex symmetrize(const ex & thisex)
575 { return thisex.symmetrize(); }
577 inline ex symmetrize(const ex & thisex, const lst & l)
578 { return thisex.symmetrize(l); }
580 inline ex antisymmetrize(const ex & thisex)
581 { return thisex.antisymmetrize(); }
583 inline ex antisymmetrize(const ex & thisex, const lst & l)
584 { return thisex.antisymmetrize(l); }
586 inline ex symmetrize_cyclic(const ex & thisex)
587 { return thisex.symmetrize_cyclic(); }
589 inline ex symmetrize_cyclic(const ex & thisex, const lst & l)
590 { return thisex.symmetrize_cyclic(l); }
592 inline ex op(const ex & thisex, size_t i)
593 { return thisex.op(i); }
595 inline ex lhs(const ex & thisex)
596 { return thisex.lhs(); }
598 inline ex rhs(const ex & thisex)
599 { return thisex.rhs(); }
601 inline bool is_zero(const ex & thisex)
602 { return thisex.is_zero(); }
604 inline void swap(ex & e1, ex & e2)
607 /* Function objects for STL sort() etc. */
608 struct ex_is_less : public std::binary_function<ex, ex, bool> {
609 bool operator() (const ex &lh, const ex &rh) const { return lh.compare(rh) < 0; }
612 struct ex_is_equal : public std::binary_function<ex, ex, bool> {
613 bool operator() (const ex &lh, const ex &rh) const { return lh.is_equal(rh); }
616 struct op0_is_equal : public std::binary_function<ex, ex, bool> {
617 bool operator() (const ex &lh, const ex &rh) const { return lh.op(0).is_equal(rh.op(0)); }
620 struct ex_swap : public std::binary_function<ex, ex, void> {
621 void operator() (ex &lh, ex &rh) const { lh.swap(rh); }
624 inline ex ex::subs(const exmap & m, unsigned options) const
626 return bp->subs(m, options);
629 inline ex subs(const ex & thisex, const exmap & m, unsigned options = 0)
630 { return thisex.subs(m, options); }
632 inline ex subs(const ex & thisex, const lst & ls, const lst & lr, unsigned options = 0)
633 { return thisex.subs(ls, lr, options); }
635 inline ex subs(const ex & thisex, const ex & e, unsigned options = 0)
636 { return thisex.subs(e, options); }
639 /* Convert function pointer to function object suitable for map(). */
640 class pointer_to_map_function : public map_function {
642 ex (*ptr)(const ex &);
644 explicit pointer_to_map_function(ex x(const ex &)) : ptr(x) {}
645 ex operator()(const ex & e) { return ptr(e); }
649 class pointer_to_map_function_1arg : public map_function {
651 ex (*ptr)(const ex &, T1);
654 explicit pointer_to_map_function_1arg(ex x(const ex &, T1), T1 a1) : ptr(x), arg1(a1) {}
655 ex operator()(const ex & e) { return ptr(e, arg1); }
658 template<class T1, class T2>
659 class pointer_to_map_function_2args : public map_function {
661 ex (*ptr)(const ex &, T1, T2);
665 explicit pointer_to_map_function_2args(ex x(const ex &, T1, T2), T1 a1, T2 a2) : ptr(x), arg1(a1), arg2(a2) {}
666 ex operator()(const ex & e) { return ptr(e, arg1, arg2); }
669 template<class T1, class T2, class T3>
670 class pointer_to_map_function_3args : public map_function {
672 ex (*ptr)(const ex &, T1, T2, T3);
677 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) {}
678 ex operator()(const ex & e) { return ptr(e, arg1, arg2, arg3); }
681 inline ex ex::map(ex f(const ex &)) const
683 pointer_to_map_function fcn(f);
687 // convenience type checker template functions
689 /** Check if ex is a handle to a T, including base classes. */
691 inline bool is_a(const ex &obj)
693 return is_a<T>(*obj.bp);
696 /** Check if ex is a handle to a T, not including base classes. */
698 inline bool is_exactly_a(const ex &obj)
700 return is_exactly_a<T>(*obj.bp);
703 /** Return a reference to the basic-derived class T object embedded in an
704 * expression. This is fast but unsafe: the result is undefined if the
705 * expression does not contain a T object at its top level. Hence, you
706 * should generally check the type of e first.
708 * @param e expression
709 * @return reference to object of class T
710 * @see is_exactly_a<class T>() */
712 inline const T &ex_to(const ex &e)
714 GINAC_ASSERT(is_a<T>(e));
715 return static_cast<const T &>(*e.bp);
721 // Specializations of Standard Library algorithms
724 /** Specialization of std::swap() for ex objects. */
726 inline void swap(GiNaC::ex &a, GiNaC::ex &b)
731 /** Specialization of std::iter_swap() for vector<ex> iterators. */
733 inline void iter_swap(vector<GiNaC::ex>::iterator i1, vector<GiNaC::ex>::iterator i2)
738 /** Specialization of std::iter_swap() for list<ex> iterators. */
740 inline void iter_swap(list<GiNaC::ex>::iterator i1, list<GiNaC::ex>::iterator i2)
747 #endif // ndef __GINAC_EX_H__