]> www.ginac.de Git - ginac.git/blob - ginac/numeric.h
- more logic on the trigonometric function stuff.
[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 #ifndef NO_GINAC_NAMESPACE
34 namespace GiNaC {
35 #endif // ndef NO_GINAC_NAMESPACE
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(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 // friends
67     friend numeric exp(const numeric & x);
68     friend numeric log(const numeric & x);
69     friend numeric sin(const numeric & x);
70     friend numeric cos(const numeric & x);
71     friend numeric tan(const numeric & x);
72     friend numeric asin(const numeric & x);
73     friend numeric acos(const numeric & x);
74     friend numeric atan(const numeric & x);
75     friend numeric atan(const numeric & y, const numeric & x);
76     friend numeric sinh(const numeric & x);
77     friend numeric cosh(const numeric & x);
78     friend numeric tanh(const numeric & x);
79     friend numeric asinh(const numeric & x);
80     friend numeric acosh(const numeric & x);
81     friend numeric atanh(const numeric & x);
82     friend numeric zeta(const numeric & x);
83     friend numeric bernoulli(const numeric & n);
84     friend numeric abs(const numeric & x);
85     friend numeric mod(const numeric & a, const numeric & b);
86     friend numeric smod(const numeric & a, const numeric & b);
87     friend numeric irem(const numeric & a, const numeric & b);
88     friend numeric irem(const numeric & a, const numeric & b, numeric & q);
89     friend numeric iquo(const numeric & a, const numeric & b);
90     friend numeric iquo(const numeric & a, const numeric & b, numeric & r);
91     friend numeric sqrt(const numeric & x);
92     friend numeric isqrt(const numeric & x);
93     friend numeric gcd(const numeric & a, const numeric & b);
94     friend numeric lcm(const numeric & a, const numeric & b);
95
96 // member functions
97
98     // default constructor, destructor, copy constructor assignment
99     // operator and helpers
100 public:
101     numeric();
102     ~numeric();
103     numeric(const numeric & other);
104     const numeric & operator=(const numeric & other);
105 protected:
106     void copy(const numeric & other);
107     void destroy(bool call_parent);
108
109     // other constructors
110 public:
111     explicit numeric(int i);
112     explicit numeric(unsigned int i);
113     explicit numeric(long i);
114     explicit numeric(unsigned long i);
115     explicit numeric(long numer, long denom);
116     explicit numeric(double d);
117     explicit numeric(char const *);
118     numeric(cl_N const & z);
119
120     // functions overriding virtual functions from bases classes
121 public:
122     basic * duplicate() const;
123     void print(ostream & os, unsigned precedence=0) const;
124     void printraw(ostream & os) const;
125     void printtree(ostream & os, unsigned indent) const;
126     void printcsrc(ostream & os, unsigned type, unsigned precedence=0) const;
127     bool info(unsigned inf) const;
128     ex evalf(int level=0) const;
129     ex diff(symbol const & s) const;
130     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
131     numeric integer_content(void) const;
132     ex smod(const numeric &xi) const;
133     numeric max_coefficient(void) const;
134 protected:
135     int compare_same_type(basic const & other) const;
136     bool is_equal_same_type(basic const & other) const;
137     unsigned calchash(void) const {
138         hashvalue=HASHVALUE_NUMERIC;
139         return HASHVALUE_NUMERIC;
140     }
141
142     // new virtual functions which can be overridden by derived classes
143     // (none)
144
145     // non-virtual functions in this class
146 public:
147     numeric add(const numeric & other) const;
148     numeric sub(const numeric & other) const;
149     numeric mul(const numeric & other) const;
150     numeric div(const numeric & other) const;
151     numeric power(const numeric & other) const;
152     const numeric & add_dyn(const numeric & other) const;
153     const numeric & sub_dyn(const numeric & other) const;
154     const numeric & mul_dyn(const numeric & other) const;
155     const numeric & div_dyn(const numeric & other) const;
156     const numeric & power_dyn(const numeric & other) const;
157     const numeric & operator=(int i);
158     const numeric & operator=(unsigned int i);
159     const numeric & operator=(long i);
160     const numeric & operator=(unsigned long i);
161     const numeric & operator=(double d);
162     const numeric & operator=(char const * s);
163     numeric inverse(void) const;
164     int csgn(void) const;
165     int compare(const numeric & other) const;
166     bool is_equal(const numeric & other) const;
167     bool is_zero(void) const;
168     bool is_positive(void) const;
169     bool is_negative(void) const;
170     bool is_integer(void) const;
171     bool is_pos_integer(void) const;
172     bool is_nonneg_integer(void) const;
173     bool is_even(void) const;
174     bool is_odd(void) const;
175     bool is_prime(void) const;
176     bool is_rational(void) const;
177     bool is_real(void) const;
178     bool is_cinteger(void) const;
179     bool is_crational(void) const;
180     bool operator==(const numeric & other) const;
181     bool operator!=(const numeric & other) const;
182     bool operator<(const numeric & other) const;
183     bool operator<=(const numeric & other) const;
184     bool operator>(const numeric & other) const;
185     bool operator>=(const numeric & other) const;
186     int to_int(void) const;
187     double to_double(void) const;
188     numeric real(void) const;
189     numeric imag(void) const;
190     numeric numer(void) const;
191     numeric denom(void) const;
192     int int_length(void) const;
193
194 // member variables
195
196 protected:
197     static unsigned precedence;
198     cl_N *value;
199 };
200
201 // global constants
202
203 extern const numeric some_numeric;
204 extern const numeric I;
205 extern type_info const & typeid_numeric;
206 extern _numeric_digits Digits;
207
208 #define is_a_numeric_hash(x) ((x)==HASHVALUE_NUMERIC)
209 // may have to be changed to ((x)>=0x80000000U)
210
211 // global functions
212
213 numeric exp(const numeric & x);
214 numeric log(const numeric & x);
215 numeric sin(const numeric & x);
216 numeric cos(const numeric & x);
217 numeric tan(const numeric & x);
218 numeric asin(const numeric & x);
219 numeric acos(const numeric & x);
220 numeric atan(const numeric & x);
221 numeric atan(const numeric & y, const numeric & x);
222 numeric sinh(const numeric & x);
223 numeric cosh(const numeric & x);
224 numeric tanh(const numeric & x);
225 numeric asinh(const numeric & x);
226 numeric acosh(const numeric & x);
227 numeric atanh(const numeric & x);
228 numeric zeta(const numeric & x);
229 numeric gamma(const numeric & x);
230 numeric psi(const numeric & x);
231 numeric psi(const numeric & n, const numeric & x);
232 numeric factorial(const numeric & n);
233 numeric doublefactorial(const numeric & n);
234 numeric binomial(const numeric & n, const numeric & k);
235 numeric bernoulli(const numeric & n);
236
237 numeric abs(const numeric & x);
238 numeric mod(const numeric & a, const numeric & b);
239 numeric smod(const numeric & a, const numeric & b);
240 numeric irem(const numeric & a, const numeric & b);
241 numeric irem(const numeric & a, const numeric & b, numeric & q);
242 numeric iquo(const numeric & a, const numeric & b);
243 numeric iquo(const numeric & a, const numeric & b, numeric & r);
244 numeric sqrt(const numeric & x);
245 numeric isqrt(const numeric & x);
246
247 numeric gcd(const numeric & a, const numeric & b);
248 numeric lcm(const numeric & a, const numeric & b);
249
250 /** Exception thrown by numeric members to signal failure */
251 struct numeric_fail
252 {
253     int failval;
254     numeric_fail(int n) { failval = n; }
255 };
256
257 // wrapper functions around member functions
258 inline numeric pow(const numeric & x, const numeric & y)
259 { return x.power(y); }
260
261 inline numeric inverse(const numeric & x)
262 { return x.inverse(); }
263
264 inline bool csgn(const numeric & x)
265 { return x.csgn(); }
266
267 inline bool is_zero(const numeric & x)
268 { return x.is_zero(); }
269
270 inline bool is_positive(const numeric & x)
271 { return x.is_positive(); }
272
273 inline bool is_integer(const numeric & x)
274 { return x.is_integer(); }
275
276 inline bool is_pos_integer(const numeric & x)
277 { return x.is_pos_integer(); }
278
279 inline bool is_nonneg_integer(const numeric & x)
280 { return x.is_nonneg_integer(); }
281
282 inline bool is_even(const numeric & x)
283 { return x.is_even(); }
284
285 inline bool is_odd(const numeric & x)
286 { return x.is_odd(); }
287
288 inline bool is_prime(const numeric & x)
289 { return x.is_prime(); }
290
291 inline bool is_rational(const numeric & x)
292 { return x.is_rational(); }
293
294 inline bool is_real(const numeric & x)
295 { return x.is_real(); }
296
297 inline bool is_cinteger(const numeric & x)
298 { return x.is_cinteger(); }
299
300 inline bool is_crational(const numeric & x)
301 { return x.is_crational(); }
302
303 inline numeric real(const numeric & x)
304 { return x.real(); }
305
306 inline numeric imag(const numeric & x)
307 { return x.imag(); }
308
309 inline numeric numer(const numeric & x)
310 { return x.numer(); }
311
312 inline numeric denom(const numeric & x)
313 { return x.denom(); }
314
315 // numeric evaluation functions for class constant objects:
316
317 ex PiEvalf(void);
318 ex EulerGammaEvalf(void);
319 ex CatalanEvalf(void);
320
321 // utility functions
322 inline const numeric &ex_to_numeric(const ex &e)
323 {
324     return static_cast<const numeric &>(*e.bp);
325 }
326
327 #ifndef NO_GINAC_NAMESPACE
328 } // namespace GiNaC
329 #endif // ndef NO_GINAC_NAMESPACE
330
331 #endif // ndef __GINAC_NUMERIC_H__