source: git/libpolys/coeffs/gnumpfl.cc @ 19ee5eb

fieker-DuValspielwiese
Last change on this file since 19ee5eb was 19ee5eb, checked in by Hans Schoenemann <hannes@…>, 3 years ago
fix: division by 0: return 0, not NULL
  • Property mode set to 100644
File size: 11.3 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/*
5* ABSTRACT: computations with GMP floating-point numbers
6*
7* ngf == number gnu floats
8*/
9
10#include "misc/auxiliary.h"
11
12#include "reporter/reporter.h"
13
14#include "coeffs/coeffs.h"
15#include "coeffs/numbers.h"
16#include "coeffs/mpr_complex.h"
17
18#include "coeffs/longrat.h"
19#include "coeffs/shortfl.h"
20#include "coeffs/gnumpfl.h"
21#include "coeffs/gnumpc.h"
22#include "coeffs/modulop.h"
23
24const char *   ngfRead (const char *s, number *a, const coeffs r);
25
26union nf
27{
28  SI_FLOAT _f;
29  number _n;
30  nf(SI_FLOAT f) {_f = f;}
31  nf(number n) {_n = n;}
32  SI_FLOAT F() const {return _f;}
33  number N() const {return _n;}
34};
35
36/*2
37* n := i
38*/
39static number ngfInit (long i, const coeffs r)
40{
41  assume( getCoeffType(r) == n_long_R );
42
43  gmp_float* n= new gmp_float( (double)i );
44  return (number)n;
45}
46
47/*2
48* convert number to int
49*/
50static long ngfInt(number &i, const coeffs r)
51{
52  assume( getCoeffType(r) == n_long_R );
53
54  double d=(double)*(gmp_float*)i;
55  if (d<0.0)
56    return (long)(d-0.5);
57  else
58    return (long)(d+0.5);
59}
60
61static BOOLEAN ngfIsZero (number a, const coeffs r)
62{
63  assume( getCoeffType(r) == n_long_R );
64
65  return ( ((gmp_float*)a)->isZero() );
66}
67
68static int ngfSize(number n, const coeffs r)
69{
70  long i = ngfInt(n, r);
71  /* basically return the largest integer in n;
72     only if this happens to be zero although n != 0,
73     return 1;
74     (this code ensures that zero has the size zero) */
75  if ((i == 0) && (ngfIsZero(n,r) == FALSE)) i = 1;
76  return ABS(i);
77}
78
79/*2
80* delete a
81*/
82static void ngfDelete (number * a, const coeffs r)
83{
84  assume( getCoeffType(r) == n_long_R );
85
86  if ( *a != NULL )
87  {
88    delete *(gmp_float**)a;
89    *a=NULL;
90  }
91}
92
93/*2
94* copy a to b
95*/
96static number ngfCopy(number a, const coeffs r)
97{
98  assume( getCoeffType(r) == n_long_R );
99
100  gmp_float* b= new gmp_float( *(gmp_float*)a );
101  return (number)b;
102}
103
104#if 0
105static number ngfCopyMap(number a, const coeffs r1, const coeffs r2)
106{
107  assume( getCoeffType(r1) == n_long_R );
108  assume( getCoeffType(r2) == n_long_R );
109
110  gmp_float* b= NULL;
111  if ( a !=  NULL )
112  {
113    b= new gmp_float( *(gmp_float*)a );
114  }
115  return (number)b;
116}
117#endif
118
119/*2
120* za:= - za
121*/
122static number ngfNeg (number a, const coeffs r)
123{
124  assume( getCoeffType(r) == n_long_R );
125
126  *(gmp_float*)a= -(*(gmp_float*)a);
127  return (number)a;
128}
129
130/*
131* 1/a
132*/
133static number ngfInvers(number a, const coeffs r)
134{
135  assume( getCoeffType(r) == n_long_R );
136
137  gmp_float* f= NULL;
138  if (((gmp_float*)a)->isZero() )
139  {
140    WerrorS(nDivBy0);
141    f= new gmp_float( 0 );
142  }
143  else
144  {
145    f= new gmp_float( gmp_float(1) / (*(gmp_float*)a) );
146  }
147  return (number)f;
148}
149
150/*2
151* u:= a + b
152*/
153static number ngfAdd (number a, number b, const coeffs R)
154{
155  assume( getCoeffType(R) == n_long_R );
156
157  gmp_float* r= new gmp_float( (*(gmp_float*)a) + (*(gmp_float*)b) );
158  return (number)r;
159}
160
161/*2
162* u:= a - b
163*/
164static number ngfSub (number a, number b, const coeffs R)
165{
166  assume( getCoeffType(R) == n_long_R );
167
168  gmp_float* r= new gmp_float( (*(gmp_float*)a) - (*(gmp_float*)b) );
169  return (number)r;
170}
171
172/*2
173* u := a * b
174*/
175static number ngfMult (number a, number b, const coeffs R)
176{
177  assume( getCoeffType(R) == n_long_R );
178
179  gmp_float* r= new gmp_float( (*(gmp_float*)a) * (*(gmp_float*)b) );
180  return (number)r;
181}
182
183/*2
184* u := a / b
185*/
186static number ngfDiv (number a, number b, const coeffs r)
187{
188  assume( getCoeffType(r) == n_long_R );
189 
190  gmp_float* f;
191  if ( ((gmp_float*)b)->isZero() )
192  {
193    // a/0 = error
194    WerrorS(nDivBy0);
195    f= new gmp_float( 0 );
196  }
197  else
198  {
199    f= new gmp_float( (*(gmp_float*)a) / (*(gmp_float*)b) );
200  }
201  return (number)f;
202}
203
204/*2
205* u:= x ^ exp
206*/
207static number ngfPower (number x, int exp, const coeffs r)
208{
209  assume( getCoeffType(r) == n_long_R );
210
211  if ( exp == 0 )
212  {
213    gmp_float* n = new gmp_float(1);
214    return (number)n;
215  }
216  else if ( ngfIsZero(x, r) ) // 0^e, e>0
217  {
218    return ngfInit(0, r);
219  }
220  else if ( exp == 1 )
221  {
222    return ngfCopy(x,r);
223  }
224  return (number) ( new gmp_float( (*(gmp_float*)x)^exp ) );
225}
226
227/* kept for compatibility reasons, to be deleted */
228static void ngfPower ( number x, int exp, number * u, const coeffs r )
229{
230  *u = ngfPower(x, exp, r);
231}
232
233/*2
234* za > 0 ?
235*/
236static BOOLEAN ngfGreaterZero (number a, const coeffs r)
237{
238  assume( getCoeffType(r) == n_long_R );
239
240  return (((gmp_float*)a)->sign() > 0);
241}
242
243/*2
244* a > b ?
245*/
246static BOOLEAN ngfGreater (number a, number b, const coeffs r)
247{
248  assume( getCoeffType(r) == n_long_R );
249
250  return ( (*(gmp_float*)a) > (*(gmp_float*)b) );
251}
252
253/*2
254* a = b ?
255*/
256static BOOLEAN ngfEqual (number a, number b, const coeffs r)
257{
258  assume( getCoeffType(r) == n_long_R );
259
260  return ( (*(gmp_float*)a) == (*(gmp_float*)b) );
261}
262
263/*2
264* a == 1 ?
265*/
266static BOOLEAN ngfIsOne (number a, const coeffs r)
267{
268  assume( getCoeffType(r) == n_long_R );
269
270  return ((gmp_float*)a)->isOne();
271}
272
273/*2
274* a == -1 ?
275*/
276static BOOLEAN ngfIsMOne (number a, const coeffs r)
277{
278  assume( getCoeffType(r) == n_long_R );
279
280  return ((gmp_float*)a)->isMOne();
281}
282
283static char * ngfEatFloatNExp(char * s )
284{
285  char *start= s;
286
287  // eat floats (mantissa) like:
288  //   0.394394993, 102.203003008,  .300303032, pssibly starting with -
289  if (*s == '-') s++;
290  while ((*s >= '0' && *s <= '9')||(*s == '.')) s++;
291
292  // eat the exponent, starts with 'e' followed by '+', '-'
293  // and digits, like:
294  //   e-202, e+393, accept also E7
295  if ( (s != start) && ((*s == 'e')||(*s=='E')))
296  {
297    if (*s=='E') *s='e';
298    s++; // skip 'e'/'E'
299    if ((*s == '+') || (*s == '-')) s++;
300    while ((*s >= '0' && *s <= '9')) s++;
301  }
302
303  return s;
304}
305
306/*2
307* extracts the number a from s, returns the rest
308*
309* This is also called to print components of complex coefficients.
310* Handle with care!
311*/
312const char * ngfRead (const char * start, number * a, const coeffs r)
313{
314  assume( getCoeffType(r) == n_long_R or getCoeffType(r) == n_long_C);
315
316  char *s= (char *)start;
317
318  //Print("%s\n",s);
319
320  s= ngfEatFloatNExp( s );
321
322  if (*s=='\0')  // 0
323  {
324    if ( *(gmp_float**)a == NULL ) (*(gmp_float**)a)= new gmp_float();
325    (*(gmp_float**)a)->setFromStr(start);
326  }
327  else if (s==start)  // 1
328  {
329    if ( *(gmp_float**)a != NULL )  delete (*(gmp_float**)a);
330    (*(gmp_float**)a)= new gmp_float(1);
331  }
332  else
333  {
334    gmp_float divisor(1.0);
335    char *start2=s;
336    if ( *s == '/' )
337    {
338      s++;
339      s= ngfEatFloatNExp( (char *)s );
340      if (s!= start2+1)
341      {
342        char tmp_c=*s;
343        *s='\0';
344        divisor.setFromStr(start2+1);
345        *s=tmp_c;
346      }
347      else
348      {
349        Werror("wrong long real format: %s",start2);
350      }
351    }
352    char c=*start2;
353    *start2='\0';
354    if ( *(gmp_float**)a == NULL ) (*(gmp_float**)a)= new gmp_float();
355    (*(gmp_float**)a)->setFromStr(start);
356    *start2=c;
357    if (divisor.isZero())
358    {
359      WerrorS(nDivBy0);
360    }
361    else
362      (**(gmp_float**)a) /= divisor;
363  }
364
365  return s;
366}
367
368/*2
369* write a floating point number
370*/
371static void ngfWrite (number a, const coeffs r)
372{
373  assume( getCoeffType(r) == n_long_R );
374
375  char *out;
376  if ( a != NULL )
377  {
378    out= floatToStr(*(gmp_float*)a, r->float_len);
379    StringAppendS(out);
380    //omFreeSize((void *)out, (strlen(out)+1)* sizeof(char) );
381    omFree( (void *)out );
382  }
383  else
384  {
385    StringAppendS("0");
386  }
387}
388
389static BOOLEAN ngfCoeffIsEqual (const coeffs r, n_coeffType n, void * parameter)
390{
391  if (n==n_long_R)
392  {
393    LongComplexInfo* p = (LongComplexInfo *)(parameter);
394    if ((p!=NULL)
395    && (p->float_len == r->float_len)
396    && (p->float_len2 == r->float_len2))
397      return TRUE;
398  }
399  return FALSE;
400}
401
402static void ngfSetChar(const coeffs r)
403{
404  setGMPFloatDigits(r->float_len, r->float_len2);
405}
406
407static char* ngfCoeffName(const coeffs r)
408{
409  STATIC_VAR char ngfCoeffName_buf[30];
410  snprintf(ngfCoeffName_buf,30,"Float(%d,%d)",r->float_len,r->float_len2);
411  return ngfCoeffName_buf;
412}
413
414static number ngfMapQ(number from, const coeffs src, const coeffs dst)
415{
416  assume( getCoeffType(dst) == n_long_R );
417  assume( src->rep == n_rep_gap_rat );
418
419  gmp_float *res=new gmp_float(numberFieldToFloat(from,QTOF));
420  return (number)res;
421}
422static number ngfMapZ(number from, const coeffs aRing, const coeffs r)
423{
424  assume( getCoeffType(r) == n_long_R );
425  assume( aRing->rep == n_rep_gap_gmp);
426
427  if ( from != NULL )
428  {
429    if (SR_HDL(from) & SR_INT)
430    {
431      gmp_float f_i= gmp_float(SR_TO_INT(from));
432      gmp_float *res=new gmp_float(f_i);
433      return (number)res;
434    }
435    gmp_float f_i=(mpz_ptr)from;
436    gmp_float *res=new gmp_float(f_i);
437    return (number)res;
438  }
439  else
440    return NULL;
441}
442
443static number ngfMapR(number from, const coeffs src, const coeffs dst)
444{
445  assume( getCoeffType(dst) == n_long_R );
446  assume( getCoeffType(src) == n_R );
447
448  gmp_float *res=new gmp_float((double)nf(from).F());
449  return (number)res;
450}
451
452static number ngfMapP(number from, const coeffs src, const coeffs dst)
453{
454  assume( getCoeffType(dst) == n_long_R );
455  assume( getCoeffType(src) ==  n_Zp );
456
457  return ngfInit(npInt(from,src), dst); // FIXME? TODO? // extern int     npInt         (number &n, const coeffs r);
458}
459
460static number ngfMapC(number from, const coeffs src, const coeffs dst)
461{
462  assume( getCoeffType(dst) == n_long_R );
463  assume( getCoeffType(src) ==  n_long_C );
464
465  gmp_float *res=new gmp_float(((gmp_complex*)from)->real());
466  return (number)res;
467}
468
469static number ngfInitMPZ(mpz_t m, const coeffs)
470{
471  gmp_float *res=new gmp_float(m);
472  return (number)res;
473}
474
475static nMapFunc ngfSetMap(const coeffs src, const coeffs dst)
476{
477  assume( getCoeffType(dst) == n_long_R );
478
479  if (src->rep==n_rep_gap_rat) /*Q, Z*/
480  {
481    return ngfMapQ;
482  }
483  if (src->rep==n_rep_gap_gmp) /*Q, Z*/
484  {
485    return ngfMapZ;
486  }
487  if ((src->rep==n_rep_gmp_float) && nCoeff_is_long_R(src))
488  {
489    return ndCopyMap; //ngfCopyMap;
490  }
491  if ((src->rep==n_rep_float) && nCoeff_is_R(src))
492  {
493    return ngfMapR;
494  }
495  if ((src->rep==n_rep_gmp_complex) && nCoeff_is_long_C(src))
496  {
497    return ngfMapC;
498  }
499  if ((src->rep==n_rep_int) && nCoeff_is_Zp(src))
500  {
501    return ngfMapP;
502  }
503  return NULL;
504}
505
506BOOLEAN ngfInitChar(coeffs n, void *parameter)
507{
508  assume( getCoeffType(n) == n_long_R );
509
510  n->is_field=TRUE;
511  n->is_domain=TRUE;
512  n->rep=n_rep_gmp_float;
513
514  //n->cfKillChar = ndKillChar; /* dummy */
515
516  n->cfSetChar = ngfSetChar;
517  n->ch = 0;
518  n->cfCoeffName=ngfCoeffName;
519
520  n->cfDelete  = ngfDelete;
521  //n->cfNormalize=ndNormalize;
522  n->cfInit    = ngfInit;
523  n->cfInitMPZ = ngfInitMPZ;
524  n->cfInt     = ngfInt;
525  n->cfAdd     = ngfAdd;
526  n->cfSub     = ngfSub;
527  n->cfMult    = ngfMult;
528  n->cfDiv     = ngfDiv;
529  n->cfExactDiv= ngfDiv;
530  n->cfInpNeg     = ngfNeg;
531  n->cfInvers  = ngfInvers;
532  n->cfCopy   = ngfCopy;
533  n->cfGreater = ngfGreater;
534  n->cfEqual   = ngfEqual;
535  n->cfIsZero  = ngfIsZero;
536  n->cfIsOne   = ngfIsOne;
537  n->cfIsMOne  = ngfIsMOne;
538  n->cfGreaterZero = ngfGreaterZero;
539  n->cfWriteLong  = ngfWrite;
540  n->cfRead    = ngfRead;
541  n->cfPower   = ngfPower;
542  n->cfSetMap = ngfSetMap;
543#ifdef LDEBUG
544  //n->cfDBTest  = ndDBTest; // not yet implemented: ngfDBTest
545#endif
546
547  n->nCoeffIsEqual = ngfCoeffIsEqual;
548
549  if( parameter != NULL)
550  {
551    LongComplexInfo* p = (LongComplexInfo*)parameter;
552
553    n->float_len = p->float_len;
554    n->float_len2 = p->float_len2;
555  } else // default values, just for testing!
556  {
557    n->float_len = SHORT_REAL_LENGTH;
558    n->float_len2 = SHORT_REAL_LENGTH;
559  }
560
561  assume( n->float_len2 >= SHORT_REAL_LENGTH );
562
563  assume( n_NumberOfParameters(n) == 0 );
564  assume( n_ParameterNames(n) == NULL );
565
566  return FALSE;
567}
Note: See TracBrowser for help on using the repository browser.