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