[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
[341696] | 4 | /* $Id$ */ |
---|
[35aab3] | 5 | /* |
---|
| 6 | * ABSTRACT: computations with GMP complex floating-point numbers |
---|
| 7 | * |
---|
| 8 | * ngc == number gnu complex |
---|
| 9 | */ |
---|
| 10 | |
---|
[599326] | 11 | #include <kernel/mod2.h> |
---|
| 12 | #include <kernel/structs.h> |
---|
| 13 | #include <kernel/febase.h> |
---|
[b1dfaf] | 14 | #include <omalloc/omalloc.h> |
---|
[599326] | 15 | #include <kernel/numbers.h> |
---|
| 16 | #include <kernel/longrat.h> |
---|
| 17 | #include <kernel/modulop.h> |
---|
| 18 | #include <kernel/gnumpc.h> |
---|
| 19 | #include <kernel/gnumpfl.h> |
---|
| 20 | #include <kernel/mpr_complex.h> |
---|
[35aab3] | 21 | |
---|
| 22 | extern size_t gmp_output_digits; |
---|
| 23 | |
---|
| 24 | |
---|
[8cf4f3f] | 25 | number ngcMapQ(number from) |
---|
[35aab3] | 26 | { |
---|
| 27 | if ( from != NULL ) |
---|
| 28 | { |
---|
| 29 | gmp_complex *res=new gmp_complex(numberFieldToFloat(from,QTOF)); |
---|
| 30 | return (number)res; |
---|
| 31 | } |
---|
| 32 | else |
---|
| 33 | return NULL; |
---|
| 34 | } |
---|
| 35 | union nf |
---|
| 36 | { |
---|
| 37 | float _f; |
---|
| 38 | number _n; |
---|
| 39 | nf(float f) {_f = f;} |
---|
| 40 | nf(number n) {_n = n;} |
---|
| 41 | float F() const {return _f;} |
---|
| 42 | number N() const {return _n;} |
---|
| 43 | }; |
---|
| 44 | static number ngcMapLongR(number from) |
---|
| 45 | { |
---|
| 46 | if ( from != NULL ) |
---|
| 47 | { |
---|
| 48 | gmp_complex *res=new gmp_complex(*((gmp_float *)from)); |
---|
| 49 | return (number)res; |
---|
| 50 | } |
---|
| 51 | else |
---|
| 52 | return NULL; |
---|
| 53 | } |
---|
| 54 | static number ngcMapR(number from) |
---|
| 55 | { |
---|
| 56 | if ( from != NULL ) |
---|
| 57 | { |
---|
| 58 | gmp_complex *res=new gmp_complex((double)nf(from).F()); |
---|
| 59 | return (number)res; |
---|
| 60 | } |
---|
| 61 | else |
---|
| 62 | return NULL; |
---|
| 63 | } |
---|
[cf74cd6] | 64 | extern ring ngfMapRing; |
---|
[35aab3] | 65 | static number ngcMapP(number from) |
---|
| 66 | { |
---|
| 67 | if ( from != NULL) |
---|
[8391d8] | 68 | return ngcInit(npInt(from,ngfMapRing), currRing); |
---|
[35aab3] | 69 | else |
---|
| 70 | return NULL; |
---|
| 71 | } |
---|
| 72 | |
---|
[208e0c] | 73 | nMapFunc ngcSetMap(const ring src,const ring dst) |
---|
[35aab3] | 74 | { |
---|
| 75 | if(rField_is_Q(src)) |
---|
| 76 | { |
---|
| 77 | return ngcMapQ; |
---|
| 78 | } |
---|
| 79 | if (rField_is_long_R(src)) |
---|
| 80 | { |
---|
| 81 | return ngcMapLongR; |
---|
| 82 | } |
---|
| 83 | if (rField_is_long_C(src)) |
---|
| 84 | { |
---|
| 85 | return ngcCopy; |
---|
| 86 | } |
---|
| 87 | if(rField_is_R(src)) |
---|
| 88 | { |
---|
| 89 | return ngcMapR; |
---|
| 90 | } |
---|
| 91 | if (rField_is_Zp(src)) |
---|
| 92 | { |
---|
[cf74cd6] | 93 | ngfMapRing=src; |
---|
[35aab3] | 94 | return ngcMapP; |
---|
| 95 | } |
---|
| 96 | return NULL; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | number ngcPar(int i) |
---|
| 100 | { |
---|
| 101 | gmp_complex* n= new gmp_complex( (long)0, (long)1 ); |
---|
| 102 | return (number)n; |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | /*2 |
---|
| 106 | * n := i |
---|
| 107 | */ |
---|
[8391d8] | 108 | number ngcInit (int i, const ring r) |
---|
[35aab3] | 109 | { |
---|
[78e67e] | 110 | gmp_complex* n= new gmp_complex( (long)i, (long)0 ); |
---|
[35aab3] | 111 | return (number)n; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /*2 |
---|
| 115 | * convert number to int |
---|
| 116 | */ |
---|
[cf74cd6] | 117 | int ngcInt(number &i, const ring r) |
---|
[35aab3] | 118 | { |
---|
| 119 | return (int)((gmp_complex*)i)->real(); |
---|
| 120 | } |
---|
| 121 | |
---|
[12cca3] | 122 | int ngcSize(number n) |
---|
| 123 | { |
---|
| 124 | int r = (int)((gmp_complex*)n)->real(); |
---|
| 125 | if (r < 0) r = -r; |
---|
| 126 | int i = (int)((gmp_complex*)n)->imag(); |
---|
| 127 | if (i < 0) i = -i; |
---|
| 128 | int oneNorm = r + i; |
---|
| 129 | /* basically return the 1-norm of n; |
---|
| 130 | only if this happens to be zero although n != 0, |
---|
| 131 | return 1; |
---|
| 132 | (this code ensures that zero has the size zero) */ |
---|
| 133 | if ((oneNorm == 0.0) & (ngcIsZero(n) == FALSE)) oneNorm = 1; |
---|
| 134 | return oneNorm; |
---|
| 135 | } |
---|
| 136 | |
---|
[35aab3] | 137 | /*2 |
---|
| 138 | * delete a |
---|
| 139 | */ |
---|
| 140 | void ngcDelete (number * a, const ring r) |
---|
| 141 | { |
---|
[7c961ba] | 142 | if ( *a != NULL ) |
---|
| 143 | { |
---|
[35aab3] | 144 | delete *(gmp_complex**)a; |
---|
| 145 | *a=NULL; |
---|
| 146 | } |
---|
| 147 | } |
---|
| 148 | |
---|
| 149 | /*2 |
---|
| 150 | * copy a to b |
---|
| 151 | */ |
---|
| 152 | number ngcCopy(number a) |
---|
| 153 | { |
---|
[78e67e] | 154 | gmp_complex* b= new gmp_complex( *(gmp_complex*)a ); |
---|
[35aab3] | 155 | return (number)b; |
---|
| 156 | } |
---|
| 157 | number ngc_Copy(number a, ring r) |
---|
| 158 | { |
---|
[78e67e] | 159 | gmp_complex* b=new gmp_complex( *(gmp_complex*)a ); |
---|
[35aab3] | 160 | return (number)b; |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /*2 |
---|
| 164 | * za:= - za |
---|
| 165 | */ |
---|
[0f4db3] | 166 | gmp_complex ngc_m1(-1); |
---|
| 167 | |
---|
[35aab3] | 168 | number ngcNeg (number a) |
---|
| 169 | { |
---|
[0f4db3] | 170 | gmp_complex* r=(gmp_complex*)a; |
---|
| 171 | (*r) *= ngc_m1; |
---|
[35aab3] | 172 | return (number)a; |
---|
| 173 | } |
---|
| 174 | |
---|
| 175 | /* |
---|
| 176 | * 1/a |
---|
| 177 | */ |
---|
| 178 | number ngcInvers(number a) |
---|
| 179 | { |
---|
[b7e838] | 180 | gmp_complex* r = NULL; |
---|
[78e67e] | 181 | if (((gmp_complex*)a)->isZero()) |
---|
[35aab3] | 182 | { |
---|
[b7e838] | 183 | WerrorS(nDivBy0); |
---|
[35aab3] | 184 | } |
---|
| 185 | else |
---|
| 186 | { |
---|
| 187 | r= new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) ); |
---|
| 188 | } |
---|
| 189 | return (number)r; |
---|
| 190 | } |
---|
| 191 | |
---|
| 192 | /*2 |
---|
| 193 | * u:= a + b |
---|
| 194 | */ |
---|
| 195 | number ngcAdd (number a, number b) |
---|
| 196 | { |
---|
[78e67e] | 197 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) ); |
---|
[35aab3] | 198 | return (number)r; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /*2 |
---|
| 202 | * u:= a - b |
---|
| 203 | */ |
---|
| 204 | number ngcSub (number a, number b) |
---|
| 205 | { |
---|
[78e67e] | 206 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) ); |
---|
[35aab3] | 207 | return (number)r; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /*2 |
---|
| 211 | * u := a * b |
---|
| 212 | */ |
---|
| 213 | number ngcMult (number a, number b) |
---|
| 214 | { |
---|
[78e67e] | 215 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) ); |
---|
[35aab3] | 216 | return (number)r; |
---|
| 217 | } |
---|
| 218 | |
---|
| 219 | /*2 |
---|
| 220 | * u := a / b |
---|
| 221 | */ |
---|
| 222 | number ngcDiv (number a, number b) |
---|
| 223 | { |
---|
[78e67e] | 224 | if (((gmp_complex*)b)->isZero()) |
---|
[35aab3] | 225 | { |
---|
[b7e838] | 226 | // a/0 = error |
---|
| 227 | WerrorS(nDivBy0); |
---|
[35aab3] | 228 | return NULL; |
---|
| 229 | } |
---|
| 230 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) ); |
---|
| 231 | return (number)r; |
---|
| 232 | } |
---|
| 233 | |
---|
| 234 | /*2 |
---|
| 235 | * u:= x ^ exp |
---|
| 236 | */ |
---|
| 237 | void ngcPower ( number x, int exp, number * u ) |
---|
| 238 | { |
---|
| 239 | if ( exp == 0 ) |
---|
| 240 | { |
---|
| 241 | gmp_complex* n = new gmp_complex(1); |
---|
| 242 | *u=(number)n; |
---|
| 243 | return; |
---|
| 244 | } |
---|
[5fe545] | 245 | else if ( exp == 1 ) |
---|
[35aab3] | 246 | { |
---|
| 247 | nNew(u); |
---|
[78e67e] | 248 | gmp_complex* n = new gmp_complex(); |
---|
| 249 | *n= *(gmp_complex*)x; |
---|
| 250 | *u=(number)n; |
---|
[35aab3] | 251 | return; |
---|
| 252 | } |
---|
[5fe545] | 253 | else if (exp == 2) |
---|
| 254 | { |
---|
| 255 | nNew(u); |
---|
[78e67e] | 256 | gmp_complex* n = new gmp_complex(); |
---|
| 257 | *n= *(gmp_complex*)x; |
---|
| 258 | *u=(number)n; |
---|
| 259 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
[5fe545] | 260 | return; |
---|
| 261 | } |
---|
| 262 | if (exp&1==1) |
---|
| 263 | { |
---|
| 264 | ngcPower(x,exp-1,u); |
---|
| 265 | gmp_complex *n=new gmp_complex(); |
---|
| 266 | *n=*(gmp_complex*)x; |
---|
| 267 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
| 268 | delete n; |
---|
| 269 | } |
---|
| 270 | else |
---|
| 271 | { |
---|
| 272 | number w; |
---|
| 273 | nNew(&w); |
---|
| 274 | ngcPower(x,exp/2,&w); |
---|
| 275 | ngcPower(w,2,u); |
---|
| 276 | nDelete(&w); |
---|
| 277 | } |
---|
[35aab3] | 278 | } |
---|
| 279 | |
---|
| 280 | BOOLEAN ngcIsZero (number a) |
---|
| 281 | { |
---|
| 282 | return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero()); |
---|
| 283 | } |
---|
| 284 | |
---|
| 285 | number ngcRePart(number a) |
---|
| 286 | { |
---|
| 287 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real()); |
---|
| 288 | return (number)n; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | number ngcImPart(number a) |
---|
| 292 | { |
---|
| 293 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag()); |
---|
| 294 | return (number)n; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | /*2 |
---|
| 298 | * za >= 0 ? |
---|
| 299 | */ |
---|
| 300 | BOOLEAN ngcGreaterZero (number a) |
---|
| 301 | { |
---|
| 302 | if ( ! ((gmp_complex*)a)->imag().isZero() ) |
---|
| 303 | return ( abs( *(gmp_complex*)a).sign() >= 0 ); |
---|
| 304 | else |
---|
| 305 | return ( ((gmp_complex*)a)->real().sign() >= 0 ); |
---|
| 306 | } |
---|
| 307 | |
---|
| 308 | /*2 |
---|
| 309 | * a > b ? |
---|
| 310 | */ |
---|
| 311 | BOOLEAN ngcGreater (number a, number b) |
---|
| 312 | { |
---|
[890d63b] | 313 | gmp_complex *aa=(gmp_complex*)a; |
---|
| 314 | gmp_complex *bb=(gmp_complex*)b; |
---|
| 315 | return (*aa) > (*bb); |
---|
[35aab3] | 316 | } |
---|
| 317 | |
---|
| 318 | /*2 |
---|
| 319 | * a = b ? |
---|
| 320 | */ |
---|
| 321 | BOOLEAN ngcEqual (number a, number b) |
---|
| 322 | { |
---|
[890d63b] | 323 | gmp_complex *aa=(gmp_complex*)a; |
---|
| 324 | gmp_complex *bb=(gmp_complex*)b; |
---|
| 325 | return (*aa) == (*bb); |
---|
[35aab3] | 326 | } |
---|
| 327 | |
---|
| 328 | /*2 |
---|
| 329 | * a == 1 ? |
---|
| 330 | */ |
---|
| 331 | BOOLEAN ngcIsOne (number a) |
---|
| 332 | { |
---|
[78e67e] | 333 | return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
| 334 | //return (((gmp_complex*)a)->real().isOne()); |
---|
[35aab3] | 335 | } |
---|
| 336 | |
---|
| 337 | /*2 |
---|
| 338 | * a == -1 ? |
---|
| 339 | */ |
---|
| 340 | BOOLEAN ngcIsMOne (number a) |
---|
| 341 | { |
---|
[78e67e] | 342 | return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
| 343 | //return (((gmp_complex*)a)->real().isMOne()); |
---|
[35aab3] | 344 | } |
---|
| 345 | |
---|
| 346 | /*2 |
---|
| 347 | * extracts the number a from s, returns the rest |
---|
| 348 | */ |
---|
[85e68dd] | 349 | const char * ngcRead (const char * s, number * a) |
---|
[35aab3] | 350 | { |
---|
[85e68dd] | 351 | const char *start= s; |
---|
[35aab3] | 352 | if ((*s >= '0') && (*s <= '9')) |
---|
| 353 | { |
---|
| 354 | gmp_float *re=NULL; |
---|
| 355 | s=ngfRead(s,(number *)&re); |
---|
| 356 | gmp_complex *aa=new gmp_complex(*re); |
---|
| 357 | *a=(number)aa; |
---|
| 358 | delete re; |
---|
| 359 | } |
---|
| 360 | else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0) |
---|
| 361 | { |
---|
| 362 | s+=strlen(currRing->parameter[0]); |
---|
| 363 | gmp_complex *aa=new gmp_complex((long)0,(long)1); |
---|
| 364 | *a=(number)aa; |
---|
| 365 | } |
---|
| 366 | else |
---|
| 367 | { |
---|
| 368 | *a=(number) new gmp_complex((long)1); |
---|
| 369 | } |
---|
| 370 | return s; |
---|
| 371 | } |
---|
| 372 | |
---|
| 373 | /*2 |
---|
| 374 | * write a floating point number |
---|
| 375 | */ |
---|
[493225] | 376 | void ngcWrite (number &a, const ring r) |
---|
[35aab3] | 377 | { |
---|
| 378 | if (a==NULL) |
---|
[e53182] | 379 | StringAppendS("0"); |
---|
[35aab3] | 380 | else |
---|
| 381 | { |
---|
| 382 | char *out; |
---|
| 383 | out= complexToStr(*(gmp_complex*)a,gmp_output_digits); |
---|
[e53182] | 384 | StringAppendS(out); |
---|
[35aab3] | 385 | // omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) ); |
---|
| 386 | omFree( (ADDRESS)out ); |
---|
| 387 | } |
---|
| 388 | } |
---|
| 389 | |
---|
| 390 | #ifdef LDEBUG |
---|
[85e68dd] | 391 | // not yet implemented |
---|
| 392 | //BOOLEAN ngcDBTest(number a, const char *f, const int l) |
---|
| 393 | //{ |
---|
| 394 | // return TRUE; |
---|
| 395 | //} |
---|
[35aab3] | 396 | #endif |
---|
| 397 | |
---|
| 398 | // local Variables: *** |
---|
| 399 | // folded-file: t *** |
---|
| 400 | // compile-command: "make installg" *** |
---|
| 401 | // End: *** |
---|