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