]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
added 'structure' to class list
[ginac.git] / ginac / basic.h
1 /** @file basic.h
2  *
3  *  Interface to GiNaC's ABC. */
4
5 /*
6  *  GiNaC Copyright (C) 1999-2003 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 <cstddef> // for size_t
27 #include <vector>
28 // CINT needs <algorithm> to work properly with <vector>
29 #include <algorithm>
30
31 #include "flags.h"
32 #include "tinfos.h"
33 #include "assertion.h"
34 #include "registrar.h"
35
36 namespace GiNaC {
37
38 class ex;
39 class ex_is_less;
40 class symbol;
41 class numeric;
42 class relational;
43 class archive_node;
44 class print_context;
45 template <class> class ptr;
46
47 typedef std::vector<ex> exvector;
48
49
50 /** Function object for map(). */
51 struct map_function {
52         typedef const ex & argument_type;
53         typedef ex result_type;
54         virtual ex operator()(const ex & e) = 0;
55 };
56
57
58 /** Degenerate base class for visitors. basic and derivative classes
59  *  support Robert C. Martin's Acyclic Visitor pattern (cf.
60  *  http://objectmentor.com/publications/acv.pdf). */
61 class visitor {
62 protected:
63         virtual ~visitor() {}
64 };
65
66
67 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
68  *  It is responsible for the reference counting. */
69 class basic
70 {
71         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
72         
73         friend class ex;
74         friend class ptr<basic>;
75         
76         // default constructor, destructor, copy constructor and assignment operator
77 protected:
78         basic() : tinfo_key(TINFO_basic), flags(0), refcount(0) {}
79
80 public:
81         /** basic destructor, virtual because class ex will delete objects of
82          *  derived classes via a basic*. */
83         virtual ~basic()
84         {
85                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
86         }
87         basic(const basic & other);
88         const basic & operator=(const basic & other);
89
90 protected:
91         /** Constructor with specified tinfo_key (used by derived classes instead
92          *  of the default constructor to avoid assigning tinfo_key twice). */
93         basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0) {}
94         
95         // new virtual functions which can be overridden by derived classes
96 public: // only const functions please (may break reference counting)
97
98         /** Create a clone of this object on the heap.  One can think of this as
99          *  simulating a virtual copy constructor which is needed for instance by
100          *  the refcounted construction of an ex from a basic. */
101         virtual basic * duplicate() const { return new basic(*this); }
102
103         // evaluation
104         virtual ex eval(int level = 0) const;
105         virtual ex evalf(int level = 0) const;
106         virtual ex evalm() const;
107 protected:
108         virtual ex eval_ncmul(const exvector & v) const;
109 public:
110         virtual ex eval_indexed(const basic & i) const;
111
112         // printing
113         virtual void print(const print_context & c, unsigned level = 0) const;
114         virtual void dbgprint() const;
115         virtual void dbgprinttree() const;
116         virtual unsigned precedence() const;
117
118         // info
119         virtual bool info(unsigned inf) const;
120
121         // operand access
122         virtual size_t nops() const;
123         virtual ex op(size_t i) const;
124         virtual ex operator[](const ex & index) const;
125         virtual ex operator[](size_t i) const;
126         virtual ex & let_op(size_t i);
127         virtual ex & operator[](const ex & index);
128         virtual ex & operator[](size_t i);
129
130         // pattern matching
131         virtual bool has(const ex & other) const;
132         virtual bool match(const ex & pattern, lst & repl_lst) const;
133 protected:
134         virtual bool match_same_type(const basic & other) const;
135 public:
136
137         // substitutions
138         virtual ex subs(const lst & ls, const lst & lr, unsigned options = 0) const;
139
140         // function mapping
141         virtual ex map(map_function & f) const;
142
143         // visitors and tree traversal
144         virtual void accept(GiNaC::visitor & v) const
145         {
146                 if (visitor *p = dynamic_cast<visitor *>(&v))
147                         p->visit(*this);
148         }
149
150         // degree/coeff
151         virtual int degree(const ex & s) const;
152         virtual int ldegree(const ex & s) const;
153         virtual ex coeff(const ex & s, int n = 1) const;
154
155         // expand/collect
156         virtual ex expand(unsigned options = 0) const;
157         virtual ex collect(const ex & s, bool distributed = false) const;
158
159         // differentiation and series expansion
160 protected:
161         virtual ex derivative(const symbol & s) const;
162 public:
163         virtual ex series(const relational & r, int order, unsigned options = 0) const;
164
165         // rational functions
166         virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
167         virtual ex to_rational(lst &repl_lst) const;
168         virtual ex to_polynomial(lst &repl_lst) const;
169
170         // polynomial algorithms
171         virtual numeric integer_content() const;
172         virtual ex smod(const numeric &xi) const;
173         virtual numeric max_coefficient() const;
174
175         // indexed objects
176         virtual exvector get_free_indices() const;
177         virtual ex add_indexed(const ex & self, const ex & other) const;
178         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
179         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
180
181         // noncommutativity
182         virtual unsigned return_type() const;
183         virtual unsigned return_type_tinfo() const;
184
185 protected: // functions that should be called from class ex only
186         virtual int compare_same_type(const basic & other) const;
187         virtual bool is_equal_same_type(const basic & other) const;
188
189         virtual unsigned calchash() const;
190         
191         // non-virtual functions in this class
192 public:
193         ex subs(const ex & e, unsigned options = 0) const;
194         ex subs_one_level(const lst & ls, const lst & lr, unsigned options) const;
195         ex diff(const symbol & s, unsigned nth = 1) const;
196         int compare(const basic & other) const;
197         bool is_equal(const basic & other) const;
198         const basic & hold() const;
199         unsigned gethash() const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
200         unsigned tinfo() const {return tinfo_key;}
201
202         /** Set some status_flags. */
203         const basic & setflag(unsigned f) const {flags |= f; return *this;}
204
205         /** Clear some status_flags. */
206         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
207
208 protected:
209         void ensure_if_modifiable() const;
210         
211         // member variables
212 protected:
213         unsigned tinfo_key;                 ///< typeinfo
214         mutable unsigned flags;             ///< of type status_flags
215         mutable unsigned hashvalue;         ///< hash value
216 private:
217         size_t refcount;                    ///< reference counter, managed by ptr<basic>
218 };
219
220
221 // global variables
222
223 extern int max_recursion_level;
224
225
226 // convenience type checker template functions
227
228 /** Check if obj is a T, including base classes. */
229 template <class T>
230 inline bool is_a(const basic &obj)
231 {
232         return dynamic_cast<const T *>(&obj) != 0;
233 }
234
235 /** Check if obj is a T, not including base classes.  This one is just an
236  *  inefficient default.  It should in all time-critical cases be overridden
237  *  by template specializations that use the TINFO_* constants directly. */
238 template <class T>
239 inline bool is_exactly_a(const class basic &obj)
240 {
241         return obj.tinfo() == T::reg_info.tinfo_key;
242 }
243
244 /** Check if ex is a handle to a T, including base classes. */
245 template <class T>
246 inline bool is_a(const ex &obj)
247 {
248         return is_a<T>(*obj.bp);
249 }
250
251 /** Check if ex is a handle to a T, not including base classes. */
252 template <class T>
253 inline bool is_exactly_a(const ex &obj)
254 {
255         return is_exactly_a<T>(*obj.bp);
256 }
257
258 /** Return a reference to the basic-derived class T object embedded in an
259  *  expression.  This is fast but unsafe: the result is undefined if the
260  *  expression does not contain a T object at its top level.  Hence, you
261  *  should generally check the type of e first.
262  *
263  *  @param e expression
264  *  @return reference to pseries object
265  *  @see is_exactly_a<class T>() */
266 template <class T>
267 inline const T &ex_to(const ex &e)
268 {
269         GINAC_ASSERT(is_a<T>(e));
270         return static_cast<const T &>(*e.bp);
271 }
272
273 } // namespace GiNaC
274
275 #endif // ndef __GINAC_BASIC_H__