]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
Added callback mechanism to watch changes to 'Digits' (to be used for look-up tables)
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2005 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_BASIC_H__
24 #define __GINAC_BASIC_H__
25
26 #include <cstddef> // for size_t
27 #include <vector>
28 #include <map>
29 // CINT needs <algorithm> to work properly with <vector>
30 #include <algorithm>
31
32 #include "flags.h"
33 #include "tinfos.h"
34 #include "ptr.h"
35 #include "assertion.h"
36 #include "registrar.h"
37
38 namespace GiNaC {
39
40 class ex;
41 class ex_is_less;
42 class symbol;
43 class numeric;
44 class relational;
45 class archive_node;
46 class print_context;
47
48 typedef std::vector<ex> exvector;
49 typedef std::map<ex, ex, ex_is_less> exmap;
50
51
52 // Define this to enable some statistical output for comparisons and hashing
53 #undef GINAC_COMPARE_STATISTICS
54
55 #ifdef GINAC_COMPARE_STATISTICS
56 class compare_statistics_t {
57 public:
58         compare_statistics_t()
59          : total_compares(0), nontrivial_compares(0), total_basic_compares(0), compare_same_hashvalue(0), compare_same_type(0),
60            total_is_equals(0), nontrivial_is_equals(0), total_basic_is_equals(0), is_equal_same_hashvalue(0), is_equal_same_type(0),
61            total_gethash(0), gethash_cached(0) {}
62         ~compare_statistics_t();
63
64         unsigned long total_compares;
65         unsigned long nontrivial_compares;
66         unsigned long total_basic_compares;
67         unsigned long compare_same_hashvalue;
68         unsigned long compare_same_type;
69
70         unsigned long total_is_equals;
71         unsigned long nontrivial_is_equals;
72         unsigned long total_basic_is_equals;
73         unsigned long is_equal_same_hashvalue;
74         unsigned long is_equal_same_type;
75
76         unsigned long total_gethash;
77         unsigned long gethash_cached;
78 };
79
80 extern compare_statistics_t compare_statistics;
81 #endif
82
83
84 /** Function object for map(). */
85 struct map_function {
86         virtual ~map_function() {}
87         typedef const ex & argument_type;
88         typedef ex result_type;
89         virtual ex operator()(const ex & e) = 0;
90 };
91
92
93 /** Degenerate base class for visitors. basic and derivative classes
94  *  support Robert C. Martin's Acyclic Visitor pattern (cf.
95  *  http://objectmentor.com/publications/acv.pdf). */
96 class visitor {
97 protected:
98         virtual ~visitor() {}
99 };
100
101
102 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy. */
103 class basic : public refcounted
104 {
105         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
106         
107         friend class ex;
108         
109         // default constructor, destructor, copy constructor and assignment operator
110 protected:
111         basic() : tinfo_key(TINFO_basic), flags(0) {}
112
113 public:
114         /** basic destructor, virtual because class ex will delete objects of
115          *  derived classes via a basic*. */
116         virtual ~basic()
117         {
118                 GINAC_ASSERT((!(flags & status_flags::dynallocated)) || (get_refcount() == 0));
119         }
120         basic(const basic & other);
121         const basic & operator=(const basic & other);
122
123 protected:
124         /** Constructor with specified tinfo_key (used by derived classes instead
125          *  of the default constructor to avoid assigning tinfo_key twice). */
126         basic(unsigned ti) : tinfo_key(ti), flags(0) {}
127         
128         // new virtual functions which can be overridden by derived classes
129 public: // only const functions please (may break reference counting)
130
131         /** Create a clone of this object on the heap.  One can think of this as
132          *  simulating a virtual copy constructor which is needed for instance by
133          *  the refcounted construction of an ex from a basic. */
134         virtual basic * duplicate() const { return new basic(*this); }
135
136         // evaluation
137         virtual ex eval(int level = 0) const;
138         virtual ex evalf(int level = 0) const;
139         virtual ex evalm() const;
140         virtual ex eval_integ() const;
141 protected:
142         virtual ex eval_ncmul(const exvector & v) const;
143 public:
144         virtual ex eval_indexed(const basic & i) const;
145
146         // printing
147         virtual void print(const print_context & c, unsigned level = 0) const;
148         virtual void dbgprint() const;
149         virtual void dbgprinttree() const;
150         virtual unsigned precedence() const;
151
152         // info
153         virtual bool info(unsigned inf) const;
154
155         // operand access
156         virtual size_t nops() const;
157         virtual ex op(size_t i) const;
158         virtual ex operator[](const ex & index) const;
159         virtual ex operator[](size_t i) const;
160         virtual ex & let_op(size_t i);
161         virtual ex & operator[](const ex & index);
162         virtual ex & operator[](size_t i);
163
164         // pattern matching
165         virtual bool has(const ex & other) const;
166         virtual bool match(const ex & pattern, lst & repl_lst) const;
167 protected:
168         virtual bool match_same_type(const basic & other) const;
169 public:
170
171         // substitutions
172         virtual ex subs(const exmap & m, unsigned options = 0) const;
173
174         // function mapping
175         virtual ex map(map_function & f) const;
176
177         // visitors and tree traversal
178         virtual void accept(GiNaC::visitor & v) const
179         {
180                 if (visitor *p = dynamic_cast<visitor *>(&v))
181                         p->visit(*this);
182         }
183
184         // degree/coeff
185         virtual int degree(const ex & s) const;
186         virtual int ldegree(const ex & s) const;
187         virtual ex coeff(const ex & s, int n = 1) const;
188
189         // expand/collect
190         virtual ex expand(unsigned options = 0) const;
191         virtual ex collect(const ex & s, bool distributed = false) const;
192
193         // differentiation and series expansion
194 protected:
195         virtual ex derivative(const symbol & s) const;
196 public:
197         virtual ex series(const relational & r, int order, unsigned options = 0) const;
198
199         // rational functions
200         virtual ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
201         virtual ex to_rational(exmap & repl) const;
202         virtual ex to_polynomial(exmap & repl) const;
203
204         // polynomial algorithms
205         virtual numeric integer_content() const;
206         virtual ex smod(const numeric &xi) const;
207         virtual numeric max_coefficient() const;
208
209         // indexed objects
210         virtual exvector get_free_indices() const;
211         virtual ex add_indexed(const ex & self, const ex & other) const;
212         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
213         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
214
215         // noncommutativity
216         virtual unsigned return_type() const;
217         virtual unsigned return_type_tinfo() const;
218
219         // complex conjugation
220         virtual ex conjugate() const;
221
222         // functions that should be called from class ex only
223 protected:
224         virtual int compare_same_type(const basic & other) const;
225         virtual bool is_equal_same_type(const basic & other) const;
226
227         virtual unsigned calchash() const;
228         
229         // non-virtual functions in this class
230 public:
231         /** Like print(), but dispatch to the specified class. Can be used by
232          *  implementations of print methods to dispatch to the method of the
233          *  superclass.
234          *
235          *  @see basic::print */
236         template <class T>
237         void print_dispatch(const print_context & c, unsigned level) const
238         {
239                 print_dispatch(T::get_class_info_static(), c, level);
240         }
241
242         void print_dispatch(const registered_class_info & ri, const print_context & c, unsigned level) const;
243
244         ex subs_one_level(const exmap & m, unsigned options) const;
245         ex diff(const symbol & s, unsigned nth = 1) const;
246         int compare(const basic & other) const;
247         bool is_equal(const basic & other) const;
248         const basic & hold() const;
249
250         unsigned gethash() const
251         {
252 #ifdef GINAC_COMPARE_STATISTICS
253                 compare_statistics.total_gethash++;
254 #endif
255                 if (flags & status_flags::hash_calculated) {
256 #ifdef GINAC_COMPARE_STATISTICS
257                         compare_statistics.gethash_cached++;
258 #endif
259                         return hashvalue;
260                 } else {
261                         return calchash();
262                 }
263         }
264
265         unsigned tinfo() const {return tinfo_key;}
266
267         /** Set some status_flags. */
268         const basic & setflag(unsigned f) const {flags |= f; return *this;}
269
270         /** Clear some status_flags. */
271         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
272
273 protected:
274         void ensure_if_modifiable() const;
275
276         void do_print(const print_context & c, unsigned level) const;
277         void do_print_tree(const print_tree & c, unsigned level) const;
278         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
279         
280         // member variables
281 protected:
282         unsigned tinfo_key;                 ///< typeinfo
283         mutable unsigned flags;             ///< of type status_flags
284         mutable unsigned hashvalue;         ///< hash value
285 };
286
287
288 // global variables
289
290 extern int max_recursion_level;
291
292
293 // convenience type checker template functions
294
295 /** Check if obj is a T, including base classes. */
296 template <class T>
297 inline bool is_a(const basic &obj)
298 {
299         return dynamic_cast<const T *>(&obj) != 0;
300 }
301
302 /** Check if obj is a T, not including base classes.  This one is just an
303  *  inefficient default.  It should in all time-critical cases be overridden
304  *  by template specializations that use the TINFO_* constants directly. */
305 template <class T>
306 inline bool is_exactly_a(const basic &obj)
307 {
308         return obj.tinfo() == T::get_class_info_static().options.get_id();
309 }
310
311 } // namespace GiNaC
312
313 #endif // ndef __GINAC_BASIC_H__