1 | /**************************************** |
---|
2 | * Computer Algebra System SINGULAR * |
---|
3 | ****************************************/ |
---|
4 | /* $Id: gnumpc.cc,v 1.14 2000-12-06 11:03:09 Singular Exp $ */ |
---|
5 | /* |
---|
6 | * ABSTRACT: computations with GMP complex floating-point numbers |
---|
7 | * |
---|
8 | * ngc == number gnu complex |
---|
9 | */ |
---|
10 | |
---|
11 | #include "mod2.h" |
---|
12 | #include "tok.h" |
---|
13 | #include "febase.h" |
---|
14 | #include "omalloc.h" |
---|
15 | #include "numbers.h" |
---|
16 | #include "longrat.h" |
---|
17 | |
---|
18 | #include "gnumpc.h" |
---|
19 | #include "gnumpfl.h" |
---|
20 | #include "mpr_complex.h" |
---|
21 | |
---|
22 | extern size_t gmp_output_digits; |
---|
23 | |
---|
24 | |
---|
25 | static 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 | |
---|
36 | BOOLEAN ngcSetMap(ring r) |
---|
37 | { |
---|
38 | if (rField_is_long_C(r)) |
---|
39 | { |
---|
40 | nMap=ngcCopy; |
---|
41 | return TRUE; |
---|
42 | } |
---|
43 | if(rField_is_Q(r)) |
---|
44 | { |
---|
45 | nMap = ngcMapQ; |
---|
46 | return TRUE; |
---|
47 | } |
---|
48 | return FALSE; |
---|
49 | } |
---|
50 | |
---|
51 | number ngcPar(int i) |
---|
52 | { |
---|
53 | gmp_complex* n= new gmp_complex( (long)0, (long)1 ); |
---|
54 | return (number)n; |
---|
55 | } |
---|
56 | |
---|
57 | void ngcNew (number * r) |
---|
58 | { |
---|
59 | *r=NULL; |
---|
60 | } |
---|
61 | |
---|
62 | /*2 |
---|
63 | * n := i |
---|
64 | */ |
---|
65 | number ngcInit (int i) |
---|
66 | { |
---|
67 | gmp_complex* n= NULL; |
---|
68 | if ( i != 0 ) |
---|
69 | { |
---|
70 | n= new gmp_complex( (long)i, (long)0 ); |
---|
71 | } |
---|
72 | return (number)n; |
---|
73 | } |
---|
74 | |
---|
75 | /*2 |
---|
76 | * convert number to int |
---|
77 | */ |
---|
78 | int ngcInt(number &i) |
---|
79 | { |
---|
80 | if ( i == NULL ) return 0; |
---|
81 | return (int)((gmp_complex*)i)->real(); |
---|
82 | } |
---|
83 | |
---|
84 | /*2 |
---|
85 | * delete a |
---|
86 | */ |
---|
87 | #ifdef LDEBUG |
---|
88 | void ngcDBDelete (number * a,char *f, int l) |
---|
89 | #else |
---|
90 | void ngcDelete (number * a) |
---|
91 | #endif |
---|
92 | { |
---|
93 | if ( *a != NULL ) { |
---|
94 | delete *(gmp_complex**)a; |
---|
95 | *a=NULL; |
---|
96 | } |
---|
97 | } |
---|
98 | |
---|
99 | /*2 |
---|
100 | * copy a to b |
---|
101 | */ |
---|
102 | number ngcCopy(number a) |
---|
103 | { |
---|
104 | gmp_complex* b= NULL; |
---|
105 | if ( a != NULL ) |
---|
106 | { |
---|
107 | b= new gmp_complex( *(gmp_complex*)a ); |
---|
108 | } |
---|
109 | return (number)b; |
---|
110 | } |
---|
111 | |
---|
112 | /*2 |
---|
113 | * za:= - za |
---|
114 | */ |
---|
115 | number ngcNeg (number a) |
---|
116 | { |
---|
117 | if ( a == NULL ) return NULL; |
---|
118 | number m1=nInit(-1); |
---|
119 | a=ngcMult(m1,a); |
---|
120 | return (number)a; |
---|
121 | } |
---|
122 | |
---|
123 | /* |
---|
124 | * 1/a |
---|
125 | */ |
---|
126 | number ngcInvers(number a) |
---|
127 | { |
---|
128 | gmp_complex* r= NULL; |
---|
129 | if ( (a==NULL) /*|| ((gmp_complex*)a)->isZero()*/ ) |
---|
130 | { |
---|
131 | WerrorS("div. 1/0"); |
---|
132 | } |
---|
133 | else |
---|
134 | { |
---|
135 | r= new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) ); |
---|
136 | } |
---|
137 | return (number)r; |
---|
138 | } |
---|
139 | |
---|
140 | /*2 |
---|
141 | * u:= a + b |
---|
142 | */ |
---|
143 | number ngcAdd (number a, number b) |
---|
144 | { |
---|
145 | gmp_complex* r= NULL; |
---|
146 | if ( a==NULL && b==NULL ) |
---|
147 | { |
---|
148 | return NULL; |
---|
149 | } |
---|
150 | else if ( a == NULL ) |
---|
151 | { |
---|
152 | r= new gmp_complex( *(gmp_complex*)b ); |
---|
153 | } |
---|
154 | else if ( b == NULL ) |
---|
155 | { |
---|
156 | r= new gmp_complex( *(gmp_complex*)a ); |
---|
157 | } |
---|
158 | else |
---|
159 | { |
---|
160 | r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) ); |
---|
161 | } |
---|
162 | return (number)r; |
---|
163 | } |
---|
164 | |
---|
165 | /*2 |
---|
166 | * u:= a - b |
---|
167 | */ |
---|
168 | number ngcSub (number a, number b) |
---|
169 | { |
---|
170 | gmp_complex* r= NULL; |
---|
171 | if ( a==NULL && b==NULL ) |
---|
172 | { |
---|
173 | return NULL; |
---|
174 | } |
---|
175 | else if ( a == NULL ) |
---|
176 | { |
---|
177 | r= new gmp_complex( (*(gmp_complex*)b) ); |
---|
178 | r= (gmp_complex *)ngcNeg((number)r); |
---|
179 | } |
---|
180 | else if ( b == NULL ) |
---|
181 | { |
---|
182 | r= new gmp_complex( *(gmp_complex*)a ); |
---|
183 | } |
---|
184 | else |
---|
185 | { |
---|
186 | r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) ); |
---|
187 | } |
---|
188 | return (number)r; |
---|
189 | } |
---|
190 | |
---|
191 | /*2 |
---|
192 | * u := a * b |
---|
193 | */ |
---|
194 | number ngcMult (number a, number b) |
---|
195 | { |
---|
196 | gmp_complex* r= NULL; |
---|
197 | if ( a==NULL || b==NULL ) |
---|
198 | { |
---|
199 | return NULL; |
---|
200 | } |
---|
201 | else |
---|
202 | { |
---|
203 | r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) ); |
---|
204 | } |
---|
205 | return (number)r; |
---|
206 | } |
---|
207 | |
---|
208 | /*2 |
---|
209 | * u := a / b |
---|
210 | */ |
---|
211 | number ngcDiv (number a, number b) |
---|
212 | { |
---|
213 | if ( b==NULL /*|| ((gmp_float*)b)->isZero()*/ ) |
---|
214 | { |
---|
215 | // a/0 = error |
---|
216 | WerrorS("div. 1/0"); |
---|
217 | return NULL; |
---|
218 | } |
---|
219 | else if ( a==NULL ) |
---|
220 | { |
---|
221 | // 0/b = 0 |
---|
222 | return NULL; |
---|
223 | } |
---|
224 | gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) ); |
---|
225 | return (number)r; |
---|
226 | } |
---|
227 | |
---|
228 | /*2 |
---|
229 | * u:= x ^ exp |
---|
230 | */ |
---|
231 | void ngcPower ( number x, int exp, number * u ) |
---|
232 | { |
---|
233 | if ( exp == 0 ) |
---|
234 | { |
---|
235 | gmp_complex* n = new gmp_complex(1); |
---|
236 | *u=(number)n; |
---|
237 | return; |
---|
238 | } |
---|
239 | if ( exp == 1 ) |
---|
240 | { |
---|
241 | nNew(u); |
---|
242 | if ( x == NULL ) |
---|
243 | { |
---|
244 | gmp_complex* n = new gmp_complex(); |
---|
245 | *u=(number)n; |
---|
246 | } |
---|
247 | else |
---|
248 | { |
---|
249 | gmp_complex* n = new gmp_complex(); |
---|
250 | *n= *(gmp_complex*)x; |
---|
251 | *u=(number)n; |
---|
252 | } |
---|
253 | return; |
---|
254 | } |
---|
255 | ngcPower(x,exp-1,u); |
---|
256 | gmp_complex *n=new gmp_complex(); |
---|
257 | *n=*(gmp_complex*)x; |
---|
258 | *(gmp_complex*)(*u) *= *(gmp_complex*)n; |
---|
259 | delete n; |
---|
260 | } |
---|
261 | |
---|
262 | BOOLEAN ngcIsZero (number a) |
---|
263 | { |
---|
264 | if ( a == NULL ) return TRUE; |
---|
265 | return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero()); |
---|
266 | } |
---|
267 | |
---|
268 | /*2 |
---|
269 | * za >= 0 ? |
---|
270 | */ |
---|
271 | BOOLEAN ngcGreaterZero (number a) |
---|
272 | { |
---|
273 | if ( a == NULL ) return TRUE; |
---|
274 | if ( ! ((gmp_complex*)a)->imag().isZero() ) |
---|
275 | return ( abs( *(gmp_complex*)a).sign() >= 0 ); |
---|
276 | else |
---|
277 | return ( ((gmp_complex*)a)->real().sign() >= 0 ); |
---|
278 | } |
---|
279 | |
---|
280 | /*2 |
---|
281 | * a > b ? |
---|
282 | */ |
---|
283 | BOOLEAN ngcGreater (number a, number b) |
---|
284 | { |
---|
285 | if ( a==NULL ) |
---|
286 | { |
---|
287 | return (((gmp_complex*)b)->real().sign() < 0); |
---|
288 | } |
---|
289 | if ( b==NULL ) |
---|
290 | { |
---|
291 | return (((gmp_complex*)a)->real().sign() < 0); |
---|
292 | } |
---|
293 | return FALSE; |
---|
294 | } |
---|
295 | |
---|
296 | /*2 |
---|
297 | * a = b ? |
---|
298 | */ |
---|
299 | BOOLEAN ngcEqual (number a, number b) |
---|
300 | { |
---|
301 | if ( a == NULL && b == NULL ) |
---|
302 | { |
---|
303 | return TRUE; |
---|
304 | } |
---|
305 | else if ( a == NULL || b == NULL ) |
---|
306 | { |
---|
307 | return FALSE; |
---|
308 | } |
---|
309 | return ( (*(gmp_complex*)a) == (*(gmp_complex*)b) ); |
---|
310 | } |
---|
311 | |
---|
312 | /*2 |
---|
313 | * a == 1 ? |
---|
314 | */ |
---|
315 | BOOLEAN ngcIsOne (number a) |
---|
316 | { |
---|
317 | if ( a == NULL ) return FALSE; |
---|
318 | //return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
319 | return (((gmp_complex*)a)->real().isOne()); |
---|
320 | } |
---|
321 | |
---|
322 | /*2 |
---|
323 | * a == -1 ? |
---|
324 | */ |
---|
325 | BOOLEAN ngcIsMOne (number a) |
---|
326 | { |
---|
327 | if ( a == NULL ) return FALSE; |
---|
328 | // return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero()); |
---|
329 | return (((gmp_complex*)a)->real().isMOne()); |
---|
330 | } |
---|
331 | |
---|
332 | /*2 |
---|
333 | * extracts the number a from s, returns the rest |
---|
334 | */ |
---|
335 | char * ngcRead (char * s, number * a) |
---|
336 | { |
---|
337 | char *start= s; |
---|
338 | if ((*s >= '0') && (*s <= '9')) |
---|
339 | { |
---|
340 | gmp_float *re=NULL; |
---|
341 | s=ngfRead(s,(number *)&re); |
---|
342 | gmp_complex *aa=new gmp_complex(*re); |
---|
343 | *a=(number)aa; |
---|
344 | delete re; |
---|
345 | } |
---|
346 | else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0) |
---|
347 | { |
---|
348 | s+=strlen(currRing->parameter[0]); |
---|
349 | gmp_complex *aa=new gmp_complex((long)0,(long)1); |
---|
350 | *a=(number)aa; |
---|
351 | } |
---|
352 | else |
---|
353 | { |
---|
354 | *a=(number) new gmp_complex((long)1); |
---|
355 | } |
---|
356 | return s; |
---|
357 | } |
---|
358 | |
---|
359 | /*2 |
---|
360 | * write a floating point number |
---|
361 | */ |
---|
362 | void ngcWrite (number &a) |
---|
363 | { |
---|
364 | if (a==NULL) |
---|
365 | StringAppend("0"); |
---|
366 | else |
---|
367 | { |
---|
368 | char *out; |
---|
369 | out= complexToStr(*(gmp_complex*)a,gmp_output_digits); |
---|
370 | StringAppend(out); |
---|
371 | // omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) ); |
---|
372 | omFree( (ADDRESS)out ); |
---|
373 | } |
---|
374 | } |
---|
375 | |
---|
376 | #ifdef LDEBUG |
---|
377 | BOOLEAN ngcDBTest(number a, char *f, int l) |
---|
378 | { |
---|
379 | return TRUE; |
---|
380 | } |
---|
381 | #endif |
---|
382 | |
---|
383 | // local Variables: *** |
---|
384 | // folded-file: t *** |
---|
385 | // compile-command: "make installg" *** |
---|
386 | // End: *** |
---|