]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
Moved header includes to source files to avoid polluting the namespace
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2009 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 "flags.h"
27 #include "ptr.h"
28 #include "assertion.h"
29 #include "registrar.h"
30
31 // CINT needs <algorithm> to work properly with <vector>
32 #include <algorithm>
33 #include <cstddef> // for size_t
34 #include <map>
35 #include <set>
36 #include <typeinfo> // for typeid
37 #include <vector>
38
39 namespace GiNaC {
40
41 class ex;
42 class ex_is_less;
43 class symbol;
44 class numeric;
45 class relational;
46 class archive_node;
47 class print_context;
48
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;
52
53 // Define this to enable some statistical output for comparisons and hashing
54 #undef GINAC_COMPARE_STATISTICS
55
56 #ifdef GINAC_COMPARE_STATISTICS
57 class compare_statistics_t {
58 public:
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();
64
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;
70
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;
76
77         unsigned long total_gethash;
78         unsigned long gethash_cached;
79 };
80
81 extern compare_statistics_t compare_statistics;
82 #endif
83
84
85 /** Function object for map(). */
86 struct map_function {
87         virtual ~map_function() {}
88         typedef const ex & argument_type;
89         typedef ex result_type;
90         virtual ex operator()(const ex & e) = 0;
91 };
92
93
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). */
97 class visitor {
98 protected:
99         virtual ~visitor() {}
100 };
101
102
103 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy. */
104 class basic : public refcounted
105 {
106         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
107         
108         friend class ex;
109         
110         // default constructor, destructor, copy constructor and assignment operator
111 protected:
112         basic() : flags(0) {}
113
114 public:
115         /** basic destructor, virtual because class ex will delete objects of
116          *  derived classes via a basic*. */
117         virtual ~basic()
118         {
119                 GINAC_ASSERT((!(flags & status_flags::dynallocated)) || (get_refcount() == 0));
120         }
121         basic(const basic & other);
122         const basic & operator=(const basic & other);
123
124 protected:
125         // new virtual functions which can be overridden by derived classes
126 public: // only const functions please (may break reference counting)
127
128         /** Create a clone of this object on the heap.  One can think of this as
129          *  simulating a virtual copy constructor which is needed for instance by
130          *  the refcounted construction of an ex from a basic. */
131         virtual basic * duplicate() const { return new basic(*this); }
132
133         // evaluation
134         virtual ex eval(int level = 0) const;
135         virtual ex evalf(int level = 0) const;
136         virtual ex evalm() const;
137         virtual ex eval_integ() const;
138 protected:
139         virtual ex eval_ncmul(const exvector & v) const;
140 public:
141         virtual ex eval_indexed(const basic & i) const;
142
143         // printing
144         virtual void print(const print_context & c, unsigned level = 0) const;
145         virtual void dbgprint() const;
146         virtual void dbgprinttree() const;
147         virtual unsigned precedence() const;
148
149         // info
150         virtual bool info(unsigned inf) const;
151
152         // operand access
153         virtual size_t nops() const;
154         virtual ex op(size_t i) const;
155         virtual ex operator[](const ex & index) const;
156         virtual ex operator[](size_t i) const;
157         virtual ex & let_op(size_t i);
158         virtual ex & operator[](const ex & index);
159         virtual ex & operator[](size_t i);
160
161         // pattern matching
162         virtual bool has(const ex & other, unsigned options = 0) const;
163         virtual bool match(const ex & pattern, exmap & repls) const;
164 protected:
165         virtual bool match_same_type(const basic & other) const;
166 public:
167
168         // substitutions
169         virtual ex subs(const exmap & m, unsigned options = 0) const;
170
171         // function mapping
172         virtual ex map(map_function & f) const;
173
174         // visitors and tree traversal
175         virtual void accept(GiNaC::visitor & v) const
176         {
177                 if (visitor *p = dynamic_cast<visitor *>(&v))
178                         p->visit(*this);
179         }
180
181         // degree/coeff
182         virtual bool is_polynomial(const ex & var) const;
183         virtual int degree(const ex & s) const;
184         virtual int ldegree(const ex & s) const;
185         virtual ex coeff(const ex & s, int n = 1) const;
186
187         // expand/collect
188         virtual ex expand(unsigned options = 0) const;
189         virtual ex collect(const ex & s, bool distributed = false) const;
190
191         // differentiation and series expansion
192 protected:
193         virtual ex derivative(const symbol & s) const;
194 public:
195         virtual ex series(const relational & r, int order, unsigned options = 0) const;
196
197         // rational functions
198         virtual ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
199         virtual ex to_rational(exmap & repl) const;
200         virtual ex to_polynomial(exmap & repl) const;
201
202         // polynomial algorithms
203         virtual numeric integer_content() const;
204         virtual ex smod(const numeric &xi) const;
205         virtual numeric max_coefficient() const;
206
207         // indexed objects
208         virtual exvector get_free_indices() const;
209         virtual ex add_indexed(const ex & self, const ex & other) const;
210         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
211         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
212
213         // noncommutativity
214         virtual unsigned return_type() const;
215         virtual return_type_t return_type_tinfo() const;
216
217         // functions for complex expressions
218         virtual ex conjugate() const;
219         virtual ex real_part() const;
220         virtual ex imag_part() 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         /** Save (serialize) the object into archive node.
245          *
246          * Losely speaking, this method turns an expression into a byte
247          * stream (which can be saved and restored later on, or sent via
248          * network, etc.)
249          */
250         virtual void archive(archive_node& n) const;
251         /** Load (deserialize) the object from an archive node.
252          *
253          *  @note This method is essentially a constructor. However,
254          *  constructors can't be virtual. So, if unarchiving routines
255          *  are implemented as constructors one would need to define such
256          *  a constructor in every class, even if all it does is simply
257          *  calling constructor of a superclass.
258          */
259         virtual void read_archive(const archive_node& n, lst& syms); // no const
260
261         ex subs_one_level(const exmap & m, unsigned options) const;
262         ex diff(const symbol & s, unsigned nth = 1) const;
263         int compare(const basic & other) const;
264         bool is_equal(const basic & other) const;
265         const basic & hold() const;
266
267         unsigned gethash() const
268         {
269 #ifdef GINAC_COMPARE_STATISTICS
270                 compare_statistics.total_gethash++;
271 #endif
272                 if (flags & status_flags::hash_calculated) {
273 #ifdef GINAC_COMPARE_STATISTICS
274                         compare_statistics.gethash_cached++;
275 #endif
276                         return hashvalue;
277                 } else {
278                         return calchash();
279                 }
280         }
281
282         /** Set some status_flags. */
283         const basic & setflag(unsigned f) const {flags |= f; return *this;}
284
285         /** Clear some status_flags. */
286         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
287
288 protected:
289         void ensure_if_modifiable() const;
290
291         void do_print(const print_context & c, unsigned level) const;
292         void do_print_tree(const print_tree & c, unsigned level) const;
293         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
294         
295         // member variables
296 protected:
297         mutable unsigned flags;             ///< of type status_flags
298         mutable unsigned hashvalue;         ///< hash value
299 };
300
301
302 // global variables
303
304 extern int max_recursion_level;
305
306
307 // convenience type checker template functions
308
309 /** Check if obj is a T, including base classes. */
310 template <class T>
311 inline bool is_a(const basic &obj)
312 {
313         return dynamic_cast<const T *>(&obj) != 0;
314 }
315
316 /** Check if obj is a T, not including base classes. */
317 template <class T>
318 inline bool is_exactly_a(const basic & obj)
319 {
320         return typeid(T) == typeid(obj);
321 }
322
323 } // namespace GiNaC
324
325 #endif // ndef GINAC_BASIC_H