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