[35aab3] | 1 | /**************************************** |
---|
| 2 | * Computer Algebra System SINGULAR * |
---|
| 3 | ****************************************/ |
---|
| 4 | /* |
---|
| 5 | * ABSTRACT: computations with GMP complex floating-point numbers |
---|
| 6 | * |
---|
| 7 | * ngc == number gnu complex |
---|
| 8 | */ |
---|
| 9 | |
---|
[16f511] | 10 | #ifdef HAVE_CONFIG_H |
---|
[691dba] | 11 | #include "config.h" |
---|
[16f511] | 12 | #endif /* HAVE_CONFIG_H */ |
---|
[c3d175] | 13 | |
---|
[7fee876] | 14 | #include <omalloc/omalloc.h> |
---|
| 15 | |
---|
| 16 | #include <misc/auxiliary.h> |
---|
| 17 | #include <misc/mylimits.h> |
---|
| 18 | |
---|
| 19 | #include <reporter/reporter.h> |
---|
| 20 | |
---|
[2d805a] | 21 | #include <coeffs/coeffs.h> |
---|
| 22 | #include <coeffs/numbers.h> |
---|
[7fee876] | 23 | |
---|
[2d805a] | 24 | #include <coeffs/longrat.h> |
---|
| 25 | #include <coeffs/modulop.h> |
---|
[7fee876] | 26 | |
---|
[2d805a] | 27 | #include <coeffs/gnumpc.h> |
---|
| 28 | #include <coeffs/gnumpfl.h> |
---|
[7fee876] | 29 | #include <coeffs/shortfl.h> |
---|
| 30 | |
---|
[2d805a] | 31 | #include <coeffs/mpr_complex.h> |
---|
[35aab3] | 32 | |
---|
| 33 | |
---|
[7fee876] | 34 | /// Get a mapping function from src into the domain of this type: long_C! |
---|
| 35 | nMapFunc ngcSetMap(const coeffs src, const coeffs dst); |
---|
| 36 | |
---|
| 37 | number ngcMapQ(number from, const coeffs r, const coeffs aRing); |
---|
| 38 | |
---|
| 39 | void ngcSetChar(const coeffs r); |
---|
| 40 | |
---|
| 41 | // Private interface should be hidden!!! |
---|
| 42 | |
---|
| 43 | /// Note: MAY NOT WORK AS EXPECTED! |
---|
| 44 | BOOLEAN ngcGreaterZero(number za, const coeffs r); |
---|
| 45 | BOOLEAN ngcGreater(number a, number b, const coeffs r); |
---|
| 46 | BOOLEAN ngcEqual(number a, number b, const coeffs r); |
---|
| 47 | BOOLEAN ngcIsOne(number a, const coeffs r); |
---|
| 48 | BOOLEAN ngcIsMOne(number a, const coeffs r); |
---|
| 49 | BOOLEAN ngcIsZero(number za, const coeffs r); |
---|
| 50 | number ngcInit(long i, const coeffs r); |
---|
| 51 | int ngcInt(number &n, const coeffs r); |
---|
| 52 | number ngcNeg(number za, const coeffs r); |
---|
| 53 | number ngcInvers(number a, const coeffs r); |
---|
| 54 | number ngcParameter(int i, const coeffs r); |
---|
| 55 | number ngcAdd(number la, number li, const coeffs r); |
---|
| 56 | number ngcSub(number la, number li, const coeffs r); |
---|
| 57 | number ngcMult(number a, number b, const coeffs r); |
---|
| 58 | number ngcDiv(number a, number b, const coeffs r); |
---|
| 59 | void ngcPower(number x, int exp, number *lu, const coeffs r); |
---|
| 60 | number ngcCopy(number a, const coeffs r); |
---|
| 61 | number ngc_Copy(number a, coeffs r); |
---|
| 62 | const char * ngcRead (const char *s, number *a, const coeffs r); |
---|
| 63 | void ngcWrite(number &a, const coeffs r); |
---|
| 64 | number ngcRePart(number a, const coeffs r); |
---|
| 65 | number ngcImPart(number a, const coeffs r); |
---|
| 66 | |
---|
| 67 | void ngcDelete(number *a, const coeffs r); |
---|
| 68 | void ngcCoeffWrite(const coeffs r, BOOLEAN details); |
---|
| 69 | |
---|
| 70 | #ifdef LDEBUG |
---|
| 71 | BOOLEAN ngcDBTest(number a, const char *f, const int l, const coeffs r); |
---|
| 72 | #endif |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | // Why is this here? who needs it? |
---|
| 76 | // number ngcMapQ(number from, const coeffs r, const coeffs aRing); |
---|
[3aae0e] | 77 | |
---|
[51d835] | 78 | /// Our Type! |
---|
| 79 | static const n_coeffType ID = n_long_C; |
---|
| 80 | |
---|
[3aae0e] | 81 | |
---|
[94b759] | 82 | #ifdef LDEBUG |
---|
| 83 | // not yet implemented |
---|
[9bb5457] | 84 | BOOLEAN ngcDBTest(number, const char *, const int, const coeffs r) |
---|
[94b759] | 85 | { |
---|
| 86 | assume( getCoeffType(r) == ID ); |
---|
| 87 | |
---|
| 88 | return TRUE; |
---|
| 89 | } |
---|
| 90 | #endif |
---|
| 91 | |
---|
[7fee876] | 92 | number ngcParameter(int i, const coeffs r) |
---|
[35aab3] | 93 | { |
---|
[94b759] | 94 | assume( getCoeffType(r) == ID ); |
---|
[121fd9] | 95 | assume(i==1); |
---|
[94b759] | 96 | |
---|
[7fee876] | 97 | if( i == 1 ) |
---|
| 98 | return (number)(new gmp_complex( (long)0, (long)1 )); |
---|
| 99 | |
---|
| 100 | return NULL; // new gmp_complex( ) // 0? |
---|
[35aab3] | 101 | } |
---|
| 102 | |
---|
| 103 | /*2 |
---|
| 104 | * n := i |
---|
| 105 | */ |
---|
[2f3764] | 106 | number ngcInit (long i, const coeffs r) |
---|
[35aab3] | 107 | { |
---|
[94b759] | 108 | assume( getCoeffType(r) == ID ); |
---|
[9bb5457] | 109 | |
---|
[78e67e] | 110 | gmp_complex* n= new gmp_complex( (long)i, (long)0 ); |
---|
[9bb5457] | 111 | |
---|
[35aab3] | 112 | return (number)n; |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | /*2 |
---|
| 116 | * convert number to int |
---|
| 117 | */ |
---|
[7d90aa] | 118 | int ngcInt(number &i, const coeffs r) |
---|
[35aab3] | 119 | { |
---|
[94b759] | 120 | assume( getCoeffType(r) == ID ); |
---|
[9bb5457] | 121 | |
---|
[35aab3] | 122 | return (int)((gmp_complex*)i)->real(); |
---|
| 123 | } |
---|
| 124 | |
---|
[95d8df] | 125 | int ngcSize(number n, const coeffs R) |
---|
[12cca3] | 126 | { |
---|
| 127 | int r = (int)((gmp_complex*)n)->real(); |
---|
| 128 | if (r < 0) r = -r; |
---|
| 129 | int i = (int)((gmp_complex*)n)->imag(); |
---|
| 130 | if (i < 0) i = -i; |
---|
| 131 | int oneNorm = r + i; |
---|
| 132 | /* basically return the 1-norm of n; |
---|
| 133 | only if this happens to be zero although n != 0, |
---|
| 134 | return 1; |
---|
| 135 | (this code ensures that zero has the size zero) */ |
---|
[95d8df] | 136 | if ((oneNorm == 0.0) & (ngcIsZero(n,R) == FALSE)) oneNorm = 1; |
---|
[12cca3] | 137 | return oneNorm; |
---|
| 138 | } |
---|
| 139 | |
---|
[35aab3] | 140 | /*2 |
---|
| 141 | * delete a |
---|
| 142 | */ |
---|
[7d90aa] | 143 | void ngcDelete (number * a, const coeffs r) |
---|
[35aab3] | 144 | { |
---|
[94b759] | 145 | assume( getCoeffType(r) == ID ); |
---|
| 146 | |
---|
[7c961ba] | 147 | if ( *a != NULL ) |
---|
| 148 | { |
---|
[35aab3] | 149 | delete *(gmp_complex**)a; |
---|
| 150 | *a=NULL; |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | /*2 |
---|
[8cb8e1] | 155 | * copy a to b |
---|
[35aab3] | 156 | */ |
---|
[94b759] | 157 | number ngcCopy(number a, const coeffs r) |
---|
[35aab3] | 158 | { |
---|
[94b759] | 159 | assume( getCoeffType(r) == ID ); |
---|
[9bb5457] | 160 | |
---|
[78e67e] | 161 | gmp_complex* b= new gmp_complex( *(gmp_complex*)a ); |
---|
[35aab3] | 162 | return (number)b; |
---|
| 163 | } |
---|
[94b759] | 164 | |
---|
[35aab3] | 165 | |
---|
| 166 | /*2 |
---|
| 167 | * za:= - za |
---|
| 168 | */ |
---|
[94b759] | 169 | number ngcNeg (number a, const coeffs R) |
---|
[35aab3] | 170 | { |
---|
[94b759] | 171 | assume( getCoeffType(R) == ID ); |
---|
| 172 | |
---|
[0f4db3] | 173 | gmp_complex* r=(gmp_complex*)a; |
---|
[95d8df] | 174 | (*r).neg(); |
---|
[35aab3] | 175 | return (number)a; |
---|
| 176 | } |
---|
| 177 | |
---|
| 178 | /* |
---|
| 179 | * 1/a |
---|
| 180 | */ |
---|
[94b759] | 181 | number ngcInvers(number a, const coeffs R) |
---|
[35aab3] | 182 | { |
---|
[94b759] | 183 | assume( getCoeffType(R) == ID ); |
---|
| 184 | |
---|
[b7e838] | 185 | gmp_complex* r = NULL; |
---|
[78e67e] | 186 | if (((gmp_complex*)a)->isZero()) |
---|
[35aab3] | 187 | { |
---|
[b7e838] | 188 | WerrorS(nDivBy0); |
---|
[35aab3] | 189 | } |
---|
| 190 | else |
---|
| 191 | { |
---|
[95d8df] | 192 | r = new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) ); |
---|
[35aab3] | 193 | } |
---|
[95d8df] | 194 | return (number)r; |
---|
[35aab3] | 195 | } |
---|
| 196 | |
---|
| 197 | /*2 |
---|
| 198 | * u:= a + b |
---|
| 199 | */ |
---|
[94b759] | 200 | number ngcAdd (number a, number b, const coeffs R) |
---|
[35aab3] | 201 | { |
---|
[94b759] | 202 | assume( getCoeffType(R) == ID ); |
---|
| 203 | |
---|
[78e67e] | 204 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) ); |
---|
[35aab3] | 205 | return (number)r; |
---|
| 206 | } |
---|
| 207 | |
---|
| 208 | /*2 |
---|
| 209 | * u:= a - b |
---|
| 210 | */ |
---|
[8cb8e1] | 211 | number ngcSub (number a, number b, const coeffs R) |
---|
[35aab3] | 212 | { |
---|
[94b759] | 213 | assume( getCoeffType(R) == ID ); |
---|
| 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 | */ |
---|
[94b759] | 222 | number ngcMult (number a, number b, const coeffs R) |
---|
[35aab3] | 223 | { |
---|
[94b759] | 224 | assume( getCoeffType(R) == ID ); |
---|
[9bb5457] | 225 | |
---|
[78e67e] | 226 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) ); |
---|
[35aab3] | 227 | return (number)r; |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | /*2 |
---|
| 231 | * u := a / b |
---|
| 232 | */ |
---|
[94b759] | 233 | number ngcDiv (number a, number b, const coeffs r) |
---|
[35aab3] | 234 | { |
---|
[94b759] | 235 | assume( getCoeffType(r) == ID ); |
---|
| 236 | |
---|
[78e67e] | 237 | if (((gmp_complex*)b)->isZero()) |
---|
[35aab3] | 238 | { |
---|
[b7e838] | 239 | // a/0 = error |
---|
| 240 | WerrorS(nDivBy0); |
---|
[35aab3] | 241 | return NULL; |
---|
| 242 | } |
---|
[94b759] | 243 | gmp_complex* res = new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) ); |
---|
| 244 | return (number)res; |
---|
[35aab3] | 245 | } |
---|
| 246 | |
---|
| 247 | /*2 |
---|
| 248 | * u:= x ^ exp |
---|
| 249 | */ |
---|
[94b759] | 250 | void ngcPower ( number x, int exp, number * u, const coeffs r) |
---|
[35aab3] | 251 | { |
---|
[94b759] | 252 | assume( getCoeffType(r) == ID ); |
---|
| 253 | |
---|
[35aab3] | 254 | if ( exp == 0 ) |
---|
| 255 | { |
---|
| 256 | gmp_complex* n = new gmp_complex(1); |
---|
| 257 | *u=(number)n; |
---|
| 258 | return; |
---|
| 259 | } |
---|
[5fe545] | 260 | else if ( exp == 1 ) |
---|
[35aab3] | 261 | { |
---|
[94b759] | 262 | n_New(u, r); |
---|
[78e67e] | 263 | gmp_complex* n = new gmp_complex(); |
---|
| 264 | *n= *(gmp_complex*)x; |
---|
| 265 | *u=(number)n; |
---|
[35aab3] | 266 | return; |
---|
| 267 | } |
---|
[5fe545] | 268 | else if (exp == 2) |
---|
| 269 | { |
---|
[94b759] | 270 | n_New(u, r); |
---|
[78e67e] | 271 | gmp_complex* n = new gmp_complex(); |
---|
| 272 | *n= *(gmp_complex*)x; |
---|
| 273 | *u=(number)n; |
---|
| 274 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
[5fe545] | 275 | return; |
---|
| 276 | } |
---|
[039016] | 277 | if ( (exp & 1) == 1 ) |
---|
[5fe545] | 278 | { |
---|
[94b759] | 279 | ngcPower(x,exp-1,u, r); |
---|
| 280 | gmp_complex *n = new gmp_complex(); |
---|
[5fe545] | 281 | *n=*(gmp_complex*)x; |
---|
| 282 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
| 283 | delete n; |
---|
| 284 | } |
---|
| 285 | else |
---|
| 286 | { |
---|
| 287 | number w; |
---|
[94b759] | 288 | n_New(&w, r); |
---|
| 289 | ngcPower(x,exp/2,&w, r); |
---|
| 290 | ngcPower(w,2,u, r); |
---|
| 291 | n_Delete(&w, r); |
---|
[5fe545] | 292 | } |
---|
[35aab3] | 293 | } |
---|
| 294 | |
---|
[94b759] | 295 | BOOLEAN ngcIsZero (number a, const coeffs r) |
---|
[35aab3] | 296 | { |
---|
[94b759] | 297 | assume( getCoeffType(r) == ID ); |
---|
| 298 | |
---|
[35aab3] | 299 | return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero()); |
---|
| 300 | } |
---|
| 301 | |
---|
[94b759] | 302 | number ngcRePart(number a, const coeffs r) |
---|
[35aab3] | 303 | { |
---|
[94b759] | 304 | assume( getCoeffType(r) == ID ); |
---|
| 305 | |
---|
[35aab3] | 306 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real()); |
---|
| 307 | return (number)n; |
---|
| 308 | } |
---|
| 309 | |
---|
[94b759] | 310 | number ngcImPart(number a, const coeffs r) |
---|
[35aab3] | 311 | { |
---|
[94b759] | 312 | assume( getCoeffType(r) == ID ); |
---|
| 313 | |
---|
[35aab3] | 314 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag()); |
---|
| 315 | return (number)n; |
---|
| 316 | } |
---|
| 317 | |
---|
| 318 | /*2 |
---|
| 319 | * za >= 0 ? |
---|
| 320 | */ |
---|
[94b759] | 321 | BOOLEAN ngcGreaterZero (number a, const coeffs r) |
---|
[35aab3] | 322 | { |
---|
[94b759] | 323 | assume( getCoeffType(r) == ID ); |
---|
| 324 | |
---|
[35aab3] | 325 | if ( ! ((gmp_complex*)a)->imag().isZero() ) |
---|
| 326 | return ( abs( *(gmp_complex*)a).sign() >= 0 ); |
---|
| 327 | else |
---|
| 328 | return ( ((gmp_complex*)a)->real().sign() >= 0 ); |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /*2 |
---|
| 332 | * a > b ? |
---|
| 333 | */ |
---|
[94b759] | 334 | BOOLEAN ngcGreater (number a, number b, const coeffs r) |
---|
[35aab3] | 335 | { |
---|
[94b759] | 336 | assume( getCoeffType(r) == ID ); |
---|
| 337 | |
---|
[890d63b] | 338 | gmp_complex *aa=(gmp_complex*)a; |
---|
| 339 | gmp_complex *bb=(gmp_complex*)b; |
---|
| 340 | return (*aa) > (*bb); |
---|
[35aab3] | 341 | } |
---|
| 342 | |
---|
| 343 | /*2 |
---|
| 344 | * a = b ? |
---|
| 345 | */ |
---|
[94b759] | 346 | BOOLEAN ngcEqual (number a, number b, const coeffs r) |
---|
[35aab3] | 347 | { |
---|
[94b759] | 348 | assume( getCoeffType(r) == ID ); |
---|
| 349 | |
---|
[890d63b] | 350 | gmp_complex *aa=(gmp_complex*)a; |
---|
| 351 | gmp_complex *bb=(gmp_complex*)b; |
---|
| 352 | return (*aa) == (*bb); |
---|
[35aab3] | 353 | } |
---|
| 354 | |
---|
| 355 | /*2 |
---|
| 356 | * a == 1 ? |
---|
| 357 | */ |
---|
[94b759] | 358 | BOOLEAN ngcIsOne (number a, const coeffs r) |
---|
[35aab3] | 359 | { |
---|
[94b759] | 360 | assume( getCoeffType(r) == ID ); |
---|
| 361 | |
---|
[78e67e] | 362 | return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
| 363 | //return (((gmp_complex*)a)->real().isOne()); |
---|
[35aab3] | 364 | } |
---|
| 365 | |
---|
| 366 | /*2 |
---|
| 367 | * a == -1 ? |
---|
| 368 | */ |
---|
[94b759] | 369 | BOOLEAN ngcIsMOne (number a, const coeffs r) |
---|
[35aab3] | 370 | { |
---|
[94b759] | 371 | assume( getCoeffType(r) == ID ); |
---|
| 372 | |
---|
[78e67e] | 373 | return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
| 374 | //return (((gmp_complex*)a)->real().isMOne()); |
---|
[35aab3] | 375 | } |
---|
| 376 | |
---|
| 377 | /*2 |
---|
| 378 | * extracts the number a from s, returns the rest |
---|
| 379 | */ |
---|
[94b759] | 380 | const char * ngcRead (const char * s, number * a, const coeffs r) |
---|
[35aab3] | 381 | { |
---|
[94b759] | 382 | assume( getCoeffType(r) == ID ); |
---|
[7fee876] | 383 | const char * const complex_parameter = n_ParameterNames(r)[0]; |
---|
| 384 | assume( complex_parameter != NULL ); |
---|
| 385 | const int N = strlen(complex_parameter); |
---|
[bfa04e] | 386 | |
---|
[35aab3] | 387 | if ((*s >= '0') && (*s <= '9')) |
---|
| 388 | { |
---|
| 389 | gmp_float *re=NULL; |
---|
[94b759] | 390 | s=ngfRead(s,(number *)&re, r); |
---|
[35aab3] | 391 | gmp_complex *aa=new gmp_complex(*re); |
---|
| 392 | *a=(number)aa; |
---|
| 393 | delete re; |
---|
| 394 | } |
---|
[7fee876] | 395 | else if (strncmp(s, complex_parameter, N)==0) |
---|
[35aab3] | 396 | { |
---|
[7fee876] | 397 | s += N; |
---|
[35aab3] | 398 | gmp_complex *aa=new gmp_complex((long)0,(long)1); |
---|
| 399 | *a=(number)aa; |
---|
| 400 | } |
---|
| 401 | else |
---|
| 402 | { |
---|
| 403 | *a=(number) new gmp_complex((long)1); |
---|
| 404 | } |
---|
| 405 | return s; |
---|
| 406 | } |
---|
| 407 | |
---|
[94b759] | 408 | |
---|
| 409 | |
---|
[35aab3] | 410 | /*2 |
---|
| 411 | * write a floating point number |
---|
| 412 | */ |
---|
[7d90aa] | 413 | void ngcWrite (number &a, const coeffs r) |
---|
[35aab3] | 414 | { |
---|
[94b759] | 415 | assume( getCoeffType(r) == ID ); |
---|
| 416 | |
---|
| 417 | extern size_t gmp_output_digits; /// comes from mpr_complex.cc |
---|
| 418 | |
---|
[35aab3] | 419 | if (a==NULL) |
---|
[e53182] | 420 | StringAppendS("0"); |
---|
[35aab3] | 421 | else |
---|
| 422 | { |
---|
| 423 | char *out; |
---|
[ac5686] | 424 | out= complexToStr(*(gmp_complex*)a, gmp_output_digits, r); |
---|
[e53182] | 425 | StringAppendS(out); |
---|
[7d90aa] | 426 | // omFreeSize((void *)out, (strlen(out)+1)* sizeof(char) ); |
---|
| 427 | omFree( (void *)out ); |
---|
[35aab3] | 428 | } |
---|
| 429 | } |
---|
| 430 | |
---|
[32cc7e] | 431 | BOOLEAN ngcCoeffIsEqual (const coeffs r, n_coeffType n, void * parameter) |
---|
| 432 | { |
---|
[47ab5b] | 433 | if (n==ID) |
---|
| 434 | { |
---|
[32cc7e] | 435 | LongComplexInfo* p = (LongComplexInfo *)(parameter); |
---|
[47ab5b] | 436 | |
---|
| 437 | if ( |
---|
| 438 | (p->float_len == r->float_len) && |
---|
| 439 | (p->float_len2 == r->float_len2) |
---|
| 440 | ) |
---|
[7fee876] | 441 | if (strcmp(p->par_name, n_ParameterNames(r)[0]) == 0) |
---|
[47ab5b] | 442 | return (TRUE); |
---|
[32cc7e] | 443 | } |
---|
[47ab5b] | 444 | return (FALSE); |
---|
[32cc7e] | 445 | } |
---|
| 446 | |
---|
[7fee876] | 447 | static void ngcKillChar(coeffs r) |
---|
| 448 | { |
---|
| 449 | char** p = (char**)n_ParameterNames(r); |
---|
| 450 | |
---|
| 451 | const int P = n_NumberOfParameters(r); |
---|
| 452 | |
---|
| 453 | for( int i = 1; i <= P; i++ ) |
---|
| 454 | if (p[i-1] != NULL) |
---|
| 455 | omFree( (ADDRESS)p[i-1] ); |
---|
| 456 | |
---|
| 457 | omFreeSize((ADDRESS)p, P * sizeof(char*)); |
---|
| 458 | } |
---|
| 459 | |
---|
[32cc7e] | 460 | BOOLEAN ngcInitChar(coeffs n, void* parameter) |
---|
[51d835] | 461 | { |
---|
| 462 | assume( getCoeffType(n) == ID ); |
---|
[32cc7e] | 463 | |
---|
[7fee876] | 464 | n->cfKillChar = ngcKillChar; |
---|
[73a9ffb] | 465 | n->ch = 0; |
---|
[51d835] | 466 | |
---|
| 467 | n->cfDelete = ngcDelete; |
---|
[7bbbef] | 468 | n->cfNormalize=ndNormalize; |
---|
[51d835] | 469 | n->cfInit = ngcInit; |
---|
[7bbbef] | 470 | n->cfInt = ngcInt; |
---|
| 471 | n->cfAdd = ngcAdd; |
---|
| 472 | n->cfSub = ngcSub; |
---|
| 473 | n->cfMult = ngcMult; |
---|
| 474 | n->cfDiv = ngcDiv; |
---|
| 475 | n->cfExactDiv= ngcDiv; |
---|
| 476 | n->cfNeg = ngcNeg; |
---|
| 477 | n->cfInvers = ngcInvers; |
---|
[51d835] | 478 | n->cfCopy = ngcCopy; |
---|
[7bbbef] | 479 | n->cfGreater = ngcGreater; |
---|
| 480 | n->cfEqual = ngcEqual; |
---|
| 481 | n->cfIsZero = ngcIsZero; |
---|
| 482 | n->cfIsOne = ngcIsOne; |
---|
| 483 | n->cfIsMOne = ngcIsMOne; |
---|
| 484 | n->cfGreaterZero = ngcGreaterZero; |
---|
[ce1f78] | 485 | |
---|
| 486 | n->cfWriteLong = ngcWrite; |
---|
| 487 | n->cfWriteShort = ngcWrite; |
---|
| 488 | |
---|
[7bbbef] | 489 | n->cfRead = ngcRead; |
---|
| 490 | n->cfPower = ngcPower; |
---|
[51d835] | 491 | n->cfSetMap = ngcSetMap; |
---|
[7bbbef] | 492 | n->cfRePart = ngcRePart; |
---|
| 493 | n->cfImPart = ngcImPart; |
---|
[7a8011] | 494 | n->cfCoeffWrite = ngcCoeffWrite; |
---|
[7bbbef] | 495 | // cfSize = ndSize; |
---|
[51d835] | 496 | #ifdef LDEBUG |
---|
[7bbbef] | 497 | n->cfDBTest = ndDBTest; // not yet implemented: ngcDBTest |
---|
[51d835] | 498 | #endif |
---|
| 499 | |
---|
[32cc7e] | 500 | n->nCoeffIsEqual = ngcCoeffIsEqual; |
---|
[039016] | 501 | |
---|
[32cc7e] | 502 | n->cfSetChar=ngcSetChar; |
---|
[51d835] | 503 | |
---|
[8df232] | 504 | n->cfInit_bigint=ngcMapQ; |
---|
[9bb5457] | 505 | |
---|
[619d03] | 506 | // we need to initialize n->nNULL at least for minpoly printing |
---|
| 507 | n->nNULL = n->cfInit(0,n); |
---|
| 508 | |
---|
[9bb5457] | 509 | /* |
---|
[51d835] | 510 | //r->cfInitChar=nlInitChar; |
---|
| 511 | r->cfKillChar=NULL; |
---|
| 512 | |
---|
[7bbbef] | 513 | r->cfMult = nlMult; |
---|
| 514 | r->cfSub = nlSub; |
---|
| 515 | r->cfAdd = nlAdd; |
---|
| 516 | r->cfDiv = nlDiv; |
---|
| 517 | r->cfIntDiv= nlIntDiv; |
---|
| 518 | r->cfIntMod= nlIntMod; |
---|
| 519 | r->cfExactDiv= nlExactDiv; |
---|
[51d835] | 520 | r->cfInit = nlInit; |
---|
[7bbbef] | 521 | r->cfSize = nlSize; |
---|
| 522 | r->cfInt = nlInt; |
---|
[51d835] | 523 | #ifdef HAVE_RINGS |
---|
[7bbbef] | 524 | r->cfDivComp = NULL; // only for ring stuff |
---|
| 525 | r->cfIsUnit = NULL; // only for ring stuff |
---|
| 526 | r->cfGetUnit = NULL; // only for ring stuff |
---|
| 527 | r->cfExtGcd = NULL; // only for ring stuff |
---|
[51d835] | 528 | #endif |
---|
[7bbbef] | 529 | r->cfNeg = nlNeg; |
---|
| 530 | r->cfInvers= nlInvers; |
---|
[51d835] | 531 | r->cfCopy = nl_Copy; |
---|
[7bbbef] | 532 | r->cfRePart = nl_Copy; |
---|
| 533 | r->cfImPart = ndReturn0; |
---|
[ce1f78] | 534 | r->cfWriteLong = nlWrite; |
---|
[7bbbef] | 535 | r->cfRead = nlRead; |
---|
| 536 | r->cfNormalize=nlNormalize; |
---|
| 537 | r->cfGreater = nlGreater; |
---|
[51d835] | 538 | #ifdef HAVE_RINGS |
---|
[7bbbef] | 539 | r->cfDivBy = NULL; // only for ring stuff |
---|
[51d835] | 540 | #endif |
---|
[7bbbef] | 541 | r->cfEqual = nlEqual; |
---|
| 542 | r->cfIsZero = nlIsZero; |
---|
| 543 | r->cfIsOne = nlIsOne; |
---|
| 544 | r->cfIsMOne = nlIsMOne; |
---|
| 545 | r->cfGreaterZero = nlGreaterZero; |
---|
| 546 | r->cfPower = nlPower; |
---|
[51d835] | 547 | r->cfGetDenom = nlGetDenom; |
---|
| 548 | r->cfGetNumerator = nlGetNumerator; |
---|
[7bbbef] | 549 | r->cfGcd = nlGcd; |
---|
| 550 | r->cfLcm = nlLcm; |
---|
[51d835] | 551 | r->cfDelete= nlDelete; |
---|
| 552 | r->cfSetMap = nlSetMap; |
---|
[7bbbef] | 553 | r->cfName = ndName; |
---|
| 554 | r->cfInpMult=nlInpMult; |
---|
| 555 | r->cfInit_bigint=nlCopyMap; |
---|
[51d835] | 556 | #ifdef LDEBUG |
---|
| 557 | // debug stuff |
---|
[7bbbef] | 558 | r->cfDBTest=nlDBTest; |
---|
[51d835] | 559 | #endif |
---|
| 560 | |
---|
| 561 | // the variables: |
---|
| 562 | r->nNULL = INT_TO_SR(0); |
---|
| 563 | r->type = n_Q; |
---|
| 564 | r->ch = 0; |
---|
| 565 | r->has_simple_Alloc=FALSE; |
---|
| 566 | r->has_simple_Inverse=FALSE; |
---|
| 567 | */ |
---|
[7fee876] | 568 | |
---|
| 569 | n->iNumberOfParameters = 1; |
---|
| 570 | n->cfParameter = ngcParameter; |
---|
| 571 | |
---|
| 572 | char ** pParameterNames = (char **) omAlloc0(sizeof(char *)); |
---|
| 573 | |
---|
[733d24] | 574 | if( parameter != NULL) |
---|
| 575 | { |
---|
| 576 | LongComplexInfo* p = (LongComplexInfo*)parameter; |
---|
[7fee876] | 577 | pParameterNames[0] = omStrDup(p->par_name); //TODO use omAlloc for allocating memory and use strcpy? |
---|
[733d24] | 578 | n->float_len = p->float_len; |
---|
| 579 | n->float_len2 = p->float_len2; |
---|
| 580 | |
---|
| 581 | } else // default values, just for testing! |
---|
| 582 | { |
---|
[7fee876] | 583 | pParameterNames[0] = omStrDup("i"); |
---|
[733d24] | 584 | n->float_len = SHORT_REAL_LENGTH; |
---|
| 585 | n->float_len2 = SHORT_REAL_LENGTH; |
---|
| 586 | } |
---|
| 587 | |
---|
| 588 | assume( n->float_len <= n->float_len2 ); |
---|
| 589 | assume( n->float_len2 >= SHORT_REAL_LENGTH ); |
---|
[7fee876] | 590 | assume( pParameterNames != NULL ); |
---|
| 591 | assume( pParameterNames[0] != NULL ); |
---|
| 592 | |
---|
| 593 | n->pParameterNames = pParameterNames; |
---|
[9bb5457] | 594 | |
---|
[7fee876] | 595 | // NOTE: n->complex_parameter was replaced by n_ParameterNames(n)[0] |
---|
| 596 | // TODO: nfKillChar MUST destroy n->pParameterNames[0] (0-term. string) && n->pParameterNames (array of size 1) |
---|
| 597 | |
---|
[5d594a9] | 598 | return FALSE; |
---|
[51d835] | 599 | } |
---|
| 600 | |
---|
[32cc7e] | 601 | void ngcSetChar(const coeffs r) |
---|
| 602 | { |
---|
| 603 | setGMPFloatDigits(r->float_len, r->float_len2); |
---|
| 604 | } |
---|
[51d835] | 605 | |
---|
| 606 | |
---|
| 607 | |
---|
| 608 | number ngcMapQ(number from, const coeffs aRing, const coeffs r) |
---|
| 609 | { |
---|
| 610 | assume( getCoeffType(r) == ID ); |
---|
| 611 | assume( getCoeffType(aRing) == n_Q ); |
---|
| 612 | |
---|
| 613 | if ( from != NULL ) |
---|
| 614 | { |
---|
| 615 | gmp_complex *res=new gmp_complex(numberFieldToFloat(from,QTOF,aRing)); |
---|
| 616 | return (number)res; |
---|
| 617 | } |
---|
| 618 | else |
---|
| 619 | return NULL; |
---|
| 620 | } |
---|
| 621 | |
---|
| 622 | static number ngcMapLongR(number from, const coeffs aRing, const coeffs r) |
---|
| 623 | { |
---|
| 624 | assume( getCoeffType(r) == ID ); |
---|
| 625 | assume( getCoeffType(aRing) == n_long_R ); |
---|
| 626 | |
---|
| 627 | if ( from != NULL ) |
---|
| 628 | { |
---|
| 629 | gmp_complex *res=new gmp_complex(*((gmp_float *)from)); |
---|
| 630 | return (number)res; |
---|
| 631 | } |
---|
| 632 | else |
---|
| 633 | return NULL; |
---|
| 634 | } |
---|
| 635 | |
---|
| 636 | static number ngcMapR(number from, const coeffs aRing, const coeffs r) |
---|
| 637 | { |
---|
| 638 | assume( getCoeffType(r) == ID ); |
---|
| 639 | assume( getCoeffType(aRing) == n_R ); |
---|
| 640 | |
---|
| 641 | if ( from != NULL ) |
---|
| 642 | { |
---|
| 643 | gmp_complex *res=new gmp_complex((double)nrFloat(from)); |
---|
| 644 | return (number)res; |
---|
| 645 | } |
---|
| 646 | else |
---|
| 647 | return NULL; |
---|
| 648 | } |
---|
| 649 | |
---|
| 650 | static number ngcMapP(number from, const coeffs aRing, const coeffs r) |
---|
| 651 | { |
---|
| 652 | assume( getCoeffType(r) == ID ); |
---|
| 653 | assume( getCoeffType(aRing) == n_Zp ); |
---|
| 654 | |
---|
[210852] | 655 | if ( from != NULL ) |
---|
[51d835] | 656 | return ngcInit(npInt(from, aRing), r); |
---|
| 657 | else |
---|
| 658 | return NULL; |
---|
| 659 | } |
---|
| 660 | |
---|
| 661 | static number ngcCopyMap(number from, const coeffs aRing, const coeffs r) |
---|
| 662 | { |
---|
| 663 | assume( getCoeffType(r) == ID ); |
---|
| 664 | assume( getCoeffType(aRing) == ID ); |
---|
| 665 | |
---|
| 666 | gmp_complex* b = NULL; |
---|
| 667 | |
---|
| 668 | if ( from != NULL ) |
---|
[9bb5457] | 669 | { |
---|
[51d835] | 670 | b = new gmp_complex( *(gmp_complex*)from ); |
---|
| 671 | } |
---|
[9bb5457] | 672 | return (number)b; |
---|
[51d835] | 673 | } |
---|
| 674 | |
---|
| 675 | nMapFunc ngcSetMap(const coeffs src, const coeffs dst) |
---|
| 676 | { |
---|
| 677 | assume( getCoeffType(dst) == ID ); |
---|
| 678 | |
---|
[1cce47] | 679 | if (nCoeff_is_Q(src)) |
---|
[51d835] | 680 | { |
---|
| 681 | return ngcMapQ; |
---|
| 682 | } |
---|
[1cce47] | 683 | if (nCoeff_is_long_R(src)) |
---|
[51d835] | 684 | { |
---|
| 685 | return ngcMapLongR; |
---|
| 686 | } |
---|
[1cce47] | 687 | if (nCoeff_is_long_C(src)) |
---|
[51d835] | 688 | { |
---|
| 689 | return ngcCopyMap; |
---|
| 690 | } |
---|
[1cce47] | 691 | if (nCoeff_is_R(src)) |
---|
[51d835] | 692 | { |
---|
| 693 | return ngcMapR; |
---|
| 694 | } |
---|
[1cce47] | 695 | if (nCoeff_is_Zp(src)) |
---|
[51d835] | 696 | { |
---|
| 697 | return ngcMapP; |
---|
| 698 | } |
---|
| 699 | return NULL; |
---|
| 700 | } |
---|
| 701 | |
---|
[2e4ec14] | 702 | void ngcCoeffWrite (const coeffs r, BOOLEAN /*details*/) |
---|
[7a8011] | 703 | { |
---|
| 704 | Print("// characteristic : 0 (complex:%d digits, additional %d digits)\n", |
---|
[03f7b5] | 705 | r->float_len, r->float_len2); /* long C */ |
---|
[7fee876] | 706 | Print("// 1 parameter : %s \n", n_ParameterNames(r)[0]); // this trailing space is for compatibility with the legacy Singular |
---|
| 707 | Print("// minpoly : (%s^2+1)\n", n_ParameterNames(r)[0]); |
---|
[7a8011] | 708 | } |
---|