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