3 * Interface to GiNaC's ABC. */
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_BASIC_H__
24 #define __GINAC_BASIC_H__
26 #include <cstddef> // for size_t
29 // CINT needs <algorithm> to work properly with <vector>
34 #include "assertion.h"
35 #include "registrar.h"
46 template <class> class ptr;
48 typedef std::vector<ex> exvector;
49 typedef std::map<ex, ex, ex_is_less> exmap;
52 /** Function object for map(). */
54 typedef const ex & argument_type;
55 typedef ex result_type;
56 virtual ex operator()(const ex & e) = 0;
60 /** Degenerate base class for visitors. basic and derivative classes
61 * support Robert C. Martin's Acyclic Visitor pattern (cf.
62 * http://objectmentor.com/publications/acv.pdf). */
69 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
70 * It is responsible for the reference counting. */
73 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
76 friend class ptr<basic>;
78 // default constructor, destructor, copy constructor and assignment operator
80 basic() : tinfo_key(TINFO_basic), flags(0), refcount(0) {}
83 /** basic destructor, virtual because class ex will delete objects of
84 * derived classes via a basic*. */
87 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
89 basic(const basic & other);
90 const basic & operator=(const basic & other);
93 /** Constructor with specified tinfo_key (used by derived classes instead
94 * of the default constructor to avoid assigning tinfo_key twice). */
95 basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0) {}
97 // new virtual functions which can be overridden by derived classes
98 public: // only const functions please (may break reference counting)
100 /** Create a clone of this object on the heap. One can think of this as
101 * simulating a virtual copy constructor which is needed for instance by
102 * the refcounted construction of an ex from a basic. */
103 virtual basic * duplicate() const { return new basic(*this); }
106 virtual ex eval(int level = 0) const;
107 virtual ex evalf(int level = 0) const;
108 virtual ex evalm() const;
110 virtual ex eval_ncmul(const exvector & v) const;
112 virtual ex eval_indexed(const basic & i) const;
115 virtual void print(const print_context & c, unsigned level = 0) const;
116 virtual void dbgprint() const;
117 virtual void dbgprinttree() const;
118 virtual unsigned precedence() const;
121 virtual bool info(unsigned inf) const;
124 virtual size_t nops() const;
125 virtual ex op(size_t i) const;
126 virtual ex operator[](const ex & index) const;
127 virtual ex operator[](size_t i) const;
128 virtual ex & let_op(size_t i);
129 virtual ex & operator[](const ex & index);
130 virtual ex & operator[](size_t i);
133 virtual bool has(const ex & other) const;
134 virtual bool match(const ex & pattern, lst & repl_lst) const;
136 virtual bool match_same_type(const basic & other) const;
140 virtual ex subs(const exmap & m, unsigned options = 0) const;
143 virtual ex map(map_function & f) const;
145 // visitors and tree traversal
146 virtual void accept(GiNaC::visitor & v) const
148 if (visitor *p = dynamic_cast<visitor *>(&v))
153 virtual int degree(const ex & s) const;
154 virtual int ldegree(const ex & s) const;
155 virtual ex coeff(const ex & s, int n = 1) const;
158 virtual ex expand(unsigned options = 0) const;
159 virtual ex collect(const ex & s, bool distributed = false) const;
161 // differentiation and series expansion
163 virtual ex derivative(const symbol & s) const;
165 virtual ex series(const relational & r, int order, unsigned options = 0) const;
167 // rational functions
168 virtual ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
169 virtual ex to_rational(lst &repl_lst) const;
170 virtual ex to_polynomial(lst &repl_lst) const;
172 // polynomial algorithms
173 virtual numeric integer_content() const;
174 virtual ex smod(const numeric &xi) const;
175 virtual numeric max_coefficient() const;
178 virtual exvector get_free_indices() const;
179 virtual ex add_indexed(const ex & self, const ex & other) const;
180 virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
181 virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
184 virtual unsigned return_type() const;
185 virtual unsigned return_type_tinfo() const;
187 protected: // functions that should be called from class ex only
188 virtual int compare_same_type(const basic & other) const;
189 virtual bool is_equal_same_type(const basic & other) const;
191 virtual unsigned calchash() const;
193 // non-virtual functions in this class
195 ex subs_one_level(const exmap & m, unsigned options) const;
196 ex diff(const symbol & s, unsigned nth = 1) const;
197 int compare(const basic & other) const;
198 bool is_equal(const basic & other) const;
199 const basic & hold() const;
200 unsigned gethash() const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
201 unsigned tinfo() const {return tinfo_key;}
203 /** Set some status_flags. */
204 const basic & setflag(unsigned f) const {flags |= f; return *this;}
206 /** Clear some status_flags. */
207 const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
210 void ensure_if_modifiable() const;
213 void do_print(const print_context & c, unsigned level) const;
214 void do_print_tree(const print_tree & c, unsigned level) const;
215 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
219 unsigned tinfo_key; ///< typeinfo
220 mutable unsigned flags; ///< of type status_flags
221 mutable unsigned hashvalue; ///< hash value
223 size_t refcount; ///< reference counter, managed by ptr<basic>
229 extern int max_recursion_level;
232 // convenience type checker template functions
234 /** Check if obj is a T, including base classes. */
236 inline bool is_a(const basic &obj)
238 return dynamic_cast<const T *>(&obj) != 0;
241 /** Check if obj is a T, not including base classes. This one is just an
242 * inefficient default. It should in all time-critical cases be overridden
243 * by template specializations that use the TINFO_* constants directly. */
245 inline bool is_exactly_a(const basic &obj)
247 return obj.tinfo() == T::reg_info.options.get_id();
252 #endif // ndef __GINAC_BASIC_H__