]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
Rename array of precomputed data in test suite.
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2015 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 #include <cstddef> // for size_t
32 #include <map>
33 #include <set>
34 #include <typeinfo> // for typeid
35 #include <vector>
36
37 namespace GiNaC {
38
39 class ex;
40 struct 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::set<ex, ex_is_less> exset;
49 typedef std::map<ex, ex, ex_is_less> exmap;
50
51 // Define this to enable some statistical output for comparisons and hashing
52 #undef GINAC_COMPARE_STATISTICS
53
54 #ifdef GINAC_COMPARE_STATISTICS
55 class compare_statistics_t {
56 public:
57         compare_statistics_t()
58          : total_compares(0), nontrivial_compares(0), total_basic_compares(0), compare_same_hashvalue(0), compare_same_type(0),
59            total_is_equals(0), nontrivial_is_equals(0), total_basic_is_equals(0), is_equal_same_hashvalue(0), is_equal_same_type(0),
60            total_gethash(0), gethash_cached(0) {}
61         ~compare_statistics_t();
62
63         unsigned long total_compares;
64         unsigned long nontrivial_compares;
65         unsigned long total_basic_compares;
66         unsigned long compare_same_hashvalue;
67         unsigned long compare_same_type;
68
69         unsigned long total_is_equals;
70         unsigned long nontrivial_is_equals;
71         unsigned long total_basic_is_equals;
72         unsigned long is_equal_same_hashvalue;
73         unsigned long is_equal_same_type;
74
75         unsigned long total_gethash;
76         unsigned long gethash_cached;
77 };
78
79 extern compare_statistics_t compare_statistics;
80 #endif
81
82
83 /** Function object for map(). */
84 struct map_function {
85         virtual ~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() : 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         // new virtual functions which can be overridden by derived classes
124 public: // only const functions please (may break reference counting)
125
126         /** Create a clone of this object on the heap.  One can think of this as
127          *  simulating a virtual copy constructor which is needed for instance by
128          *  the refcounted construction of an ex from a basic. */
129         virtual basic * duplicate() const { return new basic(*this); }
130
131         // evaluation
132         virtual ex eval(int level = 0) const;
133         virtual ex evalf(int level = 0) const;
134         virtual ex evalm() const;
135         virtual ex eval_integ() const;
136 protected:
137         virtual ex eval_ncmul(const exvector & v) const;
138 public:
139         virtual ex eval_indexed(const basic & i) const;
140
141         // printing
142         virtual void print(const print_context & c, unsigned level = 0) const;
143         virtual void dbgprint() const;
144         virtual void dbgprinttree() const;
145         virtual unsigned precedence() const;
146
147         // info
148         virtual bool info(unsigned inf) const;
149
150         // operand access
151         virtual size_t nops() const;
152         virtual ex op(size_t i) const;
153         virtual ex operator[](const ex & index) const;
154         virtual ex operator[](size_t i) const;
155         virtual ex & let_op(size_t i);
156         virtual ex & operator[](const ex & index);
157         virtual ex & operator[](size_t i);
158
159         // pattern matching
160         virtual bool has(const ex & other, unsigned options = 0) const;
161         virtual bool match(const ex & pattern, exmap & repls) const;
162 protected:
163         virtual bool match_same_type(const basic & other) const;
164 public:
165
166         // substitutions
167         virtual ex subs(const exmap & m, unsigned options = 0) const;
168
169         // function mapping
170         virtual ex map(map_function & f) const;
171
172         // visitors and tree traversal
173         virtual void accept(GiNaC::visitor & v) const
174         {
175                 if (visitor *p = dynamic_cast<visitor *>(&v))
176                         p->visit(*this);
177         }
178
179         // degree/coeff
180         virtual bool is_polynomial(const ex & var) const;
181         virtual int degree(const ex & s) const;
182         virtual int ldegree(const ex & s) const;
183         virtual ex coeff(const ex & s, int n = 1) const;
184
185         // expand/collect
186         virtual ex expand(unsigned options = 0) const;
187         virtual ex collect(const ex & s, bool distributed = false) const;
188
189         // differentiation and series expansion
190 protected:
191         virtual ex derivative(const symbol & s) const;
192 public:
193         virtual ex series(const relational & r, int order, unsigned options = 0) const;
194
195         // rational functions
196         virtual ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
197         virtual ex to_rational(exmap & repl) const;
198         virtual ex to_polynomial(exmap & repl) const;
199
200         // polynomial algorithms
201         virtual numeric integer_content() const;
202         virtual ex smod(const numeric &xi) const;
203         virtual numeric max_coefficient() const;
204
205         // indexed objects
206         virtual exvector get_free_indices() const;
207         virtual ex add_indexed(const ex & self, const ex & other) const;
208         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
209         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
210
211         // noncommutativity
212         virtual unsigned return_type() const;
213         virtual return_type_t return_type_tinfo() const;
214
215         // functions for complex expressions
216         virtual ex conjugate() const;
217         virtual ex real_part() const;
218         virtual ex imag_part() const;
219
220         // functions that should be called from class ex only
221 protected:
222         virtual int compare_same_type(const basic & other) const;
223         virtual bool is_equal_same_type(const basic & other) const;
224
225         virtual unsigned calchash() const;
226         
227         // non-virtual functions in this class
228 public:
229         /** Like print(), but dispatch to the specified class. Can be used by
230          *  implementations of print methods to dispatch to the method of the
231          *  superclass.
232          *
233          *  @see basic::print */
234         template <class T>
235         void print_dispatch(const print_context & c, unsigned level) const
236         {
237                 print_dispatch(T::get_class_info_static(), c, level);
238         }
239
240         void print_dispatch(const registered_class_info & ri, const print_context & c, unsigned level) const;
241
242         /** Save (serialize) the object into archive node.
243          *
244          * Losely speaking, this method turns an expression into a byte
245          * stream (which can be saved and restored later on, or sent via
246          * network, etc.)
247          */
248         virtual void archive(archive_node& n) const;
249         /** Load (deserialize) the object from an archive node.
250          *
251          *  @note This method is essentially a constructor. However,
252          *  constructors can't be virtual. So, if unarchiving routines
253          *  are implemented as constructors one would need to define such
254          *  a constructor in every class, even if all it does is simply
255          *  calling constructor of a superclass.
256          */
257         virtual void read_archive(const archive_node& n, lst& syms); // no const
258
259         ex subs_one_level(const exmap & m, unsigned options) const;
260         ex diff(const symbol & s, unsigned nth = 1) const;
261         int compare(const basic & other) const;
262         bool is_equal(const basic & other) const;
263         const basic & hold() const;
264
265         unsigned gethash() const
266         {
267 #ifdef GINAC_COMPARE_STATISTICS
268                 compare_statistics.total_gethash++;
269 #endif
270                 if (flags & status_flags::hash_calculated) {
271 #ifdef GINAC_COMPARE_STATISTICS
272                         compare_statistics.gethash_cached++;
273 #endif
274                         return hashvalue;
275                 } else {
276                         return calchash();
277                 }
278         }
279
280         /** Set some status_flags. */
281         const basic & setflag(unsigned f) const {flags |= f; return *this;}
282
283         /** Clear some status_flags. */
284         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
285
286 protected:
287         void ensure_if_modifiable() const;
288
289         void do_print(const print_context & c, unsigned level) const;
290         void do_print_tree(const print_tree & c, unsigned level) const;
291         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
292         
293         // member variables
294 protected:
295         mutable unsigned flags;             ///< of type status_flags
296         mutable unsigned hashvalue;         ///< hash value
297 };
298
299
300 // global variables
301
302 extern int max_recursion_level;
303
304
305 // convenience type checker template functions
306
307 /** Check if obj is a T, including base classes. */
308 template <class T>
309 inline bool is_a(const basic &obj)
310 {
311         return dynamic_cast<const T *>(&obj) != nullptr;
312 }
313
314 /** Check if obj is a T, not including base classes. */
315 template <class T>
316 inline bool is_exactly_a(const basic & obj)
317 {
318         return typeid(T) == typeid(obj);
319 }
320
321 } // namespace GiNaC
322
323 #endif // ndef GINAC_BASIC_H