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