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