My Project
gnumpfl.cc
Go to the documentation of this file.
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{
29 number _n;
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{
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{
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{
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{
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{
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{
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{
136
137 gmp_float* f= NULL;
138 if (((gmp_float*)a)->isZero() )
139 {
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{
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{
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{
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{
189
190 gmp_float* f;
191 if ( ((gmp_float*)b)->isZero() )
192 {
193 // a/0 = error
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{
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{
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{
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{
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{
269
270 return ((gmp_float*)a)->isOne();
271}
272
273/*2
274* a == -1 ?
275*/
276static BOOLEAN ngfIsMOne (number a, const coeffs r)
277{
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{
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 {
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{
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
420 return (number)res;
421}
422
423static number ngfMapZ(number from, const coeffs aRing, const coeffs r)
424{
426 assume( aRing->rep == n_rep_gmp);
427
428 gmp_float *res=new gmp_float((mpz_ptr)from);
429 return (number)res;
430}
431
432static number ngfMapR(number from, const coeffs src, const coeffs dst)
433{
434 assume( getCoeffType(dst) == n_long_R );
435 assume( getCoeffType(src) == n_R );
436
437 gmp_float *res=new gmp_float((double)nf(from).F());
438 return (number)res;
439}
440
441static number ngfMapP(number from, const coeffs src, const coeffs dst)
442{
443 assume( getCoeffType(dst) == n_long_R );
444 assume( getCoeffType(src) == n_Zp );
445
446 return ngfInit(npInt(from,src), dst); // FIXME? TODO? // extern int npInt (number &n, const coeffs r);
447}
448
449static number ngfMapC(number from, const coeffs src, const coeffs dst)
450{
451 assume( getCoeffType(dst) == n_long_R );
452 assume( getCoeffType(src) == n_long_C );
453
454 gmp_float *res=new gmp_float(((gmp_complex*)from)->real());
455 return (number)res;
456}
457
458static number ngfInitMPZ(mpz_t m, const coeffs)
459{
460 gmp_float *res=new gmp_float(m);
461 return (number)res;
462}
463
464static nMapFunc ngfSetMap(const coeffs src, const coeffs dst)
465{
466 assume( getCoeffType(dst) == n_long_R );
467
468 if (src->rep==n_rep_gap_rat) /*Q, Z*/
469 {
470 return ngfMapQ;
471 }
472 if (src->rep==n_rep_gap_gmp) /*Q, bigint*/
473 {
474 return ngfMapQ;
475 }
476 if (src->rep==n_rep_gmp) /* Z*/
477 {
478 return ngfMapZ;
479 }
480 if ((src->rep==n_rep_gmp_float) && nCoeff_is_long_R(src))
481 {
482 return ndCopyMap; //ngfCopyMap;
483 }
484 if ((src->rep==n_rep_float) && nCoeff_is_R(src))
485 {
486 return ngfMapR;
487 }
488 if ((src->rep==n_rep_gmp_complex) && nCoeff_is_long_C(src))
489 {
490 return ngfMapC;
491 }
492 if ((src->rep==n_rep_int) && nCoeff_is_Zp(src))
493 {
494 return ngfMapP;
495 }
496 return NULL;
497}
498
499BOOLEAN ngfInitChar(coeffs n, void *parameter)
500{
502
503 n->is_field=TRUE;
504 n->is_domain=TRUE;
505 n->rep=n_rep_gmp_float;
506
507 //n->cfKillChar = ndKillChar; /* dummy */
508
509 n->cfSetChar = ngfSetChar;
510 n->ch = 0;
511 n->cfCoeffName=ngfCoeffName;
512
513 n->cfDelete = ngfDelete;
514 //n->cfNormalize=ndNormalize;
515 n->cfInit = ngfInit;
516 n->cfInitMPZ = ngfInitMPZ;
517 n->cfInt = ngfInt;
518 n->cfAdd = ngfAdd;
519 n->cfSub = ngfSub;
520 n->cfMult = ngfMult;
521 n->cfDiv = ngfDiv;
522 n->cfExactDiv= ngfDiv;
523 n->cfInpNeg = ngfNeg;
524 n->cfInvers = ngfInvers;
525 n->cfCopy = ngfCopy;
526 n->cfGreater = ngfGreater;
527 n->cfEqual = ngfEqual;
528 n->cfIsZero = ngfIsZero;
529 n->cfIsOne = ngfIsOne;
530 n->cfIsMOne = ngfIsMOne;
531 n->cfGreaterZero = ngfGreaterZero;
532 n->cfWriteLong = ngfWrite;
533 n->cfRead = ngfRead;
534 n->cfPower = ngfPower;
535 n->cfSetMap = ngfSetMap;
536#ifdef LDEBUG
537 //n->cfDBTest = ndDBTest; // not yet implemented: ngfDBTest
538#endif
539
540 n->nCoeffIsEqual = ngfCoeffIsEqual;
541
542 if( parameter != NULL)
543 {
544 LongComplexInfo* p = (LongComplexInfo*)parameter;
545
546 n->float_len = p->float_len;
547 n->float_len2 = p->float_len2;
548 } else // default values, just for testing!
549 {
550 n->float_len = SHORT_REAL_LENGTH;
551 n->float_len2 = SHORT_REAL_LENGTH;
552 }
553
554 assume( n->float_len2 >= SHORT_REAL_LENGTH );
555
556 assume( n_NumberOfParameters(n) == 0 );
558
559 return FALSE;
560}
All the auxiliary stuff.
static int ABS(int v)
Definition: auxiliary.h:112
int BOOLEAN
Definition: auxiliary.h:87
#define TRUE
Definition: auxiliary.h:100
#define FALSE
Definition: auxiliary.h:96
int sign(const CanonicalForm &a)
int m
Definition: cfEzgcd.cc:128
int i
Definition: cfEzgcd.cc:132
Variable x
Definition: cfModGcd.cc:4082
int p
Definition: cfModGcd.cc:4078
CanonicalForm b
Definition: cfModGcd.cc:4103
FILE * f
Definition: checklibs.c:9
gmp_complex numbers based on
Definition: mpr_complex.h:179
void setFromStr(const char *in)
Definition: mpr_complex.cc:78
bool isZero() const
Definition: mpr_complex.cc:252
Coefficient rings, fields and other domains suitable for Singular polynomials.
number ndCopyMap(number a, const coeffs src, const coeffs dst)
Definition: numbers.cc:255
static FORCE_INLINE BOOLEAN nCoeff_is_long_R(const coeffs r)
Definition: coeffs.h:891
n_coeffType
Definition: coeffs.h:27
@ n_R
single prescision (6,6) real numbers
Definition: coeffs.h:31
@ n_long_R
real floating point (GMP) numbers
Definition: coeffs.h:33
@ n_Zp
\F{p < 2^31}
Definition: coeffs.h:29
@ n_long_C
complex floating point (GMP) numbers
Definition: coeffs.h:41
static FORCE_INLINE char const ** n_ParameterNames(const coeffs r)
Returns a (const!) pointer to (const char*) names of parameters.
Definition: coeffs.h:778
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition: coeffs.h:421
static FORCE_INLINE int n_NumberOfParameters(const coeffs r)
Returns the number of parameters.
Definition: coeffs.h:774
static FORCE_INLINE BOOLEAN nCoeff_is_Zp(const coeffs r)
Definition: coeffs.h:800
@ n_rep_gap_rat
(number), see longrat.h
Definition: coeffs.h:111
@ n_rep_gap_gmp
(), see rinteger.h, new impl.
Definition: coeffs.h:112
@ n_rep_float
(float), see shortfl.h
Definition: coeffs.h:116
@ n_rep_int
(int), see modulop.h
Definition: coeffs.h:110
@ n_rep_gmp_float
(gmp_float), see
Definition: coeffs.h:117
@ n_rep_gmp
(mpz_ptr), see rmodulon,h
Definition: coeffs.h:115
@ n_rep_gmp_complex
(gmp_complex), see gnumpc.h
Definition: coeffs.h:118
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition: coeffs.h:73
static FORCE_INLINE BOOLEAN nCoeff_is_R(const coeffs r)
Definition: coeffs.h:836
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:894
const CanonicalForm int s
Definition: facAbsFact.cc:51
CanonicalForm res
Definition: facAbsFact.cc:60
bool isZero(const CFArray &A)
checks if entries of A are zero
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define STATIC_VAR
Definition: globaldefs.h:7
static number ngfInit(long i, const coeffs r)
Definition: gnumpfl.cc:39
static number ngfMapC(number from, const coeffs src, const coeffs dst)
Definition: gnumpfl.cc:449
static number ngfCopy(number a, const coeffs r)
Definition: gnumpfl.cc:96
static BOOLEAN ngfGreater(number a, number b, const coeffs r)
Definition: gnumpfl.cc:246
static void ngfSetChar(const coeffs r)
Definition: gnumpfl.cc:402
static number ngfMapZ(number from, const coeffs aRing, const coeffs r)
Definition: gnumpfl.cc:423
static number ngfInvers(number a, const coeffs r)
Definition: gnumpfl.cc:133
static long ngfInt(number &i, const coeffs r)
Definition: gnumpfl.cc:50
static number ngfInitMPZ(mpz_t m, const coeffs)
Definition: gnumpfl.cc:458
static number ngfDiv(number a, number b, const coeffs r)
Definition: gnumpfl.cc:186
static number ngfAdd(number a, number b, const coeffs R)
Definition: gnumpfl.cc:153
static number ngfMapP(number from, const coeffs src, const coeffs dst)
Definition: gnumpfl.cc:441
static BOOLEAN ngfGreaterZero(number a, const coeffs r)
Definition: gnumpfl.cc:236
static BOOLEAN ngfIsMOne(number a, const coeffs r)
Definition: gnumpfl.cc:276
static BOOLEAN ngfIsZero(number a, const coeffs r)
Definition: gnumpfl.cc:61
static void ngfWrite(number a, const coeffs r)
Definition: gnumpfl.cc:371
static number ngfPower(number x, int exp, const coeffs r)
Definition: gnumpfl.cc:207
static BOOLEAN ngfEqual(number a, number b, const coeffs r)
Definition: gnumpfl.cc:256
static int ngfSize(number n, const coeffs r)
Definition: gnumpfl.cc:68
static char * ngfEatFloatNExp(char *s)
Definition: gnumpfl.cc:283
static void ngfDelete(number *a, const coeffs r)
Definition: gnumpfl.cc:82
static number ngfMapQ(number from, const coeffs src, const coeffs dst)
Definition: gnumpfl.cc:414
const char * ngfRead(const char *s, number *a, const coeffs r)
Definition: gnumpfl.cc:312
static BOOLEAN ngfCoeffIsEqual(const coeffs r, n_coeffType n, void *parameter)
Definition: gnumpfl.cc:389
static number ngfNeg(number a, const coeffs r)
Definition: gnumpfl.cc:122
static BOOLEAN ngfIsOne(number a, const coeffs r)
Definition: gnumpfl.cc:266
static number ngfMult(number a, number b, const coeffs R)
Definition: gnumpfl.cc:175
static nMapFunc ngfSetMap(const coeffs src, const coeffs dst)
Definition: gnumpfl.cc:464
static char * ngfCoeffName(const coeffs r)
Definition: gnumpfl.cc:407
BOOLEAN ngfInitChar(coeffs n, void *parameter)
Initialize r.
Definition: gnumpfl.cc:499
static number ngfSub(number a, number b, const coeffs R)
Definition: gnumpfl.cc:164
static number ngfMapR(number from, const coeffs src, const coeffs dst)
Definition: gnumpfl.cc:432
#define assume(x)
Definition: mod2.h:387
long npInt(number &n, const coeffs r)
Definition: modulop.cc:85
char * floatToStr(const gmp_float &r, const unsigned int oprec)
Definition: mpr_complex.cc:578
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:357
gmp_float numberFieldToFloat(number num, int cf)
Definition: mpr_complex.cc:438
void setGMPFloatDigits(size_t digits, size_t rest)
Set size of mantissa digits - the number of output digits (basis 10) the size of mantissa consists of...
Definition: mpr_complex.cc:60
#define QTOF
Definition: mpr_complex.h:19
The main handler for Singular numbers which are suitable for Singular polynomials.
const char *const nDivBy0
Definition: numbers.h:87
#define SHORT_REAL_LENGTH
Definition: numbers.h:57
#define omFree(addr)
Definition: omAllocDecl.h:261
#define NULL
Definition: omList.c:12
void StringAppendS(const char *st)
Definition: reporter.cc:107
void Werror(const char *fmt,...)
Definition: reporter.cc:189
#define SI_FLOAT
Definition: shortfl.h:15
#define R
Definition: sirandom.c:27
Definition: gnumpfl.cc:27
nf(number n)
Definition: gnumpfl.cc:31
nf(SI_FLOAT f)
Definition: gnumpfl.cc:30
SI_FLOAT _f
Definition: gnumpfl.cc:28
number _n
Definition: gnumpfl.cc:29
SI_FLOAT F() const
Definition: gnumpfl.cc:32
number N() const
Definition: gnumpfl.cc:33