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