]> www.ginac.de Git - ginac.git/blob - ginac/basic.h
* Bump up version, fix typos.
[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 <vector>
27 // CINT needs <algorithm> to work properly with <vector>
28 #include <algorithm>
29
30 #include "flags.h"
31 #include "tinfos.h"
32 #include "assertion.h"
33 #include "registrar.h"
34
35 namespace GiNaC {
36
37 class ex;
38 class symbol;
39 class lst;
40 class numeric;
41 class relational;
42 class archive_node;
43 class print_context;
44
45 typedef std::vector<ex> exvector;
46
47
48 /** Function object for map(). */
49 struct map_function {
50         typedef const ex & argument_type;
51         typedef ex result_type;
52         virtual ex operator()(const ex & e) = 0;
53 };
54
55
56 /** This class is the ABC (abstract base class) of GiNaC's class hierarchy.
57  *  It is responsible for the reference counting. */
58 class basic
59 {
60         GINAC_DECLARE_REGISTERED_CLASS_NO_CTORS(basic, void)
61         
62         friend class ex;
63         
64         // default ctor, dtor, copy ctor, assignment operator and helpers
65 public:
66         basic() : tinfo_key(TINFO_basic), flags(0), refcount(0) {}
67         /** basic dtor, virtual because class ex will delete objects via ptr. */
68         virtual ~basic()
69         {
70                 destroy(false);
71                 GINAC_ASSERT((!(flags & status_flags::dynallocated))||(refcount==0));
72         }
73         basic(const basic & other);
74         const basic & operator=(const basic & other);
75 protected:
76         /** For use by copy ctor and assignment operator. */
77         void copy(const basic & other)
78         {
79                 flags = other.flags & ~status_flags::dynallocated;
80                 hashvalue = other.hashvalue;
81                 tinfo_key = other.tinfo_key;
82         }
83         /** For use by dtor and assignment operator. */
84         virtual void destroy(bool call_parent) { }
85         
86         // other ctors
87         /** ctor with specified tinfo_key */
88         basic(unsigned ti) : tinfo_key(ti), flags(0), refcount(0) {}
89         
90         // new virtual functions which can be overridden by derived classes
91 public: // only const functions please (may break reference counting)
92         virtual basic * duplicate() const;
93         virtual void print(const print_context & c, unsigned level = 0) const;
94         virtual void dbgprint(void) const;
95         virtual void dbgprinttree(void) const;
96         virtual unsigned precedence(void) const;
97         virtual bool info(unsigned inf) const;
98         virtual unsigned nops() const;
99         virtual ex op(int i) const;
100         virtual ex & let_op(int i);
101         virtual ex operator[](const ex & index) const;
102         virtual ex operator[](int i) const;
103         virtual ex expand(unsigned options = 0) const;
104         virtual bool has(const ex & other) const;
105         virtual ex map(map_function & f) const;
106         virtual int degree(const ex & s) const;
107         virtual int ldegree(const ex & s) const;
108         virtual ex coeff(const ex & s, int n = 1) const;
109         virtual ex collect(const ex & s, bool distributed = false) const;
110         virtual ex eval(int level = 0) const;
111         virtual ex evalf(int level = 0) const;
112         virtual ex evalm(void) const;
113         virtual ex series(const relational & r, int order, unsigned options = 0) const;
114         virtual bool match(const ex & pattern, lst & repl_lst) const;
115         virtual ex subs(const lst & ls, const lst & lr, bool no_pattern = false) const;
116         virtual ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
117         virtual ex to_rational(lst &repl_lst) const;
118         virtual numeric integer_content(void) const;
119         virtual ex smod(const numeric &xi) const;
120         virtual numeric max_coefficient(void) const;
121         virtual exvector get_free_indices(void) const;
122         virtual ex eval_indexed(const basic & i) const;
123         virtual ex add_indexed(const ex & self, const ex & other) const;
124         virtual ex scalar_mul_indexed(const ex & self, const numeric & other) const;
125         virtual bool contract_with(exvector::iterator self, exvector::iterator other, exvector & v) const;
126         virtual unsigned return_type(void) const;
127         virtual unsigned return_type_tinfo(void) const;
128 protected: // functions that should be called from class ex only
129         virtual ex derivative(const symbol & s) const;
130         virtual int compare_same_type(const basic & other) const;
131         virtual bool is_equal_same_type(const basic & other) const;
132         virtual bool match_same_type(const basic & other) const;
133         virtual unsigned calchash(void) const;
134         virtual ex simplify_ncmul(const exvector & v) const;
135         
136         // non-virtual functions in this class
137 public:
138         ex subs(const ex & e, bool no_pattern = false) const;
139         ex diff(const symbol & s, unsigned nth=1) const;
140         int compare(const basic & other) const;
141         bool is_equal(const basic & other) const;
142         const basic & hold(void) const;
143         unsigned gethash(void) const { if (flags & status_flags::hash_calculated) return hashvalue; else return calchash(); }
144         unsigned tinfo(void) const {return tinfo_key;}
145
146         /** Set some status_flags. */
147         const basic & setflag(unsigned f) const {flags |= f; return *this;}
148
149         /** Clear some status_flags. */
150         const basic & clearflag(unsigned f) const {flags &= ~f; return *this;}
151
152 protected:
153         void ensure_if_modifiable(void) const;
154         
155         // member variables
156 protected:
157         unsigned tinfo_key;                 ///< typeinfo
158         mutable unsigned flags;             ///< of type status_flags
159         mutable unsigned hashvalue;         ///< hash value
160 private:
161         unsigned refcount;                  ///< Number of reference counts
162 };
163
164 // global variables
165
166 extern int max_recursion_level;
167
168 // convenience type checker template functions
169
170 /** Check if obj is a T, including base classes. */
171 template <class T>
172 inline bool is_a(const basic &obj)
173 {
174         return dynamic_cast<const T *>(&obj)!=0;
175 }
176
177 /** Check if obj is a T, not including base classes.  This one is just an
178  *  inefficient default.  It should in all time-critical cases be overridden
179  *  by template specializations that don't create a temporary. */
180 template <class T>
181 inline bool is_exactly_a(const class basic &obj)
182 {
183         const T foo; return foo.tinfo()==obj.tinfo();
184 }
185
186 /** Check if ex is a handle to a T, including base classes. */
187 template <class T>
188 inline bool is_a(const ex &obj)
189 {
190         return is_a<T>(*obj.bp);
191 }
192
193 /** Check if ex is a handle to a T, not including base classes. */
194 template <class T>
195 inline bool is_exactly_a(const ex &obj)
196 {
197         return is_exactly_a<T>(*obj.bp);
198 }
199
200 /** Return a reference to the basic-derived class T object embedded in an
201  *  expression.  This is fast but unsafe: the result is undefined if the
202  *  expression does not contain a T object at its top level.  Hence, you
203  *  should generally check the type of e first.
204  *
205  *  @param e expression
206  *  @return reference to pseries object
207  *  @see is_exactly_a<class T>() */
208 template <class T>
209 inline const T &ex_to(const ex &e)
210 {
211         GINAC_ASSERT(is_a<T>(e));
212         return static_cast<const T &>(*e.bp);
213 }
214
215 } // namespace GiNaC
216
217 #endif // ndef __GINAC_BASIC_H__