source: git/coeffs/gnumpc.cc @ e973a0

fieker-DuValspielwiese
Last change on this file since e973a0 was 7d90aa, checked in by Oleksandr Motsak <motsak@…>, 13 years ago
initial changes to 'coeffs' + first build system
  • Property mode set to 100644
File size: 6.9 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id$ */
5/*
6* ABSTRACT: computations with GMP complex floating-point numbers
7*
8* ngc == number gnu complex
9*/
10
11#include "coeffs.h"
12#include "numbers.h"
13#include "longrat.h"
14#include "modulop.h"
15#include "gnumpc.h"
16#include "gnumpfl.h"
17#include "mpr_complex.h"
18
19extern size_t gmp_output_digits;
20
21
22number ngcMapQ(number from)
23{
24  if ( from != NULL )
25  {
26    gmp_complex *res=new gmp_complex(numberFieldToFloat(from,QTOF));
27    return (number)res;
28  }
29  else
30    return NULL;
31}
32union nf
33{
34  float _f;
35  number _n;
36  nf(float f) {_f = f;}
37  nf(number n) {_n = n;}
38  float F() const {return _f;}
39  number N() const {return _n;}
40};
41static number ngcMapLongR(number from)
42{
43  if ( from != NULL )
44  {
45    gmp_complex *res=new gmp_complex(*((gmp_float *)from));
46    return (number)res;
47  }
48  else
49    return NULL;
50}
51static number ngcMapR(number from)
52{
53  if ( from != NULL )
54  {
55    gmp_complex *res=new gmp_complex((double)nf(from).F());
56    return (number)res;
57  }
58  else
59    return NULL;
60}
61extern ring ngfMapRing;
62static number ngcMapP(number from)
63{
64  if ( from != NULL)
65    return ngcInit(npInt(from,ngfMapRing), currRing);
66  else
67    return NULL;
68}
69
70nMapFunc ngcSetMap(const coeffs src,const coeffs dst)
71{
72  if(rField_is_Q(src))
73  {
74    return ngcMapQ;
75  }
76  if (rField_is_long_R(src))
77  {
78    return ngcMapLongR;
79  }
80  if (rField_is_long_C(src))
81  {
82    return ngcCopy;
83  }
84  if(rField_is_R(src))
85  {
86    return ngcMapR;
87  }
88  if (rField_is_Zp(src))
89  {
90    ngfMapRing=src;
91    return ngcMapP;
92  }
93  return NULL;
94}
95
96number   ngcPar(int i)
97{
98  gmp_complex* n= new gmp_complex( (long)0, (long)1 );
99  return (number)n;
100}
101
102/*2
103* n := i
104*/
105number ngcInit (int i, const coeffs r)
106{
107  gmp_complex* n= new gmp_complex( (long)i, (long)0 );
108  return (number)n;
109}
110
111/*2
112* convert number to int
113*/
114int ngcInt(number &i, const coeffs r)
115{
116  return (int)((gmp_complex*)i)->real();
117}
118
119int ngcSize(number n)
120{
121  int r = (int)((gmp_complex*)n)->real();
122  if (r < 0) r = -r;
123  int i = (int)((gmp_complex*)n)->imag();
124  if (i < 0) i = -i;
125  int oneNorm = r + i;
126  /* basically return the 1-norm of n;
127     only if this happens to be zero although n != 0,
128     return 1;
129     (this code ensures that zero has the size zero) */
130  if ((oneNorm == 0.0) & (ngcIsZero(n) == FALSE)) oneNorm = 1;
131  return oneNorm;
132}
133
134/*2
135* delete a
136*/
137void ngcDelete (number * a, const coeffs r)
138{
139  if ( *a != NULL )
140  {
141    delete *(gmp_complex**)a;
142    *a=NULL;
143  }
144}
145
146/*2
147* copy a to b
148*/
149number ngcCopy(number a)
150{
151  gmp_complex* b= new gmp_complex( *(gmp_complex*)a );
152  return (number)b;
153}
154number ngc_Copy(number a, const coeffs r)
155{
156  gmp_complex* b=new gmp_complex( *(gmp_complex*)a );
157  return (number)b;
158}
159
160/*2
161* za:= - za
162*/
163gmp_complex ngc_m1(-1);
164
165number ngcNeg (number a)
166{
167  gmp_complex* r=(gmp_complex*)a;
168  (*r) *= ngc_m1;
169  return (number)a;
170}
171
172/*
173* 1/a
174*/
175number ngcInvers(number a)
176{
177  gmp_complex* r = NULL;
178  if (((gmp_complex*)a)->isZero())
179  {
180    WerrorS(nDivBy0);
181  }
182  else
183  {
184    r= new gmp_complex( (gmp_complex)1 / (*(gmp_complex*)a) );
185  }
186  return (number)r;
187}
188
189/*2
190* u:= a + b
191*/
192number ngcAdd (number a, number b)
193{
194  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) + (*(gmp_complex*)b) );
195  return (number)r;
196}
197
198/*2
199* u:= a - b
200*/
201number ngcSub (number a, number b)
202{
203  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) - (*(gmp_complex*)b) );
204  return (number)r;
205}
206
207/*2
208* u := a * b
209*/
210number ngcMult (number a, number b)
211{
212  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) * (*(gmp_complex*)b) );
213  return (number)r;
214}
215
216/*2
217* u := a / b
218*/
219number ngcDiv (number a, number b)
220{
221  if (((gmp_complex*)b)->isZero())
222  {
223    // a/0 = error
224    WerrorS(nDivBy0);
225    return NULL;
226  }
227  gmp_complex* r= new gmp_complex( (*(gmp_complex*)a) / (*(gmp_complex*)b) );
228  return (number)r;
229}
230
231/*2
232* u:= x ^ exp
233*/
234void ngcPower ( number x, int exp, number * u )
235{
236  if ( exp == 0 )
237  {
238    gmp_complex* n = new gmp_complex(1);
239    *u=(number)n;
240    return;
241  }
242  else if ( exp == 1 )
243  {
244    nNew(u);
245    gmp_complex* n = new gmp_complex();
246    *n= *(gmp_complex*)x;
247    *u=(number)n;
248    return;
249  }
250  else if (exp == 2)
251  {
252    nNew(u);
253    gmp_complex* n = new gmp_complex();
254    *n= *(gmp_complex*)x;
255    *u=(number)n;
256    *(gmp_complex*)(*u) *= *(gmp_complex*)n;
257    return;
258  }
259  if (exp&1==1)
260  {
261    ngcPower(x,exp-1,u);
262    gmp_complex *n=new gmp_complex();
263    *n=*(gmp_complex*)x;
264    *(gmp_complex*)(*u) *= *(gmp_complex*)n;
265    delete n;
266  }
267  else
268  {
269    number w;
270    nNew(&w);
271    ngcPower(x,exp/2,&w);
272    ngcPower(w,2,u);
273    nDelete(&w);
274  }
275}
276
277BOOLEAN ngcIsZero (number a)
278{
279  return ( ((gmp_complex*)a)->real().isZero() && ((gmp_complex*)a)->imag().isZero());
280}
281
282number ngcRePart(number a)
283{
284  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->real());
285  return (number)n;
286}
287
288number ngcImPart(number a)
289{
290  gmp_complex* n = new gmp_complex(((gmp_complex*)a)->imag());
291  return (number)n;
292}
293
294/*2
295* za >= 0 ?
296*/
297BOOLEAN ngcGreaterZero (number a)
298{
299  if ( ! ((gmp_complex*)a)->imag().isZero() )
300    return ( abs( *(gmp_complex*)a).sign() >= 0 );
301  else
302    return ( ((gmp_complex*)a)->real().sign() >= 0 );
303}
304
305/*2
306* a > b ?
307*/
308BOOLEAN ngcGreater (number a, number b)
309{
310  gmp_complex *aa=(gmp_complex*)a;
311  gmp_complex *bb=(gmp_complex*)b;
312  return (*aa) > (*bb);
313}
314
315/*2
316* a = b ?
317*/
318BOOLEAN ngcEqual (number a, number b)
319{
320  gmp_complex *aa=(gmp_complex*)a;
321  gmp_complex *bb=(gmp_complex*)b;
322  return (*aa) == (*bb);
323}
324
325/*2
326* a == 1 ?
327*/
328BOOLEAN ngcIsOne (number a)
329{
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  return (((gmp_complex*)a)->real().isMOne() && ((gmp_complex*)a)->imag().isZero());
340  //return (((gmp_complex*)a)->real().isMOne());
341}
342
343/*2
344* extracts the number a from s, returns the rest
345*/
346const char * ngcRead (const char * s, number * a)
347{
348  if ((*s >= '0') && (*s <= '9'))
349  {
350    gmp_float *re=NULL;
351    s=ngfRead(s,(number *)&re);
352    gmp_complex *aa=new gmp_complex(*re);
353    *a=(number)aa;
354    delete re;
355  }
356  else if (strncmp(s,currRing->parameter[0],strlen(currRing->parameter[0]))==0)
357  {
358    s+=strlen(currRing->parameter[0]);
359    gmp_complex *aa=new gmp_complex((long)0,(long)1);
360    *a=(number)aa;
361  }
362  else
363  {
364    *a=(number) new gmp_complex((long)1);
365  }
366  return s;
367}
368
369/*2
370* write a floating point number
371*/
372void ngcWrite (number &a, const coeffs r)
373{
374  if (a==NULL)
375    StringAppendS("0");
376  else
377  {
378    char *out;
379    out= complexToStr(*(gmp_complex*)a,gmp_output_digits);
380    StringAppendS(out);
381    //    omFreeSize((void *)out, (strlen(out)+1)* sizeof(char) );
382    omFree( (void *)out );
383  }
384}
385
386#ifdef LDEBUG
387// not yet implemented
388//BOOLEAN ngcDBTest(number a, const char *f, const int l)
389//{
390//  return TRUE;
391//}
392#endif
393
394// local Variables: ***
395// folded-file: t ***
396// compile-command: "make installg" ***
397// End: ***
Note: See TracBrowser for help on using the repository browser.