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