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 unsigned precedence() const {return 30;}
99 bool info(unsigned inf) const;
100 int degree(const ex & s) const;
101 int ldegree(const ex & s) const;
102 ex coeff(const ex & s, int n = 1) const;
103 bool has(const ex &other) const;
104 ex eval(int level = 0) const;
105 ex evalf(int level = 0) const;
106 ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
107 ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
108 ex to_rational(exmap & repl) const;
109 ex to_polynomial(exmap & repl) const;
110 numeric integer_content() const;
111 ex smod(const numeric &xi) const;
112 numeric max_coefficient() const;
114 /** Implementation of ex::diff for a numeric always returns 0.
116 ex derivative(const symbol &s) const { return 0; }
117 bool is_equal_same_type(const basic &other) const;
118 unsigned calchash() const;
120 // new virtual functions which can be overridden by derived classes
123 // non-virtual functions in this class
125 const numeric add(const numeric &other) const;
126 const numeric sub(const numeric &other) const;
127 const numeric mul(const numeric &other) const;
128 const numeric div(const numeric &other) const;
129 const numeric power(const numeric &other) const;
130 const numeric & add_dyn(const numeric &other) const;
131 const numeric & sub_dyn(const numeric &other) const;
132 const numeric & mul_dyn(const numeric &other) const;
133 const numeric & div_dyn(const numeric &other) const;
134 const numeric & power_dyn(const numeric &other) const;
135 const numeric & operator=(int i);
136 const numeric & operator=(unsigned int i);
137 const numeric & operator=(long i);
138 const numeric & operator=(unsigned long i);
139 const numeric & operator=(double d);
140 const numeric & operator=(const char *s);
141 const numeric inverse() const;
143 int compare(const numeric &other) const;
144 bool is_equal(const numeric &other) const;
145 bool is_zero() const;
146 bool is_positive() const;
147 bool is_negative() const;
148 bool is_integer() const;
149 bool is_pos_integer() const;
150 bool is_nonneg_integer() const;
151 bool is_even() const;
153 bool is_prime() const;
154 bool is_rational() const;
155 bool is_real() const;
156 bool is_cinteger() const;
157 bool is_crational() const;
158 bool operator==(const numeric &other) 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;
165 long to_long() const;
166 double to_double() const;
167 cln::cl_N to_cl_N() const;
168 const numeric real() const;
169 const numeric imag() const;
170 const numeric numer() const;
171 const numeric denom() const;
172 int int_length() const;
173 // converting routines for interfacing with CLN:
174 numeric(const cln::cl_N &z);
177 void print_numeric(const print_context & c, const char *par_open, const char *par_close, const char *imag_sym, const char *mul_sym, unsigned level) const;
178 void do_print(const print_context & c, unsigned level) const;
179 void do_print_latex(const print_latex & c, unsigned level) const;
180 void do_print_csrc(const print_csrc & c, unsigned level) const;
181 void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
182 void do_print_tree(const print_tree & c, unsigned level) const;
183 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
188 cln::cl_number value;
194 extern const numeric I;
195 extern _numeric_digits Digits;
199 const numeric exp(const numeric &x);
200 const numeric log(const numeric &x);
201 const numeric sin(const numeric &x);
202 const numeric cos(const numeric &x);
203 const numeric tan(const numeric &x);
204 const numeric asin(const numeric &x);
205 const numeric acos(const numeric &x);
206 const numeric atan(const numeric &x);
207 const numeric atan(const numeric &y, const numeric &x);
208 const numeric sinh(const numeric &x);
209 const numeric cosh(const numeric &x);
210 const numeric tanh(const numeric &x);
211 const numeric asinh(const numeric &x);
212 const numeric acosh(const numeric &x);
213 const numeric atanh(const numeric &x);
214 const numeric Li2(const numeric &x);
215 const numeric zeta(const numeric &x);
216 const numeric lgamma(const numeric &x);
217 const numeric tgamma(const numeric &x);
218 const numeric psi(const numeric &x);
219 const numeric psi(const numeric &n, const numeric &x);
220 const numeric factorial(const numeric &n);
221 const numeric doublefactorial(const numeric &n);
222 const numeric binomial(const numeric &n, const numeric &k);
223 const numeric bernoulli(const numeric &n);
224 const numeric fibonacci(const numeric &n);
225 const numeric isqrt(const numeric &x);
226 const numeric sqrt(const numeric &x);
227 const numeric abs(const numeric &x);
228 const numeric mod(const numeric &a, const numeric &b);
229 const numeric smod(const numeric &a, const numeric &b);
230 const numeric irem(const numeric &a, const numeric &b);
231 const numeric irem(const numeric &a, const numeric &b, numeric &q);
232 const numeric iquo(const numeric &a, const numeric &b);
233 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
234 const numeric gcd(const numeric &a, const numeric &b);
235 const numeric lcm(const numeric &a, const numeric &b);
237 // wrapper functions around member functions
238 inline const numeric pow(const numeric &x, const numeric &y)
239 { return x.power(y); }
241 inline const numeric inverse(const numeric &x)
242 { return x.inverse(); }
244 inline int csgn(const numeric &x)
247 inline bool is_zero(const numeric &x)
248 { return x.is_zero(); }
250 inline bool is_positive(const numeric &x)
251 { return x.is_positive(); }
253 inline bool is_integer(const numeric &x)
254 { return x.is_integer(); }
256 inline bool is_pos_integer(const numeric &x)
257 { return x.is_pos_integer(); }
259 inline bool is_nonneg_integer(const numeric &x)
260 { return x.is_nonneg_integer(); }
262 inline bool is_even(const numeric &x)
263 { return x.is_even(); }
265 inline bool is_odd(const numeric &x)
266 { return x.is_odd(); }
268 inline bool is_prime(const numeric &x)
269 { return x.is_prime(); }
271 inline bool is_rational(const numeric &x)
272 { return x.is_rational(); }
274 inline bool is_real(const numeric &x)
275 { return x.is_real(); }
277 inline bool is_cinteger(const numeric &x)
278 { return x.is_cinteger(); }
280 inline bool is_crational(const numeric &x)
281 { return x.is_crational(); }
283 inline int to_int(const numeric &x)
284 { return x.to_int(); }
286 inline long to_long(const numeric &x)
287 { return x.to_long(); }
289 inline double to_double(const numeric &x)
290 { return x.to_double(); }
292 inline const numeric real(const numeric &x)
295 inline const numeric imag(const numeric &x)
298 inline const numeric numer(const numeric &x)
299 { return x.numer(); }
301 inline const numeric denom(const numeric &x)
302 { return x.denom(); }
304 // numeric evaluation functions for class constant objects:
313 /** Specialization of is_exactly_a<numeric>(obj) for numeric objects. */
314 template<> inline bool is_exactly_a<numeric>(const basic & obj)
316 return obj.tinfo()==TINFO_numeric;
322 #pragma link off defined_in cln/number.h;
323 #pragma link off defined_in cln/complex_class.h;
326 #endif // ndef __GINAC_NUMERIC_H__