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

Contents of /ginac/numeric.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.36 - (hide annotations)
Thu Jul 20 21:12:42 2000 UTC (12 years, 10 months ago) by kreckel
Branch: MAIN
Changes since 1.35: +9 -6 lines
File MIME type: text/plain
- Derivatives are now assembled in a slightly different manner (i.e. they
  'look' different on first sight).  Under certain circumstances this can
  result in a dramatic speedup because it gives hashing a better chance,
  especially when computing higher derivatives.
- Series expansion accepts an additional bool argument now, telling it
  whether branch cuts are to be honored (as Maple does it) or not (as
  Mathematica does it).  It defaults to true (i.e. the Maple way).
- Many series expansions of built-in functions have been reengineered.
- The Dilogarithm (Li2) now has floating point evaluation, derivative and a
  proper series expansion.
- Namespace 'std' cleanly disentangled, as demanded by ISO/EIC 14882-1998(E).
- Some helpers updated from upstream.

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.17 * GiNaC Copyright (C) 1999-2000 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     #include <strstream>
27 cbauer 1.21 #include "basic.h"
28     #include "ex.h"
29 cbauer 1.1
30     class cl_N; // We want to include cln.h only in numeric.cpp in order to
31     // avoid namespace pollution and keep compile-time low.
32    
33 kreckel 1.22 #ifndef NO_NAMESPACE_GINAC
34 cbauer 1.5 namespace GiNaC {
35 kreckel 1.22 #endif // ndef NO_NAMESPACE_GINAC
36 cbauer 1.5
37     #define HASHVALUE_NUMERIC 0x80000001U
38    
39 cbauer 1.1 /** This class is used to instantiate a global object Digits which
40     * behaves just like Maple's Digits. We need an object rather than a
41     * dumber basic type since as a side-effect we let it change
42     * cl_default_float_format when it gets changed. The only other
43     * meaningful thing to do with it is converting it to an unsigned,
44     * for temprary storing its value e.g. The user must not create an
45     * own working object of this class! Since C++ forces us to make the
46     * class definition visible in order to use an object we put in a
47     * flag which prevents other objects of that class to be created. */
48     class _numeric_digits
49     {
50     // member functions
51     public:
52     _numeric_digits();
53     _numeric_digits& operator=(long prec);
54     operator long();
55 kreckel 1.36 void print(std::ostream & os) const;
56 cbauer 1.1 // member variables
57     private:
58     long digits;
59     static bool too_late;
60     };
61    
62     /** This class is a wrapper around CLN-numbers within the GiNaC class
63     * hierarchy. Objects of this type may directly be created by the user.*/
64     class numeric : public basic
65     {
66 cbauer 1.18 GINAC_DECLARE_REGISTERED_CLASS(numeric, basic)
67    
68 cbauer 1.1 // friends
69 kreckel 1.20 friend const numeric exp(const numeric & x);
70     friend const numeric log(const numeric & x);
71     friend const numeric sin(const numeric & x);
72     friend const numeric cos(const numeric & x);
73     friend const numeric tan(const numeric & x);
74     friend const numeric asin(const numeric & x);
75     friend const numeric acos(const numeric & x);
76     friend const numeric atan(const numeric & x);
77     friend const numeric atan(const numeric & y, const numeric & x);
78     friend const numeric sinh(const numeric & x);
79     friend const numeric cosh(const numeric & x);
80     friend const numeric tanh(const numeric & x);
81     friend const numeric asinh(const numeric & x);
82     friend const numeric acosh(const numeric & x);
83     friend const numeric atanh(const numeric & x);
84 kreckel 1.36 friend const numeric Li2(const numeric & x);
85 kreckel 1.20 friend const numeric zeta(const numeric & x);
86 kreckel 1.36 // friend const numeric bernoulli(const numeric & n);
87 kreckel 1.27 friend const numeric fibonacci(const numeric & n);
88 kreckel 1.16 friend numeric abs(const numeric & x);
89     friend numeric mod(const numeric & a, const numeric & b);
90     friend numeric smod(const numeric & a, const numeric & b);
91     friend numeric irem(const numeric & a, const numeric & b);
92     friend numeric irem(const numeric & a, const numeric & b, numeric & q);
93     friend numeric iquo(const numeric & a, const numeric & b);
94     friend numeric iquo(const numeric & a, const numeric & b, numeric & r);
95     friend numeric sqrt(const numeric & x);
96     friend numeric isqrt(const numeric & x);
97     friend numeric gcd(const numeric & a, const numeric & b);
98     friend numeric lcm(const numeric & a, const numeric & b);
99 cbauer 1.1
100     // member functions
101    
102     // default constructor, destructor, copy constructor assignment
103     // operator and helpers
104     public:
105     numeric();
106     ~numeric();
107 kreckel 1.16 numeric(const numeric & other);
108     const numeric & operator=(const numeric & other);
109 cbauer 1.1 protected:
110 kreckel 1.16 void copy(const numeric & other);
111 cbauer 1.1 void destroy(bool call_parent);
112    
113     // other constructors
114     public:
115     explicit numeric(int i);
116     explicit numeric(unsigned int i);
117     explicit numeric(long i);
118     explicit numeric(unsigned long i);
119     explicit numeric(long numer, long denom);
120     explicit numeric(double d);
121 cbauer 1.19 explicit numeric(const char *);
122 kreckel 1.24 numeric(const cl_N & z);
123    
124 cbauer 1.1 // functions overriding virtual functions from bases classes
125     public:
126     basic * duplicate() const;
127 kreckel 1.36 void print(std::ostream & os, unsigned precedence=0) const;
128     void printraw(std::ostream & os) const;
129     void printtree(std::ostream & os, unsigned indent) const;
130     void printcsrc(std::ostream & os, unsigned type, unsigned precedence=0) const;
131 cbauer 1.1 bool info(unsigned inf) const;
132 kreckel 1.25 bool has(const ex & other) const;
133 kreckel 1.22 ex eval(int level=0) const;
134 cbauer 1.1 ex evalf(int level=0) const;
135     ex normal(lst &sym_lst, lst &repl_lst, int level=0) const;
136 cbauer 1.33 ex to_rational(lst &repl_lst) const;
137 cbauer 1.1 numeric integer_content(void) const;
138 kreckel 1.16 ex smod(const numeric &xi) const;
139 cbauer 1.1 numeric max_coefficient(void) const;
140     protected:
141 kreckel 1.23 ex derivative(const symbol & s) const;
142 cbauer 1.19 int compare_same_type(const basic & other) const;
143     bool is_equal_same_type(const basic & other) const;
144 frink 1.26 unsigned calchash(void) const;
145 cbauer 1.1
146     // new virtual functions which can be overridden by derived classes
147     // (none)
148    
149     // non-virtual functions in this class
150     public:
151 kreckel 1.16 numeric add(const numeric & other) const;
152     numeric sub(const numeric & other) const;
153     numeric mul(const numeric & other) const;
154     numeric div(const numeric & other) const;
155     numeric power(const numeric & other) const;
156     const numeric & add_dyn(const numeric & other) const;
157     const numeric & sub_dyn(const numeric & other) const;
158     const numeric & mul_dyn(const numeric & other) const;
159     const numeric & div_dyn(const numeric & other) const;
160     const numeric & power_dyn(const numeric & other) const;
161     const numeric & operator=(int i);
162     const numeric & operator=(unsigned int i);
163     const numeric & operator=(long i);
164     const numeric & operator=(unsigned long i);
165     const numeric & operator=(double d);
166 cbauer 1.19 const numeric & operator=(const char * s);
167 cbauer 1.1 numeric inverse(void) const;
168 kreckel 1.6 int csgn(void) const;
169 kreckel 1.36 ::cl_N* clnptr(void) const { return value; } /**< ptr to representation. */
170 kreckel 1.16 int compare(const numeric & other) const;
171     bool is_equal(const numeric & other) const;
172 cbauer 1.1 bool is_zero(void) const;
173     bool is_positive(void) const;
174     bool is_negative(void) const;
175     bool is_integer(void) const;
176     bool is_pos_integer(void) const;
177     bool is_nonneg_integer(void) const;
178     bool is_even(void) const;
179     bool is_odd(void) const;
180     bool is_prime(void) const;
181     bool is_rational(void) const;
182     bool is_real(void) const;
183 kreckel 1.13 bool is_cinteger(void) const;
184     bool is_crational(void) const;
185 kreckel 1.16 bool operator==(const numeric & other) const;
186     bool operator!=(const numeric & other) const;
187     bool operator<(const numeric & other) const;
188     bool operator<=(const numeric & other) const;
189     bool operator>(const numeric & other) const;
190     bool operator>=(const numeric & other) const;
191 cbauer 1.1 int to_int(void) const;
192 kreckel 1.20 long to_long(void) const;
193 cbauer 1.1 double to_double(void) const;
194 kreckel 1.28 const numeric real(void) const;
195     const numeric imag(void) const;
196     const numeric numer(void) const;
197     const numeric denom(void) const;
198 cbauer 1.1 int int_length(void) const;
199    
200     // member variables
201    
202     protected:
203     static unsigned precedence;
204 kreckel 1.35 ::cl_N *value;
205 cbauer 1.1 };
206    
207     // global constants
208    
209     extern const numeric some_numeric;
210     extern const numeric I;
211 cbauer 1.19 extern const type_info & typeid_numeric;
212 cbauer 1.1 extern _numeric_digits Digits;
213    
214 frink 1.26 //#define is_a_numeric_hash(x) ((x)==HASHVALUE_NUMERIC)
215 cbauer 1.1 // may have to be changed to ((x)>=0x80000000U)
216 frink 1.26
217     // has been changed
218     //#define is_a_numeric_hash(x) ((x)&0x80000000U)
219 cbauer 1.1
220     // global functions
221    
222 kreckel 1.20 const numeric exp(const numeric & x);
223     const numeric log(const numeric & x);
224     const numeric sin(const numeric & x);
225     const numeric cos(const numeric & x);
226     const numeric tan(const numeric & x);
227     const numeric asin(const numeric & x);
228     const numeric acos(const numeric & x);
229     const numeric atan(const numeric & x);
230     const numeric atan(const numeric & y, const numeric & x);
231     const numeric sinh(const numeric & x);
232     const numeric cosh(const numeric & x);
233     const numeric tanh(const numeric & x);
234     const numeric asinh(const numeric & x);
235     const numeric acosh(const numeric & x);
236     const numeric atanh(const numeric & x);
237 kreckel 1.36 const numeric Li2(const numeric & x);
238 kreckel 1.20 const numeric zeta(const numeric & x);
239 kreckel 1.30 const numeric lgamma(const numeric & x);
240     const numeric tgamma(const numeric & x);
241 kreckel 1.20 const numeric psi(const numeric & x);
242     const numeric psi(const numeric & n, const numeric & x);
243     const numeric factorial(const numeric & n);
244     const numeric doublefactorial(const numeric & n);
245     const numeric binomial(const numeric & n, const numeric & k);
246     const numeric bernoulli(const numeric & n);
247     const numeric fibonacci(const numeric & n);
248 kreckel 1.16
249     numeric abs(const numeric & x);
250     numeric mod(const numeric & a, const numeric & b);
251     numeric smod(const numeric & a, const numeric & b);
252     numeric irem(const numeric & a, const numeric & b);
253     numeric irem(const numeric & a, const numeric & b, numeric & q);
254     numeric iquo(const numeric & a, const numeric & b);
255     numeric iquo(const numeric & a, const numeric & b, numeric & r);
256     numeric sqrt(const numeric & x);
257     numeric isqrt(const numeric & x);
258 cbauer 1.1
259 kreckel 1.16 numeric gcd(const numeric & a, const numeric & b);
260     numeric lcm(const numeric & a, const numeric & b);
261 cbauer 1.1
262     // wrapper functions around member functions
263 kreckel 1.16 inline numeric pow(const numeric & x, const numeric & y)
264     { return x.power(y); }
265    
266     inline numeric inverse(const numeric & x)
267 cbauer 1.1 { return x.inverse(); }
268 kreckel 1.6
269 kreckel 1.32 inline int csgn(const numeric & x)
270 kreckel 1.6 { return x.csgn(); }
271 cbauer 1.1
272 kreckel 1.16 inline bool is_zero(const numeric & x)
273 cbauer 1.1 { return x.is_zero(); }
274    
275 kreckel 1.16 inline bool is_positive(const numeric & x)
276 cbauer 1.1 { return x.is_positive(); }
277    
278 kreckel 1.16 inline bool is_integer(const numeric & x)
279 cbauer 1.1 { return x.is_integer(); }
280    
281 kreckel 1.16 inline bool is_pos_integer(const numeric & x)
282 cbauer 1.1 { return x.is_pos_integer(); }
283    
284 kreckel 1.16 inline bool is_nonneg_integer(const numeric & x)
285 cbauer 1.1 { return x.is_nonneg_integer(); }
286    
287 kreckel 1.16 inline bool is_even(const numeric & x)
288 cbauer 1.1 { return x.is_even(); }
289    
290 kreckel 1.16 inline bool is_odd(const numeric & x)
291 cbauer 1.1 { return x.is_odd(); }
292    
293 kreckel 1.16 inline bool is_prime(const numeric & x)
294 cbauer 1.1 { return x.is_prime(); }
295    
296 kreckel 1.16 inline bool is_rational(const numeric & x)
297 cbauer 1.1 { return x.is_rational(); }
298    
299 kreckel 1.16 inline bool is_real(const numeric & x)
300 cbauer 1.1 { return x.is_real(); }
301    
302 kreckel 1.16 inline bool is_cinteger(const numeric & x)
303 kreckel 1.14 { return x.is_cinteger(); }
304    
305 kreckel 1.16 inline bool is_crational(const numeric & x)
306 kreckel 1.14 { return x.is_crational(); }
307    
308 kreckel 1.32 inline const numeric real(const numeric & x)
309 cbauer 1.1 { return x.real(); }
310    
311 kreckel 1.32 inline const numeric imag(const numeric & x)
312 cbauer 1.1 { return x.imag(); }
313    
314 kreckel 1.32 inline const numeric numer(const numeric & x)
315 cbauer 1.1 { return x.numer(); }
316    
317 kreckel 1.32 inline const numeric denom(const numeric & x)
318 cbauer 1.1 { return x.denom(); }
319    
320 kreckel 1.11 // numeric evaluation functions for class constant objects:
321    
322 cbauer 1.1 ex PiEvalf(void);
323 kreckel 1.31 ex EulerEvalf(void);
324 cbauer 1.1 ex CatalanEvalf(void);
325    
326 kreckel 1.23
327 cbauer 1.5 // utility functions
328 kreckel 1.23 inline const numeric &ex_to_numeric(const ex &e)
329     {
330     return static_cast<const numeric &>(*e.bp);
331     }
332    
333 cbauer 1.1
334 kreckel 1.22 #ifndef NO_NAMESPACE_GINAC
335 cbauer 1.5 } // namespace GiNaC
336 kreckel 1.22 #endif // ndef NO_NAMESPACE_GINAC
337 cbauer 1.1
338 cbauer 1.2 #endif // ndef __GINAC_NUMERIC_H__

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