1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id$ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: computations with GMP complex floating-point numbers |
---|
7 | * |
---|
8 | * ngc == number gnu complex |
---|
9 | */ |
---|
10 | |
---|
11 | #include "mod2.h" |
---|
12 | #include "structs.h" |
---|
13 | #include "febase.h" |
---|
14 | #include "omalloc.h" |
---|
15 | #include "numbers.h" |
---|
16 | #include "longrat.h" |
---|
17 | #include "modulop.h" |
---|
18 | #include "gnumpc.h" |
---|
19 | #include "gnumpfl.h" |
---|
20 | #include "mpr_complex.h" |
---|
21 | |
---|
22 | extern size_t gmp_output_digits; |
---|
23 | |
---|
24 | |
---|
25 | number ngcMapQ(number from) |
---|
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 | } |
---|
64 | extern ring ngfMapRing; |
---|
65 | static number ngcMapP(number from) |
---|
66 | { |
---|
67 | if ( from != NULL) |
---|
68 | return ngcInit(npInt(from,ngfMapRing), currRing); |
---|
69 | else |
---|
70 | return NULL; |
---|
71 | } |
---|
72 | |
---|
73 | nMapFunc ngcSetMap(const ring src,const ring dst) |
---|
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 | { |
---|
93 | ngfMapRing=src; |
---|
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 | */ |
---|
108 | number ngcInit (int i, const ring r) |
---|
109 | { |
---|
110 | gmp_complex* n= NULL; |
---|
111 | if ( i != 0 ) |
---|
112 | { |
---|
113 | n= new gmp_complex( (long)i, (long)0 ); |
---|
114 | } |
---|
115 | return (number)n; |
---|
116 | } |
---|
117 | |
---|
118 | /*2 |
---|
119 | * convert number to int |
---|
120 | */ |
---|
121 | int ngcInt(number &i, const ring r) |
---|
122 | { |
---|
123 | if ( i == NULL ) return 0; |
---|
124 | return (int)((gmp_complex*)i)->real(); |
---|
125 | } |
---|
126 | |
---|
127 | /*2 |
---|
128 | * delete a |
---|
129 | */ |
---|
130 | void ngcDelete (number * a, const ring r) |
---|
131 | { |
---|
132 | if ( *a != NULL ) |
---|
133 | { |
---|
134 | delete *(gmp_complex**)a; |
---|
135 | *a=NULL; |
---|
136 | } |
---|
137 | } |
---|
138 | |
---|
139 | /*2 |
---|
140 | * copy a to b |
---|
141 | */ |
---|
142 | number ngcCopy(number a) |
---|
143 | { |
---|
144 | gmp_complex* b= NULL; |
---|
145 | if ( a != NULL ) |
---|
146 | { |
---|
147 | b= new gmp_complex( *(gmp_complex*)a ); |
---|
148 | } |
---|
149 | return (number)b; |
---|
150 | } |
---|
151 | number ngc_Copy(number a, ring r) |
---|
152 | { |
---|
153 | gmp_complex* b= NULL; |
---|
154 | if ( a != NULL ) |
---|
155 | { |
---|
156 | b= new gmp_complex( *(gmp_complex*)a ); |
---|
157 | } |
---|
158 | return (number)b; |
---|
159 | } |
---|
160 | |
---|
161 | /*2 |
---|
162 | * za:= - za |
---|
163 | */ |
---|
164 | gmp_complex ngc_m1(-1); |
---|
165 | |
---|
166 | number ngcNeg (number a) |
---|
167 | { |
---|
168 | if ( a == NULL ) return NULL; |
---|
169 | gmp_complex* r=(gmp_complex*)a; |
---|
170 | (*r) *= ngc_m1; |
---|
171 | return (number)a; |
---|
172 | } |
---|
173 | |
---|
174 | /* |
---|
175 | * 1/a |
---|
176 | */ |
---|
177 | number ngcInvers(number a) |
---|
178 | { |
---|
179 | gmp_complex* r = NULL; |
---|
180 | if ( (a==NULL) |
---|
181 | || (((gmp_complex*)a)->isZero())) |
---|
182 | { |
---|
183 | WerrorS(nDivBy0); |
---|
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 | { |
---|
197 | gmp_complex* r= NULL; |
---|
198 | if ( a==NULL && b==NULL ) |
---|
199 | { |
---|
200 | return NULL; |
---|
201 | } |
---|
202 | else if ( a == NULL ) |
---|
203 | { |
---|
204 | r= new gmp_complex( *(gmp_complex*)b ); |
---|
205 | } |
---|
206 | else if ( b == NULL ) |
---|
207 | { |
---|
208 | r= new gmp_complex( *(gmp_complex*)a ); |
---|
209 | } |
---|
210 | else |
---|
211 | { |
---|
212 | r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) ); |
---|
213 | } |
---|
214 | return (number)r; |
---|
215 | } |
---|
216 | |
---|
217 | /*2 |
---|
218 | * u:= a - b |
---|
219 | */ |
---|
220 | number ngcSub (number a, number b) |
---|
221 | { |
---|
222 | gmp_complex* r= NULL; |
---|
223 | if ( a==NULL && b==NULL ) |
---|
224 | { |
---|
225 | return NULL; |
---|
226 | } |
---|
227 | else if ( a == NULL ) |
---|
228 | { |
---|
229 | r= new gmp_complex( (*(gmp_complex*)b) ); |
---|
230 | r= (gmp_complex *)ngcNeg((number)r); |
---|
231 | } |
---|
232 | else if ( b == NULL ) |
---|
233 | { |
---|
234 | r= new gmp_complex( *(gmp_complex*)a ); |
---|
235 | } |
---|
236 | else |
---|
237 | { |
---|
238 | r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) ); |
---|
239 | } |
---|
240 | return (number)r; |
---|
241 | } |
---|
242 | |
---|
243 | /*2 |
---|
244 | * u := a * b |
---|
245 | */ |
---|
246 | number ngcMult (number a, number b) |
---|
247 | { |
---|
248 | gmp_complex* r= NULL; |
---|
249 | if ( a==NULL || b==NULL ) |
---|
250 | { |
---|
251 | return NULL; |
---|
252 | } |
---|
253 | else |
---|
254 | { |
---|
255 | r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) ); |
---|
256 | } |
---|
257 | return (number)r; |
---|
258 | } |
---|
259 | |
---|
260 | /*2 |
---|
261 | * u := a / b |
---|
262 | */ |
---|
263 | number ngcDiv (number a, number b) |
---|
264 | { |
---|
265 | if ( a==NULL ) |
---|
266 | { |
---|
267 | // 0/b = 0 |
---|
268 | return NULL; |
---|
269 | } |
---|
270 | else if (( b==NULL ) |
---|
271 | || (((gmp_complex*)b)->isZero())) |
---|
272 | { |
---|
273 | // a/0 = error |
---|
274 | WerrorS(nDivBy0); |
---|
275 | return NULL; |
---|
276 | } |
---|
277 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) ); |
---|
278 | return (number)r; |
---|
279 | } |
---|
280 | |
---|
281 | /*2 |
---|
282 | * u:= x ^ exp |
---|
283 | */ |
---|
284 | void ngcPower ( number x, int exp, number * u ) |
---|
285 | { |
---|
286 | if ( exp == 0 ) |
---|
287 | { |
---|
288 | gmp_complex* n = new gmp_complex(1); |
---|
289 | *u=(number)n; |
---|
290 | return; |
---|
291 | } |
---|
292 | else if ( exp == 1 ) |
---|
293 | { |
---|
294 | nNew(u); |
---|
295 | if ( x == NULL ) |
---|
296 | { |
---|
297 | gmp_complex* n = new gmp_complex(); |
---|
298 | *u=(number)n; |
---|
299 | } |
---|
300 | else |
---|
301 | { |
---|
302 | gmp_complex* n = new gmp_complex(); |
---|
303 | *n= *(gmp_complex*)x; |
---|
304 | *u=(number)n; |
---|
305 | } |
---|
306 | return; |
---|
307 | } |
---|
308 | else if (exp == 2) |
---|
309 | { |
---|
310 | nNew(u); |
---|
311 | if ( x == NULL ) |
---|
312 | { |
---|
313 | gmp_complex* n = new gmp_complex(); |
---|
314 | *u=(number)n; |
---|
315 | } |
---|
316 | else |
---|
317 | { |
---|
318 | gmp_complex* n = new gmp_complex(); |
---|
319 | *n= *(gmp_complex*)x; |
---|
320 | *u=(number)n; |
---|
321 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
322 | } |
---|
323 | return; |
---|
324 | } |
---|
325 | if (exp&1==1) |
---|
326 | { |
---|
327 | ngcPower(x,exp-1,u); |
---|
328 | gmp_complex *n=new gmp_complex(); |
---|
329 | *n=*(gmp_complex*)x; |
---|
330 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
331 | delete n; |
---|
332 | } |
---|
333 | else |
---|
334 | { |
---|
335 | number w; |
---|
336 | nNew(&w); |
---|
337 | ngcPower(x,exp/2,&w); |
---|
338 | ngcPower(w,2,u); |
---|
339 | nDelete(&w); |
---|
340 | } |
---|
341 | } |
---|
342 | |
---|
343 | BOOLEAN ngcIsZero (number a) |
---|
344 | { |
---|
345 | if ( a == NULL ) return TRUE; |
---|
346 | return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero()); |
---|
347 | } |
---|
348 | |
---|
349 | number ngcRePart(number a) |
---|
350 | { |
---|
351 | if ((a==NULL) || ((gmp_complex*)a)->real().isZero()) return NULL; |
---|
352 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real()); |
---|
353 | return (number)n; |
---|
354 | } |
---|
355 | |
---|
356 | number ngcImPart(number a) |
---|
357 | { |
---|
358 | if ((a==NULL) || ((gmp_complex*)a)->imag().isZero()) return NULL; |
---|
359 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag()); |
---|
360 | return (number)n; |
---|
361 | } |
---|
362 | |
---|
363 | /*2 |
---|
364 | * za >= 0 ? |
---|
365 | */ |
---|
366 | BOOLEAN ngcGreaterZero (number a) |
---|
367 | { |
---|
368 | if ( a == NULL ) return TRUE; |
---|
369 | if ( ! ((gmp_complex*)a)->imag().isZero() ) |
---|
370 | return ( abs( *(gmp_complex*)a).sign() >= 0 ); |
---|
371 | else |
---|
372 | return ( ((gmp_complex*)a)->real().sign() >= 0 ); |
---|
373 | } |
---|
374 | |
---|
375 | /*2 |
---|
376 | * a > b ? |
---|
377 | */ |
---|
378 | BOOLEAN ngcGreater (number a, number b) |
---|
379 | { |
---|
380 | if ( a==NULL ) |
---|
381 | { |
---|
382 | return (((gmp_complex*)b)->real().sign() < 0); |
---|
383 | } |
---|
384 | if ( b==NULL ) |
---|
385 | { |
---|
386 | return (((gmp_complex*)a)->real().sign() < 0); |
---|
387 | } |
---|
388 | return FALSE; |
---|
389 | } |
---|
390 | |
---|
391 | /*2 |
---|
392 | * a = b ? |
---|
393 | */ |
---|
394 | BOOLEAN ngcEqual (number a, number b) |
---|
395 | { |
---|
396 | if ( a == NULL && b == NULL ) |
---|
397 | { |
---|
398 | return TRUE; |
---|
399 | } |
---|
400 | else if ( a == NULL || b == NULL ) |
---|
401 | { |
---|
402 | return FALSE; |
---|
403 | } |
---|
404 | return ( (*(gmp_complex*)a) == (*(gmp_complex*)b) ); |
---|
405 | } |
---|
406 | |
---|
407 | /*2 |
---|
408 | * a == 1 ? |
---|
409 | */ |
---|
410 | BOOLEAN ngcIsOne (number a) |
---|
411 | { |
---|
412 | if ( a == NULL ) return FALSE; |
---|
413 | //return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
414 | return (((gmp_complex*)a)->real().isOne()); |
---|
415 | } |
---|
416 | |
---|
417 | /*2 |
---|
418 | * a == -1 ? |
---|
419 | */ |
---|
420 | BOOLEAN ngcIsMOne (number a) |
---|
421 | { |
---|
422 | if ( a == NULL ) return FALSE; |
---|
423 | // return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
424 | return (((gmp_complex*)a)->real().isMOne()); |
---|
425 | } |
---|
426 | |
---|
427 | /*2 |
---|
428 | * extracts the number a from s, returns the rest |
---|
429 | */ |
---|
430 | const char * ngcRead (const char * s, number * a) |
---|
431 | { |
---|
432 | const char *start= s; |
---|
433 | if ((*s >= '0') && (*s <= '9')) |
---|
434 | { |
---|
435 | gmp_float *re=NULL; |
---|
436 | s=ngfRead(s,(number *)&re); |
---|
437 | gmp_complex *aa=new gmp_complex(*re); |
---|
438 | *a=(number)aa; |
---|
439 | delete re; |
---|
440 | } |
---|
441 | else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0) |
---|
442 | { |
---|
443 | s+=strlen(currRing->parameter[0]); |
---|
444 | gmp_complex *aa=new gmp_complex((long)0,(long)1); |
---|
445 | *a=(number)aa; |
---|
446 | } |
---|
447 | else |
---|
448 | { |
---|
449 | *a=(number) new gmp_complex((long)1); |
---|
450 | } |
---|
451 | return s; |
---|
452 | } |
---|
453 | |
---|
454 | /*2 |
---|
455 | * write a floating point number |
---|
456 | */ |
---|
457 | void ngcWrite (number &a, const ring r) |
---|
458 | { |
---|
459 | if (a==NULL) |
---|
460 | StringAppendS("0"); |
---|
461 | else |
---|
462 | { |
---|
463 | char *out; |
---|
464 | out= complexToStr(*(gmp_complex*)a,gmp_output_digits); |
---|
465 | StringAppendS(out); |
---|
466 | // omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) ); |
---|
467 | omFree( (ADDRESS)out ); |
---|
468 | } |
---|
469 | } |
---|
470 | |
---|
471 | #ifdef LDEBUG |
---|
472 | // not yet implemented |
---|
473 | //BOOLEAN ngcDBTest(number a, const char *f, const int l) |
---|
474 | //{ |
---|
475 | // return TRUE; |
---|
476 | //} |
---|
477 | #endif |
---|
478 | |
---|
479 | // local Variables: *** |
---|
480 | // folded-file: t *** |
---|
481 | // compile-command: "make installg" *** |
---|
482 | // End: *** |
---|