1 | #ifndef MPR_COMPLEX_H |
---|
2 | #define MPR_COMPLEX_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: mpr_complex.h,v 1.4 1999-06-24 07:46:51 wenk Exp $ */ |
---|
7 | |
---|
8 | /* |
---|
9 | * ABSTRACT - multipolynomial resultants - real floating-point numbers using gmp |
---|
10 | * and complex numbers based on pairs of real floating-point numbers |
---|
11 | * |
---|
12 | */ |
---|
13 | |
---|
14 | //-> include & define stuff |
---|
15 | // must have gmp version >= 2 |
---|
16 | extern "C" { |
---|
17 | #include <gmp.h> |
---|
18 | } |
---|
19 | #include "numbers.h" |
---|
20 | #include "ring.h" |
---|
21 | #include "mpr_global.h" |
---|
22 | |
---|
23 | #define DEFPREC 20 // minimum number of digits (output operations) |
---|
24 | |
---|
25 | #define GMP_DEFAULT_PREC_BITS 512 // size of mantissa of floating-point number |
---|
26 | #define GMP_NEEDEQUAL_BITS 512-64 // a == b for the first gmp_equalupto_bits bits |
---|
27 | //<- |
---|
28 | |
---|
29 | void setGMPFloatPrecBytes( unsigned long int bytes ); |
---|
30 | unsigned long int getGMPFloatPrecBytes(); |
---|
31 | |
---|
32 | void setGMPFloatDigits( size_t digits ); |
---|
33 | size_t getGMPFloatDigits(); |
---|
34 | |
---|
35 | //-> class gmp_float |
---|
36 | /** |
---|
37 | * @short wrapper class for GNU Multi Precision Floats |
---|
38 | */ |
---|
39 | class gmp_float |
---|
40 | { |
---|
41 | public: |
---|
42 | gmp_float( const int v = 0 ) |
---|
43 | { |
---|
44 | mpf_init2( t, gmp_default_prec_bits ); |
---|
45 | mpf_set_si( t, (signed long int) v ); |
---|
46 | } |
---|
47 | gmp_float( const long v ) |
---|
48 | { |
---|
49 | mpf_init2( t, gmp_default_prec_bits ); |
---|
50 | mpf_set_si( t, (signed long int) v ); |
---|
51 | } |
---|
52 | gmp_float( const mprfloat v ) // double |
---|
53 | { |
---|
54 | mpf_init2( t, gmp_default_prec_bits ); |
---|
55 | mpf_set_d( t, (double) v ); |
---|
56 | } |
---|
57 | gmp_float( const mpf_t v ) |
---|
58 | { |
---|
59 | mpf_init2( t, gmp_default_prec_bits ); |
---|
60 | mpf_set( t, v ); |
---|
61 | } |
---|
62 | gmp_float( const mpz_t v ) // gnu mp Z |
---|
63 | { |
---|
64 | mpf_init2( t, gmp_default_prec_bits ); |
---|
65 | mpf_set_z( t, v ); |
---|
66 | } |
---|
67 | gmp_float( const gmp_float & v ) // copy constructor |
---|
68 | { |
---|
69 | //mpf_init2( t, mpf_get_prec( v.t ) ); |
---|
70 | mpf_init2( t, gmp_default_prec_bits ); |
---|
71 | mpf_set( t, v.t ); |
---|
72 | } |
---|
73 | |
---|
74 | ~gmp_float() |
---|
75 | { |
---|
76 | mpf_clear( t ); |
---|
77 | } |
---|
78 | |
---|
79 | friend gmp_float operator + ( const gmp_float & a, const gmp_float & b ); |
---|
80 | friend gmp_float operator - ( const gmp_float & a, const gmp_float & b ); |
---|
81 | friend gmp_float operator * ( const gmp_float & a, const gmp_float & b ); |
---|
82 | friend gmp_float operator / ( const gmp_float & a, const gmp_float & b ); |
---|
83 | |
---|
84 | inline gmp_float & operator += ( const gmp_float & a ); |
---|
85 | inline gmp_float & operator -= ( const gmp_float & a ); |
---|
86 | inline gmp_float & operator *= ( const gmp_float & a ); |
---|
87 | inline gmp_float & operator /= ( const gmp_float & a ); |
---|
88 | |
---|
89 | friend bool operator == ( const gmp_float & a, const gmp_float & b ); |
---|
90 | friend bool operator > ( const gmp_float & a, const gmp_float & b ); |
---|
91 | friend bool operator < ( const gmp_float & a, const gmp_float & b ); |
---|
92 | friend bool operator >= ( const gmp_float & a, const gmp_float & b ); |
---|
93 | friend bool operator <= ( const gmp_float & a, const gmp_float & b ); |
---|
94 | |
---|
95 | friend gmp_float operator - ( const gmp_float & a ); |
---|
96 | |
---|
97 | gmp_float & operator = ( const gmp_float & a ); |
---|
98 | gmp_float & operator = ( const mpz_t & a ); |
---|
99 | gmp_float & operator = ( const mprfloat a ); |
---|
100 | gmp_float & operator = ( const long a ); |
---|
101 | |
---|
102 | inline int sign(); // t>0:+1, t==0:0, t<0:-1 |
---|
103 | inline bool isZero(); // t == 0 ? |
---|
104 | inline bool isOne(); // t == 1 ? |
---|
105 | inline bool isMOne(); // t == -1 ? |
---|
106 | |
---|
107 | inline bool setFromStr( char * in ); |
---|
108 | |
---|
109 | // access |
---|
110 | inline const mpf_t *mpfp() const; |
---|
111 | |
---|
112 | inline operator double(); |
---|
113 | inline operator double() const; |
---|
114 | |
---|
115 | inline operator int(); |
---|
116 | inline operator int() const; |
---|
117 | |
---|
118 | public: |
---|
119 | static void setPrecision( const unsigned long int prec ) |
---|
120 | { gmp_default_prec_bits= prec; } |
---|
121 | static void setEqualBits( const unsigned long int prec ) |
---|
122 | { gmp_needequal_bits= prec; } |
---|
123 | |
---|
124 | static const unsigned long int getPrecision() |
---|
125 | { return gmp_default_prec_bits; } |
---|
126 | static const unsigned long int getEqualBits() |
---|
127 | { return gmp_needequal_bits; } |
---|
128 | |
---|
129 | private: |
---|
130 | static unsigned long int gmp_default_prec_bits; |
---|
131 | static unsigned long int gmp_needequal_bits; |
---|
132 | |
---|
133 | mpf_t t; |
---|
134 | }; |
---|
135 | |
---|
136 | static const gmp_float gmpOne= 1; |
---|
137 | static const gmp_float gmpMOne= -1; |
---|
138 | static const gmp_float gmpZero= 0; |
---|
139 | |
---|
140 | // <gmp_float> operator <gmp_float> |
---|
141 | inline gmp_float & gmp_float::operator += ( const gmp_float & a ) |
---|
142 | { |
---|
143 | mpf_add( t, t, a.t ); |
---|
144 | return *this; |
---|
145 | } |
---|
146 | inline gmp_float & gmp_float::operator -= ( const gmp_float & a ) |
---|
147 | { |
---|
148 | mpf_sub( t, t, a.t ); |
---|
149 | return *this; |
---|
150 | } |
---|
151 | inline gmp_float & gmp_float::operator *= ( const gmp_float & a ) |
---|
152 | { |
---|
153 | mpf_mul( t, t, a.t ); |
---|
154 | return *this; |
---|
155 | } |
---|
156 | inline gmp_float & gmp_float::operator /= ( const gmp_float & a ) |
---|
157 | { |
---|
158 | mpf_div( t, t, a.t ); |
---|
159 | return *this; |
---|
160 | } |
---|
161 | |
---|
162 | // <gmp_float> = <*> |
---|
163 | inline gmp_float & gmp_float::operator = ( const gmp_float & a ) |
---|
164 | { |
---|
165 | mpf_set( t, a.t ); |
---|
166 | return *this; |
---|
167 | } |
---|
168 | inline gmp_float & gmp_float::operator = ( const mpz_t & a ) |
---|
169 | { |
---|
170 | mpf_set_z( t, a ); |
---|
171 | return *this; |
---|
172 | } |
---|
173 | inline gmp_float & gmp_float::operator = ( const mprfloat a ) |
---|
174 | { |
---|
175 | mpf_set_d( t, (double) a ); |
---|
176 | return *this; |
---|
177 | } |
---|
178 | inline gmp_float & gmp_float::operator = ( const long a ) |
---|
179 | { |
---|
180 | mpf_set_si( t, (signed long int) a ); |
---|
181 | return *this; |
---|
182 | } |
---|
183 | |
---|
184 | // cast to double |
---|
185 | inline gmp_float::operator double() |
---|
186 | { |
---|
187 | return mpf_get_d( t ); |
---|
188 | } |
---|
189 | inline gmp_float::operator double() const |
---|
190 | { |
---|
191 | return mpf_get_d( t ); |
---|
192 | } |
---|
193 | |
---|
194 | // cast to int |
---|
195 | inline gmp_float::operator int() |
---|
196 | { |
---|
197 | return (int)mpf_get_d( t ); |
---|
198 | } |
---|
199 | inline gmp_float::operator int() const |
---|
200 | { |
---|
201 | return (int)mpf_get_d( t ); |
---|
202 | } |
---|
203 | |
---|
204 | // get sign of real number ( -1: t < 0; 0: t==0; 1: t > 0 ) |
---|
205 | inline int gmp_float::sign() |
---|
206 | { |
---|
207 | return mpf_sgn( t ); |
---|
208 | } |
---|
209 | // t == 0 ? |
---|
210 | inline bool gmp_float::isZero() |
---|
211 | { |
---|
212 | #ifdef VARIANTE_1 |
---|
213 | return (mpf_sgn( t ) == 0); |
---|
214 | #else |
---|
215 | return mpf_eq( t , gmpZero.t , gmp_float::gmp_needequal_bits ); |
---|
216 | #endif |
---|
217 | } |
---|
218 | // t == 1 ? |
---|
219 | inline bool gmp_float::isOne() |
---|
220 | { |
---|
221 | #ifdef VARIANTE_1 |
---|
222 | return (mpf_cmp_ui( t , 1 ) == 0); |
---|
223 | #else |
---|
224 | return mpf_eq( t , gmpOne.t , gmp_float::gmp_needequal_bits ); |
---|
225 | #endif |
---|
226 | } |
---|
227 | // t == -1 ? |
---|
228 | inline bool gmp_float::isMOne() |
---|
229 | { |
---|
230 | #ifdef VARIANTE_1 |
---|
231 | return (mpf_cmp_si( t , -1 ) == 0); |
---|
232 | #else |
---|
233 | return mpf_eq( t , gmpMOne.t , gmp_float::gmp_needequal_bits ); |
---|
234 | #endif |
---|
235 | } |
---|
236 | |
---|
237 | inline bool gmp_float::setFromStr( char * in ) |
---|
238 | { |
---|
239 | return ( mpf_set_str( t, in, 10 ) == 0 ); |
---|
240 | } |
---|
241 | |
---|
242 | // access pointer |
---|
243 | inline const mpf_t *gmp_float::mpfp() const |
---|
244 | { |
---|
245 | return &t; |
---|
246 | } |
---|
247 | |
---|
248 | // built-in functions of GMP |
---|
249 | gmp_float abs( const gmp_float & ); |
---|
250 | gmp_float sqrt( const gmp_float & ); |
---|
251 | gmp_float hypot( const gmp_float &, const gmp_float & ); |
---|
252 | |
---|
253 | // simulated functions using double functions |
---|
254 | gmp_float sin( const gmp_float & ); |
---|
255 | gmp_float cos( const gmp_float & ); |
---|
256 | gmp_float log( const gmp_float & ); |
---|
257 | gmp_float exp( const gmp_float & ); |
---|
258 | |
---|
259 | gmp_float max( const gmp_float &, const gmp_float & ); |
---|
260 | |
---|
261 | gmp_float numberToFloat( number num ); |
---|
262 | char *floatToStr( const gmp_float & r, const unsigned int oprec ); |
---|
263 | //<- |
---|
264 | |
---|
265 | //-> class gmp_complex |
---|
266 | /** |
---|
267 | * @short gmp_complex numbers based on |
---|
268 | */ |
---|
269 | class gmp_complex |
---|
270 | { |
---|
271 | private: |
---|
272 | gmp_float r, i; |
---|
273 | |
---|
274 | public: |
---|
275 | gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 ) |
---|
276 | { |
---|
277 | r= re; |
---|
278 | i= im; |
---|
279 | } |
---|
280 | gmp_complex( const mprfloat re, const mprfloat im = 0.0 ) |
---|
281 | { |
---|
282 | r= re; |
---|
283 | i= im; |
---|
284 | } |
---|
285 | gmp_complex( const long re, const long im ) |
---|
286 | { |
---|
287 | r= re; |
---|
288 | i= im; |
---|
289 | } |
---|
290 | gmp_complex( const gmp_complex & v ) |
---|
291 | { |
---|
292 | r= v.r; |
---|
293 | i= v.i; |
---|
294 | } |
---|
295 | ~gmp_complex() {} |
---|
296 | |
---|
297 | friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b ); |
---|
298 | friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b ); |
---|
299 | friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b ); |
---|
300 | friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b ); |
---|
301 | |
---|
302 | // gmp_complex <operator> real |
---|
303 | inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ); |
---|
304 | inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ); |
---|
305 | inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ); |
---|
306 | inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ); |
---|
307 | |
---|
308 | gmp_complex & operator += ( const gmp_complex & a ); |
---|
309 | gmp_complex & operator -= ( const gmp_complex & a ); |
---|
310 | gmp_complex & operator *= ( const gmp_complex & a ); |
---|
311 | gmp_complex & operator /= ( const gmp_complex & a ); |
---|
312 | |
---|
313 | inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b ); |
---|
314 | |
---|
315 | inline gmp_complex & operator = ( const gmp_complex & a ); |
---|
316 | |
---|
317 | // access to real and imaginary part |
---|
318 | inline gmp_float real() const { return r; } |
---|
319 | inline gmp_float imag() const { return i; } |
---|
320 | |
---|
321 | inline void real( gmp_float val ) { r = val; } |
---|
322 | inline void imag( gmp_float val ) { i = val; } |
---|
323 | }; |
---|
324 | |
---|
325 | // <gmp_complex> = <gmp_complex> operator <gmp_float> |
---|
326 | // |
---|
327 | inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ) |
---|
328 | { |
---|
329 | return gmp_complex( a.r + b_d, a.i ); |
---|
330 | } |
---|
331 | inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ) |
---|
332 | { |
---|
333 | return gmp_complex( a.r - b_d, a.i ); |
---|
334 | } |
---|
335 | inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ) |
---|
336 | { |
---|
337 | return gmp_complex( a.r * b_d, a.i * b_d ); |
---|
338 | } |
---|
339 | inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ) |
---|
340 | { |
---|
341 | return gmp_complex( a.r / b_d, a.i / b_d ); |
---|
342 | } |
---|
343 | |
---|
344 | // <gmp_complex> == <gmp_complex> ? |
---|
345 | inline bool operator == ( const gmp_complex & a, const gmp_complex & b ) |
---|
346 | { |
---|
347 | return ( b.real() == a.real() ) && ( b.imag() == a.imag() ); |
---|
348 | } |
---|
349 | |
---|
350 | // <gmp_complex> = <gmp_complex> |
---|
351 | inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a ) |
---|
352 | { |
---|
353 | r= a.r; |
---|
354 | i= a.i; |
---|
355 | return *this; |
---|
356 | } |
---|
357 | |
---|
358 | // Returns absolute value of a gmp_complex number |
---|
359 | // |
---|
360 | inline gmp_float abs( const gmp_complex & c ) |
---|
361 | { |
---|
362 | return hypot(c.real(),c.imag()); |
---|
363 | } |
---|
364 | |
---|
365 | gmp_complex sqrt( const gmp_complex & x ); |
---|
366 | |
---|
367 | inline gmp_complex numberToGmp_Complex( number num ) |
---|
368 | { |
---|
369 | if (rField_is_long_C()) { |
---|
370 | return *(gmp_complex*)num; |
---|
371 | } else { |
---|
372 | return gmp_complex( numberToFloat(num) ); |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | char *complexToStr( const gmp_complex & c, const unsigned int oprec ); |
---|
377 | //<- |
---|
378 | |
---|
379 | #endif MPR_COMPLEX_H |
---|
380 | |
---|
381 | // local Variables: *** |
---|
382 | // folded-file: t *** |
---|
383 | // compile-command-1: "make installg" *** |
---|
384 | // compile-command-2: "make install" *** |
---|
385 | // End: *** |
---|
386 | |
---|
387 | |
---|
388 | |
---|
389 | |
---|
390 | |
---|