]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
subs() and normal() use maps instead of lists, resulting in a huge performance
[ginac.git] / ginac / numeric.h
1 /** @file numeric.h
2  *
3  *  Makes the interface to the underlying bignum package available. */
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_NUMERIC_H__
24 #define __GINAC_NUMERIC_H__
25
26 #include "basic.h"
27 #include "ex.h"
28
29 #include <stdexcept>
30
31 #include <cln/number.h>
32 // forward decln of cln::cl_N, since cln/complex_class.h is not included:
33 namespace cln { class cl_N; }
34
35 #if defined(G__CINTVERSION) && !defined(__MAKECINT__)
36 // Cint @$#$! doesn't like forward declaring classes used for casting operators
37 // so we have to include the definition of cln::cl_N here, but it is enough to
38 // do so for the compiler, hence the !defined(__MAKECINT__).
39   #include <cln/complex_class.h>
40 #endif
41
42 namespace GiNaC {
43
44 /** This class is used to instantiate a global singleton object Digits
45  *  which behaves just like Maple's Digits.  We need an object rather 
46  *  than a dumber basic type since as a side-effect we let it change
47  *  cl_default_float_format when it gets changed.  The only other
48  *  meaningful thing to do with it is converting it to an unsigned,
49  *  for temprary storing its value e.g.  The user must not create an
50  *  own working object of this class!  Since C++ forces us to make the
51  *  class definition visible in order to use an object we put in a
52  *  flag which prevents other objects of that class to be created. */
53 class _numeric_digits
54 {
55 // member functions
56 public:
57         _numeric_digits();
58         _numeric_digits& operator=(long prec);
59         operator long();
60         void print(std::ostream &os) const;
61 // member variables
62 private:
63         long digits;                        ///< Number of decimal digits
64         static bool too_late;               ///< Already one object present
65 };
66
67
68 /** Exception class thrown when a singularity is encountered. */
69 class pole_error : public std::domain_error {
70 public:
71         explicit pole_error(const std::string& what_arg, int degree);
72         int degree() const;
73 private:
74         int deg;
75 };
76
77
78 /** This class is a wrapper around CLN-numbers within the GiNaC class
79  *  hierarchy. Objects of this type may directly be created by the user.*/
80 class numeric : public basic
81 {
82         GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
83         
84 // member functions
85         
86         // other constructors
87 public:
88         numeric(int i);
89         numeric(unsigned int i);
90         numeric(long i);
91         numeric(unsigned long i);
92         numeric(long numer, long denom);
93         numeric(double d);
94         numeric(const char *);
95         
96         // functions overriding virtual functions from base classes
97 public:
98         void print(const print_context & c, unsigned level = 0) const;
99         unsigned precedence() const {return 30;}
100         bool info(unsigned inf) const;
101         int degree(const ex & s) const;
102         int ldegree(const ex & s) const;
103         ex coeff(const ex & s, int n = 1) const;
104         bool has(const ex &other) const;
105         ex eval(int level = 0) const;
106         ex evalf(int level = 0) const;
107         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
108         ex normal(exmap & repl, int level = 0) const;
109         ex to_rational(lst &repl_lst) const;
110         ex to_polynomial(lst &repl_lst) const;
111         numeric integer_content() const;
112         ex smod(const numeric &xi) const;
113         numeric max_coefficient() const;
114 protected:
115         /** Implementation of ex::diff for a numeric always returns 0.
116          *  @see ex::diff */
117         ex derivative(const symbol &s) const { return 0; }
118         bool is_equal_same_type(const basic &other) const;
119         unsigned calchash() const;
120         
121         // new virtual functions which can be overridden by derived classes
122         // (none)
123         
124         // non-virtual functions in this class
125 public:
126         const numeric add(const numeric &other) const;
127         const numeric sub(const numeric &other) const;
128         const numeric mul(const numeric &other) const;
129         const numeric div(const numeric &other) const;
130         const numeric power(const numeric &other) const;
131         const numeric & add_dyn(const numeric &other) const;
132         const numeric & sub_dyn(const numeric &other) const;
133         const numeric & mul_dyn(const numeric &other) const;
134         const numeric & div_dyn(const numeric &other) const;
135         const numeric & power_dyn(const numeric &other) const;
136         const numeric & operator=(int i);
137         const numeric & operator=(unsigned int i);
138         const numeric & operator=(long i);
139         const numeric & operator=(unsigned long i);
140         const numeric & operator=(double d);
141         const numeric & operator=(const char *s);
142         const numeric inverse() const;
143         int csgn() const;
144         int compare(const numeric &other) const;
145         bool is_equal(const numeric &other) const;
146         bool is_zero() const;
147         bool is_positive() const;
148         bool is_negative() const;
149         bool is_integer() const;
150         bool is_pos_integer() const;
151         bool is_nonneg_integer() const;
152         bool is_even() const;
153         bool is_odd() const;
154         bool is_prime() const;
155         bool is_rational() const;
156         bool is_real() const;
157         bool is_cinteger() const;
158         bool is_crational() const;
159         bool operator==(const numeric &other) const;
160         bool operator!=(const numeric &other) const;
161         bool operator<(const numeric &other) const;
162         bool operator<=(const numeric &other) const;
163         bool operator>(const numeric &other) const;
164         bool operator>=(const numeric &other) const;
165         int to_int() const;
166         long to_long() const;
167         double to_double() const;
168         cln::cl_N to_cl_N() const;
169         const numeric real() const;
170         const numeric imag() const;
171         const numeric numer() const;
172         const numeric denom() const;
173         int int_length() const;
174         // converting routines for interfacing with CLN:
175         numeric(const cln::cl_N &z);
176
177 // member variables
178
179 protected:
180         cln::cl_number value;
181 };
182
183
184 // global constants
185
186 extern const numeric I;
187 extern _numeric_digits Digits;
188
189 // global functions
190
191 const numeric exp(const numeric &x);
192 const numeric log(const numeric &x);
193 const numeric sin(const numeric &x);
194 const numeric cos(const numeric &x);
195 const numeric tan(const numeric &x);
196 const numeric asin(const numeric &x);
197 const numeric acos(const numeric &x);
198 const numeric atan(const numeric &x);
199 const numeric atan(const numeric &y, const numeric &x);
200 const numeric sinh(const numeric &x);
201 const numeric cosh(const numeric &x);
202 const numeric tanh(const numeric &x);
203 const numeric asinh(const numeric &x);
204 const numeric acosh(const numeric &x);
205 const numeric atanh(const numeric &x);
206 const numeric Li2(const numeric &x);
207 const numeric zeta(const numeric &x);
208 const numeric lgamma(const numeric &x);
209 const numeric tgamma(const numeric &x);
210 const numeric psi(const numeric &x);
211 const numeric psi(const numeric &n, const numeric &x);
212 const numeric factorial(const numeric &n);
213 const numeric doublefactorial(const numeric &n);
214 const numeric binomial(const numeric &n, const numeric &k);
215 const numeric bernoulli(const numeric &n);
216 const numeric fibonacci(const numeric &n);
217 const numeric isqrt(const numeric &x);
218 const numeric sqrt(const numeric &x);
219 const numeric abs(const numeric &x);
220 const numeric mod(const numeric &a, const numeric &b);
221 const numeric smod(const numeric &a, const numeric &b);
222 const numeric irem(const numeric &a, const numeric &b);
223 const numeric irem(const numeric &a, const numeric &b, numeric &q);
224 const numeric iquo(const numeric &a, const numeric &b);
225 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
226 const numeric gcd(const numeric &a, const numeric &b);
227 const numeric lcm(const numeric &a, const numeric &b);
228
229 // wrapper functions around member functions
230 inline const numeric pow(const numeric &x, const numeric &y)
231 { return x.power(y); }
232
233 inline const numeric inverse(const numeric &x)
234 { return x.inverse(); }
235
236 inline int csgn(const numeric &x)
237 { return x.csgn(); }
238
239 inline bool is_zero(const numeric &x)
240 { return x.is_zero(); }
241
242 inline bool is_positive(const numeric &x)
243 { return x.is_positive(); }
244
245 inline bool is_integer(const numeric &x)
246 { return x.is_integer(); }
247
248 inline bool is_pos_integer(const numeric &x)
249 { return x.is_pos_integer(); }
250
251 inline bool is_nonneg_integer(const numeric &x)
252 { return x.is_nonneg_integer(); }
253
254 inline bool is_even(const numeric &x)
255 { return x.is_even(); }
256
257 inline bool is_odd(const numeric &x)
258 { return x.is_odd(); }
259
260 inline bool is_prime(const numeric &x)
261 { return x.is_prime(); }
262
263 inline bool is_rational(const numeric &x)
264 { return x.is_rational(); }
265
266 inline bool is_real(const numeric &x)
267 { return x.is_real(); }
268
269 inline bool is_cinteger(const numeric &x)
270 { return x.is_cinteger(); }
271
272 inline bool is_crational(const numeric &x)
273 { return x.is_crational(); }
274
275 inline int to_int(const numeric &x)
276 { return x.to_int(); }
277
278 inline long to_long(const numeric &x)
279 { return x.to_long(); }
280
281 inline double to_double(const numeric &x)
282 { return x.to_double(); }
283
284 inline const numeric real(const numeric &x)
285 { return x.real(); }
286
287 inline const numeric imag(const numeric &x)
288 { return x.imag(); }
289
290 inline const numeric numer(const numeric &x)
291 { return x.numer(); }
292
293 inline const numeric denom(const numeric &x)
294 { return x.denom(); }
295
296 // numeric evaluation functions for class constant objects:
297
298 ex PiEvalf();
299 ex EulerEvalf();
300 ex CatalanEvalf();
301
302
303 // utility functions
304
305 /** Specialization of is_exactly_a<numeric>(obj) for numeric objects. */
306 template<> inline bool is_exactly_a<numeric>(const basic & obj)
307 {
308         return obj.tinfo()==TINFO_numeric;
309 }
310
311 } // namespace GiNaC
312
313 #ifdef __MAKECINT__
314 #pragma link off defined_in cln/number.h;
315 #pragma link off defined_in cln/complex_class.h;
316 #endif
317
318 #endif // ndef __GINAC_NUMERIC_H__