/[GiNaC]/ginac/numeric.h
ViewVC logotype

Contents of /ginac/numeric.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.13 - (show annotations)
Fri Dec 10 17:23:57 1999 UTC (13 years, 5 months ago) by kreckel
Branch: MAIN
Changes since 1.12: +2 -0 lines
File MIME type: text/plain
- changed behaviour of numeric::is_rational() and added numeric::is_cinteger()
  and numeric::is_crational() which work for complex numbers now
- added default ctor for constant to please CInt
- documentation reflects the changes for upcoming 0.4.1

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

Christian Bauer">Christian Bauer
ViewVC Help
Powered by ViewVC 1.1.15