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