]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
- Lewis-Wester tests D and E now benchmark the time to expand the
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2001 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 #ifndef __GINAC_BASIC_H__
24 #define __GINAC_BASIC_H__
25
26 #include <iostream>
27 #include <typeinfo>
28 #include <vector>
29
30 // CINT needs <algorithm> to work properly with <vector>
31 #include <algorithm>
32
33 #include "flags.h"
34 #include "tinfos.h"
35 #include "assertion.h"
36 #include "registrar.h"
37
38 #ifndef NO_NAMESPACE_GINAC
39 namespace GiNaC {
40 #endif // ndef NO_NAMESPACE_GINAC
41
42 class basic;
43 class ex;
44 class symbol;
45 class lst;
46 class numeric;
47 class relational;
48 class archive_node;
49
50 // Cint doesn't like vector<..,default_alloc> but malloc_alloc is
51 // unstandardized and not supported by newer GCCs.
52 #if defined(__GNUC__) && ((__GNUC__ == 2) && (__GNUC_MINOR__ < 97))
53 typedef std::vector<ex,malloc_alloc> exvector;
54 #else
55 typedef std::vector<ex> exvector;
56 #endif
57
58 #define INLINE_BASIC_CONSTRUCTORS
59
60 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
61  *  It is responsible for the reference counting. */
62 class basic
63 {
64         GINAC_DECLARE_REGISTERED_CLASS(basic, void)
65
66         friend class ex;
67
68 // member functions
69
70         // default constructor, destructor, copy constructor assignment operator and helpers
71 public:
72         basic()
73 #ifdef INLINE_BASIC_CONSTRUCTORS
74                 : tinfo_key(TINFO_basic), flags(0), refcount(0)
75         {
76         }
77 #else
78 ;
79 #endif // def INLINE_BASIC_CONSTRUCTORS
80
81         virtual ~basic()
82 #ifdef INLINE_BASIC_CONSTRUCTORS
83         {
84                 destroy(0);
85                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
86         }
87 #else
88 ;
89 #endif // def INLINE_BASIC_CONSTRUCTORS
90
91         basic(const basic & other)
92 #ifdef INLINE_BASIC_CONSTRUCTORS
93         {
94                 copy(other);
95         }
96 #else
97 ;
98 #endif // def INLINE_BASIC_CONSTRUCTORS
99         
100         const basic & operator=(const basic & other);
101         
102 protected:
103         void copy(const basic & other)
104         {
105                 flags = other.flags & ~status_flags::dynallocated;
106                 hashvalue = other.hashvalue;
107                 tinfo_key = other.tinfo_key;
108         }
109         void destroy(bool call_parent) {}
110
111         // other constructors
112         basic(unsigned ti)
113 #ifdef INLINE_BASIC_CONSTRUCTORS
114                            : tinfo_key(ti), flags(0), refcount(0)
115         {
116         }
117 #else
118 ;
119 #endif // def INLINE_BASIC_CONSTRUCTORS
120
121         // functions overriding virtual functions from bases classes
122         // none
123         
124         // new virtual functions which can be overridden by derived classes
125 public: // only const functions please (may break reference counting)
126         virtual basic * duplicate() const;
127         virtual void print(std::ostream & os,unsigned upper_precedence = 0) const;
128         virtual void printraw(std::ostream & os) const;
129         virtual void printtree(std::ostream & os, unsigned indent) const;
130         virtual void printcsrc(std::ostream & os, unsigned type, unsigned upper_precedence = 0) const;
131         virtual void dbgprint(void) const;
132         virtual void dbgprinttree(void) const;
133         virtual bool info(unsigned inf) const;
134         virtual unsigned nops() const;
135         virtual ex op(int i) const;
136         virtual ex & let_op(int i);
137         virtual ex operator[](const ex & index) const;
138         virtual ex operator[](int i) const;
139         virtual bool has(const ex & other) const;
140         virtual int degree(const symbol & s) const;
141         virtual int ldegree(const symbol & s) const;
142         virtual ex coeff(const symbol & s, int n = 1) const;
143         virtual ex collect(const symbol & s) const;
144         virtual ex eval(int level = 0) const;
145         virtual ex evalf(int level = 0) const;
146         virtual ex series(const relational & r, int order, unsigned options = 0) const;
147         virtual ex subs(const lst & ls, const lst & lr) const;
148         virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
149         virtual ex to_rational(lst &repl_lst) const;
150         virtual numeric integer_content(void) const;
151         virtual ex smod(const numeric &xi) const;
152         virtual numeric max_coefficient(void) const;
153         virtual exvector get_indices(void) const;
154         virtual ex simplify_ncmul(const exvector & v) const;
155 protected: // non-const functions should be called from class ex only
156         virtual ex derivative(const symbol & s) const;
157         virtual int compare_same_type(const basic & other) const;
158         virtual bool is_equal_same_type(const basic & other) const;
159         virtual unsigned return_type(void) const;
160         virtual unsigned return_type_tinfo(void) const;
161         virtual unsigned calchash(void) const;
162         virtual ex expand(unsigned options=0) const;
163
164         // non-virtual functions in this class
165 public:
166         ex subs(const ex & e) const;
167         ex diff(const symbol & s, unsigned nth=1) const;
168         int compare(const basic & other) const;
169         bool is_equal(const basic & other) const;
170         const basic & hold(void) const;
171         unsigned gethash(void) const {if (flags & status_flags::hash_calculated) return hashvalue; else return calchash();}
172         unsigned tinfo(void) const {return tinfo_key;}
173         const basic & setflag(unsigned f) const {flags |= f; return *this;}
174         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
175 protected:
176         void ensure_if_modifiable(void) const;
177
178 // member variables
179         
180 protected:
181         unsigned tinfo_key;
182         mutable unsigned flags;
183         mutable unsigned hashvalue;
184         static unsigned precedence;
185         static unsigned delta_indent;
186 private:
187         unsigned refcount;
188 };
189
190 // global constants
191
192 extern const basic some_basic;
193 extern const std::type_info & typeid_basic;
194
195 // global variables
196
197 extern int max_recursion_level;
198
199 // convenience macros
200
201 #ifndef NO_NAMESPACE_GINAC
202
203 #define is_of_type(OBJ,TYPE) \
204         (dynamic_cast<TYPE *>(const_cast<GiNaC::basic *>(&OBJ))!=0)
205
206 #define is_exactly_of_type(OBJ,TYPE) \
207         ((OBJ).tinfo()==GiNaC::TINFO_##TYPE)
208
209 #define is_ex_of_type(OBJ,TYPE) \
210         (dynamic_cast<TYPE *>(const_cast<GiNaC::basic *>((OBJ).bp))!=0)
211
212 #define is_ex_exactly_of_type(OBJ,TYPE) \
213         ((*(OBJ).bp).tinfo()==GiNaC::TINFO_##TYPE)
214
215 #else // ndef NO_NAMESPACE_GINAC
216
217 #define is_of_type(OBJ,TYPE) \
218         (dynamic_cast<TYPE *>(const_cast<basic *>(&OBJ))!=0)
219
220 #define is_exactly_of_type(OBJ,TYPE) \
221         ((OBJ).tinfo()==TINFO_##TYPE)
222
223 #define is_ex_of_type(OBJ,TYPE) \
224         (dynamic_cast<TYPE *>(const_cast<basic *>((OBJ).bp))!=0)
225
226 #define is_ex_exactly_of_type(OBJ,TYPE) \
227         ((*(OBJ).bp).tinfo()==TINFO_##TYPE)
228
229 #endif // ndef NO_NAMESPACE_GINAC
230
231 #ifndef NO_NAMESPACE_GINAC
232 } // namespace GiNaC
233 #endif // ndef NO_NAMESPACE_GINAC
234
235 #endif // ndef __GINAC_BASIC_H__