3 * Makes the interface to the underlying bignum package available. */
6 * GiNaC Copyright (C) 1999 Johannes Gutenberg University Mainz, Germany
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.
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.
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
23 #ifndef __GINAC_NUMERIC_H__
24 #define __GINAC_NUMERIC_H__
27 #include <ginac/basic.h>
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.
33 #ifndef NO_GINAC_NAMESPACE
35 #endif // ndef NO_GINAC_NAMESPACE
37 #define HASHVALUE_NUMERIC 0x80000001U
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. */
53 _numeric_digits& operator=(long prec);
55 void print(ostream & os) const;
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
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 zeta(numeric const & x);
83 friend numeric bernoulli(numeric const & n);
84 friend numeric abs(numeric const & x);
85 friend numeric mod(numeric const & a, numeric const & b);
86 friend numeric smod(numeric const & a, numeric const & b);
87 friend numeric irem(numeric const & a, numeric const & b);
88 friend numeric irem(numeric const & a, numeric const & b, numeric & q);
89 friend numeric iquo(numeric const & a, numeric const & b);
90 friend numeric iquo(numeric const & a, numeric const & b, numeric & r);
91 friend numeric sqrt(numeric const & x);
92 friend numeric isqrt(numeric const & x);
93 friend numeric gcd(numeric const & a, numeric const & b);
94 friend numeric lcm(numeric const & a, numeric const & b);
98 // default constructor, destructor, copy constructor assignment
99 // operator and helpers
103 numeric(numeric const & other);
104 numeric const & operator=(numeric const & other);
106 void copy(numeric const & other);
107 void destroy(bool call_parent);
109 // other constructors
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);
120 // functions overriding virtual functions from bases classes
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(numeric const &xi) const;
133 numeric max_coefficient(void) const;
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;
142 // new virtual functions which can be overridden by derived classes
145 // non-virtual functions in this class
147 numeric add(numeric const & other) const;
148 numeric sub(numeric const & other) const;
149 numeric mul(numeric const & other) const;
150 numeric div(numeric const & other) const;
151 numeric power(numeric const & other) const;
152 numeric const & add_dyn(numeric const & other) const;
153 numeric const & sub_dyn(numeric const & other) const;
154 numeric const & mul_dyn(numeric const & other) const;
155 numeric const & div_dyn(numeric const & other) const;
156 numeric const & power_dyn(numeric const & other) const;
157 numeric const & operator=(int i);
158 numeric const & operator=(unsigned int i);
159 numeric const & operator=(long i);
160 numeric const & operator=(unsigned long i);
161 numeric const & operator=(double d);
162 numeric const & operator=(char const * s);
164 numeric add_dyn(numeric const & other) const { return add(other); }
165 numeric sub_dyn(numeric const & other) const { return sub(other); }
166 numeric mul_dyn(numeric const & other) const { return mul(other); }
167 numeric div_dyn(numeric const & other) const { return div(other); }
168 numeric power_dyn(numeric const & other) const { return power(other); }
170 numeric inverse(void) const;
171 int csgn(void) const;
172 int compare(numeric const & other) const;
173 bool is_equal(numeric const & other) const;
174 bool is_zero(void) const;
175 bool is_positive(void) const;
176 bool is_negative(void) const;
177 bool is_integer(void) const;
178 bool is_pos_integer(void) const;
179 bool is_nonneg_integer(void) const;
180 bool is_even(void) const;
181 bool is_odd(void) const;
182 bool is_prime(void) const;
183 bool is_rational(void) const;
184 bool is_real(void) const;
185 bool is_cinteger(void) const;
186 bool is_crational(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;
204 static unsigned precedence;
210 extern const numeric some_numeric;
211 extern const numeric I;
212 extern type_info const & typeid_numeric;
213 extern _numeric_digits Digits;
215 #define is_a_numeric_hash(x) ((x)==HASHVALUE_NUMERIC)
216 // may have to be changed to ((x)>=0x80000000U)
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 zeta(numeric const & x);
236 numeric gamma(numeric const & x);
237 numeric psi(numeric const & x);
238 numeric psi(numeric const & n, numeric const & x);
239 numeric factorial(numeric const & n);
240 numeric doublefactorial(numeric const & n);
241 numeric binomial(numeric const & n, numeric const & k);
242 numeric bernoulli(numeric const & n);
244 numeric abs(numeric const & x);
245 numeric mod(numeric const & a, numeric const & b);
246 numeric smod(numeric const & a, numeric const & b);
247 numeric irem(numeric const & a, numeric const & b);
248 numeric irem(numeric const & a, numeric const & b, numeric & q);
249 numeric iquo(numeric const & a, numeric const & b);
250 numeric iquo(numeric const & a, numeric const & b, numeric & r);
251 numeric sqrt(numeric const & x);
252 numeric isqrt(numeric const & x);
254 numeric gcd(numeric const & a, numeric const & b);
255 numeric lcm(numeric const & a, numeric const & b);
257 /** Exception thrown by numeric members to signal failure */
261 numeric_fail(int n) { failval = n; }
264 // wrapper functions around member functions
265 inline numeric inverse(numeric const & x)
266 { return x.inverse(); }
268 inline bool csgn(numeric const & x)
271 inline bool is_zero(numeric const & x)
272 { return x.is_zero(); }
274 inline bool is_positive(numeric const & x)
275 { return x.is_positive(); }
277 inline bool is_integer(numeric const & x)
278 { return x.is_integer(); }
280 inline bool is_pos_integer(numeric const & x)
281 { return x.is_pos_integer(); }
283 inline bool is_nonneg_integer(numeric const & x)
284 { return x.is_nonneg_integer(); }
286 inline bool is_even(numeric const & x)
287 { return x.is_even(); }
289 inline bool is_odd(numeric const & x)
290 { return x.is_odd(); }
292 inline bool is_prime(numeric const & x)
293 { return x.is_prime(); }
295 inline bool is_rational(numeric const & x)
296 { return x.is_rational(); }
298 inline bool is_real(numeric const & x)
299 { return x.is_real(); }
301 inline bool is_cinteger(numeric const & x)
302 { return x.is_cinteger(); }
304 inline bool is_crational(numeric const & x)
305 { return x.is_crational(); }
307 inline numeric real(numeric const & x)
310 inline numeric imag(numeric const & x)
313 inline numeric numer(numeric const & x)
314 { return x.numer(); }
316 inline numeric denom(numeric const & x)
317 { return x.denom(); }
319 // numeric evaluation functions for class constant objects:
322 ex EulerGammaEvalf(void);
323 ex CatalanEvalf(void);
326 inline const numeric &ex_to_numeric(const ex &e)
328 return static_cast<const numeric &>(*e.bp);
331 #ifndef NO_GINAC_NAMESPACE
333 #endif // ndef NO_GINAC_NAMESPACE
335 #endif // ndef __GINAC_NUMERIC_H__