]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
some more comments and cleanups to mul::expand() and ncmul::expand()
[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-2001 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 <cln/number.h>
30 // forward decln of cln::cl_N, since cln/complex_class.h is not included:
31 namespace cln { class cl_N; }
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 /** This class is a wrapper around CLN-numbers within the GiNaC class
66  *  hierarchy. Objects of this type may directly be created by the user.*/
67 class numeric : public basic
68 {
69         GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
70         
71 // member functions
72         
73         // other ctors
74 public:
75         explicit numeric(int i);
76         explicit numeric(unsigned int i);
77         explicit numeric(long i);
78         explicit numeric(unsigned long i);
79         explicit numeric(long numer, long denom);
80         explicit numeric(double d);
81         explicit numeric(const char *);
82         
83         // functions overriding virtual functions from bases classes
84 public:
85         void print(const print_context & c, unsigned level = 0) const;
86         unsigned precedence(void) const {return 30;}
87         bool info(unsigned inf) const;
88         bool has(const ex &other) const;
89         ex eval(int level = 0) const;
90         ex evalf(int level = 0) const;
91         ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
92         ex to_rational(lst &repl_lst) const;
93         numeric integer_content(void) const;
94         ex smod(const numeric &xi) const;
95         numeric max_coefficient(void) const;
96 protected:
97         /** Implementation of ex::diff for a numeric always returns 0.
98          *  @see ex::diff */
99         ex derivative(const symbol &s) const { return _ex0(); }
100         bool is_equal_same_type(const basic &other) const;
101         unsigned calchash(void) const;
102         
103         // new virtual functions which can be overridden by derived classes
104         // (none)
105         
106         // non-virtual functions in this class
107 public:
108         const numeric add(const numeric &other) const;
109         const numeric sub(const numeric &other) const;
110         const numeric mul(const numeric &other) const;
111         const numeric div(const numeric &other) const;
112         const numeric power(const numeric &other) const;
113         const numeric & add_dyn(const numeric &other) const;
114         const numeric & sub_dyn(const numeric &other) const;
115         const numeric & mul_dyn(const numeric &other) const;
116         const numeric & div_dyn(const numeric &other) const;
117         const numeric & power_dyn(const numeric &other) const;
118         const numeric & operator=(int i);
119         const numeric & operator=(unsigned int i);
120         const numeric & operator=(long i);
121         const numeric & operator=(unsigned long i);
122         const numeric & operator=(double d);
123         const numeric & operator=(const char *s);
124         const numeric inverse(void) const;
125         int csgn(void) const;
126         int compare(const numeric &other) const;
127         bool is_equal(const numeric &other) const;
128         bool is_zero(void) const;
129         bool is_positive(void) const;
130         bool is_negative(void) const;
131         bool is_integer(void) const;
132         bool is_pos_integer(void) const;
133         bool is_nonneg_integer(void) const;
134         bool is_even(void) const;
135         bool is_odd(void) const;
136         bool is_prime(void) const;
137         bool is_rational(void) const;
138         bool is_real(void) const;
139         bool is_cinteger(void) const;
140         bool is_crational(void) const;
141         bool operator==(const numeric &other) const;
142         bool operator!=(const numeric &other) const;
143         bool operator<(const numeric &other) const;
144         bool operator<=(const numeric &other) const;
145         bool operator>(const numeric &other) const;
146         bool operator>=(const numeric &other) const;
147         int to_int(void) const;
148         long to_long(void) const;
149         double to_double(void) const;
150         cln::cl_N to_cl_N(void) const;
151         const numeric real(void) const;
152         const numeric imag(void) const;
153         const numeric numer(void) const;
154         const numeric denom(void) const;
155         int int_length(void) const;
156         // converting routines for interfacing with CLN:
157         numeric(const cln::cl_N &z);
158
159 // member variables
160
161 protected:
162         cln::cl_number value;
163 };
164
165 // global constants
166
167 extern const numeric I;
168 extern _numeric_digits Digits;
169
170 // deprecated macro, for internal use only
171 #define is_a_numeric_hash(x) ((x)&0x80000000U)
172
173 // global functions
174
175 const numeric exp(const numeric &x);
176 const numeric log(const numeric &x);
177 const numeric sin(const numeric &x);
178 const numeric cos(const numeric &x);
179 const numeric tan(const numeric &x);
180 const numeric asin(const numeric &x);
181 const numeric acos(const numeric &x);
182 const numeric atan(const numeric &x);
183 const numeric atan(const numeric &y, const numeric &x);
184 const numeric sinh(const numeric &x);
185 const numeric cosh(const numeric &x);
186 const numeric tanh(const numeric &x);
187 const numeric asinh(const numeric &x);
188 const numeric acosh(const numeric &x);
189 const numeric atanh(const numeric &x);
190 const numeric Li2(const numeric &x);
191 const numeric zeta(const numeric &x);
192 const numeric lgamma(const numeric &x);
193 const numeric tgamma(const numeric &x);
194 const numeric psi(const numeric &x);
195 const numeric psi(const numeric &n, const numeric &x);
196 const numeric factorial(const numeric &n);
197 const numeric doublefactorial(const numeric &n);
198 const numeric binomial(const numeric &n, const numeric &k);
199 const numeric bernoulli(const numeric &n);
200 const numeric fibonacci(const numeric &n);
201 const numeric isqrt(const numeric &x);
202 const numeric sqrt(const numeric &x);
203 const numeric abs(const numeric &x);
204 const numeric mod(const numeric &a, const numeric &b);
205 const numeric smod(const numeric &a, const numeric &b);
206 const numeric irem(const numeric &a, const numeric &b);
207 const numeric irem(const numeric &a, const numeric &b, numeric &q);
208 const numeric iquo(const numeric &a, const numeric &b);
209 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
210 const numeric gcd(const numeric &a, const numeric &b);
211 const numeric lcm(const numeric &a, const numeric &b);
212
213 // wrapper functions around member functions
214 inline const numeric pow(const numeric &x, const numeric &y)
215 { return x.power(y); }
216
217 inline const numeric inverse(const numeric &x)
218 { return x.inverse(); }
219
220 inline int csgn(const numeric &x)
221 { return x.csgn(); }
222
223 inline bool is_zero(const numeric &x)
224 { return x.is_zero(); }
225
226 inline bool is_positive(const numeric &x)
227 { return x.is_positive(); }
228
229 inline bool is_integer(const numeric &x)
230 { return x.is_integer(); }
231
232 inline bool is_pos_integer(const numeric &x)
233 { return x.is_pos_integer(); }
234
235 inline bool is_nonneg_integer(const numeric &x)
236 { return x.is_nonneg_integer(); }
237
238 inline bool is_even(const numeric &x)
239 { return x.is_even(); }
240
241 inline bool is_odd(const numeric &x)
242 { return x.is_odd(); }
243
244 inline bool is_prime(const numeric &x)
245 { return x.is_prime(); }
246
247 inline bool is_rational(const numeric &x)
248 { return x.is_rational(); }
249
250 inline bool is_real(const numeric &x)
251 { return x.is_real(); }
252
253 inline bool is_cinteger(const numeric &x)
254 { return x.is_cinteger(); }
255
256 inline bool is_crational(const numeric &x)
257 { return x.is_crational(); }
258
259 inline int to_int(const numeric &x)
260 { return x.to_int(); }
261
262 inline long to_long(const numeric &x)
263 { return x.to_long(); }
264
265 inline double to_double(const numeric &x)
266 { return x.to_double(); }
267
268 inline const numeric real(const numeric &x)
269 { return x.real(); }
270
271 inline const numeric imag(const numeric &x)
272 { return x.imag(); }
273
274 inline const numeric numer(const numeric &x)
275 { return x.numer(); }
276
277 inline const numeric denom(const numeric &x)
278 { return x.denom(); }
279
280 // numeric evaluation functions for class constant objects:
281
282 ex PiEvalf(void);
283 ex EulerEvalf(void);
284 ex CatalanEvalf(void);
285
286
287 // utility functions
288
289 /** Return the numeric object handled by an ex.  Deprecated: use ex_to<numeric>().
290  *  This is unsafe: you need to check the type first. */
291 inline const numeric &ex_to_numeric(const ex &e)
292 {
293         return static_cast<const numeric &>(*e.bp);
294 }
295
296 /** Specialization of is_exactly_a<numeric>(obj) for numeric objects. */
297 template<> inline bool is_exactly_a<numeric>(const basic & obj)
298 {
299         return obj.tinfo()==TINFO_numeric;
300 }
301
302 } // namespace GiNaC
303
304 #ifdef __MAKECINT__
305 #pragma link off defined_in cln/number.h;
306 #pragma link off defined_in cln/complex_class.h;
307 #endif
308
309 #endif // ndef __GINAC_NUMERIC_H__