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

Contents of /ginac/numeric.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.77 - (hide annotations)
Mon Mar 27 15:13:22 2006 UTC (7 years, 1 month ago) by chrisd
Branch: MAIN
Branch point for: experimental_fclasses
Changes since 1.76: +1 -0 lines
File MIME type: text/plain
Added .is_polynomial() method.

1 cbauer 1.1 /** @file numeric.h
2     *
3 cbauer 1.4 * Makes the interface to the underlying bignum package available. */
4    
5     /*
6 vollinga 1.72 * GiNaC Copyright (C) 1999-2006 Johannes Gutenberg University Mainz, Germany
7 cbauer 1.2 *
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 kreckel 1.70 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 cbauer 1.2 */
22 cbauer 1.1
23 cbauer 1.2 #ifndef __GINAC_NUMERIC_H__
24     #define __GINAC_NUMERIC_H__
25 cbauer 1.1
26 cbauer 1.21 #include "basic.h"
27     #include "ex.h"
28 cbauer 1.1
29 cbauer 1.63 #include <stdexcept>
30 vollinga 1.71 #include <vector>
31 cbauer 1.63
32 vollinga 1.68 #include <cln/complex.h>
33 kreckel 1.41
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>
39     #endif
40 cbauer 1.1
41 cbauer 1.5 namespace GiNaC {
42    
43 vollinga 1.71 /** 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);
48    
49 kreckel 1.48 /** 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 cbauer 1.1 * 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. */
58     class _numeric_digits
59     {
60     // member functions
61     public:
62 cbauer 1.38 _numeric_digits();
63     _numeric_digits& operator=(long prec);
64     operator long();
65 vollinga 1.71 void print(std::ostream& os) const;
66     void add_callback(digits_changed_callback callback);
67 cbauer 1.1 // member variables
68     private:
69 kreckel 1.48 long digits; ///< Number of decimal digits
70     static bool too_late; ///< Already one object present
71 vollinga 1.71 // Holds a list of functions that get called when digits is changed.
72     std::vector<digits_changed_callback> callbacklist;
73 cbauer 1.1 };
74    
75 cbauer 1.63
76     /** Exception class thrown when a singularity is encountered. */
77     class pole_error : public std::domain_error {
78     public:
79     explicit pole_error(const std::string& what_arg, int degree);
80     int degree() const;
81     private:
82     int deg;
83     };
84    
85    
86 cbauer 1.1 /** 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
89     {
90 cbauer 1.38 GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
91 kreckel 1.48
92 cbauer 1.1 // member functions
93 kreckel 1.48
94 cbauer 1.63 // other constructors
95 cbauer 1.1 public:
96 kreckel 1.56 numeric(int i);
97     numeric(unsigned int i);
98     numeric(long i);
99     numeric(unsigned long i);
100     numeric(long numer, long denom);
101     numeric(double d);
102     numeric(const char *);
103 cbauer 1.38
104 cbauer 1.57 // functions overriding virtual functions from base classes
105 cbauer 1.1 public:
106 cbauer 1.63 unsigned precedence() const {return 30;}
107 cbauer 1.38 bool info(unsigned inf) const;
108 chrisd 1.77 bool is_polynomial(const ex & var) const;
109 cbauer 1.61 int degree(const ex & s) const;
110     int ldegree(const ex & s) const;
111     ex coeff(const ex & s, int n = 1) const;
112 chrisd 1.73 bool has(const ex &other, unsigned options = 0) const;
113 kreckel 1.47 ex eval(int level = 0) const;
114     ex evalf(int level = 0) const;
115 cbauer 1.64 ex subs(const exmap & m, unsigned options = 0) const { return subs_one_level(m, options); } // overwrites basic::subs() for performance reasons
116 cbauer 1.65 ex normal(exmap & repl, exmap & rev_lookup, int level = 0) const;
117 cbauer 1.67 ex to_rational(exmap & repl) const;
118     ex to_polynomial(exmap & repl) const;
119 cbauer 1.63 numeric integer_content() const;
120 cbauer 1.38 ex smod(const numeric &xi) const;
121 cbauer 1.63 numeric max_coefficient() const;
122 vollinga 1.68 ex conjugate() const;
123 cbauer 1.1 protected:
124 kreckel 1.48 /** Implementation of ex::diff for a numeric always returns 0.
125     * @see ex::diff */
126 kreckel 1.59 ex derivative(const symbol &s) const { return 0; }
127 kreckel 1.48 bool is_equal_same_type(const basic &other) const;
128 cbauer 1.63 unsigned calchash() const;
129 kreckel 1.48
130 cbauer 1.38 // new virtual functions which can be overridden by derived classes
131     // (none)
132 kreckel 1.48
133 cbauer 1.38 // non-virtual functions in this class
134 cbauer 1.1 public:
135 kreckel 1.48 const numeric add(const numeric &other) const;
136     const numeric sub(const numeric &other) const;
137     const numeric mul(const numeric &other) const;
138     const numeric div(const numeric &other) const;
139     const numeric power(const numeric &other) const;
140     const numeric & add_dyn(const numeric &other) const;
141     const numeric & sub_dyn(const numeric &other) const;
142     const numeric & mul_dyn(const numeric &other) const;
143     const numeric & div_dyn(const numeric &other) const;
144     const numeric & power_dyn(const numeric &other) const;
145 cbauer 1.38 const numeric & operator=(int i);
146     const numeric & operator=(unsigned int i);
147     const numeric & operator=(long i);
148     const numeric & operator=(unsigned long i);
149     const numeric & operator=(double d);
150 kreckel 1.48 const numeric & operator=(const char *s);
151 cbauer 1.63 const numeric inverse() const;
152 chrisd 1.76 numeric step() const;
153 cbauer 1.63 int csgn() const;
154 kreckel 1.48 int compare(const numeric &other) const;
155     bool is_equal(const numeric &other) const;
156 cbauer 1.63 bool is_zero() const;
157     bool is_positive() const;
158     bool is_negative() const;
159     bool is_integer() const;
160     bool is_pos_integer() const;
161     bool is_nonneg_integer() const;
162     bool is_even() const;
163     bool is_odd() const;
164     bool is_prime() const;
165     bool is_rational() const;
166     bool is_real() const;
167     bool is_cinteger() const;
168     bool is_crational() const;
169 kreckel 1.48 bool operator==(const numeric &other) const;
170     bool operator!=(const numeric &other) 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 cbauer 1.63 int to_int() const;
176     long to_long() const;
177     double to_double() const;
178     cln::cl_N to_cl_N() const;
179     const numeric real() const;
180     const numeric imag() const;
181     const numeric numer() const;
182     const numeric denom() const;
183     int int_length() const;
184 kreckel 1.41 // converting routines for interfacing with CLN:
185 kreckel 1.48 numeric(const cln::cl_N &z);
186 cbauer 1.66
187     protected:
188     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;
189     void do_print(const print_context & c, unsigned level) const;
190     void do_print_latex(const print_latex & c, unsigned level) const;
191     void do_print_csrc(const print_csrc & c, unsigned level) const;
192     void do_print_csrc_cl_N(const print_csrc_cl_N & c, unsigned level) const;
193     void do_print_tree(const print_tree & c, unsigned level) const;
194     void do_print_python_repr(const print_python_repr & c, unsigned level) const;
195 cbauer 1.1
196     // member variables
197    
198     protected:
199 vollinga 1.68 cln::cl_N value;
200 cbauer 1.1 };
201    
202 cbauer 1.63
203 cbauer 1.1 // global constants
204    
205     extern const numeric I;
206     extern _numeric_digits Digits;
207    
208     // global functions
209    
210 kreckel 1.48 const numeric exp(const numeric &x);
211     const numeric log(const numeric &x);
212     const numeric sin(const numeric &x);
213     const numeric cos(const numeric &x);
214     const numeric tan(const numeric &x);
215     const numeric asin(const numeric &x);
216     const numeric acos(const numeric &x);
217     const numeric atan(const numeric &x);
218     const numeric atan(const numeric &y, const numeric &x);
219     const numeric sinh(const numeric &x);
220     const numeric cosh(const numeric &x);
221     const numeric tanh(const numeric &x);
222     const numeric asinh(const numeric &x);
223     const numeric acosh(const numeric &x);
224     const numeric atanh(const numeric &x);
225     const numeric Li2(const numeric &x);
226     const numeric zeta(const numeric &x);
227     const numeric lgamma(const numeric &x);
228     const numeric tgamma(const numeric &x);
229     const numeric psi(const numeric &x);
230     const numeric psi(const numeric &n, const numeric &x);
231     const numeric factorial(const numeric &n);
232     const numeric doublefactorial(const numeric &n);
233     const numeric binomial(const numeric &n, const numeric &k);
234     const numeric bernoulli(const numeric &n);
235     const numeric fibonacci(const numeric &n);
236     const numeric isqrt(const numeric &x);
237     const numeric sqrt(const numeric &x);
238     const numeric abs(const numeric &x);
239     const numeric mod(const numeric &a, const numeric &b);
240     const numeric smod(const numeric &a, const numeric &b);
241     const numeric irem(const numeric &a, const numeric &b);
242     const numeric irem(const numeric &a, const numeric &b, numeric &q);
243     const numeric iquo(const numeric &a, const numeric &b);
244     const numeric iquo(const numeric &a, const numeric &b, numeric &r);
245     const numeric gcd(const numeric &a, const numeric &b);
246     const numeric lcm(const numeric &a, const numeric &b);
247 cbauer 1.1
248     // wrapper functions around member functions
249 kreckel 1.48 inline const numeric pow(const numeric &x, const numeric &y)
250 kreckel 1.16 { return x.power(y); }
251    
252 kreckel 1.48 inline const numeric inverse(const numeric &x)
253 cbauer 1.1 { return x.inverse(); }
254 kreckel 1.6
255 chrisd 1.76 inline numeric step(const numeric &x)
256 chrisd 1.74 { return x.step(); }
257    
258 kreckel 1.48 inline int csgn(const numeric &x)
259 kreckel 1.6 { return x.csgn(); }
260 cbauer 1.1
261 kreckel 1.48 inline bool is_zero(const numeric &x)
262 cbauer 1.1 { return x.is_zero(); }
263    
264 kreckel 1.48 inline bool is_positive(const numeric &x)
265 cbauer 1.1 { return x.is_positive(); }
266    
267 kreckel 1.48 inline bool is_integer(const numeric &x)
268 cbauer 1.1 { return x.is_integer(); }
269    
270 kreckel 1.48 inline bool is_pos_integer(const numeric &x)
271 cbauer 1.1 { return x.is_pos_integer(); }
272    
273 kreckel 1.48 inline bool is_nonneg_integer(const numeric &x)
274 cbauer 1.1 { return x.is_nonneg_integer(); }
275    
276 kreckel 1.48 inline bool is_even(const numeric &x)
277 cbauer 1.1 { return x.is_even(); }
278    
279 kreckel 1.48 inline bool is_odd(const numeric &x)
280 cbauer 1.1 { return x.is_odd(); }
281    
282 kreckel 1.48 inline bool is_prime(const numeric &x)
283 cbauer 1.1 { return x.is_prime(); }
284    
285 kreckel 1.48 inline bool is_rational(const numeric &x)
286 cbauer 1.1 { return x.is_rational(); }
287    
288 kreckel 1.48 inline bool is_real(const numeric &x)
289 cbauer 1.1 { return x.is_real(); }
290    
291 kreckel 1.48 inline bool is_cinteger(const numeric &x)
292 kreckel 1.14 { return x.is_cinteger(); }
293    
294 kreckel 1.48 inline bool is_crational(const numeric &x)
295 kreckel 1.14 { return x.is_crational(); }
296 kreckel 1.42
297 kreckel 1.48 inline int to_int(const numeric &x)
298 kreckel 1.42 { return x.to_int(); }
299    
300 kreckel 1.48 inline long to_long(const numeric &x)
301 kreckel 1.42 { return x.to_long(); }
302    
303 kreckel 1.48 inline double to_double(const numeric &x)
304 kreckel 1.42 { return x.to_double(); }
305 kreckel 1.14
306 kreckel 1.48 inline const numeric real(const numeric &x)
307 cbauer 1.1 { return x.real(); }
308    
309 kreckel 1.48 inline const numeric imag(const numeric &x)
310 cbauer 1.1 { return x.imag(); }
311    
312 kreckel 1.48 inline const numeric numer(const numeric &x)
313 cbauer 1.1 { return x.numer(); }
314    
315 kreckel 1.48 inline const numeric denom(const numeric &x)
316 cbauer 1.1 { return x.denom(); }
317    
318 kreckel 1.11 // numeric evaluation functions for class constant objects:
319    
320 cbauer 1.63 ex PiEvalf();
321     ex EulerEvalf();
322     ex CatalanEvalf();
323 cbauer 1.1
324 kreckel 1.23
325 cbauer 1.5 } // namespace GiNaC
326 kreckel 1.41
327     #ifdef __MAKECINT__
328     #pragma link off defined_in cln/number.h;
329     #pragma link off defined_in cln/complex_class.h;
330     #endif
331 cbauer 1.1
332 cbauer 1.2 #endif // ndef __GINAC_NUMERIC_H__

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