]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
use new-style print methods
[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         unsigned precedence() const {return 30;}
99         bool info(unsigned inf) const;
100         int degree(const ex & s) const;
101         int ldegree(const ex & s) const;
102         ex coeff(const ex & s, int n = 1) const;
103         bool has(const ex &other) const;
104         ex eval(int level = 0) const;
105         ex evalf(int level = 0) const;
106         ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
107         ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
108         ex to_rational(lst &repl_lst) const;
109         ex to_polynomial(lst &repl_lst) const;
110         numeric integer_content() const;
111         ex smod(const numeric &xi) const;
112         numeric max_coefficient() const;
113 protected:
114         /** Implementation of ex::diff for a numeric always returns 0.
115          *  @see ex::diff */
116         ex derivative(const symbol &s) const { return 0; }
117         bool is_equal_same_type(const basic &other) const;
118         unsigned calchash() const;
119         
120         // new virtual functions which can be overridden by derived classes
121         // (none)
122         
123         // non-virtual functions in this class
124 public:
125         const numeric add(const numeric &other) const;
126         const numeric sub(const numeric &other) const;
127         const numeric mul(const numeric &other) const;
128         const numeric div(const numeric &other) const;
129         const numeric power(const numeric &other) const;
130         const numeric & add_dyn(const numeric &other) const;
131         const numeric & sub_dyn(const numeric &other) const;
132         const numeric & mul_dyn(const numeric &other) const;
133         const numeric & div_dyn(const numeric &other) const;
134         const numeric & power_dyn(const numeric &other) const;
135         const numeric & operator=(int i);
136         const numeric & operator=(unsigned int i);
137         const numeric & operator=(long i);
138         const numeric & operator=(unsigned long i);
139         const numeric & operator=(double d);
140         const numeric & operator=(const char *s);
141         const numeric inverse() const;
142         int csgn() const;
143         int compare(const numeric &other) const;
144         bool is_equal(const numeric &other) const;
145         bool is_zero() const;
146         bool is_positive() const;
147         bool is_negative() const;
148         bool is_integer() const;
149         bool is_pos_integer() const;
150         bool is_nonneg_integer() const;
151         bool is_even() const;
152         bool is_odd() const;
153         bool is_prime() const;
154         bool is_rational() const;
155         bool is_real() const;
156         bool is_cinteger() const;
157         bool is_crational() 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         bool operator>=(const numeric &other) const;
164         int to_int() const;
165         long to_long() const;
166         double to_double() const;
167         cln::cl_N to_cl_N() const;
168         const numeric real() const;
169         const numeric imag() const;
170         const numeric numer() const;
171         const numeric denom() const;
172         int int_length() const;
173         // converting routines for interfacing with CLN:
174         numeric(const cln::cl_N &z);
175
176 protected:
177         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;
178         void do_print(const print_context & c, unsigned level) const;
179         void do_print_latex(const print_latex & c, unsigned level) const;
180         void do_print_csrc(const print_csrc & c, unsigned level) const;
181         void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
182         void do_print_tree(const print_tree & c, unsigned level) const;
183         void do_print_python_repr(const print_python_repr & c, unsigned level) const;
184
185 // member variables
186
187 protected:
188         cln::cl_number value;
189 };
190
191
192 // global constants
193
194 extern const numeric I;
195 extern _numeric_digits Digits;
196
197 // global functions
198
199 const numeric exp(const numeric &x);
200 const numeric log(const numeric &x);
201 const numeric sin(const numeric &x);
202 const numeric cos(const numeric &x);
203 const numeric tan(const numeric &x);
204 const numeric asin(const numeric &x);
205 const numeric acos(const numeric &x);
206 const numeric atan(const numeric &x);
207 const numeric atan(const numeric &y, const numeric &x);
208 const numeric sinh(const numeric &x);
209 const numeric cosh(const numeric &x);
210 const numeric tanh(const numeric &x);
211 const numeric asinh(const numeric &x);
212 const numeric acosh(const numeric &x);
213 const numeric atanh(const numeric &x);
214 const numeric Li2(const numeric &x);
215 const numeric zeta(const numeric &x);
216 const numeric lgamma(const numeric &x);
217 const numeric tgamma(const numeric &x);
218 const numeric psi(const numeric &x);
219 const numeric psi(const numeric &n, const numeric &x);
220 const numeric factorial(const numeric &n);
221 const numeric doublefactorial(const numeric &n);
222 const numeric binomial(const numeric &n, const numeric &k);
223 const numeric bernoulli(const numeric &n);
224 const numeric fibonacci(const numeric &n);
225 const numeric isqrt(const numeric &x);
226 const numeric sqrt(const numeric &x);
227 const numeric abs(const numeric &x);
228 const numeric mod(const numeric &a, const numeric &b);
229 const numeric smod(const numeric &a, const numeric &b);
230 const numeric irem(const numeric &a, const numeric &b);
231 const numeric irem(const numeric &a, const numeric &b, numeric &q);
232 const numeric iquo(const numeric &a, const numeric &b);
233 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
234 const numeric gcd(const numeric &a, const numeric &b);
235 const numeric lcm(const numeric &a, const numeric &b);
236
237 // wrapper functions around member functions
238 inline const numeric pow(const numeric &x, const numeric &y)
239 { return x.power(y); }
240
241 inline const numeric inverse(const numeric &x)
242 { return x.inverse(); }
243
244 inline int csgn(const numeric &x)
245 { return x.csgn(); }
246
247 inline bool is_zero(const numeric &x)
248 { return x.is_zero(); }
249
250 inline bool is_positive(const numeric &x)
251 { return x.is_positive(); }
252
253 inline bool is_integer(const numeric &x)
254 { return x.is_integer(); }
255
256 inline bool is_pos_integer(const numeric &x)
257 { return x.is_pos_integer(); }
258
259 inline bool is_nonneg_integer(const numeric &x)
260 { return x.is_nonneg_integer(); }
261
262 inline bool is_even(const numeric &x)
263 { return x.is_even(); }
264
265 inline bool is_odd(const numeric &x)
266 { return x.is_odd(); }
267
268 inline bool is_prime(const numeric &x)
269 { return x.is_prime(); }
270
271 inline bool is_rational(const numeric &x)
272 { return x.is_rational(); }
273
274 inline bool is_real(const numeric &x)
275 { return x.is_real(); }
276
277 inline bool is_cinteger(const numeric &x)
278 { return x.is_cinteger(); }
279
280 inline bool is_crational(const numeric &x)
281 { return x.is_crational(); }
282
283 inline int to_int(const numeric &x)
284 { return x.to_int(); }
285
286 inline long to_long(const numeric &x)
287 { return x.to_long(); }
288
289 inline double to_double(const numeric &x)
290 { return x.to_double(); }
291
292 inline const numeric real(const numeric &x)
293 { return x.real(); }
294
295 inline const numeric imag(const numeric &x)
296 { return x.imag(); }
297
298 inline const numeric numer(const numeric &x)
299 { return x.numer(); }
300
301 inline const numeric denom(const numeric &x)
302 { return x.denom(); }
303
304 // numeric evaluation functions for class constant objects:
305
306 ex PiEvalf();
307 ex EulerEvalf();
308 ex CatalanEvalf();
309
310
311 // utility functions
312
313 /** Specialization of is_exactly_a<numeric>(obj) for numeric objects. */
314 template<> inline bool is_exactly_a<numeric>(const basic & obj)
315 {
316         return obj.tinfo()==TINFO_numeric;
317 }
318
319 } // namespace GiNaC
320
321 #ifdef __MAKECINT__
322 #pragma link off defined_in cln/number.h;
323 #pragma link off defined_in cln/complex_class.h;
324 #endif
325
326 #endif // ndef __GINAC_NUMERIC_H__