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= new gmp_complex( (long)i, (long)0 ); |
---|
111 | return (number)n; |
---|
112 | } |
---|
113 | |
---|
114 | /*2 |
---|
115 | * convert number to int |
---|
116 | */ |
---|
117 | int ngcInt(number &i, const ring r) |
---|
118 | { |
---|
119 | return (int)((gmp_complex*)i)->real(); |
---|
120 | } |
---|
121 | |
---|
122 | int ngcSize(number n) |
---|
123 | { |
---|
124 | int r = (int)((gmp_complex*)n)->real(); |
---|
125 | if (r < 0) r = -r; |
---|
126 | int i = (int)((gmp_complex*)n)->imag(); |
---|
127 | if (i < 0) i = -i; |
---|
128 | int oneNorm = r + i; |
---|
129 | /* basically return the 1-norm of n; |
---|
130 | only if this happens to be zero although n != 0, |
---|
131 | return 1; |
---|
132 | (this code ensures that zero has the size zero) */ |
---|
133 | if ((oneNorm == 0.0) & (ngcIsZero(n) == FALSE)) oneNorm = 1; |
---|
134 | return oneNorm; |
---|
135 | } |
---|
136 | |
---|
137 | /*2 |
---|
138 | * delete a |
---|
139 | */ |
---|
140 | void ngcDelete (number * a, const ring r) |
---|
141 | { |
---|
142 | if ( *a != NULL ) |
---|
143 | { |
---|
144 | delete *(gmp_complex**)a; |
---|
145 | *a=NULL; |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | /*2 |
---|
150 | * copy a to b |
---|
151 | */ |
---|
152 | number ngcCopy(number a) |
---|
153 | { |
---|
154 | gmp_complex* b= new gmp_complex( *(gmp_complex*)a ); |
---|
155 | return (number)b; |
---|
156 | } |
---|
157 | number ngc_Copy(number a, ring r) |
---|
158 | { |
---|
159 | gmp_complex* b=new gmp_complex( *(gmp_complex*)a ); |
---|
160 | return (number)b; |
---|
161 | } |
---|
162 | |
---|
163 | /*2 |
---|
164 | * za:= - za |
---|
165 | */ |
---|
166 | gmp_complex ngc_m1(-1); |
---|
167 | |
---|
168 | number ngcNeg (number a) |
---|
169 | { |
---|
170 | gmp_complex* r=(gmp_complex*)a; |
---|
171 | (*r) *= ngc_m1; |
---|
172 | return (number)a; |
---|
173 | } |
---|
174 | |
---|
175 | /* |
---|
176 | * 1/a |
---|
177 | */ |
---|
178 | number ngcInvers(number a) |
---|
179 | { |
---|
180 | gmp_complex* r = NULL; |
---|
181 | if (((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= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) ); |
---|
198 | return (number)r; |
---|
199 | } |
---|
200 | |
---|
201 | /*2 |
---|
202 | * u:= a - b |
---|
203 | */ |
---|
204 | number ngcSub (number a, number b) |
---|
205 | { |
---|
206 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) ); |
---|
207 | return (number)r; |
---|
208 | } |
---|
209 | |
---|
210 | /*2 |
---|
211 | * u := a * b |
---|
212 | */ |
---|
213 | number ngcMult (number a, number b) |
---|
214 | { |
---|
215 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) ); |
---|
216 | return (number)r; |
---|
217 | } |
---|
218 | |
---|
219 | /*2 |
---|
220 | * u := a / b |
---|
221 | */ |
---|
222 | number ngcDiv (number a, number b) |
---|
223 | { |
---|
224 | if (((gmp_complex*)b)->isZero()) |
---|
225 | { |
---|
226 | // a/0 = error |
---|
227 | WerrorS(nDivBy0); |
---|
228 | return NULL; |
---|
229 | } |
---|
230 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) ); |
---|
231 | return (number)r; |
---|
232 | } |
---|
233 | |
---|
234 | /*2 |
---|
235 | * u:= x ^ exp |
---|
236 | */ |
---|
237 | void ngcPower ( number x, int exp, number * u ) |
---|
238 | { |
---|
239 | if ( exp == 0 ) |
---|
240 | { |
---|
241 | gmp_complex* n = new gmp_complex(1); |
---|
242 | *u=(number)n; |
---|
243 | return; |
---|
244 | } |
---|
245 | else if ( exp == 1 ) |
---|
246 | { |
---|
247 | nNew(u); |
---|
248 | gmp_complex* n = new gmp_complex(); |
---|
249 | *n= *(gmp_complex*)x; |
---|
250 | *u=(number)n; |
---|
251 | return; |
---|
252 | } |
---|
253 | else if (exp == 2) |
---|
254 | { |
---|
255 | nNew(u); |
---|
256 | gmp_complex* n = new gmp_complex(); |
---|
257 | *n= *(gmp_complex*)x; |
---|
258 | *u=(number)n; |
---|
259 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
260 | return; |
---|
261 | } |
---|
262 | if (exp&1==1) |
---|
263 | { |
---|
264 | ngcPower(x,exp-1,u); |
---|
265 | gmp_complex *n=new gmp_complex(); |
---|
266 | *n=*(gmp_complex*)x; |
---|
267 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
268 | delete n; |
---|
269 | } |
---|
270 | else |
---|
271 | { |
---|
272 | number w; |
---|
273 | nNew(&w); |
---|
274 | ngcPower(x,exp/2,&w); |
---|
275 | ngcPower(w,2,u); |
---|
276 | nDelete(&w); |
---|
277 | } |
---|
278 | } |
---|
279 | |
---|
280 | BOOLEAN ngcIsZero (number a) |
---|
281 | { |
---|
282 | return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero()); |
---|
283 | } |
---|
284 | |
---|
285 | number ngcRePart(number a) |
---|
286 | { |
---|
287 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real()); |
---|
288 | return (number)n; |
---|
289 | } |
---|
290 | |
---|
291 | number ngcImPart(number a) |
---|
292 | { |
---|
293 | gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag()); |
---|
294 | return (number)n; |
---|
295 | } |
---|
296 | |
---|
297 | /*2 |
---|
298 | * za >= 0 ? |
---|
299 | */ |
---|
300 | BOOLEAN ngcGreaterZero (number a) |
---|
301 | { |
---|
302 | if ( ! ((gmp_complex*)a)->imag().isZero() ) |
---|
303 | return ( abs( *(gmp_complex*)a).sign() >= 0 ); |
---|
304 | else |
---|
305 | return ( ((gmp_complex*)a)->real().sign() >= 0 ); |
---|
306 | } |
---|
307 | |
---|
308 | /*2 |
---|
309 | * a > b ? |
---|
310 | */ |
---|
311 | BOOLEAN ngcGreater (number a, number b) |
---|
312 | { |
---|
313 | gmp_complex *aa=(gmp_complex*)a; |
---|
314 | gmp_complex *bb=(gmp_complex*)b; |
---|
315 | return (*aa) > (*bb); |
---|
316 | } |
---|
317 | |
---|
318 | /*2 |
---|
319 | * a = b ? |
---|
320 | */ |
---|
321 | BOOLEAN ngcEqual (number a, number b) |
---|
322 | { |
---|
323 | gmp_complex *aa=(gmp_complex*)a; |
---|
324 | gmp_complex *bb=(gmp_complex*)b; |
---|
325 | return (*aa) == (*bb); |
---|
326 | } |
---|
327 | |
---|
328 | /*2 |
---|
329 | * a == 1 ? |
---|
330 | */ |
---|
331 | BOOLEAN ngcIsOne (number a) |
---|
332 | { |
---|
333 | return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
334 | //return (((gmp_complex*)a)->real().isOne()); |
---|
335 | } |
---|
336 | |
---|
337 | /*2 |
---|
338 | * a == -1 ? |
---|
339 | */ |
---|
340 | BOOLEAN ngcIsMOne (number a) |
---|
341 | { |
---|
342 | return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
343 | //return (((gmp_complex*)a)->real().isMOne()); |
---|
344 | } |
---|
345 | |
---|
346 | /*2 |
---|
347 | * extracts the number a from s, returns the rest |
---|
348 | */ |
---|
349 | const char * ngcRead (const char * s, number * a) |
---|
350 | { |
---|
351 | const char *start= s; |
---|
352 | if ((*s >= '0') && (*s <= '9')) |
---|
353 | { |
---|
354 | gmp_float *re=NULL; |
---|
355 | s=ngfRead(s,(number *)&re); |
---|
356 | gmp_complex *aa=new gmp_complex(*re); |
---|
357 | *a=(number)aa; |
---|
358 | delete re; |
---|
359 | } |
---|
360 | else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0) |
---|
361 | { |
---|
362 | s+=strlen(currRing->parameter[0]); |
---|
363 | gmp_complex *aa=new gmp_complex((long)0,(long)1); |
---|
364 | *a=(number)aa; |
---|
365 | } |
---|
366 | else |
---|
367 | { |
---|
368 | *a=(number) new gmp_complex((long)1); |
---|
369 | } |
---|
370 | return s; |
---|
371 | } |
---|
372 | |
---|
373 | /*2 |
---|
374 | * write a floating point number |
---|
375 | */ |
---|
376 | void ngcWrite (number &a, const ring r) |
---|
377 | { |
---|
378 | if (a==NULL) |
---|
379 | StringAppendS("0"); |
---|
380 | else |
---|
381 | { |
---|
382 | char *out; |
---|
383 | out= complexToStr(*(gmp_complex*)a,gmp_output_digits); |
---|
384 | StringAppendS(out); |
---|
385 | // omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) ); |
---|
386 | omFree( (ADDRESS)out ); |
---|
387 | } |
---|
388 | } |
---|
389 | |
---|
390 | #ifdef LDEBUG |
---|
391 | // not yet implemented |
---|
392 | //BOOLEAN ngcDBTest(number a, const char *f, const int l) |
---|
393 | //{ |
---|
394 | // return TRUE; |
---|
395 | //} |
---|
396 | #endif |
---|
397 | |
---|
398 | // local Variables: *** |
---|
399 | // folded-file: t *** |
---|
400 | // compile-command: "make installg" *** |
---|
401 | // End: *** |
---|