Loading...
Searching...
No Matches
gnumpfl.cc
Go to the documentation of this file.
/****************************************
* Computer Algebra System SINGULAR *
****************************************/
/*
* ABSTRACT: computations with GMP floating-point numbers
*
* ngf == number gnu floats
*/
#include "misc/auxiliary.h"
#include "reporter/reporter.h"
#include "coeffs/coeffs.h"
#include "coeffs/numbers.h"
#include "coeffs/mpr_complex.h"
#include "coeffs/longrat.h"
#include "coeffs/shortfl.h"
#include "coeffs/gnumpfl.h"
#include "coeffs/gnumpc.h"
#include "coeffs/modulop.h"
union nf
{
SI_FLOAT _f;
number _n;
nf(SI_FLOAT f) {_f = f;}
nf(number n) {_n = n;}
SI_FLOAT F() const {return _f;}
number N() const {return _n;}
};
/*2
* n := i
*/
static number ngfInit (long i, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
gmp_float* n= new gmp_float( (double)i );
return (number)n;
}
/*2
* convert number to int
*/
static long ngfInt(number &i, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
double d=(double)*(gmp_float*)i;
if (d<0.0)
return (long)(d-0.5);
else
return (long)(d+0.5);
}
static BOOLEAN ngfIsZero (number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
return ( ((gmp_float*)a)->isZero() );
}
static int ngfSize(number n, const coeffs r)
{
long i = ngfInt(n, r);
/* basically return the largest integer in n;
only if this happens to be zero although n != 0,
return 1;
(this code ensures that zero has the size zero) */
if ((i == 0) && (ngfIsZero(n,r) == FALSE)) i = 1;
return ABS(i);
}
/*2
* delete a
*/
static void ngfDelete (number * a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
if ( *a != NULL )
{
delete *(gmp_float**)a;
*a=NULL;
}
}
/*2
* copy a to b
*/
static number ngfCopy(number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
gmp_float* b= new gmp_float( *(gmp_float*)a );
return (number)b;
}
#if 0
static number ngfCopyMap(number a, const coeffs r1, const coeffs r2)
{
assume( getCoeffType(r1) == n_long_R );
assume( getCoeffType(r2) == n_long_R );
gmp_float* b= NULL;
if ( a != NULL )
{
b= new gmp_float( *(gmp_float*)a );
}
return (number)b;
}
#endif
/*2
* za:= - za
*/
static number ngfNeg (number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
*(gmp_float*)a= -(*(gmp_float*)a);
return (number)a;
}
/*
* 1/a
*/
static number ngfInvers(number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
gmp_float* f= NULL;
if (((gmp_float*)a)->isZero() )
{
WerrorS(nDivBy0);
f= new gmp_float( 0 );
}
else
{
f= new gmp_float( gmp_float(1) / (*(gmp_float*)a) );
}
return (number)f;
}
/*2
* u:= a + b
*/
static number ngfAdd (number a, number b, const coeffs R)
{
assume( getCoeffType(R) == n_long_R );
gmp_float* r= new gmp_float( (*(gmp_float*)a) + (*(gmp_float*)b) );
return (number)r;
}
static void ngfInpAdd (number &a, number b, const coeffs R)
{
assume( getCoeffType(R) == n_long_R );
(*(gmp_float*)a) += (*(gmp_float*)b);
}
/*2
* u:= a - b
*/
static number ngfSub (number a, number b, const coeffs R)
{
assume( getCoeffType(R) == n_long_R );
gmp_float* r= new gmp_float( (*(gmp_float*)a) - (*(gmp_float*)b) );
return (number)r;
}
/*2
* u := a * b
*/
static number ngfMult (number a, number b, const coeffs R)
{
assume( getCoeffType(R) == n_long_R );
gmp_float* r= new gmp_float( (*(gmp_float*)a) * (*(gmp_float*)b) );
return (number)r;
}
static void ngfInpMult (number &a, number b, const coeffs R)
{
assume( getCoeffType(R) == n_long_R );
(*(gmp_float*)a) *= (*(gmp_float*)b);
}
/*2
* u := a / b
*/
static number ngfDiv (number a, number b, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
gmp_float* f;
if ( ((gmp_float*)b)->isZero() )
{
// a/0 = error
WerrorS(nDivBy0);
f= new gmp_float( 0 );
}
else
{
f= new gmp_float( (*(gmp_float*)a) / (*(gmp_float*)b) );
}
return (number)f;
}
/*2
* u:= x ^ exp
*/
static number ngfPower (number x, int exp, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
if ( exp == 0 )
{
gmp_float* n = new gmp_float(1);
return (number)n;
}
else if ( ngfIsZero(x, r) ) // 0^e, e>0
{
return ngfInit(0, r);
}
else if ( exp == 1 )
{
return ngfCopy(x,r);
}
return (number) ( new gmp_float( (*(gmp_float*)x)^exp ) );
}
/* kept for compatibility reasons, to be deleted */
static void ngfPower ( number x, int exp, number * u, const coeffs r )
{
*u = ngfPower(x, exp, r);
}
/*2
* za > 0 ?
*/
static BOOLEAN ngfGreaterZero (number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
return (((gmp_float*)a)->sign() > 0);
}
/*2
* a > b ?
*/
static BOOLEAN ngfGreater (number a, number b, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
return ( (*(gmp_float*)a) > (*(gmp_float*)b) );
}
/*2
* a = b ?
*/
static BOOLEAN ngfEqual (number a, number b, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
return ( (*(gmp_float*)a) == (*(gmp_float*)b) );
}
/*2
* a == 1 ?
*/
static BOOLEAN ngfIsOne (number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
return ((gmp_float*)a)->isOne();
}
/*2
* a == -1 ?
*/
static BOOLEAN ngfIsMOne (number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
return ((gmp_float*)a)->isMOne();
}
static char * ngfEatFloatNExp(char * s )
{
char *start= s;
// eat floats (mantissa) like:
// 0.394394993, 102.203003008, .300303032, possibly starting with -
if (*s == '-') s++;
while ((*s >= '0' && *s <= '9')||(*s == '.')) s++;
// eat the exponent, starts with 'e' followed by '+', '-'
// and digits, like:
// e-202, e+393, accept also E7
if ( (s != start) && ((*s == 'e')||(*s=='E')))
{
if (*s=='E') *s='e';
s++; // skip 'e'/'E'
if ((*s == '+') || (*s == '-')) s++;
while ((*s >= '0' && *s <= '9')) s++;
}
return s;
}
/*2
* extracts the number a from s, returns the rest
*
* This is also called to print components of complex coefficients.
* Handle with care!
*/
const char * ngfRead (const char * start, number * a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R or getCoeffType(r) == n_long_C);
char *s= (char *)start;
//Print("%s\n",s);
s= ngfEatFloatNExp( s );
if (*s=='\0') // 0
{
if ( *(gmp_float**)a == NULL ) (*(gmp_float**)a)= new gmp_float();
(*(gmp_float**)a)->setFromStr(start);
}
else if (s==start) // 1
{
if ( *(gmp_float**)a != NULL ) delete (*(gmp_float**)a);
(*(gmp_float**)a)= new gmp_float(1);
}
else
{
gmp_float divisor(1.0);
char *start2=s;
if ( *s == '/' )
{
s++;
s= ngfEatFloatNExp( (char *)s );
if (s!= start2+1)
{
char tmp_c=*s;
*s='\0';
divisor.setFromStr(start2+1);
*s=tmp_c;
}
else
{
Werror("wrong long real format: %s",start2);
}
}
char c=*start2;
*start2='\0';
if ( *(gmp_float**)a == NULL ) (*(gmp_float**)a)= new gmp_float();
(*(gmp_float**)a)->setFromStr(start);
*start2=c;
if (divisor.isZero())
{
WerrorS(nDivBy0);
}
else
(**(gmp_float**)a) /= divisor;
}
return s;
}
/*2
* write a floating point number
*/
static void ngfWrite (number a, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
char *out;
if ( a != NULL )
{
out= floatToStr(*(gmp_float*)a, r->float_len);
StringAppendS(out);
//omFreeSize((void *)out, (strlen(out)+1)* sizeof(char) );
omFree( (void *)out );
}
else
{
StringAppendS("0");
}
}
static BOOLEAN ngfCoeffIsEqual (const coeffs r, n_coeffType n, void * parameter)
{
if (n==n_long_R)
{
LongComplexInfo* p = (LongComplexInfo *)(parameter);
if ((p!=NULL)
&& (p->float_len == r->float_len)
&& (p->float_len2 == r->float_len2))
return TRUE;
}
return FALSE;
}
static void ngfSetChar(const coeffs r)
{
setGMPFloatDigits(r->float_len, r->float_len2);
}
static char* ngfCoeffName(const coeffs r)
{
STATIC_VAR char ngfCoeffName_buf[30];
snprintf(ngfCoeffName_buf,30,"Float(%d,%d)",r->float_len,r->float_len2);
return ngfCoeffName_buf;
}
static number ngfMapQ(number from, const coeffs src, const coeffs dst)
{
assume( getCoeffType(dst) == n_long_R );
assume( src->rep == n_rep_gap_rat );
gmp_float *res=new gmp_float(numberFieldToFloat(from,QTOF));
return (number)res;
}
static number ngfMapZ(number from, const coeffs aRing, const coeffs r)
{
assume( getCoeffType(r) == n_long_R );
assume( aRing->rep == n_rep_gmp);
gmp_float *res=new gmp_float((mpz_ptr)from);
return (number)res;
}
static number ngfMapR(number from, const coeffs src, const coeffs dst)
{
assume( getCoeffType(dst) == n_long_R );
assume( getCoeffType(src) == n_R );
gmp_float *res=new gmp_float((double)nf(from).F());
return (number)res;
}
static number ngfMapP(number from, const coeffs src, const coeffs dst)
{
assume( getCoeffType(dst) == n_long_R );
assume( getCoeffType(src) == n_Zp );
return ngfInit(npInt(from,src), dst); // FIXME? TODO? // extern int npInt (number &n, const coeffs r);
}
static number ngfMapC(number from, const coeffs src, const coeffs dst)
{
assume( getCoeffType(dst) == n_long_R );
assume( getCoeffType(src) == n_long_C );
gmp_float *res=new gmp_float(((gmp_complex*)from)->real());
return (number)res;
}
static number ngfInitMPZ(mpz_t m, const coeffs)
{
gmp_float *res=new gmp_float(m);
return (number)res;
}
static nMapFunc ngfSetMap(const coeffs src, const coeffs dst)
{
assume( getCoeffType(dst) == n_long_R );
if (src->rep==n_rep_gap_rat) /*Q, Z*/
{
return ngfMapQ;
}
if (src->rep==n_rep_gap_gmp) /*Q, bigint*/
{
return ngfMapQ;
}
if (src->rep==n_rep_gmp) /* Z*/
{
return ngfMapZ;
}
if ((src->rep==n_rep_gmp_float) && nCoeff_is_long_R(src))
{
return ndCopyMap; //ngfCopyMap;
}
if ((src->rep==n_rep_float) && nCoeff_is_R(src))
{
return ngfMapR;
}
if ((src->rep==n_rep_gmp_complex) && nCoeff_is_long_C(src))
{
return ngfMapC;
}
if ((src->rep==n_rep_int) && nCoeff_is_Zp(src))
{
return ngfMapP;
}
return NULL;
}
BOOLEAN ngfInitChar(coeffs n, void *parameter)
{
assume( getCoeffType(n) == n_long_R );
n->is_field=TRUE;
n->is_domain=TRUE;
n->rep=n_rep_gmp_float;
//n->cfKillChar = ndKillChar; /* dummy */
n->cfSetChar = ngfSetChar;
n->ch = 0;
n->cfCoeffName=ngfCoeffName;
n->cfDelete = ngfDelete;
//n->cfNormalize=ndNormalize;
n->cfInit = ngfInit;
n->cfInitMPZ = ngfInitMPZ;
n->cfInt = ngfInt;
n->cfAdd = ngfAdd;
n->cfInpAdd = ngfInpAdd;
n->cfSub = ngfSub;
n->cfMult = ngfMult;
n->cfInpMult = ngfInpMult;
n->cfDiv = ngfDiv;
n->cfExactDiv= ngfDiv;
n->cfInpNeg = ngfNeg;
n->cfInvers = ngfInvers;
n->cfCopy = ngfCopy;
n->cfGreater = ngfGreater;
n->cfEqual = ngfEqual;
n->cfIsZero = ngfIsZero;
n->cfIsOne = ngfIsOne;
n->cfIsMOne = ngfIsMOne;
n->cfGreaterZero = ngfGreaterZero;
n->cfWriteLong = ngfWrite;
n->cfRead = ngfRead;
n->cfPower = ngfPower;
n->cfSetMap = ngfSetMap;
n->cfSize = ngfSize;
#ifdef LDEBUG
//n->cfDBTest = ndDBTest; // not yet implemented: ngfDBTest
#endif
n->nCoeffIsEqual = ngfCoeffIsEqual;
if( parameter != NULL)
{
LongComplexInfo* p = (LongComplexInfo*)parameter;
n->float_len = p->float_len;
n->float_len2 = p->float_len2;
} else // default values, just for testing!
{
n->float_len = SHORT_REAL_LENGTH;
n->float_len2 = SHORT_REAL_LENGTH;
}
assume( n->float_len2 >= SHORT_REAL_LENGTH );
assume( n_NumberOfParameters(n) == 0 );
assume( n_ParameterNames(n) == NULL );
return FALSE;
}
auxiliary.h
All the auxiliary stuff.
ABS
static int ABS(int v)
Definition auxiliary.h:113
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
m
int m
Definition cfEzgcd.cc:128
i
int i
Definition cfEzgcd.cc:132
x
Variable x
Definition cfModGcd.cc:4090
p
int p
Definition cfModGcd.cc:4086
b
CanonicalForm b
Definition cfModGcd.cc:4111
f
FILE * f
Definition checklibs.c:9
gmp_complex
gmp_complex numbers based on
Definition mpr_complex.h:181
gmp_float
Definition mpr_complex.h:32
gmp_float::setFromStr
void setFromStr(const char *in)
Definition mpr_complex.cc:78
gmp_float::isZero
bool isZero() const
Definition mpr_complex.cc:252
coeffs.h
Coefficient rings, fields and other domains suitable for Singular polynomials.
ndCopyMap
number ndCopyMap(number a, const coeffs src, const coeffs dst)
Definition numbers.cc:293
nCoeff_is_long_R
static FORCE_INLINE BOOLEAN nCoeff_is_long_R(const coeffs r)
Definition coeffs.h:886
n_coeffType
n_coeffType
Definition coeffs.h:27
n_R
@ n_R
single prescision (6,6) real numbers
Definition coeffs.h:31
n_long_R
@ n_long_R
real floating point (GMP) numbers
Definition coeffs.h:33
n_Zp
@ n_Zp
\F{p < 2^31}
Definition coeffs.h:29
n_long_C
@ n_long_C
complex floating point (GMP) numbers
Definition coeffs.h:41
n_ParameterNames
static FORCE_INLINE char const ** n_ParameterNames(const coeffs r)
Returns a (const!) pointer to (const char*) names of parameters.
Definition coeffs.h:773
getCoeffType
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition coeffs.h:431
n_NumberOfParameters
static FORCE_INLINE int n_NumberOfParameters(const coeffs r)
Returns the number of parameters.
Definition coeffs.h:769
nCoeff_is_Zp
static FORCE_INLINE BOOLEAN nCoeff_is_Zp(const coeffs r)
Definition coeffs.h:795
n_rep_gap_rat
@ n_rep_gap_rat
(number), see longrat.h
Definition coeffs.h:118
n_rep_gap_gmp
@ n_rep_gap_gmp
(), see rinteger.h, new impl.
Definition coeffs.h:119
n_rep_float
@ n_rep_float
(float), see shortfl.h
Definition coeffs.h:123
n_rep_int
@ n_rep_int
(int), see modulop.h
Definition coeffs.h:117
n_rep_gmp_float
@ n_rep_gmp_float
(gmp_float), see
Definition coeffs.h:124
n_rep_gmp
@ n_rep_gmp
(mpz_ptr), see rmodulon,h
Definition coeffs.h:122
n_rep_gmp_complex
@ n_rep_gmp_complex
(gmp_complex), see gnumpc.h
Definition coeffs.h:125
nMapFunc
number(* nMapFunc)(number a, const coeffs src, const coeffs dst)
maps "a", which lives in src, into dst
Definition coeffs.h:80
nCoeff_is_R
static FORCE_INLINE BOOLEAN nCoeff_is_R(const coeffs r)
Definition coeffs.h:831
nCoeff_is_long_C
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition coeffs.h:889
LongComplexInfo
Definition coeffs.h:107
s
const CanonicalForm int s
Definition facAbsFact.cc:51
res
CanonicalForm res
Definition facAbsFact.cc:60
isZero
bool isZero(const CFArray &A)
checks if entries of A are zero
Definition facSparseHensel.h:468
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
STATIC_VAR
#define STATIC_VAR
Definition globaldefs.h:7
gnumpc.h
ngfInit
static number ngfInit(long i, const coeffs r)
Definition gnumpfl.cc:37
ngfMapC
static number ngfMapC(number from, const coeffs src, const coeffs dst)
Definition gnumpfl.cc:461
ngfCopy
static number ngfCopy(number a, const coeffs r)
Definition gnumpfl.cc:94
ngfGreater
static BOOLEAN ngfGreater(number a, number b, const coeffs r)
Definition gnumpfl.cc:258
ngfSetChar
static void ngfSetChar(const coeffs r)
Definition gnumpfl.cc:414
ngfInpAdd
static void ngfInpAdd(number &a, number b, const coeffs R)
Definition gnumpfl.cc:159
ngfMapZ
static number ngfMapZ(number from, const coeffs aRing, const coeffs r)
Definition gnumpfl.cc:435
ngfInvers
static number ngfInvers(number a, const coeffs r)
Definition gnumpfl.cc:131
ngfInt
static long ngfInt(number &i, const coeffs r)
Definition gnumpfl.cc:48
ngfInitMPZ
static number ngfInitMPZ(mpz_t m, const coeffs)
Definition gnumpfl.cc:470
ngfDiv
static number ngfDiv(number a, number b, const coeffs r)
Definition gnumpfl.cc:198
ngfAdd
static number ngfAdd(number a, number b, const coeffs R)
Definition gnumpfl.cc:151
ngfMapP
static number ngfMapP(number from, const coeffs src, const coeffs dst)
Definition gnumpfl.cc:453
ngfRead
const char * ngfRead(const char *start, number *a, const coeffs r)
Definition gnumpfl.cc:324
ngfGreaterZero
static BOOLEAN ngfGreaterZero(number a, const coeffs r)
Definition gnumpfl.cc:248
ngfIsMOne
static BOOLEAN ngfIsMOne(number a, const coeffs r)
Definition gnumpfl.cc:288
ngfIsZero
static BOOLEAN ngfIsZero(number a, const coeffs r)
Definition gnumpfl.cc:59
ngfWrite
static void ngfWrite(number a, const coeffs r)
Definition gnumpfl.cc:383
ngfPower
static number ngfPower(number x, int exp, const coeffs r)
Definition gnumpfl.cc:219
ngfEqual
static BOOLEAN ngfEqual(number a, number b, const coeffs r)
Definition gnumpfl.cc:268
ngfSize
static int ngfSize(number n, const coeffs r)
Definition gnumpfl.cc:66
ngfEatFloatNExp
static char * ngfEatFloatNExp(char *s)
Definition gnumpfl.cc:295
ngfDelete
static void ngfDelete(number *a, const coeffs r)
Definition gnumpfl.cc:80
ngfMapQ
static number ngfMapQ(number from, const coeffs src, const coeffs dst)
Definition gnumpfl.cc:426
ngfCoeffIsEqual
static BOOLEAN ngfCoeffIsEqual(const coeffs r, n_coeffType n, void *parameter)
Definition gnumpfl.cc:401
ngfNeg
static number ngfNeg(number a, const coeffs r)
Definition gnumpfl.cc:120
ngfInpMult
static void ngfInpMult(number &a, number b, const coeffs R)
Definition gnumpfl.cc:188
ngfIsOne
static BOOLEAN ngfIsOne(number a, const coeffs r)
Definition gnumpfl.cc:278
ngfMult
static number ngfMult(number a, number b, const coeffs R)
Definition gnumpfl.cc:180
ngfSetMap
static nMapFunc ngfSetMap(const coeffs src, const coeffs dst)
Definition gnumpfl.cc:476
ngfCoeffName
static char * ngfCoeffName(const coeffs r)
Definition gnumpfl.cc:419
ngfInitChar
BOOLEAN ngfInitChar(coeffs n, void *parameter)
Initialize r.
Definition gnumpfl.cc:511
ngfSub
static number ngfSub(number a, number b, const coeffs R)
Definition gnumpfl.cc:169
ngfMapR
static number ngfMapR(number from, const coeffs src, const coeffs dst)
Definition gnumpfl.cc:444
gnumpfl.h
longrat.h
assume
#define assume(x)
Definition mod2.h:389
npInt
long npInt(number &n, const coeffs r)
Definition modulop.cc:83
modulop.h
floatToStr
char * floatToStr(const gmp_float &r, const unsigned int oprec)
Definition mpr_complex.cc:567
exp
gmp_float exp(const gmp_float &a)
Definition mpr_complex.cc:349
numberFieldToFloat
gmp_float numberFieldToFloat(number num, int cf)
Definition mpr_complex.cc:430
setGMPFloatDigits
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
mpr_complex.h
QTOF
#define QTOF
Definition mpr_complex.h:19
coeffs
The main handler for Singular numbers which are suitable for Singular polynomials.
numbers.h
nDivBy0
const char *const nDivBy0
Definition numbers.h:90
SHORT_REAL_LENGTH
#define SHORT_REAL_LENGTH
Definition numbers.h:57
omFree
#define omFree(addr)
Definition omAllocDecl.h:261
NULL
#define NULL
Definition omList.c:12
StringAppendS
void StringAppendS(const char *st)
Definition reporter.cc:107
Werror
void Werror(const char *fmt,...)
Definition reporter.cc:189
reporter.h
sign
static int sign(int x)
Definition ring.cc:3504
shortfl.h
SI_FLOAT
#define SI_FLOAT
Definition shortfl.h:15
R
#define R
Definition sirandom.c:27
nf
Definition gnumpfl.cc:25
nf::nf
nf(number n)
Definition gnumpfl.cc:29
nf::nf
nf(SI_FLOAT f)
Definition gnumpfl.cc:28
nf::_f
SI_FLOAT _f
Definition gnumpfl.cc:26
nf::_n
number _n
Definition gnumpfl.cc:27
nf::F
SI_FLOAT F() const
Definition gnumpfl.cc:30
nf::N
number N() const
Definition gnumpfl.cc:31