]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
Univariate Hensel lifting now uses upoly.
[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-2008 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22
23 #ifndef __GINAC_NUMERIC_H__
24 #define __GINAC_NUMERIC_H__
25
26 #include "basic.h"
27 #include "ex.h"
28 #include "archive.h"
29
30 #include <stdexcept>
31 #include <vector>
32
33 #include <cln/complex.h>
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 /** Function pointer to implement callbacks in the case 'Digits' gets changed.
45  *  Main purpose of such callbacks is to adjust look-up tables of certain
46  *  functions to the new precision. Parameter contains the signed difference
47  *  between new Digits and old Digits. */
48 typedef void (* digits_changed_callback)(long);
49
50 /** This class is used to instantiate a global singleton object Digits
51  *  which behaves just like Maple's Digits.  We need an object rather 
52  *  than a dumber basic type since as a side-effect we let it change
53  *  cl_default_float_format when it gets changed.  The only other
54  *  meaningful thing to do with it is converting it to an unsigned,
55  *  for temprary storing its value e.g.  The user must not create an
56  *  own working object of this class!  Since C++ forces us to make the
57  *  class definition visible in order to use an object we put in a
58  *  flag which prevents other objects of that class to be created. */
59 class _numeric_digits
60 {
61 // member functions
62 public:
63         _numeric_digits();
64         _numeric_digits& operator=(long prec);
65         operator long();
66         void print(std::ostream& os) const;
67         void add_callback(digits_changed_callback callback);
68 // member variables
69 private:
70         long digits;                        ///< Number of decimal digits
71         static bool too_late;               ///< Already one object present
72         // Holds a list of functions that get called when digits is changed.
73         std::vector<digits_changed_callback> callbacklist;
74 };
75
76
77 /** Exception class thrown when a singularity is encountered. */
78 class pole_error : public std::domain_error {
79 public:
80         explicit pole_error(const std::string& what_arg, int degree);
81         int degree() const;
82 private:
83         int deg;
84 };
85
86
87 /** This class is a wrapper around CLN-numbers within the GiNaC class
88  *  hierarchy. Objects of this type may directly be created by the user.*/
89 class numeric : public basic
90 {
91         GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
92         
93 // member functions
94         
95         // other constructors
96 public:
97         numeric(int i);
98         numeric(unsigned int i);
99         numeric(long i);
100         numeric(unsigned long i);
101         numeric(long numer, long denom);
102         numeric(double d);
103         numeric(const char *);
104         
105         // functions overriding virtual functions from base classes
106 public:
107         unsigned precedence() const {return 30;}
108         bool info(unsigned inf) const;
109         bool is_polynomial(const ex & var) const;
110         int degree(const ex & s) const;
111         int ldegree(const ex & s) const;
112         ex coeff(const ex & s, int n = 1) const;
113         bool has(const ex &other, unsigned options = 0) const;
114         ex eval(int level = 0) const;
115         ex evalf(int level = 0) const;
116         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
117         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
118         ex to_rational(exmap & repl) const;
119         ex to_polynomial(exmap & repl) const;
120         numeric integer_content() const;
121         ex smod(const numeric &xi) const;
122         numeric max_coefficient() const;
123         ex conjugate() const;
124         ex real_part() const;
125         ex imag_part() const;
126         /** Save (a.k.a. serialize) object into archive. */
127         void archive(archive_node& n) const;
128         /** Read (a.k.a. deserialize) object from archive. */
129         void read_archive(const archive_node& n, lst& syms);
130 protected:
131         /** Implementation of ex::diff for a numeric always returns 0.
132          *  @see ex::diff */
133         ex derivative(const symbol &s) const { return 0; }
134         bool is_equal_same_type(const basic &other) const;
135         unsigned calchash() const;
136         
137         // new virtual functions which can be overridden by derived classes
138         // (none)
139         
140         // non-virtual functions in this class
141 public:
142         const numeric add(const numeric &other) const;
143         const numeric sub(const numeric &other) const;
144         const numeric mul(const numeric &other) const;
145         const numeric div(const numeric &other) const;
146         const numeric power(const numeric &other) const;
147         const numeric & add_dyn(const numeric &other) const;
148         const numeric & sub_dyn(const numeric &other) const;
149         const numeric & mul_dyn(const numeric &other) const;
150         const numeric & div_dyn(const numeric &other) const;
151         const numeric & power_dyn(const numeric &other) const;
152         const numeric & operator=(int i);
153         const numeric & operator=(unsigned int i);
154         const numeric & operator=(long i);
155         const numeric & operator=(unsigned long i);
156         const numeric & operator=(double d);
157         const numeric & operator=(const char *s);
158         const numeric inverse() const;
159         numeric step() const;
160         int csgn() const;
161         int compare(const numeric &other) const;
162         bool is_equal(const numeric &other) const;
163         bool is_zero() const;
164         bool is_positive() const;
165         bool is_negative() const;
166         bool is_integer() const;
167         bool is_pos_integer() const;
168         bool is_nonneg_integer() const;
169         bool is_even() const;
170         bool is_odd() const;
171         bool is_prime() const;
172         bool is_rational() const;
173         bool is_real() const;
174         bool is_cinteger() const;
175         bool is_crational() const;
176         bool operator==(const numeric &other) const;
177         bool operator!=(const numeric &other) const;
178         bool operator<(const numeric &other) const;
179         bool operator<=(const numeric &other) const;
180         bool operator>(const numeric &other) const;
181         bool operator>=(const numeric &other) const;
182         int to_int() const;
183         long to_long() const;
184         double to_double() const;
185         cln::cl_N to_cl_N() const;
186         const numeric real() const;
187         const numeric imag() const;
188         const numeric numer() const;
189         const numeric denom() const;
190         int int_length() const;
191         // converting routines for interfacing with CLN:
192         explicit numeric(const cln::cl_N &z);
193
194 protected:
195         void print_numeric(const print_context & c, const char *par_open, const char *par_close, const char *imag_sym, const char *mul_sym, unsigned level) const;
196         void do_print(const print_context & c, unsigned level) const;
197         void do_print_latex(const print_latex & c, unsigned level) const;
198         void do_print_csrc(const print_csrc & c, unsigned level) const;
199         void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
200         void do_print_tree(const print_tree & c, unsigned level) const;
201         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
202
203 // member variables
204
205 protected:
206         cln::cl_N value;
207 };
208 GINAC_DECLARE_UNARCHIVER(numeric); 
209
210
211 // global constants
212
213 extern const numeric I;
214 extern _numeric_digits Digits;
215
216 // global functions
217
218 const numeric exp(const numeric &x);
219 const numeric log(const numeric &x);
220 const numeric sin(const numeric &x);
221 const numeric cos(const numeric &x);
222 const numeric tan(const numeric &x);
223 const numeric asin(const numeric &x);
224 const numeric acos(const numeric &x);
225 const numeric atan(const numeric &x);
226 const numeric atan(const numeric &y, const numeric &x);
227 const numeric sinh(const numeric &x);
228 const numeric cosh(const numeric &x);
229 const numeric tanh(const numeric &x);
230 const numeric asinh(const numeric &x);
231 const numeric acosh(const numeric &x);
232 const numeric atanh(const numeric &x);
233 const numeric Li2(const numeric &x);
234 const numeric zeta(const numeric &x);
235 const numeric lgamma(const numeric &x);
236 const numeric tgamma(const numeric &x);
237 const numeric psi(const numeric &x);
238 const numeric psi(const numeric &n, const numeric &x);
239 const numeric factorial(const numeric &n);
240 const numeric doublefactorial(const numeric &n);
241 const numeric binomial(const numeric &n, const numeric &k);
242 const numeric bernoulli(const numeric &n);
243 const numeric fibonacci(const numeric &n);
244 const numeric isqrt(const numeric &x);
245 const numeric sqrt(const numeric &x);
246 const numeric abs(const numeric &x);
247 const numeric mod(const numeric &a, const numeric &b);
248 const numeric smod(const numeric &a, const numeric &b);
249 const numeric irem(const numeric &a, const numeric &b);
250 const numeric irem(const numeric &a, const numeric &b, numeric &q);
251 const numeric iquo(const numeric &a, const numeric &b);
252 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
253 const numeric gcd(const numeric &a, const numeric &b);
254 const numeric lcm(const numeric &a, const numeric &b);
255
256 // wrapper functions around member functions
257 inline const numeric pow(const numeric &x, const numeric &y)
258 { return x.power(y); }
259
260 inline const numeric inverse(const numeric &x)
261 { return x.inverse(); }
262
263 inline numeric step(const numeric &x)
264 { return x.step(); }
265
266 inline int csgn(const numeric &x)
267 { return x.csgn(); }
268
269 inline bool is_zero(const numeric &x)
270 { return x.is_zero(); }
271
272 inline bool is_positive(const numeric &x)
273 { return x.is_positive(); }
274
275 inline bool is_negative(const numeric &x)
276 { return x.is_negative(); }
277
278 inline bool is_integer(const numeric &x)
279 { return x.is_integer(); }
280
281 inline bool is_pos_integer(const numeric &x)
282 { return x.is_pos_integer(); }
283
284 inline bool is_nonneg_integer(const numeric &x)
285 { return x.is_nonneg_integer(); }
286
287 inline bool is_even(const numeric &x)
288 { return x.is_even(); }
289
290 inline bool is_odd(const numeric &x)
291 { return x.is_odd(); }
292
293 inline bool is_prime(const numeric &x)
294 { return x.is_prime(); }
295
296 inline bool is_rational(const numeric &x)
297 { return x.is_rational(); }
298
299 inline bool is_real(const numeric &x)
300 { return x.is_real(); }
301
302 inline bool is_cinteger(const numeric &x)
303 { return x.is_cinteger(); }
304
305 inline bool is_crational(const numeric &x)
306 { return x.is_crational(); }
307
308 inline int to_int(const numeric &x)
309 { return x.to_int(); }
310
311 inline long to_long(const numeric &x)
312 { return x.to_long(); }
313
314 inline double to_double(const numeric &x)
315 { return x.to_double(); }
316
317 inline const numeric real(const numeric &x)
318 { return x.real(); }
319
320 inline const numeric imag(const numeric &x)
321 { return x.imag(); }
322
323 inline const numeric numer(const numeric &x)
324 { return x.numer(); }
325
326 inline const numeric denom(const numeric &x)
327 { return x.denom(); }
328
329 // numeric evaluation functions for class constant objects:
330
331 ex PiEvalf();
332 ex EulerEvalf();
333 ex CatalanEvalf();
334
335
336 } // namespace GiNaC
337
338 #ifdef __MAKECINT__
339 #pragma link off defined_in cln/number.h;
340 #pragma link off defined_in cln/complex_class.h;
341 #endif
342
343 #endif // ndef __GINAC_NUMERIC_H__