source: git/Singular/gnumpc.cc @ 48aa42

fieker-DuValspielwiese
Last change on this file since 48aa42 was 3095a1, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: repart/impart git-svn-id: file:///usr/local/Singular/svn/trunk@4886 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.15 2000-12-13 17:49:37 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
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  *r=NULL;
60}
61
62/*2
63* n := i
64*/
65number 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*/
78int 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
88void ngcDBDelete (number * a,char *f, int l)
89#else
90void 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*/
102number 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*/
115number 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*/
126number 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*/
143number 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*/
168number 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*/
194number 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*/
211number 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*/
231void 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
262BOOLEAN ngcIsZero (number a)
263{
264  if ( a == NULL ) return TRUE;
265  return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero());
266}
267
268number ngcRePart(number a)
269{
270  if (((gmp_complex*)a)->real().isZero()) return NULL;
271  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real());
272  return (number)n;
273} 
274 
275number ngcImPart(number a)
276{
277  if (((gmp_complex*)a)->imag().isZero()) return NULL;
278  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag());
279  return (number)n;
280} 
281 
282/*2
283* za >= 0 ?
284*/
285BOOLEAN ngcGreaterZero (number a)
286{
287  if ( a == NULL ) return TRUE;
288  if ( ! ((gmp_complex*)a)->imag().isZero() )
289    return ( abs( *(gmp_complex*)a).sign() >= 0 );
290  else
291    return ( ((gmp_complex*)a)->real().sign() >= 0 );
292}
293
294/*2
295* a > b ?
296*/
297BOOLEAN 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*/
313BOOLEAN 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*/
329BOOLEAN ngcIsOne (number a)
330{
331  if ( a == NULL ) return FALSE;
332  //return (((gmp_complex*)a)->real().isOne() && ((gmp_complex*)a)->imag().isZero());
333  return (((gmp_complex*)a)->real().isOne());
334}
335
336/*2
337* a == -1 ?
338*/
339BOOLEAN ngcIsMOne (number a)
340{
341  if ( a == NULL ) return FALSE;
342  //  return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero());
343  return (((gmp_complex*)a)->real().isMOne());
344}
345
346/*2
347* extracts the number a from s, returns the rest
348*/
349char * ngcRead (char * s, number * a)
350{
351  char *start= s;
352  if ((*s >= '0') && (*s <= '9'))
353  {
354    gmp_float *re=NULL;
355    s=ngfRead(s,(number *)&re);
356    gmp_complex *aa=new gmp_complex(*re);
357    *a=(number)aa;
358    delete re;
359  }
360  else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0)
361  {
362    s+=strlen(currRing->parameter[0]);
363    gmp_complex *aa=new gmp_complex((long)0,(long)1);
364    *a=(number)aa;
365  }
366  else
367  {
368    *a=(number) new gmp_complex((long)1);
369  }
370  return s;
371}
372
373/*2
374* write a floating point number
375*/
376void ngcWrite (number &a)
377{
378  if (a==NULL)
379    StringAppend("0");
380  else
381  {
382    char *out;
383    out= complexToStr(*(gmp_complex*)a,gmp_output_digits);
384    StringAppend(out);
385    //    omFreeSize((ADDRESS)out, (strlen(out)+1)* sizeof(char) );
386    omFree( (ADDRESS)out );
387  }
388}
389
390#ifdef LDEBUG
391BOOLEAN ngcDBTest(number a, char *f, int l)
392{
393  return TRUE;
394}
395#endif
396
397// local Variables: ***
398// folded-file: t ***
399// compile-command: "make installg" ***
400// End: ***
Note: See TracBrowser for help on using the repository browser.