1 | #ifndef MPR_COMPLEX_H |
---|
2 | #define MPR_COMPLEX_H |
---|
3 | /**************************************** |
---|
4 | * Computer Algebra System SINGULAR * |
---|
5 | ****************************************/ |
---|
6 | /* $Id: mpr_complex.h,v 1.18 2001-10-09 16:36:10 Singular 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 ZTOF 1 |
---|
24 | #define QTOF 2 |
---|
25 | #define RTOF 3 |
---|
26 | #define CTOF 4 |
---|
27 | |
---|
28 | void setGMPFloatDigits( size_t digits, size_t rest ); |
---|
29 | size_t getGMPFloatDigits(); |
---|
30 | |
---|
31 | //-> class gmp_float |
---|
32 | /** |
---|
33 | * @short wrapper class for GNU Multi Precision Floats |
---|
34 | */ |
---|
35 | class gmp_float |
---|
36 | { |
---|
37 | public: |
---|
38 | gmp_float( const int v = 0 ) |
---|
39 | { |
---|
40 | mpf_init_set_d( t, (double) v ); |
---|
41 | } |
---|
42 | gmp_float( const long v ) |
---|
43 | { |
---|
44 | mpf_init_set_d( t, (double) v ); |
---|
45 | } |
---|
46 | gmp_float( const mprfloat v ) // double |
---|
47 | { |
---|
48 | mpf_init_set_d( t, (double) v ); |
---|
49 | } |
---|
50 | gmp_float( const mpf_t v ) |
---|
51 | { |
---|
52 | mpf_init_set( t, v ); |
---|
53 | } |
---|
54 | gmp_float( const mpz_t v ) // gnu mp Z |
---|
55 | { |
---|
56 | mpf_init( t ); |
---|
57 | mpf_set_z( t, v ); |
---|
58 | } |
---|
59 | gmp_float( const gmp_float & v ) // copy constructor |
---|
60 | { |
---|
61 | mpf_init_set( t, v.t ); |
---|
62 | } |
---|
63 | |
---|
64 | ~gmp_float() |
---|
65 | { |
---|
66 | mpf_clear( t ); |
---|
67 | } |
---|
68 | |
---|
69 | friend gmp_float operator + ( const gmp_float & a, const gmp_float & b ); |
---|
70 | friend gmp_float operator - ( const gmp_float & a, const gmp_float & b ); |
---|
71 | friend gmp_float operator * ( const gmp_float & a, const gmp_float & b ); |
---|
72 | friend gmp_float operator / ( const gmp_float & a, const gmp_float & b ); |
---|
73 | |
---|
74 | gmp_float & operator += ( const gmp_float & a ); |
---|
75 | gmp_float & operator -= ( const gmp_float & a ); |
---|
76 | inline gmp_float & operator *= ( const gmp_float & a ); |
---|
77 | inline gmp_float & operator /= ( const gmp_float & a ); |
---|
78 | |
---|
79 | friend bool operator == ( const gmp_float & a, const gmp_float & b ); |
---|
80 | friend bool operator > ( const gmp_float & a, const gmp_float & b ); |
---|
81 | friend bool operator < ( const gmp_float & a, const gmp_float & b ); |
---|
82 | friend bool operator >= ( const gmp_float & a, const gmp_float & b ); |
---|
83 | friend bool operator <= ( const gmp_float & a, const gmp_float & b ); |
---|
84 | |
---|
85 | friend gmp_float operator - ( const gmp_float & a ); |
---|
86 | |
---|
87 | gmp_float & operator = ( const gmp_float & a ); |
---|
88 | gmp_float & operator = ( const mpz_t & a ); |
---|
89 | gmp_float & operator = ( const mprfloat a ); |
---|
90 | gmp_float & operator = ( const long a ); |
---|
91 | |
---|
92 | inline int sign(); // t>0:+1, t==0:0, t<0:-1 |
---|
93 | bool isZero(); // t == 0 ? |
---|
94 | bool isOne(); // t == 1 ? |
---|
95 | bool isMOne(); // t == -1 ? |
---|
96 | |
---|
97 | void setFromStr( char * in ); |
---|
98 | |
---|
99 | // access |
---|
100 | inline const mpf_t *mpfp() const; |
---|
101 | inline mpf_t *_mpfp(); |
---|
102 | |
---|
103 | inline operator double(); |
---|
104 | inline operator double() const; |
---|
105 | |
---|
106 | inline operator int(); |
---|
107 | inline operator int() const; |
---|
108 | |
---|
109 | private: |
---|
110 | mpf_t t; |
---|
111 | }; |
---|
112 | |
---|
113 | // <gmp_float> operator <gmp_float> |
---|
114 | inline gmp_float & gmp_float::operator *= ( const gmp_float & a ) |
---|
115 | { |
---|
116 | mpf_mul( t, t, a.t ); |
---|
117 | return *this; |
---|
118 | } |
---|
119 | inline gmp_float & gmp_float::operator /= ( const gmp_float & a ) |
---|
120 | { |
---|
121 | mpf_div( t, t, a.t ); |
---|
122 | return *this; |
---|
123 | } |
---|
124 | |
---|
125 | // <gmp_float> = <*> |
---|
126 | inline gmp_float & gmp_float::operator = ( const gmp_float & a ) |
---|
127 | { |
---|
128 | mpf_set( t, a.t ); |
---|
129 | return *this; |
---|
130 | } |
---|
131 | inline gmp_float & gmp_float::operator = ( const mpz_t & a ) |
---|
132 | { |
---|
133 | mpf_set_z( t, a ); |
---|
134 | return *this; |
---|
135 | } |
---|
136 | inline gmp_float & gmp_float::operator = ( const mprfloat a ) |
---|
137 | { |
---|
138 | mpf_set_d( t, (double) a ); |
---|
139 | return *this; |
---|
140 | } |
---|
141 | inline gmp_float & gmp_float::operator = ( const long a ) |
---|
142 | { |
---|
143 | mpf_set_d( t, (double) a ); |
---|
144 | return *this; |
---|
145 | } |
---|
146 | |
---|
147 | // cast to double |
---|
148 | inline gmp_float::operator double() |
---|
149 | { |
---|
150 | return mpf_get_d( t ); |
---|
151 | } |
---|
152 | inline gmp_float::operator double() const |
---|
153 | { |
---|
154 | return mpf_get_d( t ); |
---|
155 | } |
---|
156 | |
---|
157 | // cast to int |
---|
158 | inline gmp_float::operator int() |
---|
159 | { |
---|
160 | return (int)mpf_get_d( t ); |
---|
161 | } |
---|
162 | inline gmp_float::operator int() const |
---|
163 | { |
---|
164 | return (int)mpf_get_d( t ); |
---|
165 | } |
---|
166 | |
---|
167 | // get sign of real number ( -1: t < 0; 0: t==0; 1: t > 0 ) |
---|
168 | inline int gmp_float::sign() |
---|
169 | { |
---|
170 | return mpf_sgn( t ); |
---|
171 | } |
---|
172 | |
---|
173 | // access pointer |
---|
174 | inline const mpf_t *gmp_float::mpfp() const |
---|
175 | { |
---|
176 | return &t; |
---|
177 | } |
---|
178 | |
---|
179 | inline mpf_t *gmp_float::_mpfp() |
---|
180 | { |
---|
181 | return &t; |
---|
182 | } |
---|
183 | |
---|
184 | // built-in functions of GMP |
---|
185 | gmp_float abs( const gmp_float & ); |
---|
186 | gmp_float sqrt( const gmp_float & ); |
---|
187 | gmp_float hypot( const gmp_float &, const gmp_float & ); |
---|
188 | //gmp_float pow( const gmp_float &, int & ); |
---|
189 | |
---|
190 | // simulated functions using double functions |
---|
191 | gmp_float sin( const gmp_float & ); |
---|
192 | gmp_float cos( const gmp_float & ); |
---|
193 | gmp_float log( const gmp_float & ); |
---|
194 | gmp_float exp( const gmp_float & ); |
---|
195 | |
---|
196 | gmp_float max( const gmp_float &, const gmp_float & ); |
---|
197 | |
---|
198 | gmp_float numberToFloat( number num ); |
---|
199 | gmp_float numberFieldToFloat( number num, int k ); |
---|
200 | char *floatToStr( const gmp_float & r, const unsigned int oprec ); |
---|
201 | //<- |
---|
202 | |
---|
203 | //-> class gmp_complex |
---|
204 | /** |
---|
205 | * @short gmp_complex numbers based on |
---|
206 | */ |
---|
207 | class gmp_complex |
---|
208 | { |
---|
209 | private: |
---|
210 | gmp_float r, i; |
---|
211 | |
---|
212 | public: |
---|
213 | gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 ) |
---|
214 | { |
---|
215 | r= re; |
---|
216 | i= im; |
---|
217 | } |
---|
218 | gmp_complex( const mprfloat re, const mprfloat im = 0.0 ) |
---|
219 | { |
---|
220 | r= re; |
---|
221 | i= im; |
---|
222 | } |
---|
223 | gmp_complex( const long re, const long im ) |
---|
224 | { |
---|
225 | r= re; |
---|
226 | i= im; |
---|
227 | } |
---|
228 | gmp_complex( const gmp_complex & v ) |
---|
229 | { |
---|
230 | r= v.r; |
---|
231 | i= v.i; |
---|
232 | } |
---|
233 | ~gmp_complex() {} |
---|
234 | |
---|
235 | friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b ); |
---|
236 | friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b ); |
---|
237 | friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b ); |
---|
238 | friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b ); |
---|
239 | |
---|
240 | // gmp_complex <operator> real |
---|
241 | inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ); |
---|
242 | inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ); |
---|
243 | inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ); |
---|
244 | inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ); |
---|
245 | |
---|
246 | gmp_complex & operator += ( const gmp_complex & a ); |
---|
247 | gmp_complex & operator -= ( const gmp_complex & a ); |
---|
248 | gmp_complex & operator *= ( const gmp_complex & a ); |
---|
249 | gmp_complex & operator /= ( const gmp_complex & a ); |
---|
250 | |
---|
251 | inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b ); |
---|
252 | |
---|
253 | inline gmp_complex & operator = ( const gmp_complex & a ); |
---|
254 | inline gmp_complex & operator = ( const gmp_float & f ); |
---|
255 | |
---|
256 | // access to real and imaginary part |
---|
257 | inline gmp_float real() const { return r; } |
---|
258 | inline gmp_float imag() const { return i; } |
---|
259 | |
---|
260 | inline void real( gmp_float val ) { r = val; } |
---|
261 | inline void imag( gmp_float val ) { i = val; } |
---|
262 | |
---|
263 | void SmallToZero(); |
---|
264 | }; |
---|
265 | |
---|
266 | // <gmp_complex> = <gmp_complex> operator <gmp_float> |
---|
267 | // |
---|
268 | inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ) |
---|
269 | { |
---|
270 | return gmp_complex( a.r + b_d, a.i ); |
---|
271 | } |
---|
272 | inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ) |
---|
273 | { |
---|
274 | return gmp_complex( a.r - b_d, a.i ); |
---|
275 | } |
---|
276 | inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ) |
---|
277 | { |
---|
278 | return gmp_complex( a.r * b_d, a.i * b_d ); |
---|
279 | } |
---|
280 | inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ) |
---|
281 | { |
---|
282 | return gmp_complex( a.r / b_d, a.i / b_d ); |
---|
283 | } |
---|
284 | |
---|
285 | // <gmp_complex> == <gmp_complex> ? |
---|
286 | inline bool operator == ( const gmp_complex & a, const gmp_complex & b ) |
---|
287 | { |
---|
288 | return ( b.real() == a.real() ) && ( b.imag() == a.imag() ); |
---|
289 | } |
---|
290 | |
---|
291 | // <gmp_complex> = <gmp_complex> |
---|
292 | inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a ) |
---|
293 | { |
---|
294 | r= a.r; |
---|
295 | i= a.i; |
---|
296 | return *this; |
---|
297 | } |
---|
298 | |
---|
299 | // <gmp_complex> = <gmp_complex> |
---|
300 | inline gmp_complex & gmp_complex::operator = ( const gmp_float & f ) |
---|
301 | { |
---|
302 | r= f; |
---|
303 | i= (long int)0; |
---|
304 | return *this; |
---|
305 | } |
---|
306 | |
---|
307 | // Returns absolute value of a gmp_complex number |
---|
308 | // |
---|
309 | inline gmp_float abs( const gmp_complex & c ) |
---|
310 | { |
---|
311 | return hypot(c.real(),c.imag()); |
---|
312 | } |
---|
313 | |
---|
314 | gmp_complex sqrt( const gmp_complex & x ); |
---|
315 | |
---|
316 | inline gmp_complex numberToComplex( number num ) |
---|
317 | { |
---|
318 | if (rField_is_long_C()) { |
---|
319 | return *(gmp_complex*)num; |
---|
320 | } else { |
---|
321 | return gmp_complex( numberToFloat(num) ); |
---|
322 | } |
---|
323 | } |
---|
324 | |
---|
325 | char *complexToStr( gmp_complex & c, const unsigned int oprec ); |
---|
326 | //<- |
---|
327 | |
---|
328 | bool complexNearZero( gmp_complex * c, int digits ); |
---|
329 | |
---|
330 | #endif /* MPR_COMPLEX_H */ |
---|
331 | |
---|
332 | // local Variables: *** |
---|
333 | // folded-file: t *** |
---|
334 | // compile-command-1: "make installg" *** |
---|
335 | // compile-command-2: "make install" *** |
---|
336 | // End: *** |
---|