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

Contents of /ginac/numeric.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.61 - (hide annotations)
Fri Jan 25 15:33:07 2002 UTC (11 years, 4 months ago) by cbauer
Branch: MAIN
CVS Tags: release_1-0-8, release_1-0-9, release_1-0-10, release_1-0-12, release_1-0-11, release_1-0-6, release_1-0-7
Changes since 1.60: +3 -0 lines
File MIME type: text/plain
- (l)degree(s), coeff(s, n) and collect(s) were extended to accept expressions
  of any class (except add/mul/ncmul/numeric) for "s". They should even work
  if "s" is a "power" object, as long as the exponent is non-integer, but with
  some limitations. For example, you can "collect(a*2^x+b*2^x, 2^x)" to get
  "(a+b)*2^x", but "degree(2^(3*x), 2^x)" yields 0 instead of 3).

1 cbauer 1.1 /** @file numeric.h
2     *
3 cbauer 1.4 * Makes the interface to the underlying bignum package available. */
4    
5     /*
6 kreckel 1.60 * GiNaC Copyright (C) 1999-2002 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     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21     */
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 kreckel 1.41 #include <cln/number.h>
30     // forward decln of cln::cl_N, since cln/complex_class.h is not included:
31     namespace cln { class cl_N; }
32    
33     #if defined(G__CINTVERSION) && !defined(__MAKECINT__)
34     // Cint @$#$! doesn't like forward declaring classes used for casting operators
35     // so we have to include the definition of cln::cl_N here, but it is enough to
36     // do so for the compiler, hence the !defined(__MAKECINT__).
37     #include <cln/complex_class.h>
38     #endif
39 cbauer 1.1
40 cbauer 1.5 namespace GiNaC {
41    
42 kreckel 1.48 /** This class is used to instantiate a global singleton object Digits
43     * which behaves just like Maple's Digits. We need an object rather
44     * than a dumber basic type since as a side-effect we let it change
45 cbauer 1.1 * cl_default_float_format when it gets changed. The only other
46     * meaningful thing to do with it is converting it to an unsigned,
47     * for temprary storing its value e.g. The user must not create an
48     * own working object of this class! Since C++ forces us to make the
49     * class definition visible in order to use an object we put in a
50     * flag which prevents other objects of that class to be created. */
51     class _numeric_digits
52     {
53     // member functions
54     public:
55 cbauer 1.38 _numeric_digits();
56     _numeric_digits& operator=(long prec);
57     operator long();
58 kreckel 1.48 void print(std::ostream &os) const;
59 cbauer 1.1 // member variables
60     private:
61 kreckel 1.48 long digits; ///< Number of decimal digits
62     static bool too_late; ///< Already one object present
63 cbauer 1.1 };
64    
65     /** This class is a wrapper around CLN-numbers within the GiNaC class
66     * hierarchy. Objects of this type may directly be created by the user.*/
67     class numeric : public basic
68     {
69 cbauer 1.38 GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
70 kreckel 1.48
71 cbauer 1.1 // member functions
72 kreckel 1.48
73     // other ctors
74 cbauer 1.1 public:
75 kreckel 1.56 numeric(int i);
76     numeric(unsigned int i);
77     numeric(long i);
78     numeric(unsigned long i);
79     numeric(long numer, long denom);
80     numeric(double d);
81     numeric(const char *);
82 cbauer 1.38
83 cbauer 1.57 // functions overriding virtual functions from base classes
84 cbauer 1.1 public:
85 cbauer 1.49 void print(const print_context & c, unsigned level = 0) const;
86 cbauer 1.51 unsigned precedence(void) const {return 30;}
87 cbauer 1.38 bool info(unsigned inf) const;
88 cbauer 1.61 int degree(const ex & s) const;
89     int ldegree(const ex & s) const;
90     ex coeff(const ex & s, int n = 1) const;
91 kreckel 1.48 bool has(const ex &other) const;
92 kreckel 1.47 ex eval(int level = 0) const;
93     ex evalf(int level = 0) const;
94 kreckel 1.48 ex normal(lst &sym_lst, lst &repl_lst, int level = 0) const;
95 cbauer 1.38 ex to_rational(lst &repl_lst) const;
96     numeric integer_content(void) const;
97     ex smod(const numeric &xi) const;
98     numeric max_coefficient(void) const;
99 cbauer 1.1 protected:
100 kreckel 1.48 /** Implementation of ex::diff for a numeric always returns 0.
101     * @see ex::diff */
102 kreckel 1.59 ex derivative(const symbol &s) const { return 0; }
103 kreckel 1.48 bool is_equal_same_type(const basic &other) const;
104 cbauer 1.38 unsigned calchash(void) const;
105 kreckel 1.48
106 cbauer 1.38 // new virtual functions which can be overridden by derived classes
107     // (none)
108 kreckel 1.48
109 cbauer 1.38 // non-virtual functions in this class
110 cbauer 1.1 public:
111 kreckel 1.48 const numeric add(const numeric &other) const;
112     const numeric sub(const numeric &other) const;
113     const numeric mul(const numeric &other) const;
114     const numeric div(const numeric &other) const;
115     const numeric power(const numeric &other) const;
116     const numeric & add_dyn(const numeric &other) const;
117     const numeric & sub_dyn(const numeric &other) const;
118     const numeric & mul_dyn(const numeric &other) const;
119     const numeric & div_dyn(const numeric &other) const;
120     const numeric & power_dyn(const numeric &other) const;
121 cbauer 1.38 const numeric & operator=(int i);
122     const numeric & operator=(unsigned int i);
123     const numeric & operator=(long i);
124     const numeric & operator=(unsigned long i);
125     const numeric & operator=(double d);
126 kreckel 1.48 const numeric & operator=(const char *s);
127 kreckel 1.41 const numeric inverse(void) const;
128 cbauer 1.38 int csgn(void) const;
129 kreckel 1.48 int compare(const numeric &other) const;
130     bool is_equal(const numeric &other) const;
131 cbauer 1.38 bool is_zero(void) const;
132     bool is_positive(void) const;
133     bool is_negative(void) const;
134     bool is_integer(void) const;
135     bool is_pos_integer(void) const;
136     bool is_nonneg_integer(void) const;
137     bool is_even(void) const;
138     bool is_odd(void) const;
139     bool is_prime(void) const;
140     bool is_rational(void) const;
141     bool is_real(void) const;
142     bool is_cinteger(void) const;
143     bool is_crational(void) const;
144 kreckel 1.48 bool operator==(const numeric &other) const;
145     bool operator!=(const numeric &other) const;
146     bool operator<(const numeric &other) const;
147     bool operator<=(const numeric &other) const;
148     bool operator>(const numeric &other) const;
149     bool operator>=(const numeric &other) const;
150 cbauer 1.38 int to_int(void) const;
151     long to_long(void) const;
152     double to_double(void) const;
153 kreckel 1.42 cln::cl_N to_cl_N(void) const;
154 cbauer 1.38 const numeric real(void) const;
155     const numeric imag(void) const;
156     const numeric numer(void) const;
157     const numeric denom(void) const;
158     int int_length(void) const;
159 kreckel 1.41 // converting routines for interfacing with CLN:
160 kreckel 1.48 numeric(const cln::cl_N &z);
161 cbauer 1.1
162     // member variables
163    
164     protected:
165 kreckel 1.41 cln::cl_number value;
166 cbauer 1.1 };
167    
168     // global constants
169    
170     extern const numeric I;
171     extern _numeric_digits Digits;
172    
173 kreckel 1.48 // deprecated macro, for internal use only
174     #define is_a_numeric_hash(x) ((x)&0x80000000U)
175 cbauer 1.1
176     // global functions
177    
178 kreckel 1.48 const numeric exp(const numeric &x);
179     const numeric log(const numeric &x);
180     const numeric sin(const numeric &x);
181     const numeric cos(const numeric &x);
182     const numeric tan(const numeric &x);
183     const numeric asin(const numeric &x);
184     const numeric acos(const numeric &x);
185     const numeric atan(const numeric &x);
186     const numeric atan(const numeric &y, const numeric &x);
187     const numeric sinh(const numeric &x);
188     const numeric cosh(const numeric &x);
189     const numeric tanh(const numeric &x);
190     const numeric asinh(const numeric &x);
191     const numeric acosh(const numeric &x);
192     const numeric atanh(const numeric &x);
193     const numeric Li2(const numeric &x);
194     const numeric zeta(const numeric &x);
195     const numeric lgamma(const numeric &x);
196     const numeric tgamma(const numeric &x);
197     const numeric psi(const numeric &x);
198     const numeric psi(const numeric &n, const numeric &x);
199     const numeric factorial(const numeric &n);
200     const numeric doublefactorial(const numeric &n);
201     const numeric binomial(const numeric &n, const numeric &k);
202     const numeric bernoulli(const numeric &n);
203     const numeric fibonacci(const numeric &n);
204     const numeric isqrt(const numeric &x);
205     const numeric sqrt(const numeric &x);
206     const numeric abs(const numeric &x);
207     const numeric mod(const numeric &a, const numeric &b);
208     const numeric smod(const numeric &a, const numeric &b);
209     const numeric irem(const numeric &a, const numeric &b);
210     const numeric irem(const numeric &a, const numeric &b, numeric &q);
211     const numeric iquo(const numeric &a, const numeric &b);
212     const numeric iquo(const numeric &a, const numeric &b, numeric &r);
213     const numeric gcd(const numeric &a, const numeric &b);
214     const numeric lcm(const numeric &a, const numeric &b);
215 cbauer 1.1
216     // wrapper functions around member functions
217 kreckel 1.48 inline const numeric pow(const numeric &x, const numeric &y)
218 kreckel 1.16 { return x.power(y); }
219    
220 kreckel 1.48 inline const numeric inverse(const numeric &x)
221 cbauer 1.1 { return x.inverse(); }
222 kreckel 1.6
223 kreckel 1.48 inline int csgn(const numeric &x)
224 kreckel 1.6 { return x.csgn(); }
225 cbauer 1.1
226 kreckel 1.48 inline bool is_zero(const numeric &x)
227 cbauer 1.1 { return x.is_zero(); }
228    
229 kreckel 1.48 inline bool is_positive(const numeric &x)
230 cbauer 1.1 { return x.is_positive(); }
231    
232 kreckel 1.48 inline bool is_integer(const numeric &x)
233 cbauer 1.1 { return x.is_integer(); }
234    
235 kreckel 1.48 inline bool is_pos_integer(const numeric &x)
236 cbauer 1.1 { return x.is_pos_integer(); }
237    
238 kreckel 1.48 inline bool is_nonneg_integer(const numeric &x)
239 cbauer 1.1 { return x.is_nonneg_integer(); }
240    
241 kreckel 1.48 inline bool is_even(const numeric &x)
242 cbauer 1.1 { return x.is_even(); }
243    
244 kreckel 1.48 inline bool is_odd(const numeric &x)
245 cbauer 1.1 { return x.is_odd(); }
246    
247 kreckel 1.48 inline bool is_prime(const numeric &x)
248 cbauer 1.1 { return x.is_prime(); }
249    
250 kreckel 1.48 inline bool is_rational(const numeric &x)
251 cbauer 1.1 { return x.is_rational(); }
252    
253 kreckel 1.48 inline bool is_real(const numeric &x)
254 cbauer 1.1 { return x.is_real(); }
255    
256 kreckel 1.48 inline bool is_cinteger(const numeric &x)
257 kreckel 1.14 { return x.is_cinteger(); }
258    
259 kreckel 1.48 inline bool is_crational(const numeric &x)
260 kreckel 1.14 { return x.is_crational(); }
261 kreckel 1.42
262 kreckel 1.48 inline int to_int(const numeric &x)
263 kreckel 1.42 { return x.to_int(); }
264    
265 kreckel 1.48 inline long to_long(const numeric &x)
266 kreckel 1.42 { return x.to_long(); }
267    
268 kreckel 1.48 inline double to_double(const numeric &x)
269 kreckel 1.42 { return x.to_double(); }
270 kreckel 1.14
271 kreckel 1.48 inline const numeric real(const numeric &x)
272 cbauer 1.1 { return x.real(); }
273    
274 kreckel 1.48 inline const numeric imag(const numeric &x)
275 cbauer 1.1 { return x.imag(); }
276    
277 kreckel 1.48 inline const numeric numer(const numeric &x)
278 cbauer 1.1 { return x.numer(); }
279    
280 kreckel 1.48 inline const numeric denom(const numeric &x)
281 cbauer 1.1 { return x.denom(); }
282    
283 kreckel 1.11 // numeric evaluation functions for class constant objects:
284    
285 cbauer 1.1 ex PiEvalf(void);
286 kreckel 1.31 ex EulerEvalf(void);
287 cbauer 1.1 ex CatalanEvalf(void);
288    
289 kreckel 1.23
290 cbauer 1.5 // utility functions
291 kreckel 1.54
292     /** Specialization of is_exactly_a<numeric>(obj) for numeric objects. */
293     template<> inline bool is_exactly_a<numeric>(const basic & obj)
294     {
295     return obj.tinfo()==TINFO_numeric;
296 kreckel 1.23 }
297    
298 cbauer 1.5 } // namespace GiNaC
299 kreckel 1.41
300     #ifdef __MAKECINT__
301     #pragma link off defined_in cln/number.h;
302     #pragma link off defined_in cln/complex_class.h;
303     #endif
304 cbauer 1.1
305 cbauer 1.2 #endif // ndef __GINAC_NUMERIC_H__

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