3 * Interface to GiNaC's ABC. */
6 * GiNaC Copyright (C) 1999-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef __GINAC_BASIC_H__
24 #define __GINAC_BASIC_H__
26 #include <cstddef> // for size_t
30 #include <typeinfo> // for typeid
31 // CINT needs <algorithm> to work properly with <vector>
36 #include "assertion.h"
37 #include "registrar.h"
49 typedef std::vector<ex> exvector;
50 typedef std::set<ex, ex_is_less> exset;
51 typedef std::map<ex, ex, ex_is_less> exmap;
53 // Define this to enable some statistical output for comparisons and hashing
54 #undef GINAC_COMPARE_STATISTICS
56 #ifdef GINAC_COMPARE_STATISTICS
57 class compare_statistics_t {
59 compare_statistics_t()
60 : total_compares(0), nontrivial_compares(0), total_basic_compares(0), compare_same_hashvalue(0), compare_same_type(0),
61 total_is_equals(0), nontrivial_is_equals(0), total_basic_is_equals(0), is_equal_same_hashvalue(0), is_equal_same_type(0),
62 total_gethash(0), gethash_cached(0) {}
63 ~compare_statistics_t();
65 unsigned long total_compares;
66 unsigned long nontrivial_compares;
67 unsigned long total_basic_compares;
68 unsigned long compare_same_hashvalue;
69 unsigned long compare_same_type;
71 unsigned long total_is_equals;
72 unsigned long nontrivial_is_equals;
73 unsigned long total_basic_is_equals;
74 unsigned long is_equal_same_hashvalue;
75 unsigned long is_equal_same_type;
77 unsigned long total_gethash;
78 unsigned long gethash_cached;
81 extern compare_statistics_t compare_statistics;
85 /** Function object for map(). */
87 virtual ~map_function() {}
88 typedef const ex & argument_type;
89 typedef ex result_type;
90 virtual ex operator()(const ex & e) = 0;
94 /** Degenerate base class for visitors. basic and derivative classes
95 * support Robert C. Martin's Acyclic Visitor pattern (cf.
96 * http://objectmentor.com/publications/acv.pdf). */
103 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy. */
104 class basic : public refcounted
106 GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
110 // default constructor, destructor, copy constructor and assignment operator
112 basic() : tinfo_key(&tinfo_static), flags(0) {}
115 /** basic destructor, virtual because class ex will delete objects of
116 * derived classes via a basic*. */
119 GINAC_ASSERT((!(flags & status_flags::dynallocated)) || (get_refcount() == 0));
121 basic(const basic & other);
122 const basic & operator=(const basic & other);
125 /** Constructor with specified tinfo_key (used by derived classes instead
126 * of the default constructor to avoid assigning tinfo_key twice). */
127 basic(tinfo_t ti) : tinfo_key(ti), flags(0) {}
129 // new virtual functions which can be overridden by derived classes
130 public: // only const functions please (may break reference counting)
132 /** Create a clone of this object on the heap. One can think of this as
133 * simulating a virtual copy constructor which is needed for instance by
134 * the refcounted construction of an ex from a basic. */
135 virtual basic * duplicate() const { return new basic(*this); }
138 virtual ex eval(int level = 0) const;
139 virtual ex evalf(int level = 0) const;
140 virtual ex evalm() const;
141 virtual ex eval_integ() const;
143 virtual ex eval_ncmul(const exvector & v) const;
145 virtual ex eval_indexed(const basic & i) const;
148 virtual void print(const print_context & c, unsigned level = 0) const;
149 virtual void dbgprint() const;
150 virtual void dbgprinttree() const;
151 virtual unsigned precedence() const;
154 virtual bool info(unsigned inf) const;
157 virtual size_t nops() const;
158 virtual ex op(size_t i) const;
159 virtual ex operator[](const ex & index) const;
160 virtual ex operator[](size_t i) const;
161 virtual ex & let_op(size_t i);
162 virtual ex & operator[](const ex & index);
163 virtual ex & operator[](size_t i);
166 virtual bool has(const ex & other, unsigned options = 0) const;
167 virtual bool match(const ex & pattern, exmap & repls) const;
169 virtual bool match_same_type(const basic & other) const;
173 virtual ex subs(const exmap & m, unsigned options = 0) const;
176 virtual ex map(map_function & f) const;
178 // visitors and tree traversal
179 virtual void accept(GiNaC::visitor & v) const
181 if (visitor *p = dynamic_cast<visitor *>(&v))
186 virtual bool is_polynomial(const ex & var) const;
187 virtual int degree(const ex & s) const;
188 virtual int ldegree(const ex & s) const;
189 virtual ex coeff(const ex & s, int n = 1) const;
192 virtual ex expand(unsigned options = 0) const;
193 virtual ex collect(const ex & s, bool distributed = false) const;
195 // differentiation and series expansion
197 virtual ex derivative(const symbol & s) const;
199 virtual ex series(const relational & r, int order, unsigned options = 0) const;
201 // rational functions
202 virtual ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
203 virtual ex to_rational(exmap & repl) const;
204 virtual ex to_polynomial(exmap & repl) const;
206 // polynomial algorithms
207 virtual numeric integer_content() const;
208 virtual ex smod(const numeric &xi) const;
209 virtual numeric max_coefficient() const;
212 virtual exvector get_free_indices() const;
213 virtual ex add_indexed(const ex & self, const ex & other) const;
214 virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
215 virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
218 virtual unsigned return_type() const;
219 virtual tinfo_t return_type_tinfo() const;
221 // functions for complex expressions
222 virtual ex conjugate() const;
223 virtual ex real_part() const;
224 virtual ex imag_part() const;
226 // functions that should be called from class ex only
228 virtual int compare_same_type(const basic & other) const;
229 virtual bool is_equal_same_type(const basic & other) const;
231 virtual unsigned calchash() const;
233 // non-virtual functions in this class
235 /** Like print(), but dispatch to the specified class. Can be used by
236 * implementations of print methods to dispatch to the method of the
239 * @see basic::print */
241 void print_dispatch(const print_context & c, unsigned level) const
243 print_dispatch(T::get_class_info_static(), c, level);
246 void print_dispatch(const registered_class_info & ri, const print_context & c, unsigned level) const;
248 ex subs_one_level(const exmap & m, unsigned options) const;
249 ex diff(const symbol & s, unsigned nth = 1) const;
250 int compare(const basic & other) const;
251 bool is_equal(const basic & other) const;
252 const basic & hold() const;
254 unsigned gethash() const
256 #ifdef GINAC_COMPARE_STATISTICS
257 compare_statistics.total_gethash++;
259 if (flags & status_flags::hash_calculated) {
260 #ifdef GINAC_COMPARE_STATISTICS
261 compare_statistics.gethash_cached++;
269 tinfo_t tinfo() const {return tinfo_key;}
271 /** Set some status_flags. */
272 const basic & setflag(unsigned f) const {flags |= f; return *this;}
274 /** Clear some status_flags. */
275 const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
278 void ensure_if_modifiable() const;
280 void do_print(const print_context & c, unsigned level) const;
281 void do_print_tree(const print_tree & c, unsigned level) const;
282 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
286 tinfo_t tinfo_key; ///< type info
287 mutable unsigned flags; ///< of type status_flags
288 mutable unsigned hashvalue; ///< hash value
294 extern int max_recursion_level;
297 // convenience type checker template functions
299 /** Check if obj is a T, including base classes. */
301 inline bool is_a(const basic &obj)
303 return dynamic_cast<const T *>(&obj) != 0;
306 /** Check if obj is a T, not including base classes. */
308 inline bool is_exactly_a(const basic & obj)
310 return typeid(T) == typeid(obj);
315 #endif // ndef __GINAC_BASIC_H__