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