[35aab3] | 1 | #ifndef MPR_COMPLEX_H |
---|
| 2 | #define MPR_COMPLEX_H |
---|
| 3 | /**************************************** |
---|
| 4 | * Computer Algebra System SINGULAR * |
---|
| 5 | ****************************************/ |
---|
[85e68dd] | 6 | /* $Id: mpr_complex.h,v 1.6 2008-03-19 17:44:10 Singular Exp $ */ |
---|
[35aab3] | 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 "numbers.h" |
---|
| 18 | #include "ring.h" |
---|
| 19 | #include "mpr_global.h" |
---|
| 20 | |
---|
| 21 | #define ZTOF 1 |
---|
| 22 | #define QTOF 2 |
---|
| 23 | #define RTOF 3 |
---|
| 24 | #define CTOF 4 |
---|
| 25 | |
---|
| 26 | void setGMPFloatDigits( size_t digits, size_t rest ); |
---|
| 27 | size_t getGMPFloatDigits(); |
---|
| 28 | |
---|
| 29 | //-> class gmp_float |
---|
| 30 | /** |
---|
| 31 | * @short wrapper class for GNU Multi Precision Floats |
---|
| 32 | */ |
---|
[3c6379] | 33 | class gmp_float; |
---|
| 34 | char *floatToStr( const gmp_float & r, const unsigned int oprec ); |
---|
[35aab3] | 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 | |
---|
[da434c] | 69 | inline gmp_float & operator = ( const gmp_float & a ) |
---|
| 70 | { |
---|
| 71 | mpf_set( t, a.t ); |
---|
| 72 | return *this; |
---|
| 73 | }; |
---|
| 74 | inline gmp_float & operator = ( const mpz_t & a ) |
---|
| 75 | { |
---|
| 76 | mpf_set_z( t, a ); |
---|
| 77 | return *this; |
---|
| 78 | }; |
---|
| 79 | inline gmp_float & operator = ( const mprfloat a ) |
---|
| 80 | { |
---|
| 81 | mpf_set_d( t, (double) a ); |
---|
| 82 | return *this; |
---|
| 83 | }; |
---|
| 84 | inline gmp_float & operator = ( const long a ) |
---|
| 85 | { |
---|
| 86 | mpf_set_d( t, (double) a ); |
---|
| 87 | return *this; |
---|
| 88 | }; |
---|
| 89 | |
---|
| 90 | gmp_float & operator += ( const gmp_float & a ); |
---|
| 91 | gmp_float & operator -= ( const gmp_float & a ); |
---|
| 92 | inline gmp_float & operator *= ( const gmp_float & a ) |
---|
| 93 | { |
---|
| 94 | mpf_mul( t, t, a.t ); |
---|
| 95 | return *this; |
---|
| 96 | }; |
---|
| 97 | |
---|
| 98 | inline gmp_float & operator /= ( const gmp_float & a ) |
---|
| 99 | { |
---|
| 100 | mpf_div( t, t, a.t ); |
---|
| 101 | return *this; |
---|
| 102 | }; |
---|
| 103 | |
---|
[35aab3] | 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 | friend bool operator == ( const gmp_float & a, const gmp_float & b ); |
---|
| 110 | friend bool operator > ( const gmp_float & a, const gmp_float & b ); |
---|
| 111 | friend bool operator < ( const gmp_float & a, const gmp_float & b ); |
---|
| 112 | friend bool operator >= ( const gmp_float & a, const gmp_float & b ); |
---|
| 113 | friend bool operator <= ( const gmp_float & a, const gmp_float & b ); |
---|
| 114 | |
---|
| 115 | friend gmp_float operator - ( const gmp_float & a ); |
---|
| 116 | |
---|
[da434c] | 117 | inline int sign() // t>0:+1, t==0:0, t<0:-1 |
---|
| 118 | { return mpf_sgn( t ); }; |
---|
[b7e838] | 119 | |
---|
[35aab3] | 120 | bool isZero(); // t == 0 ? |
---|
| 121 | bool isOne(); // t == 1 ? |
---|
| 122 | bool isMOne(); // t == -1 ? |
---|
| 123 | |
---|
[85e68dd] | 124 | void setFromStr(const char * in ); |
---|
[35aab3] | 125 | |
---|
| 126 | // access |
---|
[da434c] | 127 | inline const mpf_t *mpfp() const { return &t; }; |
---|
| 128 | inline mpf_t *_mpfp() { return &t; }; |
---|
[b7e838] | 129 | |
---|
[da434c] | 130 | inline operator double() { return mpf_get_d( t ); }; |
---|
| 131 | inline operator double() const { return mpf_get_d( t ); }; |
---|
[35aab3] | 132 | |
---|
[f12d4f6] | 133 | #if 0 |
---|
[da434c] | 134 | inline operator int() { return (int)mpf_get_d( t ); }; |
---|
| 135 | inline operator int() const { return (int)mpf_get_d( t ); }; |
---|
[3c6379] | 136 | //#else |
---|
[f12d4f6] | 137 | inline operator int() const |
---|
| 138 | { if (mpf_fits_sint_p(t)) |
---|
[3c6379] | 139 | { return (int)mpf_get_si( t ); } |
---|
[f12d4f6] | 140 | return 0; |
---|
| 141 | }; |
---|
[b7e838] | 142 | #endif |
---|
[35aab3] | 143 | |
---|
| 144 | private: |
---|
| 145 | mpf_t t; |
---|
| 146 | }; |
---|
| 147 | |
---|
| 148 | |
---|
| 149 | // built-in functions of GMP |
---|
| 150 | gmp_float abs( const gmp_float & ); |
---|
| 151 | gmp_float sqrt( const gmp_float & ); |
---|
| 152 | gmp_float hypot( const gmp_float &, const gmp_float & ); |
---|
| 153 | //gmp_float pow( const gmp_float &, int & ); |
---|
| 154 | |
---|
| 155 | // simulated functions using double functions |
---|
| 156 | gmp_float sin( const gmp_float & ); |
---|
| 157 | gmp_float cos( const gmp_float & ); |
---|
| 158 | gmp_float log( const gmp_float & ); |
---|
| 159 | gmp_float exp( const gmp_float & ); |
---|
| 160 | |
---|
| 161 | gmp_float max( const gmp_float &, const gmp_float & ); |
---|
| 162 | |
---|
| 163 | gmp_float numberToFloat( number num ); |
---|
| 164 | gmp_float numberFieldToFloat( number num, int k ); |
---|
[3c6379] | 165 | //char *floatToStr( const gmp_float & r, const unsigned int oprec ); |
---|
[35aab3] | 166 | //<- |
---|
| 167 | |
---|
| 168 | //-> class gmp_complex |
---|
| 169 | /** |
---|
| 170 | * @short gmp_complex numbers based on |
---|
| 171 | */ |
---|
| 172 | class gmp_complex |
---|
| 173 | { |
---|
| 174 | private: |
---|
| 175 | gmp_float r, i; |
---|
| 176 | |
---|
| 177 | public: |
---|
| 178 | gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 ) |
---|
| 179 | { |
---|
| 180 | r= re; |
---|
| 181 | i= im; |
---|
| 182 | } |
---|
| 183 | gmp_complex( const mprfloat re, const mprfloat im = 0.0 ) |
---|
| 184 | { |
---|
| 185 | r= re; |
---|
| 186 | i= im; |
---|
| 187 | } |
---|
| 188 | gmp_complex( const long re, const long im ) |
---|
| 189 | { |
---|
| 190 | r= re; |
---|
| 191 | i= im; |
---|
| 192 | } |
---|
| 193 | gmp_complex( const gmp_complex & v ) |
---|
| 194 | { |
---|
| 195 | r= v.r; |
---|
| 196 | i= v.i; |
---|
| 197 | } |
---|
| 198 | ~gmp_complex() {} |
---|
| 199 | |
---|
| 200 | friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 201 | friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 202 | friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 203 | friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 204 | |
---|
| 205 | // gmp_complex <operator> real |
---|
| 206 | inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 207 | inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 208 | inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 209 | inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ); |
---|
| 210 | |
---|
| 211 | gmp_complex & operator += ( const gmp_complex & a ); |
---|
| 212 | gmp_complex & operator -= ( const gmp_complex & a ); |
---|
| 213 | gmp_complex & operator *= ( const gmp_complex & a ); |
---|
| 214 | gmp_complex & operator /= ( const gmp_complex & a ); |
---|
| 215 | |
---|
| 216 | inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b ); |
---|
| 217 | |
---|
| 218 | inline gmp_complex & operator = ( const gmp_complex & a ); |
---|
| 219 | inline gmp_complex & operator = ( const gmp_float & f ); |
---|
| 220 | |
---|
| 221 | // access to real and imaginary part |
---|
| 222 | inline gmp_float real() const { return r; } |
---|
| 223 | inline gmp_float imag() const { return i; } |
---|
| 224 | |
---|
| 225 | inline void real( gmp_float val ) { r = val; } |
---|
| 226 | inline void imag( gmp_float val ) { i = val; } |
---|
| 227 | |
---|
[b7e838] | 228 | |
---|
| 229 | inline bool isZero() { return (r.isZero() && i.isZero()); } |
---|
[35aab3] | 230 | void SmallToZero(); |
---|
| 231 | }; |
---|
| 232 | |
---|
| 233 | // <gmp_complex> = <gmp_complex> operator <gmp_float> |
---|
| 234 | // |
---|
| 235 | inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 236 | { |
---|
| 237 | return gmp_complex( a.r + b_d, a.i ); |
---|
| 238 | } |
---|
| 239 | inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 240 | { |
---|
| 241 | return gmp_complex( a.r - b_d, a.i ); |
---|
| 242 | } |
---|
| 243 | inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 244 | { |
---|
| 245 | return gmp_complex( a.r * b_d, a.i * b_d ); |
---|
| 246 | } |
---|
| 247 | inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ) |
---|
| 248 | { |
---|
| 249 | return gmp_complex( a.r / b_d, a.i / b_d ); |
---|
| 250 | } |
---|
| 251 | |
---|
| 252 | // <gmp_complex> == <gmp_complex> ? |
---|
| 253 | inline bool operator == ( const gmp_complex & a, const gmp_complex & b ) |
---|
| 254 | { |
---|
| 255 | return ( b.real() == a.real() ) && ( b.imag() == a.imag() ); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // <gmp_complex> = <gmp_complex> |
---|
| 259 | inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a ) |
---|
| 260 | { |
---|
| 261 | r= a.r; |
---|
| 262 | i= a.i; |
---|
| 263 | return *this; |
---|
| 264 | } |
---|
| 265 | |
---|
| 266 | // <gmp_complex> = <gmp_complex> |
---|
| 267 | inline gmp_complex & gmp_complex::operator = ( const gmp_float & f ) |
---|
| 268 | { |
---|
| 269 | r= f; |
---|
| 270 | i= (long int)0; |
---|
| 271 | return *this; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | // Returns absolute value of a gmp_complex number |
---|
| 275 | // |
---|
| 276 | inline gmp_float abs( const gmp_complex & c ) |
---|
| 277 | { |
---|
| 278 | return hypot(c.real(),c.imag()); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | gmp_complex sqrt( const gmp_complex & x ); |
---|
| 282 | |
---|
| 283 | inline gmp_complex numberToComplex( number num ) |
---|
| 284 | { |
---|
| 285 | if (rField_is_long_C()) { |
---|
| 286 | return *(gmp_complex*)num; |
---|
| 287 | } else { |
---|
| 288 | return gmp_complex( numberToFloat(num) ); |
---|
| 289 | } |
---|
| 290 | } |
---|
| 291 | |
---|
| 292 | char *complexToStr( gmp_complex & c, const unsigned int oprec ); |
---|
| 293 | //<- |
---|
| 294 | |
---|
| 295 | bool complexNearZero( gmp_complex * c, int digits ); |
---|
| 296 | |
---|
| 297 | #endif /* MPR_COMPLEX_H */ |
---|
| 298 | |
---|
| 299 | // local Variables: *** |
---|
| 300 | // folded-file: t *** |
---|
| 301 | // compile-command-1: "make installg" *** |
---|
| 302 | // compile-command-2: "make install" *** |
---|
| 303 | // End: *** |
---|