source: git/Singular/gnumpc.cc @ 2c694a2

spielwiese
Last change on this file since 2c694a2 was 416465, checked in by Olaf Bachmann <obachman@…>, 24 years ago
* bug-fixes from work with Thomas git-svn-id: file:///usr/local/Singular/svn/trunk@3826 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 6.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: gnumpc.cc,v 1.10 1999-11-15 17:20:03 obachman 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
23extern size_t gmp_output_digits;
24
25
26static number ngcMapQ(number from)
27{
28  gmp_complex *res= new gmp_complex();
29  if ( from != NULL )
30  {
31    *res= numberFieldToFloat(from,QTOF);
32  }
33  return (number)res;
34}
35
36BOOLEAN 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
51number   ngcPar(int i)
52{
53  gmp_complex* n= new gmp_complex( (long)0, (long)1 );
54  return (number)n;
55}
56
57void ngcNew (number * r)
58{
59  gmp_complex* n= new gmp_complex( );
60  *r=(number)n;
61}
62
63/*2
64* n := i
65*/
66number ngcInit (int i)
67{
68  gmp_complex* n= NULL;
69  if ( i != 0 )
70  {
71    n= new gmp_complex( (long)i, (long)0 );
72  }
73  return (number)n;
74}
75
76/*2
77* convert number to int
78*/
79int ngcInt(number &i)
80{
81  if ( i == NULL ) return 0;
82  return (int)((gmp_complex*)i)->real();
83}
84
85/*2
86* delete a
87*/
88#ifdef LDEBUG
89void ngcDBDelete (number * a,char *f, int l)
90#else
91void ngcDelete (number * a)
92#endif
93{
94  if ( *a != NULL ) {
95    delete *(gmp_complex**)a;
96    *a=NULL;
97  }
98}
99
100/*2
101* copy a to b
102*/
103number ngcCopy(number a)
104{
105  gmp_complex* b= NULL;
106  if ( a !=  NULL )
107  {
108    b= new gmp_complex( *(gmp_complex*)a );
109  }
110  return (number)b;
111}
112
113/*2
114* za:= - za
115*/
116number ngcNeg (number a)
117{
118  if ( a == NULL ) return NULL;
119  number m1=nInit(-1);
120  a=ngcMult(m1,a);
121  return (number)a;
122}
123
124/*
125* 1/a
126*/
127number ngcInvers(number a)
128{
129  gmp_complex* r= NULL;
130  if ( (a==NULL) /*|| ((gmp_complex*)a)->isZero()*/ )
131  {
132    WerrorS("div. 1/0");
133  }
134  else
135  {
136    r= new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) );
137  }
138  return (number)r;
139}
140
141/*2
142* u:= a + b
143*/
144number ngcAdd (number a, number b)
145{
146  gmp_complex* r= NULL;
147  if ( a==NULL && b==NULL )
148  {
149    return NULL;
150  }
151  else if ( a == NULL )
152  {
153    r= new gmp_complex( *(gmp_complex*)b );
154  }
155  else if ( b == NULL )
156  {
157    r= new gmp_complex( *(gmp_complex*)a );
158  }
159  else
160  {
161    r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) );
162  }
163  return (number)r;
164}
165
166/*2
167* u:= a - b
168*/
169number ngcSub (number a, number b)
170{
171  gmp_complex* r= NULL;
172  if ( a==NULL && b==NULL )
173  {
174    return NULL;
175  }
176  else if ( a == NULL )
177  {
178    r= new gmp_complex( (*(gmp_complex*)b) );
179    r= (gmp_complex *)ngcNeg((number)r);
180  }
181  else if ( b == NULL )
182  {
183    r= new gmp_complex( *(gmp_complex*)a );
184  }
185  else
186  {
187    r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) );
188  }
189  return (number)r;
190}
191
192/*2
193* u := a * b
194*/
195number ngcMult (number a, number b)
196{
197  gmp_complex* r= NULL;
198  if ( a==NULL || b==NULL )
199  {
200    return NULL;
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*/
212number ngcDiv (number a, number b)
213{
214  if ( b==NULL /*|| ((gmp_float*)b)->isZero()*/ )
215  {
216    // a/0 = error
217    WerrorS("div. 1/0");
218    return NULL;
219  }
220  else if ( a==NULL )
221  {
222    // 0/b = 0
223    return NULL;
224  }
225  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) );
226  return (number)r;
227}
228
229/*2
230* u:= x ^ exp
231*/
232void ngcPower ( number x, int exp, number * u )
233{
234  if ( exp == 0 )
235  {
236    gmp_complex* n = new gmp_complex(1);
237    *u=(number)n;
238    return;
239  }
240  if ( exp == 1 )
241  {
242    nNew(u);
243    if ( x == NULL )
244    {
245      gmp_complex* n = new gmp_complex();
246      *u=(number)n;
247    }
248    else
249    {
250      gmp_complex* n = new gmp_complex();
251      *n= *(gmp_complex*)x;
252      *u=(number)n;
253    }
254    return;
255  }
256  ngcPower(x,exp-1,u);
257  gmp_complex *n=new gmp_complex();
258  *n=*(gmp_complex*)x;
259  *(gmp_complex*)(*u) *= *(gmp_complex*)n;
260  delete n;
261}
262
263BOOLEAN ngcIsZero (number a)
264{
265  if ( a == NULL ) return TRUE;
266  return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero());
267}
268
269/*2
270* za >= 0 ?
271*/
272BOOLEAN ngcGreaterZero (number a)
273{
274  if ( a == NULL ) return TRUE;
275  if ( ! ((gmp_complex*)a)->imag().isZero() )
276    return ( abs( *(gmp_complex*)a).sign() >= 0 );
277  else
278    return ( ((gmp_complex*)a)->real().sign() >= 0 );
279}
280
281/*2
282* a > b ?
283*/
284BOOLEAN ngcGreater (number a, number b)
285{
286  if ( a==NULL )
287  {
288    return (((gmp_complex*)b)->real().sign() < 0);
289  }
290  if ( b==NULL )
291  {
292    return (((gmp_complex*)a)->real().sign() < 0);
293  }
294  return FALSE;
295}
296
297/*2
298* a = b ?
299*/
300BOOLEAN ngcEqual (number a, number b)
301{
302  if ( a == NULL && b == NULL )
303  {
304    return TRUE;
305  }
306  else if ( a == NULL || b == NULL )
307  {
308    return FALSE;
309  }
310  return ( (*(gmp_complex*)a) == (*(gmp_complex*)b) );
311}
312
313/*2
314* a == 1 ?
315*/
316BOOLEAN ngcIsOne (number a)
317{
318  if ( a == NULL ) return FALSE;
319  //return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero());
320  return (((gmp_complex*)a)->real().isOne());
321}
322
323/*2
324* a == -1 ?
325*/
326BOOLEAN ngcIsMOne (number a)
327{
328  if ( a == NULL ) return FALSE;
329  //  return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero());
330  return (((gmp_complex*)a)->real().isMOne());
331}
332
333/*2
334* extracts the number a from s, returns the rest
335*/
336char * ngcRead (char * s, number * a)
337{
338  char *start= s;
339  if ((*s >= '0') && (*s <= '9'))
340  {
341    gmp_float *re=NULL;
342    s=ngfRead(s,(number *)&re);
343    gmp_complex *aa=new gmp_complex(*re);
344    *a=(number)aa;
345    delete re;
346  }
347  else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0)
348  {
349    s+=strlen(currRing->parameter[0]);
350    gmp_complex *aa=new gmp_complex((long)0,(long)1);
351    *a=(number)aa;
352  }
353  else
354  {
355    *a=(number) new gmp_complex((long)1);
356  }
357  return s;
358}
359
360/*2
361* write a floating point number
362*/
363void ngcWrite (number &a)
364{
365  if (a==NULL)
366    StringAppend("0");
367  else
368  {
369    char *out;
370    out= complexToStr(*(gmp_complex*)a,gmp_output_digits);
371    StringAppend(out);
372    //    Free((ADDRESS)out, (strlen(out)+1)* sizeof(char) );
373    FreeL( (ADDRESS)out );
374  }
375}
376
377#ifdef LDEBUG
378BOOLEAN ngcDBTest(number a, char *f, int l)
379{
380  return TRUE;
381}
382#endif
383
384// local Variables: ***
385// folded-file: t ***
386// compile-command: "make installg" ***
387// End: ***
Note: See TracBrowser for help on using the repository browser.