[8a150b] | 1 | #ifndef MPR_COMPLEX_H |
---|
| 2 | #define MPR_COMPLEX_H |
---|
| 3 | /**************************************** |
---|
| 4 | * Computer Algebra System SINGULAR * |
---|
| 5 | ****************************************/ |
---|
[a5f15a] | 6 | /* $Id: mpr_complex.h,v 1.5 1999-06-28 12:48:13 wenk Exp $ */ |
---|
[8a150b] | 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" |
---|
[f543de1] | 20 | #include "ring.h" |
---|
[8a150b] | 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(); |
---|
[f543de1] | 31 | |
---|
[8a150b] | 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: |
---|
[f543de1] | 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 | } |
---|
[8a150b] | 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 | |
---|
[f543de1] | 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 ); |
---|
[8a150b] | 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 | |
---|
[f543de1] | 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 ? |
---|
[8a150b] | 106 | |
---|
[f543de1] | 107 | inline bool setFromStr( char * in ); |
---|
[8a150b] | 108 | |
---|
| 109 | // access |
---|
| 110 | inline const mpf_t *mpfp() const; |
---|
| 111 | |
---|
[f543de1] | 112 | inline operator double(); |
---|
| 113 | inline operator double() const; |
---|
[8a150b] | 114 | |
---|
[f543de1] | 115 | inline operator int(); |
---|
| 116 | inline operator int() const; |
---|
[8a150b] | 117 | |
---|
| 118 | public: |
---|
[a5f15a] | 119 | static void setPrecision( const unsigned long int prec ) { |
---|
| 120 | gmp_default_prec_bits= prec; |
---|
| 121 | } |
---|
| 122 | static void setEqualBits( const unsigned long int prec ) { |
---|
| 123 | gmp_needequal_bits= prec; |
---|
| 124 | } |
---|
[8a150b] | 125 | |
---|
[a5f15a] | 126 | static const unsigned long int getPrecision() { |
---|
| 127 | return gmp_default_prec_bits; |
---|
| 128 | } |
---|
| 129 | static const unsigned long int getEqualBits() { |
---|
| 130 | return gmp_needequal_bits; |
---|
| 131 | } |
---|
[8a150b] | 132 | |
---|
| 133 | private: |
---|
| 134 | static unsigned long int gmp_default_prec_bits; |
---|
| 135 | static unsigned long int gmp_needequal_bits; |
---|
| 136 | |
---|
| 137 | mpf_t t; |
---|
| 138 | }; |
---|
| 139 | |
---|
[f543de1] | 140 | static const gmp_float gmpOne= 1; |
---|
| 141 | static const gmp_float gmpMOne= -1; |
---|
| 142 | static const gmp_float gmpZero= 0; |
---|
| 143 | |
---|
| 144 | // <gmp_float> operator <gmp_float> |
---|
| 145 | inline gmp_float & gmp_float::operator += ( const gmp_float & a ) |
---|
| 146 | { |
---|
| 147 | mpf_add( t, t, a.t ); |
---|
| 148 | return *this; |
---|
| 149 | } |
---|
| 150 | inline gmp_float & gmp_float::operator -= ( const gmp_float & a ) |
---|
| 151 | { |
---|
| 152 | mpf_sub( t, t, a.t ); |
---|
| 153 | return *this; |
---|
| 154 | } |
---|
| 155 | inline gmp_float & gmp_float::operator *= ( const gmp_float & a ) |
---|
| 156 | { |
---|
| 157 | mpf_mul( t, t, a.t ); |
---|
| 158 | return *this; |
---|
| 159 | } |
---|
| 160 | inline gmp_float & gmp_float::operator /= ( const gmp_float & a ) |
---|
| 161 | { |
---|
| 162 | mpf_div( t, t, a.t ); |
---|
| 163 | return *this; |
---|
| 164 | } |
---|
| 165 | |
---|
| 166 | // <gmp_float> = <*> |
---|
| 167 | inline gmp_float & gmp_float::operator = ( const gmp_float & a ) |
---|
| 168 | { |
---|
| 169 | mpf_set( t, a.t ); |
---|
| 170 | return *this; |
---|
| 171 | } |
---|
| 172 | inline gmp_float & gmp_float::operator = ( const mpz_t & a ) |
---|
| 173 | { |
---|
| 174 | mpf_set_z( t, a ); |
---|
| 175 | return *this; |
---|
| 176 | } |
---|
| 177 | inline gmp_float & gmp_float::operator = ( const mprfloat a ) |
---|
| 178 | { |
---|
| 179 | mpf_set_d( t, (double) a ); |
---|
| 180 | return *this; |
---|
| 181 | } |
---|
| 182 | inline gmp_float & gmp_float::operator = ( const long a ) |
---|
| 183 | { |
---|
| 184 | mpf_set_si( t, (signed long int) a ); |
---|
| 185 | return *this; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // cast to double |
---|
| 189 | inline gmp_float::operator double() |
---|
| 190 | { |
---|
| 191 | return mpf_get_d( t ); |
---|
| 192 | } |
---|
| 193 | inline gmp_float::operator double() const |
---|
| 194 | { |
---|
| 195 | return mpf_get_d( t ); |
---|
| 196 | } |
---|
| 197 | |
---|
| 198 | // cast to int |
---|
| 199 | inline gmp_float::operator int() |
---|
| 200 | { |
---|
| 201 | return (int)mpf_get_d( t ); |
---|
| 202 | } |
---|
| 203 | inline gmp_float::operator int() const |
---|
| 204 | { |
---|
| 205 | return (int)mpf_get_d( t ); |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | // get sign of real number ( -1: t < 0; 0: t==0; 1: t > 0 ) |
---|
| 209 | inline int gmp_float::sign() |
---|
| 210 | { |
---|
| 211 | return mpf_sgn( t ); |
---|
| 212 | } |
---|
| 213 | // t == 0 ? |
---|
| 214 | inline bool gmp_float::isZero() |
---|
| 215 | { |
---|
| 216 | #ifdef VARIANTE_1 |
---|
| 217 | return (mpf_sgn( t ) == 0); |
---|
| 218 | #else |
---|
| 219 | return mpf_eq( t , gmpZero.t , gmp_float::gmp_needequal_bits ); |
---|
| 220 | #endif |
---|
| 221 | } |
---|
| 222 | // t == 1 ? |
---|
| 223 | inline bool gmp_float::isOne() |
---|
| 224 | { |
---|
| 225 | #ifdef VARIANTE_1 |
---|
| 226 | return (mpf_cmp_ui( t , 1 ) == 0); |
---|
| 227 | #else |
---|
| 228 | return mpf_eq( t , gmpOne.t , gmp_float::gmp_needequal_bits ); |
---|
| 229 | #endif |
---|
| 230 | } |
---|
| 231 | // t == -1 ? |
---|
| 232 | inline bool gmp_float::isMOne() |
---|
| 233 | { |
---|
| 234 | #ifdef VARIANTE_1 |
---|
| 235 | return (mpf_cmp_si( t , -1 ) == 0); |
---|
| 236 | #else |
---|
| 237 | return mpf_eq( t , gmpMOne.t , gmp_float::gmp_needequal_bits ); |
---|
| 238 | #endif |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | inline bool gmp_float::setFromStr( char * in ) |
---|
| 242 | { |
---|
| 243 | return ( mpf_set_str( t, in, 10 ) == 0 ); |
---|
| 244 | } |
---|
| 245 | |
---|
| 246 | // access pointer |
---|
| 247 | inline const mpf_t *gmp_float::mpfp() const |
---|
| 248 | { |
---|
| 249 | return &t; |
---|
| 250 | } |
---|
| 251 | |
---|
[8a150b] | 252 | // built-in functions of GMP |
---|
| 253 | gmp_float abs( const gmp_float & ); |
---|
| 254 | gmp_float sqrt( const gmp_float & ); |
---|
| 255 | gmp_float hypot( const gmp_float &, const gmp_float & ); |
---|
[a5f15a] | 256 | //gmp_float pow( const gmp_float &, int & ); |
---|
[8a150b] | 257 | |
---|
| 258 | // simulated functions using double functions |
---|
| 259 | gmp_float sin( const gmp_float & ); |
---|
| 260 | gmp_float cos( const gmp_float & ); |
---|
| 261 | gmp_float log( const gmp_float & ); |
---|
| 262 | gmp_float exp( const gmp_float & ); |
---|
| 263 | |
---|
| 264 | gmp_float max( const gmp_float &, const gmp_float & ); |
---|
| 265 | |
---|
| 266 | gmp_float numberToFloat( number num ); |
---|
[f543de1] | 267 | char *floatToStr( const gmp_float & r, const unsigned int oprec ); |
---|
[8a150b] | 268 | //<- |
---|
| 269 | |
---|
[f543de1] | 270 | //-> class gmp_complex |
---|
[8a150b] | 271 | /** |
---|
[f543de1] | 272 | * @short gmp_complex numbers based on |
---|
[8a150b] | 273 | */ |
---|
[f543de1] | 274 | class gmp_complex |
---|
[8a150b] | 275 | { |
---|
[0f0a1d] | 276 | private: |
---|
[f543de1] | 277 | gmp_float r, i; |
---|
[0f0a1d] | 278 | |
---|
[8a150b] | 279 | public: |
---|
[f543de1] | 280 | gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 ) |
---|
| 281 | { |
---|
| 282 | r= re; |
---|
| 283 | i= im; |
---|
| 284 | } |
---|
| 285 | gmp_complex( const mprfloat re, const mprfloat im = 0.0 ) |
---|
| 286 | { |
---|
| 287 | r= re; |
---|
| 288 | i= im; |
---|
| 289 | } |
---|
| 290 | gmp_complex( const long re, const long im ) |
---|
| 291 | { |
---|
| 292 | r= re; |
---|
| 293 | i= im; |
---|
| 294 | } |
---|
| 295 | gmp_complex( const gmp_complex & v ) |
---|
| 296 | { |
---|
| 297 | r= v.r; |
---|
| 298 | i= v.i; |
---|
| 299 | } |
---|
| 300 | ~gmp_complex() {} |
---|
| 301 | |
---|
| 302 | friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 303 | friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 304 | friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 305 | friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 306 | |
---|
| 307 | // gmp_complex <operator> real |
---|
| 308 | inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 309 | inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 310 | inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 311 | inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 312 | |
---|
| 313 | gmp_complex & operator += ( const gmp_complex & a ); |
---|
| 314 | gmp_complex & operator -= ( const gmp_complex & a ); |
---|
| 315 | gmp_complex & operator *= ( const gmp_complex & a ); |
---|
| 316 | gmp_complex & operator /= ( const gmp_complex & a ); |
---|
| 317 | |
---|
| 318 | inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 319 | |
---|
| 320 | inline gmp_complex & operator = ( const gmp_complex & a ); |
---|
[8a150b] | 321 | |
---|
[f543de1] | 322 | // access to real and imaginary part |
---|
| 323 | inline gmp_float real() const { return r; } |
---|
| 324 | inline gmp_float imag() const { return i; } |
---|
[8a150b] | 325 | |
---|
[f543de1] | 326 | inline void real( gmp_float val ) { r = val; } |
---|
| 327 | inline void imag( gmp_float val ) { i = val; } |
---|
| 328 | }; |
---|
[8a150b] | 329 | |
---|
[f543de1] | 330 | // <gmp_complex> = <gmp_complex> operator <gmp_float> |
---|
| 331 | // |
---|
| 332 | inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 333 | { |
---|
| 334 | return gmp_complex( a.r + b_d, a.i ); |
---|
| 335 | } |
---|
| 336 | inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 337 | { |
---|
| 338 | return gmp_complex( a.r - b_d, a.i ); |
---|
| 339 | } |
---|
| 340 | inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 341 | { |
---|
| 342 | return gmp_complex( a.r * b_d, a.i * b_d ); |
---|
| 343 | } |
---|
| 344 | inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 345 | { |
---|
| 346 | return gmp_complex( a.r / b_d, a.i / b_d ); |
---|
| 347 | } |
---|
[8a150b] | 348 | |
---|
[f543de1] | 349 | // <gmp_complex> == <gmp_complex> ? |
---|
| 350 | inline bool operator == ( const gmp_complex & a, const gmp_complex & b ) |
---|
| 351 | { |
---|
| 352 | return ( b.real() == a.real() ) && ( b.imag() == a.imag() ); |
---|
| 353 | } |
---|
[8a150b] | 354 | |
---|
[f543de1] | 355 | // <gmp_complex> = <gmp_complex> |
---|
| 356 | inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a ) |
---|
| 357 | { |
---|
| 358 | r= a.r; |
---|
| 359 | i= a.i; |
---|
| 360 | return *this; |
---|
| 361 | } |
---|
[8a150b] | 362 | |
---|
[f543de1] | 363 | // Returns absolute value of a gmp_complex number |
---|
| 364 | // |
---|
| 365 | inline gmp_float abs( const gmp_complex & c ) |
---|
| 366 | { |
---|
| 367 | return hypot(c.real(),c.imag()); |
---|
| 368 | } |
---|
[8a150b] | 369 | |
---|
[f543de1] | 370 | gmp_complex sqrt( const gmp_complex & x ); |
---|
[8a150b] | 371 | |
---|
[f543de1] | 372 | inline gmp_complex numberToGmp_Complex( number num ) |
---|
[0f0a1d] | 373 | { |
---|
[f543de1] | 374 | if (rField_is_long_C()) { |
---|
| 375 | return *(gmp_complex*)num; |
---|
| 376 | } else { |
---|
| 377 | return gmp_complex( numberToFloat(num) ); |
---|
| 378 | } |
---|
[0f0a1d] | 379 | } |
---|
[f543de1] | 380 | |
---|
| 381 | char *complexToStr( const gmp_complex & c, const unsigned int oprec ); |
---|
[8a150b] | 382 | //<- |
---|
| 383 | |
---|
| 384 | #endif MPR_COMPLEX_H |
---|
| 385 | |
---|
| 386 | // local Variables: *** |
---|
| 387 | // folded-file: t *** |
---|
| 388 | // compile-command-1: "make installg" *** |
---|
| 389 | // compile-command-2: "make install" *** |
---|
| 390 | // End: *** |
---|
[f543de1] | 391 | |
---|
| 392 | |
---|
| 393 | |
---|
| 394 | |
---|
| 395 | |
---|