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