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