source: git/Singular/gnumpc.cc @ 584f84d

spielwiese
Last change on this file since 584f84d was 4508ce5, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: nMap removed nSetMap returns now the routine git-svn-id: file:///usr/local/Singular/svn/trunk@4920 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.6 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: gnumpc.cc,v 1.16 2000-12-15 18:49:28 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
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}
35
36nMapFunc ngcSetMap(ring src,ring dst)
37{
38  if (rField_is_long_C(src))
39  {
40    return ngcCopy;
41  }
42  if(rField_is_Q(src))
43  {
44    return ngcMapQ;
45  }
46  return NULL;
47}
48
49number   ngcPar(int i)
50{
51  gmp_complex* n= new gmp_complex( (long)0, (long)1 );
52  return (number)n;
53}
54
55void ngcNew (number * r)
56{
57  *r=NULL;
58}
59
60/*2
61* n := i
62*/
63number ngcInit (int i)
64{
65  gmp_complex* n= NULL;
66  if ( i != 0 )
67  {
68    n= new gmp_complex( (long)i, (long)0 );
69  }
70  return (number)n;
71}
72
73/*2
74* convert number to int
75*/
76int ngcInt(number &i)
77{
78  if ( i == NULL ) return 0;
79  return (int)((gmp_complex*)i)->real();
80}
81
82/*2
83* delete a
84*/
85#ifdef LDEBUG
86void ngcDBDelete (number * a,char *f, int l)
87#else
88void ngcDelete (number * a)
89#endif
90{
91  if ( *a != NULL ) {
92    delete *(gmp_complex**)a;
93    *a=NULL;
94  }
95}
96
97/*2
98* copy a to b
99*/
100number ngcCopy(number a)
101{
102  gmp_complex* b= NULL;
103  if ( a !=  NULL )
104  {
105    b= new gmp_complex( *(gmp_complex*)a );
106  }
107  return (number)b;
108}
109
110/*2
111* za:= - za
112*/
113number ngcNeg (number a)
114{
115  if ( a == NULL ) return NULL;
116  number m1=nInit(-1);
117  a=ngcMult(m1,a);
118  return (number)a;
119}
120
121/*
122* 1/a
123*/
124number ngcInvers(number a)
125{
126  gmp_complex* r= NULL;
127  if ( (a==NULL) /*|| ((gmp_complex*)a)->isZero()*/ )
128  {
129    WerrorS("div. 1/0");
130  }
131  else
132  {
133    r= new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) );
134  }
135  return (number)r;
136}
137
138/*2
139* u:= a + b
140*/
141number ngcAdd (number a, number b)
142{
143  gmp_complex* r= NULL;
144  if ( a==NULL && b==NULL )
145  {
146    return NULL;
147  }
148  else if ( a == NULL )
149  {
150    r= new gmp_complex( *(gmp_complex*)b );
151  }
152  else if ( b == NULL )
153  {
154    r= new gmp_complex( *(gmp_complex*)a );
155  }
156  else
157  {
158    r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) );
159  }
160  return (number)r;
161}
162
163/*2
164* u:= a - b
165*/
166number ngcSub (number a, number b)
167{
168  gmp_complex* r= NULL;
169  if ( a==NULL && b==NULL )
170  {
171    return NULL;
172  }
173  else if ( a == NULL )
174  {
175    r= new gmp_complex( (*(gmp_complex*)b) );
176    r= (gmp_complex *)ngcNeg((number)r);
177  }
178  else if ( b == NULL )
179  {
180    r= new gmp_complex( *(gmp_complex*)a );
181  }
182  else
183  {
184    r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) );
185  }
186  return (number)r;
187}
188
189/*2
190* u := a * b
191*/
192number ngcMult (number a, number b)
193{
194  gmp_complex* r= NULL;
195  if ( a==NULL || b==NULL )
196  {
197    return NULL;
198  }
199  else
200  {
201    r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) );
202  }
203  return (number)r;
204}
205
206/*2
207* u := a / b
208*/
209number ngcDiv (number a, number b)
210{
211  if ( b==NULL /*|| ((gmp_float*)b)->isZero()*/ )
212  {
213    // a/0 = error
214    WerrorS("div. 1/0");
215    return NULL;
216  }
217  else if ( a==NULL )
218  {
219    // 0/b = 0
220    return NULL;
221  }
222  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) );
223  return (number)r;
224}
225
226/*2
227* u:= x ^ exp
228*/
229void ngcPower ( number x, int exp, number * u )
230{
231  if ( exp == 0 )
232  {
233    gmp_complex* n = new gmp_complex(1);
234    *u=(number)n;
235    return;
236  }
237  if ( exp == 1 )
238  {
239    nNew(u);
240    if ( x == NULL )
241    {
242      gmp_complex* n = new gmp_complex();
243      *u=(number)n;
244    }
245    else
246    {
247      gmp_complex* n = new gmp_complex();
248      *n= *(gmp_complex*)x;
249      *u=(number)n;
250    }
251    return;
252  }
253  ngcPower(x,exp-1,u);
254  gmp_complex *n=new gmp_complex();
255  *n=*(gmp_complex*)x;
256  *(gmp_complex*)(*u) *= *(gmp_complex*)n;
257  delete n;
258}
259
260BOOLEAN ngcIsZero (number a)
261{
262  if ( a == NULL ) return TRUE;
263  return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero());
264}
265
266number ngcRePart(number a)
267{
268  if (((gmp_complex*)a)->real().isZero()) return NULL;
269  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real());
270  return (number)n;
271} 
272 
273number ngcImPart(number a)
274{
275  if (((gmp_complex*)a)->imag().isZero()) return NULL;
276  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag());
277  return (number)n;
278} 
279 
280/*2
281* za >= 0 ?
282*/
283BOOLEAN ngcGreaterZero (number a)
284{
285  if ( a == NULL ) return TRUE;
286  if ( ! ((gmp_complex*)a)->imag().isZero() )
287    return ( abs( *(gmp_complex*)a).sign() >= 0 );
288  else
289    return ( ((gmp_complex*)a)->real().sign() >= 0 );
290}
291
292/*2
293* a > b ?
294*/
295BOOLEAN ngcGreater (number a, number b)
296{
297  if ( a==NULL )
298  {
299    return (((gmp_complex*)b)->real().sign() < 0);
300  }
301  if ( b==NULL )
302  {
303    return (((gmp_complex*)a)->real().sign() < 0);
304  }
305  return FALSE;
306}
307
308/*2
309* a = b ?
310*/
311BOOLEAN ngcEqual (number a, number b)
312{
313  if ( a == NULL && b == NULL )
314  {
315    return TRUE;
316  }
317  else if ( a == NULL || b == NULL )
318  {
319    return FALSE;
320  }
321  return ( (*(gmp_complex*)a) == (*(gmp_complex*)b) );
322}
323
324/*2
325* a == 1 ?
326*/
327BOOLEAN ngcIsOne (number a)
328{
329  if ( a == NULL ) return FALSE;
330  //return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero());
331  return (((gmp_complex*)a)->real().isOne());
332}
333
334/*2
335* a == -1 ?
336*/
337BOOLEAN ngcIsMOne (number a)
338{
339  if ( a == NULL ) return FALSE;
340  //  return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero());
341  return (((gmp_complex*)a)->real().isMOne());
342}
343
344/*2
345* extracts the number a from s, returns the rest
346*/
347char * 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*/
374void 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    //    omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) );
384    omFree( (ADDRESS)out );
385  }
386}
387
388#ifdef LDEBUG
389BOOLEAN 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: ***
Note: See TracBrowser for help on using the repository browser.