3 * Makes the interface to the underlying bignum package available. */
6 * GiNaC Copyright (C) 1999-2007 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #ifndef __GINAC_NUMERIC_H__
24 #define __GINAC_NUMERIC_H__
32 #include <cln/complex.h>
34 #if defined(G__CINTVERSION) && !defined(__MAKECINT__)
35 // Cint @$#$! doesn't like forward declaring classes used for casting operators
36 // so we have to include the definition of cln::cl_N here, but it is enough to
37 // do so for the compiler, hence the !defined(__MAKECINT__).
38 #include <cln/complex_class.h>
43 /** Function pointer to implement callbacks in the case 'Digits' gets changed.
44 * Main purpose of such callbacks is to adjust look-up tables of certain
45 * functions to the new precision. Parameter contains the signed difference
46 * between new Digits and old Digits. */
47 typedef void (* digits_changed_callback)(long);
49 /** This class is used to instantiate a global singleton object Digits
50 * which behaves just like Maple's Digits. We need an object rather
51 * than a dumber basic type since as a side-effect we let it change
52 * cl_default_float_format when it gets changed. The only other
53 * meaningful thing to do with it is converting it to an unsigned,
54 * for temprary storing its value e.g. The user must not create an
55 * own working object of this class! Since C++ forces us to make the
56 * class definition visible in order to use an object we put in a
57 * flag which prevents other objects of that class to be created. */
63 _numeric_digits& operator=(long prec);
65 void print(std::ostream& os) const;
66 void add_callback(digits_changed_callback callback);
69 long digits; ///< Number of decimal digits
70 static bool too_late; ///< Already one object present
71 // Holds a list of functions that get called when digits is changed.
72 std::vector<digits_changed_callback> callbacklist;
76 /** Exception class thrown when a singularity is encountered. */
77 class pole_error : public std::domain_error {
79 explicit pole_error(const std::string& what_arg, int degree);
86 /** This class is a wrapper around CLN-numbers within the GiNaC class
87 * hierarchy. Objects of this type may directly be created by the user.*/
88 class numeric : public basic
90 GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
97 numeric(unsigned int i);
99 numeric(unsigned long i);
100 numeric(long numer, long denom);
102 numeric(const char *);
104 // functions overriding virtual functions from base classes
106 unsigned precedence() const {return 30;}
107 bool info(unsigned inf) const;
108 bool is_polynomial(const ex & var) const;
109 int degree(const ex & s) const;
110 int ldegree(const ex & s) const;
111 ex coeff(const ex & s, int n = 1) const;
112 bool has(const ex &other, unsigned options = 0) const;
113 ex eval(int level = 0) const;
114 ex evalf(int level = 0) const;
115 ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
116 ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
117 ex to_rational(exmap & repl) const;
118 ex to_polynomial(exmap & repl) const;
119 numeric integer_content() const;
120 ex smod(const numeric &xi) const;
121 numeric max_coefficient() const;
122 ex conjugate() const;
123 ex real_part() const;
124 ex imag_part() const;
126 /** Implementation of ex::diff for a numeric always returns 0.
128 ex derivative(const symbol &s) const { return 0; }
129 bool is_equal_same_type(const basic &other) const;
130 unsigned calchash() const;
132 // new virtual functions which can be overridden by derived classes
135 // non-virtual functions in this class
137 const numeric add(const numeric &other) const;
138 const numeric sub(const numeric &other) const;
139 const numeric mul(const numeric &other) const;
140 const numeric div(const numeric &other) const;
141 const numeric power(const numeric &other) const;
142 const numeric & add_dyn(const numeric &other) const;
143 const numeric & sub_dyn(const numeric &other) const;
144 const numeric & mul_dyn(const numeric &other) const;
145 const numeric & div_dyn(const numeric &other) const;
146 const numeric & power_dyn(const numeric &other) const;
147 const numeric & operator=(int i);
148 const numeric & operator=(unsigned int i);
149 const numeric & operator=(long i);
150 const numeric & operator=(unsigned long i);
151 const numeric & operator=(double d);
152 const numeric & operator=(const char *s);
153 const numeric inverse() const;
154 numeric step() const;
156 int compare(const numeric &other) const;
157 bool is_equal(const numeric &other) const;
158 bool is_zero() const;
159 bool is_positive() const;
160 bool is_negative() const;
161 bool is_integer() const;
162 bool is_pos_integer() const;
163 bool is_nonneg_integer() const;
164 bool is_even() const;
166 bool is_prime() const;
167 bool is_rational() const;
168 bool is_real() const;
169 bool is_cinteger() const;
170 bool is_crational() const;
171 bool operator==(const numeric &other) const;
172 bool operator!=(const numeric &other) const;
173 bool operator<(const numeric &other) const;
174 bool operator<=(const numeric &other) const;
175 bool operator>(const numeric &other) const;
176 bool operator>=(const numeric &other) const;
178 long to_long() const;
179 double to_double() const;
180 cln::cl_N to_cl_N() const;
181 const numeric real() const;
182 const numeric imag() const;
183 const numeric numer() const;
184 const numeric denom() const;
185 int int_length() const;
186 // converting routines for interfacing with CLN:
187 numeric(const cln::cl_N &z);
190 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;
191 void do_print(const print_context & c, unsigned level) const;
192 void do_print_latex(const print_latex & c, unsigned level) const;
193 void do_print_csrc(const print_csrc & c, unsigned level) const;
194 void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
195 void do_print_tree(const print_tree & c, unsigned level) const;
196 void do_print_python_repr(const print_python_repr & c, unsigned level) const;
207 extern const numeric I;
208 extern _numeric_digits Digits;
212 const numeric exp(const numeric &x);
213 const numeric log(const numeric &x);
214 const numeric sin(const numeric &x);
215 const numeric cos(const numeric &x);
216 const numeric tan(const numeric &x);
217 const numeric asin(const numeric &x);
218 const numeric acos(const numeric &x);
219 const numeric atan(const numeric &x);
220 const numeric atan(const numeric &y, const numeric &x);
221 const numeric sinh(const numeric &x);
222 const numeric cosh(const numeric &x);
223 const numeric tanh(const numeric &x);
224 const numeric asinh(const numeric &x);
225 const numeric acosh(const numeric &x);
226 const numeric atanh(const numeric &x);
227 const numeric Li2(const numeric &x);
228 const numeric zeta(const numeric &x);
229 const numeric lgamma(const numeric &x);
230 const numeric tgamma(const numeric &x);
231 const numeric psi(const numeric &x);
232 const numeric psi(const numeric &n, const numeric &x);
233 const numeric factorial(const numeric &n);
234 const numeric doublefactorial(const numeric &n);
235 const numeric binomial(const numeric &n, const numeric &k);
236 const numeric bernoulli(const numeric &n);
237 const numeric fibonacci(const numeric &n);
238 const numeric isqrt(const numeric &x);
239 const numeric sqrt(const numeric &x);
240 const numeric abs(const numeric &x);
241 const numeric mod(const numeric &a, const numeric &b);
242 const numeric smod(const numeric &a, const numeric &b);
243 const numeric irem(const numeric &a, const numeric &b);
244 const numeric irem(const numeric &a, const numeric &b, numeric &q);
245 const numeric iquo(const numeric &a, const numeric &b);
246 const numeric iquo(const numeric &a, const numeric &b, numeric &r);
247 const numeric gcd(const numeric &a, const numeric &b);
248 const numeric lcm(const numeric &a, const numeric &b);
250 // wrapper functions around member functions
251 inline const numeric pow(const numeric &x, const numeric &y)
252 { return x.power(y); }
254 inline const numeric inverse(const numeric &x)
255 { return x.inverse(); }
257 inline numeric step(const numeric &x)
260 inline int csgn(const numeric &x)
263 inline bool is_zero(const numeric &x)
264 { return x.is_zero(); }
266 inline bool is_positive(const numeric &x)
267 { return x.is_positive(); }
269 inline bool is_integer(const numeric &x)
270 { return x.is_integer(); }
272 inline bool is_pos_integer(const numeric &x)
273 { return x.is_pos_integer(); }
275 inline bool is_nonneg_integer(const numeric &x)
276 { return x.is_nonneg_integer(); }
278 inline bool is_even(const numeric &x)
279 { return x.is_even(); }
281 inline bool is_odd(const numeric &x)
282 { return x.is_odd(); }
284 inline bool is_prime(const numeric &x)
285 { return x.is_prime(); }
287 inline bool is_rational(const numeric &x)
288 { return x.is_rational(); }
290 inline bool is_real(const numeric &x)
291 { return x.is_real(); }
293 inline bool is_cinteger(const numeric &x)
294 { return x.is_cinteger(); }
296 inline bool is_crational(const numeric &x)
297 { return x.is_crational(); }
299 inline int to_int(const numeric &x)
300 { return x.to_int(); }
302 inline long to_long(const numeric &x)
303 { return x.to_long(); }
305 inline double to_double(const numeric &x)
306 { return x.to_double(); }
308 inline const numeric real(const numeric &x)
311 inline const numeric imag(const numeric &x)
314 inline const numeric numer(const numeric &x)
315 { return x.numer(); }
317 inline const numeric denom(const numeric &x)
318 { return x.denom(); }
320 // numeric evaluation functions for class constant objects:
330 #pragma link off defined_in cln/number.h;
331 #pragma link off defined_in cln/complex_class.h;
334 #endif // ndef __GINAC_NUMERIC_H__