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