]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
b579a3be00fa7f1840e8a3c70c33a6db5ee1639c
[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-2000 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 <strstream>
27 #include "basic.h"
28 #include "ex.h"
29
30 class cl_N;     // We want to include cln.h only in numeric.cpp in order to 
31                                 // avoid namespace pollution and keep compile-time low.
32
33 #ifndef NO_NAMESPACE_GINAC
34 namespace GiNaC {
35 #endif // ndef NO_NAMESPACE_GINAC
36
37 #define HASHVALUE_NUMERIC 0x80000001U
38
39 /** This class is used to instantiate a global object Digits which
40  *  behaves just like Maple's Digits.  We need an object rather than a
41  *  dumber basic type since as a side-effect we let it change
42  *  cl_default_float_format when it gets changed.  The only other
43  *  meaningful thing to do with it is converting it to an unsigned,
44  *  for temprary storing its value e.g.  The user must not create an
45  *  own working object of this class!  Since C++ forces us to make the
46  *  class definition visible in order to use an object we put in a
47  *  flag which prevents other objects of that class to be created. */
48 class _numeric_digits
49 {
50 // member functions
51 public:
52         _numeric_digits();
53         _numeric_digits& operator=(long prec);
54         operator long();
55         void print(std::ostream & os) const;
56 // member variables
57 private:
58         long digits;
59         static bool too_late;
60 };
61
62 /** This class is a wrapper around CLN-numbers within the GiNaC class
63  *  hierarchy. Objects of this type may directly be created by the user.*/
64 class numeric : public basic
65 {
66         GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
67
68 // friends
69         friend const numeric exp(const numeric & x);
70         friend const numeric log(const numeric & x);
71         friend const numeric sin(const numeric & x);
72         friend const numeric cos(const numeric & x);
73         friend const numeric tan(const numeric & x);
74         friend const numeric asin(const numeric & x);
75         friend const numeric acos(const numeric & x);
76         friend const numeric atan(const numeric & x);
77         friend const numeric atan(const numeric & y, const numeric & x);
78         friend const numeric sinh(const numeric & x);
79         friend const numeric cosh(const numeric & x);
80         friend const numeric tanh(const numeric & x);
81         friend const numeric asinh(const numeric & x);
82         friend const numeric acosh(const numeric & x);
83         friend const numeric atanh(const numeric & x);
84         friend const numeric Li2(const numeric & x);
85         friend const numeric zeta(const numeric & x);
86         friend const numeric fibonacci(const numeric & n);
87         friend numeric abs(const numeric & x);
88         friend numeric mod(const numeric & a, const numeric & b);
89         friend numeric smod(const numeric & a, const numeric & b);
90         friend numeric irem(const numeric & a, const numeric & b);
91         friend numeric irem(const numeric & a, const numeric & b, numeric & q);
92         friend numeric iquo(const numeric & a, const numeric & b);
93         friend numeric iquo(const numeric & a, const numeric & b, numeric & r);
94         friend numeric sqrt(const numeric & x);
95         friend numeric isqrt(const numeric & x);
96         friend numeric gcd(const numeric & a, const numeric & b);
97         friend numeric lcm(const numeric & a, const numeric & b);
98
99 // member functions
100
101         // default constructor, destructor, copy constructor assignment
102         // operator and helpers
103 public:
104         numeric();
105         ~numeric();
106         numeric(const numeric & other);
107         const numeric & operator=(const numeric & other);
108 protected:
109         void copy(const numeric & other);
110         void destroy(bool call_parent);
111
112         // other constructors
113 public:
114         explicit numeric(int i);
115         explicit numeric(unsigned int i);
116         explicit numeric(long i);
117         explicit numeric(unsigned long i);
118         explicit numeric(long numer, long denom);
119         explicit numeric(double d);
120         explicit numeric(const char *);
121         numeric(const cl_N & z);
122         
123         // functions overriding virtual functions from bases classes
124 public:
125         basic * duplicate() const;
126         void print(std::ostream & os, unsigned precedence=0) const;
127         void printraw(std::ostream & os) const;
128         void printtree(std::ostream & os, unsigned indent) const;
129         void printcsrc(std::ostream & os, unsigned type, unsigned precedence=0) const;
130         bool info(unsigned inf) const;
131         bool has(const ex & other) const;
132         ex eval(int level=0) const;
133         ex evalf(int level=0) const;
134         ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
135         ex to_rational(lst &repl_lst) const;
136         numeric integer_content(void) const;
137         ex smod(const numeric &xi) const;
138         numeric max_coefficient(void) const;
139 protected:
140         ex derivative(const symbol & s) const;
141         int compare_same_type(const basic & other) const;
142         bool is_equal_same_type(const basic & other) const;
143         unsigned calchash(void) const;
144
145         // new virtual functions which can be overridden by derived classes
146         // (none)
147
148         // non-virtual functions in this class
149 public:
150         numeric add(const numeric & other) const;
151         numeric sub(const numeric & other) const;
152         numeric mul(const numeric & other) const;
153         numeric div(const numeric & other) const;
154         numeric power(const numeric & other) const;
155         const numeric & add_dyn(const numeric & other) const;
156         const numeric & sub_dyn(const numeric & other) const;
157         const numeric & mul_dyn(const numeric & other) const;
158         const numeric & div_dyn(const numeric & other) const;
159         const numeric & power_dyn(const numeric & other) const;
160         const numeric & operator=(int i);
161         const numeric & operator=(unsigned int i);
162         const numeric & operator=(long i);
163         const numeric & operator=(unsigned long i);
164         const numeric & operator=(double d);
165         const numeric & operator=(const char * s);
166         numeric inverse(void) const;
167         int csgn(void) const;
168         ::cl_N* clnptr(void) const { return value; } /**< ptr to representation. */
169         int compare(const numeric & other) const;
170         bool is_equal(const numeric & other) const;
171         bool is_zero(void) const;
172         bool is_positive(void) const;
173         bool is_negative(void) const;
174         bool is_integer(void) const;
175         bool is_pos_integer(void) const;
176         bool is_nonneg_integer(void) const;
177         bool is_even(void) const;
178         bool is_odd(void) const;
179         bool is_prime(void) const;
180         bool is_rational(void) const;
181         bool is_real(void) const;
182         bool is_cinteger(void) const;
183         bool is_crational(void) const;
184         bool operator==(const numeric & other) const;
185         bool operator!=(const numeric & other) const;
186         bool operator<(const numeric & other) const;
187         bool operator<=(const numeric & other) const;
188         bool operator>(const numeric & other) const;
189         bool operator>=(const numeric & other) const;
190         int to_int(void) const;
191         long to_long(void) const;
192         double to_double(void) const;
193         const numeric real(void) const;
194         const numeric imag(void) const;
195         const numeric numer(void) const;
196         const numeric denom(void) const;
197         int int_length(void) const;
198
199 // member variables
200
201 protected:
202         static unsigned precedence;
203         ::cl_N *value;
204 };
205
206 // global constants
207
208 extern const numeric some_numeric;
209 extern const numeric I;
210 extern const type_info & typeid_numeric;
211 extern _numeric_digits Digits;
212
213 //#define is_a_numeric_hash(x) ((x)==HASHVALUE_NUMERIC)
214 // may have to be changed to ((x)>=0x80000000U)
215
216 // has been changed
217 //#define is_a_numeric_hash(x) ((x)&0x80000000U)
218
219 // global functions
220
221 const numeric exp(const numeric & x);
222 const numeric log(const numeric & x);
223 const numeric sin(const numeric & x);
224 const numeric cos(const numeric & x);
225 const numeric tan(const numeric & x);
226 const numeric asin(const numeric & x);
227 const numeric acos(const numeric & x);
228 const numeric atan(const numeric & x);
229 const numeric atan(const numeric & y, const numeric & x);
230 const numeric sinh(const numeric & x);
231 const numeric cosh(const numeric & x);
232 const numeric tanh(const numeric & x);
233 const numeric asinh(const numeric & x);
234 const numeric acosh(const numeric & x);
235 const numeric atanh(const numeric & x);
236 const numeric Li2(const numeric & x);
237 const numeric zeta(const numeric & x);
238 const numeric lgamma(const numeric & x);
239 const numeric tgamma(const numeric & x);
240 const numeric psi(const numeric & x);
241 const numeric psi(const numeric & n, const numeric & x);
242 const numeric factorial(const numeric & n);
243 const numeric doublefactorial(const numeric & n);
244 const numeric binomial(const numeric & n, const numeric & k);
245 const numeric bernoulli(const numeric & n);
246 const numeric fibonacci(const numeric & n);
247
248 numeric abs(const numeric & x);
249 numeric mod(const numeric & a, const numeric & b);
250 numeric smod(const numeric & a, const numeric & b);
251 numeric irem(const numeric & a, const numeric & b);
252 numeric irem(const numeric & a, const numeric & b, numeric & q);
253 numeric iquo(const numeric & a, const numeric & b);
254 numeric iquo(const numeric & a, const numeric & b, numeric & r);
255 numeric sqrt(const numeric & x);
256 numeric isqrt(const numeric & x);
257
258 numeric gcd(const numeric & a, const numeric & b);
259 numeric lcm(const numeric & a, const numeric & b);
260
261 // wrapper functions around member functions
262 inline numeric pow(const numeric & x, const numeric & y)
263 { return x.power(y); }
264
265 inline numeric inverse(const numeric & x)
266 { return x.inverse(); }
267
268 inline int csgn(const numeric & x)
269 { return x.csgn(); }
270
271 inline bool is_zero(const numeric & x)
272 { return x.is_zero(); }
273
274 inline bool is_positive(const numeric & x)
275 { return x.is_positive(); }
276
277 inline bool is_integer(const numeric & x)
278 { return x.is_integer(); }
279
280 inline bool is_pos_integer(const numeric & x)
281 { return x.is_pos_integer(); }
282
283 inline bool is_nonneg_integer(const numeric & x)
284 { return x.is_nonneg_integer(); }
285
286 inline bool is_even(const numeric & x)
287 { return x.is_even(); }
288
289 inline bool is_odd(const numeric & x)
290 { return x.is_odd(); }
291
292 inline bool is_prime(const numeric & x)
293 { return x.is_prime(); }
294
295 inline bool is_rational(const numeric & x)
296 { return x.is_rational(); }
297
298 inline bool is_real(const numeric & x)
299 { return x.is_real(); }
300
301 inline bool is_cinteger(const numeric & x)
302 { return x.is_cinteger(); }
303
304 inline bool is_crational(const numeric & x)
305 { return x.is_crational(); }
306
307 inline const numeric real(const numeric & x)
308 { return x.real(); }
309
310 inline const numeric imag(const numeric & x)
311 { return x.imag(); }
312
313 inline const numeric numer(const numeric & x)
314 { return x.numer(); }
315
316 inline const numeric denom(const numeric & x)
317 { return x.denom(); }
318
319 // numeric evaluation functions for class constant objects:
320
321 ex PiEvalf(void);
322 ex EulerEvalf(void);
323 ex CatalanEvalf(void);
324
325
326 // utility functions
327 inline const numeric &ex_to_numeric(const ex &e)
328 {
329         return static_cast<const numeric &>(*e.bp);
330 }
331
332
333 #ifndef NO_NAMESPACE_GINAC
334 } // namespace GiNaC
335 #endif // ndef NO_NAMESPACE_GINAC
336
337 #endif // ndef __GINAC_NUMERIC_H__