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