3 * Makes the interface to the underlying bignum package available. */
6 * GiNaC Copyright (C) 1999-2003 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__
31 #include <cln/number.h>
32 // forward decln of cln::cl_N, since cln/complex_class.h is not included:
33 namespace cln { class cl_N; }
35 #if defined(G__CINTVERSION) && !defined(__MAKECINT__)
36 // Cint @$#$! doesn't like forward declaring classes used for casting operators
37 // so we have to include the definition of cln::cl_N here, but it is enough to
38 // do so for the compiler, hence the !defined(__MAKECINT__).
39 #include <cln/complex_class.h>
44 /** This class is used to instantiate a global singleton object Digits
45 * which behaves just like Maple's Digits. We need an object rather
46 * than a dumber basic type since as a side-effect we let it change
47 * cl_default_float_format when it gets changed. The only other
48 * meaningful thing to do with it is converting it to an unsigned,
49 * for temprary storing its value e.g. The user must not create an
50 * own working object of this class! Since C++ forces us to make the
51 * class definition visible in order to use an object we put in a
52 * flag which prevents other objects of that class to be created. */
58 _numeric_digits& operator=(long prec);
60 void print(std::ostream &os) const;
63 long digits; ///< Number of decimal digits
64 static bool too_late; ///< Already one object present
68 /** Exception class thrown when a singularity is encountered. */
69 class pole_error : public std::domain_error {
71 explicit pole_error(const std::string& what_arg, int degree);
78 /** This class is a wrapper around CLN-numbers within the GiNaC class
79 * hierarchy. Objects of this type may directly be created by the user.*/
80 class numeric : public basic
82 GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
89 numeric(unsigned int i);
91 numeric(unsigned long i);
92 numeric(long numer, long denom);
94 numeric(const char *);
96 // functions overriding virtual functions from base classes
98 void print(const print_context & c, unsigned level = 0) const;
99 unsigned precedence() const {return 30;}
100 bool info(unsigned inf) const;
101 int degree(const ex & s) const;
102 int ldegree(const ex & s) const;
103 ex coeff(const ex & s, int n = 1) const;
104 bool has(const ex &other) const;
105 ex eval(int level = 0) const;
106 ex evalf(int level = 0) const;
107 ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
108 ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
109 ex to_rational(lst &repl_lst) const;
110 ex to_polynomial(lst &repl_lst) const;
111 numeric integer_content() const;
112 ex smod(const numeric &xi) const;
113 numeric max_coefficient() const;
115 /** Implementation of ex::diff for a numeric always returns 0.
117 ex derivative(const symbol &s) const { return 0; }
118 bool is_equal_same_type(const basic &other) const;
119 unsigned calchash() const;
121 // new virtual functions which can be overridden by derived classes
124 // non-virtual functions in this class
126 const numeric add(const numeric &other) const;
127 const numeric sub(const numeric &other) const;
128 const numeric mul(const numeric &other) const;
129 const numeric div(const numeric &other) const;
130 const numeric power(const numeric &other) const;
131 const numeric & add_dyn(const numeric &other) const;
132 const numeric & sub_dyn(const numeric &other) const;
133 const numeric & mul_dyn(const numeric &other) const;
134 const numeric & div_dyn(const numeric &other) const;
135 const numeric & power_dyn(const numeric &other) const;
136 const numeric & operator=(int i);
137 const numeric & operator=(unsigned int i);
138 const numeric & operator=(long i);
139 const numeric & operator=(unsigned long i);
140 const numeric & operator=(double d);
141 const numeric & operator=(const char *s);
142 const numeric inverse() const;
144 int compare(const numeric &other) const;
145 bool is_equal(const numeric &other) const;
146 bool is_zero() const;
147 bool is_positive() const;
148 bool is_negative() const;
149 bool is_integer() const;
150 bool is_pos_integer() const;
151 bool is_nonneg_integer() const;
152 bool is_even() const;
154 bool is_prime() const;
155 bool is_rational() const;
156 bool is_real() const;
157 bool is_cinteger() const;
158 bool is_crational() const;
159 bool operator==(const numeric &other) const;
160 bool operator!=(const numeric &other) const;
161 bool operator<(const numeric &other) const;
162 bool operator<=(const numeric &other) const;
163 bool operator>(const numeric &other) const;
164 bool operator>=(const numeric &other) const;
166 long to_long() const;
167 double to_double() const;
168 cln::cl_N to_cl_N() const;
169 const numeric real() const;
170 const numeric imag() const;
171 const numeric numer() const;
172 const numeric denom() const;
173 int int_length() const;
174 // converting routines for interfacing with CLN:
175 numeric(const cln::cl_N &z);
180 cln::cl_number value;
186 extern const numeric I;
187 extern _numeric_digits Digits;
191 const numeric exp(const numeric &x);
192 const numeric log(const numeric &x);
193 const numeric sin(const numeric &x);
194 const numeric cos(const numeric &x);
195 const numeric tan(const numeric &x);
196 const numeric asin(const numeric &x);
197 const numeric acos(const numeric &x);
198 const numeric atan(const numeric &x);
199 const numeric atan(const numeric &y, const numeric &x);
200 const numeric sinh(const numeric &x);
201 const numeric cosh(const numeric &x);
202 const numeric tanh(const numeric &x);
203 const numeric asinh(const numeric &x);
204 const numeric acosh(const numeric &x);
205 const numeric atanh(const numeric &x);
206 const numeric Li2(const numeric &x);
207 const numeric zeta(const numeric &x);
208 const numeric lgamma(const numeric &x);
209 const numeric tgamma(const numeric &x);
210 const numeric psi(const numeric &x);
211 const numeric psi(const numeric &n, const numeric &x);
212 const numeric factorial(const numeric &n);
213 const numeric doublefactorial(const numeric &n);
214 const numeric binomial(const numeric &n, const numeric &k);
215 const numeric bernoulli(const numeric &n);
216 const numeric fibonacci(const numeric &n);
217 const numeric isqrt(const numeric &x);
218 const numeric sqrt(const numeric &x);
219 const numeric abs(const numeric &x);
220 const numeric mod(const numeric &a, const numeric &b);
221 const numeric smod(const numeric &a, const numeric &b);
222 const numeric irem(const numeric &a, const numeric &b);
223 const numeric irem(const numeric &a, const numeric &b, numeric &q);
224 const numeric iquo(const numeric &a, const numeric &b);
225 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
226 const numeric gcd(const numeric &a, const numeric &b);
227 const numeric lcm(const numeric &a, const numeric &b);
229 // wrapper functions around member functions
230 inline const numeric pow(const numeric &x, const numeric &y)
231 { return x.power(y); }
233 inline const numeric inverse(const numeric &x)
234 { return x.inverse(); }
236 inline int csgn(const numeric &x)
239 inline bool is_zero(const numeric &x)
240 { return x.is_zero(); }
242 inline bool is_positive(const numeric &x)
243 { return x.is_positive(); }
245 inline bool is_integer(const numeric &x)
246 { return x.is_integer(); }
248 inline bool is_pos_integer(const numeric &x)
249 { return x.is_pos_integer(); }
251 inline bool is_nonneg_integer(const numeric &x)
252 { return x.is_nonneg_integer(); }
254 inline bool is_even(const numeric &x)
255 { return x.is_even(); }
257 inline bool is_odd(const numeric &x)
258 { return x.is_odd(); }
260 inline bool is_prime(const numeric &x)
261 { return x.is_prime(); }
263 inline bool is_rational(const numeric &x)
264 { return x.is_rational(); }
266 inline bool is_real(const numeric &x)
267 { return x.is_real(); }
269 inline bool is_cinteger(const numeric &x)
270 { return x.is_cinteger(); }
272 inline bool is_crational(const numeric &x)
273 { return x.is_crational(); }
275 inline int to_int(const numeric &x)
276 { return x.to_int(); }
278 inline long to_long(const numeric &x)
279 { return x.to_long(); }
281 inline double to_double(const numeric &x)
282 { return x.to_double(); }
284 inline const numeric real(const numeric &x)
287 inline const numeric imag(const numeric &x)
290 inline const numeric numer(const numeric &x)
291 { return x.numer(); }
293 inline const numeric denom(const numeric &x)
294 { return x.denom(); }
296 // numeric evaluation functions for class constant objects:
305 /** Specialization of is_exactly_a<numeric>(obj) for numeric objects. */
306 template<> inline bool is_exactly_a<numeric>(const basic & obj)
308 return obj.tinfo()==TINFO_numeric;
314 #pragma link off defined_in cln/number.h;
315 #pragma link off defined_in cln/complex_class.h;
318 #endif // ndef __GINAC_NUMERIC_H__