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