source: git/Singular/gnumpc.cc @ a3bc95e

spielwiese
Last change on this file since a3bc95e was a3bc95e, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: namespaces ->ns git-svn-id: file:///usr/local/Singular/svn/trunk@5651 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 7.4 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: gnumpc.cc,v 1.21 2001-10-09 16:36:00 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#include "modulop.h"
18#include "gnumpc.h"
19#include "gnumpfl.h"
20#include "mpr_complex.h"
21
22extern size_t gmp_output_digits;
23
24
25static 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}
35union 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};
44static 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}
54static 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}
64static number ngcMapP(number from)
65{
66  if ( from != NULL)
67    return ngcInit(npInt(from));
68  else
69    return NULL;
70}
71
72nMapFunc ngcSetMap(ring src,ring dst)
73{
74  if(rField_is_Q(src))
75  {
76    return ngcMapQ;
77  }
78  if (rField_is_long_R(src))
79  {
80    return ngcMapLongR;
81  }
82  if (rField_is_long_C(src))
83  {
84    return ngcCopy;
85  }
86  if(rField_is_R(src))
87  {
88    return ngcMapR;
89  }
90  if (rField_is_Zp(src))
91  {
92    return ngcMapP;
93  }
94  return NULL;
95}
96
97number   ngcPar(int i)
98{
99  gmp_complex* n= new gmp_complex( (long)0, (long)1 );
100  return (number)n;
101}
102
103void ngcNew (number * r)
104{
105  *r=NULL;
106}
107
108/*2
109* n := i
110*/
111number ngcInit (int i)
112{
113  gmp_complex* n= NULL;
114  if ( i != 0 )
115  {
116    n= new gmp_complex( (long)i, (long)0 );
117  }
118  return (number)n;
119}
120
121/*2
122* convert number to int
123*/
124int ngcInt(number &i)
125{
126  if ( i == NULL ) return 0;
127  return (int)((gmp_complex*)i)->real();
128}
129
130/*2
131* delete a
132*/
133void ngcDelete (number * a, const ring r)
134{
135  if ( *a != NULL ) {
136    delete *(gmp_complex**)a;
137    *a=NULL;
138  }
139}
140
141/*2
142* copy a to b
143*/
144number ngcCopy(number a)
145{
146  gmp_complex* b= NULL;
147  if ( a !=  NULL )
148  {
149    b= new gmp_complex( *(gmp_complex*)a );
150  }
151  return (number)b;
152}
153number ngc_Copy(number a, ring r)
154{
155  gmp_complex* b= NULL;
156  if ( a !=  NULL )
157  {
158    b= new gmp_complex( *(gmp_complex*)a );
159  }
160  return (number)b;
161}
162
163/*2
164* za:= - za
165*/
166number ngcNeg (number a)
167{
168  if ( a == NULL ) return NULL;
169  number m1=nInit(-1);
170  a=ngcMult(m1,a);
171  return (number)a;
172}
173
174/*
175* 1/a
176*/
177number ngcInvers(number a)
178{
179  gmp_complex* r= NULL;
180  if ( (a==NULL) /*|| ((gmp_complex*)a)->isZero()*/ )
181  {
182    WerrorS("div. 1/0");
183  }
184  else
185  {
186    r= new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) );
187  }
188  return (number)r;
189}
190
191/*2
192* u:= a + b
193*/
194number ngcAdd (number a, number b)
195{
196  gmp_complex* r= NULL;
197  if ( a==NULL && b==NULL )
198  {
199    return NULL;
200  }
201  else if ( a == NULL )
202  {
203    r= new gmp_complex( *(gmp_complex*)b );
204  }
205  else if ( b == NULL )
206  {
207    r= new gmp_complex( *(gmp_complex*)a );
208  }
209  else
210  {
211    r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) );
212  }
213  return (number)r;
214}
215
216/*2
217* u:= a - b
218*/
219number ngcSub (number a, number b)
220{
221  gmp_complex* r= NULL;
222  if ( a==NULL && b==NULL )
223  {
224    return NULL;
225  }
226  else if ( a == NULL )
227  {
228    r= new gmp_complex( (*(gmp_complex*)b) );
229    r= (gmp_complex *)ngcNeg((number)r);
230  }
231  else if ( b == NULL )
232  {
233    r= new gmp_complex( *(gmp_complex*)a );
234  }
235  else
236  {
237    r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) );
238  }
239  return (number)r;
240}
241
242/*2
243* u := a * b
244*/
245number ngcMult (number a, number b)
246{
247  gmp_complex* r= NULL;
248  if ( a==NULL || b==NULL )
249  {
250    return NULL;
251  }
252  else
253  {
254    r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) );
255  }
256  return (number)r;
257}
258
259/*2
260* u := a / b
261*/
262number ngcDiv (number a, number b)
263{
264  if ( b==NULL /*|| ((gmp_float*)b)->isZero()*/ )
265  {
266    // a/0 = error
267    WerrorS("div. 1/0");
268    return NULL;
269  }
270  else if ( a==NULL )
271  {
272    // 0/b = 0
273    return NULL;
274  }
275  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) );
276  return (number)r;
277}
278
279/*2
280* u:= x ^ exp
281*/
282void ngcPower ( number x, int exp, number * u )
283{
284  if ( exp == 0 )
285  {
286    gmp_complex* n = new gmp_complex(1);
287    *u=(number)n;
288    return;
289  }
290  if ( exp == 1 )
291  {
292    nNew(u);
293    if ( x == NULL )
294    {
295      gmp_complex* n = new gmp_complex();
296      *u=(number)n;
297    }
298    else
299    {
300      gmp_complex* n = new gmp_complex();
301      *n= *(gmp_complex*)x;
302      *u=(number)n;
303    }
304    return;
305  }
306  ngcPower(x,exp-1,u);
307  gmp_complex *n=new gmp_complex();
308  *n=*(gmp_complex*)x;
309  *(gmp_complex*)(*u) *= *(gmp_complex*)n;
310  delete n;
311}
312
313BOOLEAN ngcIsZero (number a)
314{
315  if ( a == NULL ) return TRUE;
316  return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero());
317}
318
319number ngcRePart(number a)
320{
321  if (((gmp_complex*)a)->real().isZero()) return NULL;
322  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real());
323  return (number)n;
324}
325
326number ngcImPart(number a)
327{
328  if (((gmp_complex*)a)->imag().isZero()) return NULL;
329  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag());
330  return (number)n;
331}
332
333/*2
334* za >= 0 ?
335*/
336BOOLEAN ngcGreaterZero (number a)
337{
338  if ( a == NULL ) return TRUE;
339  if ( ! ((gmp_complex*)a)->imag().isZero() )
340    return ( abs( *(gmp_complex*)a).sign() >= 0 );
341  else
342    return ( ((gmp_complex*)a)->real().sign() >= 0 );
343}
344
345/*2
346* a > b ?
347*/
348BOOLEAN ngcGreater (number a, number b)
349{
350  if ( a==NULL )
351  {
352    return (((gmp_complex*)b)->real().sign() < 0);
353  }
354  if ( b==NULL )
355  {
356    return (((gmp_complex*)a)->real().sign() < 0);
357  }
358  return FALSE;
359}
360
361/*2
362* a = b ?
363*/
364BOOLEAN ngcEqual (number a, number b)
365{
366  if ( a == NULL && b == NULL )
367  {
368    return TRUE;
369  }
370  else if ( a == NULL || b == NULL )
371  {
372    return FALSE;
373  }
374  return ( (*(gmp_complex*)a) == (*(gmp_complex*)b) );
375}
376
377/*2
378* a == 1 ?
379*/
380BOOLEAN ngcIsOne (number a)
381{
382  if ( a == NULL ) return FALSE;
383  //return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero());
384  return (((gmp_complex*)a)->real().isOne());
385}
386
387/*2
388* a == -1 ?
389*/
390BOOLEAN ngcIsMOne (number a)
391{
392  if ( a == NULL ) return FALSE;
393  //  return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero());
394  return (((gmp_complex*)a)->real().isMOne());
395}
396
397/*2
398* extracts the number a from s, returns the rest
399*/
400char * ngcRead (char * s, number * a)
401{
402  char *start= s;
403  if ((*s >= '0') && (*s <= '9'))
404  {
405    gmp_float *re=NULL;
406    s=ngfRead(s,(number *)&re);
407    gmp_complex *aa=new gmp_complex(*re);
408    *a=(number)aa;
409    delete re;
410  }
411  else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0)
412  {
413    s+=strlen(currRing->parameter[0]);
414    gmp_complex *aa=new gmp_complex((long)0,(long)1);
415    *a=(number)aa;
416  }
417  else
418  {
419    *a=(number) new gmp_complex((long)1);
420  }
421  return s;
422}
423
424/*2
425* write a floating point number
426*/
427void ngcWrite (number &a)
428{
429  if (a==NULL)
430    StringAppend("0");
431  else
432  {
433    char *out;
434    out= complexToStr(*(gmp_complex*)a,gmp_output_digits);
435    StringAppend(out);
436    //    omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) );
437    omFree( (ADDRESS)out );
438  }
439}
440
441#ifdef LDEBUG
442BOOLEAN ngcDBTest(number a, char *f, int l)
443{
444  return TRUE;
445}
446#endif
447
448// local Variables: ***
449// folded-file: t ***
450// compile-command: "make installg" ***
451// End: ***
Note: See TracBrowser for help on using the repository browser.