My Project  UNKNOWN_GIT_VERSION
Data Structures | Macros | Functions
mpr_complex.h File Reference
#include "coeffs/si_gmp.h"
#include "coeffs/mpr_global.h"

Go to the source code of this file.

Data Structures

class  gmp_float
 
class  gmp_complex
 gmp_complex numbers based on More...
 

Macros

#define ZTOF   1
 
#define QTOF   2
 
#define RTOF   3
 
#define CTOF   4
 

Functions

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 two parts: the "output" part a and the "rest" part b. More...
 
char * floatToStr (const gmp_float &r, const unsigned int oprec)
 
gmp_float abs (const gmp_float &)
 
gmp_float sqrt (const gmp_float &)
 
gmp_float hypot (const gmp_float &, const gmp_float &)
 
gmp_float sin (const gmp_float &)
 
gmp_float cos (const gmp_float &)
 
gmp_float log (const gmp_float &)
 
gmp_float exp (const gmp_float &)
 
gmp_float max (const gmp_float &, const gmp_float &)
 
gmp_float numberToFloat (number num, const coeffs src)
 
gmp_float numberFieldToFloat (number num, int src)
 
gmp_complex operator+ (const gmp_complex &a, const gmp_float b_d)
 
gmp_complex operator- (const gmp_complex &a, const gmp_float b_d)
 
gmp_complex operator* (const gmp_complex &a, const gmp_float b_d)
 
gmp_complex operator/ (const gmp_complex &a, const gmp_float b_d)
 
bool operator== (const gmp_complex &a, const gmp_complex &b)
 
bool operator> (const gmp_complex &a, const gmp_complex &b)
 
bool operator< (const gmp_complex &a, const gmp_complex &b)
 
bool operator>= (const gmp_complex &a, const gmp_complex &b)
 
bool operator<= (const gmp_complex &a, const gmp_complex &b)
 
gmp_float abs (const gmp_complex &c)
 
gmp_complex sqrt (const gmp_complex &x)
 
gmp_complex numberToComplex (number num, const coeffs r)
 
char * complexToStr (gmp_complex &c, const unsigned int oprec, const coeffs src)
 
bool complexNearZero (gmp_complex *c, int digits)
 

Macro Definition Documentation

◆ CTOF

#define CTOF   4

Definition at line 20 of file mpr_complex.h.

◆ QTOF

#define QTOF   2

Definition at line 18 of file mpr_complex.h.

◆ RTOF

#define RTOF   3

Definition at line 19 of file mpr_complex.h.

◆ ZTOF

#define ZTOF   1

Definition at line 17 of file mpr_complex.h.

Function Documentation

◆ abs() [1/2]

gmp_float abs ( const gmp_complex c)
inline

Definition at line 304 of file mpr_complex.h.

306 {
307  return hypot(c.real(),c.imag());

◆ abs() [2/2]

gmp_float abs ( const gmp_float )

Definition at line 321 of file mpr_complex.cc.

323 {
324  gmp_float tmp;
325  mpf_abs( *(tmp._mpfp()), *a.mpfp() );
326  return tmp;

◆ complexNearZero()

bool complexNearZero ( gmp_complex c,
int  digits 
)

Definition at line 765 of file mpr_complex.cc.

767 {
768  gmp_float eps,epsm;
769 
770  if ( digits < 1 ) return true;
771 
772  eps=pow(10.0,(int)digits);
773  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
774  eps=(gmp_float)1.0/eps;
775  epsm=-eps;
776 
777  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
778 
779  if ( c->real().sign() > 0 ) // +
780  return (c->real() < eps && (c->imag() < eps && c->imag() > epsm));
781  else // -
782  return (c->real() > epsm && (c->imag() < eps && c->imag() > epsm));

◆ complexToStr()

char* complexToStr ( gmp_complex c,
const unsigned int  oprec,
const coeffs  src 
)

Definition at line 704 of file mpr_complex.cc.

706 {
707  const char * complex_parameter = "I";
708  int N = 1; // strlen(complex_parameter);
709 
710  if (nCoeff_is_long_C(src))
711  {
712  complex_parameter = n_ParameterNames(src)[0];
713  N = strlen(complex_parameter);
714  }
715 
716  assume( complex_parameter != NULL && N > 0);
717 
718  char *out,*in_imag,*in_real;
719 
720  c.SmallToZero();
721  if ( !c.imag().isZero() )
722  {
723 
724  in_real=floatToStr( c.real(), oprec ); // get real part
725  in_imag=floatToStr( abs(c.imag()), oprec ); // get imaginary part
726 
727  if (nCoeff_is_long_C(src))
728  {
729  int len=(strlen(in_real)+strlen(in_imag)+7+N)*sizeof(char);
730  out=(char*)omAlloc(len);
731  memset(out,0,len);
732  if ( !c.real().isZero() ) // (-23-i*5.43) or (15.1+i*5.3)
733  sprintf(out,"(%s%s%s*%s)",in_real,c.imag().sign()>=0?"+":"-",complex_parameter,in_imag);
734  else // (-i*43) or (i*34)
735  {
736  if (c.imag().isOne())
737  sprintf(out,"%s", complex_parameter);
738  else if (c.imag().isMOne())
739  sprintf(out,"-%s", complex_parameter);
740  else
741  sprintf(out,"(%s%s*%s)",c.imag().sign()>=0?"":"-", complex_parameter,in_imag);
742  }
743  }
744  else
745  {
746  int len=(strlen(in_real)+strlen(in_imag)+9) * sizeof(char);
747  out=(char*)omAlloc( len );
748  memset(out,0,len);
749  if ( !c.real().isZero() )
750  sprintf(out,"(%s%s%s)",in_real,c.imag().sign()>=0?"+I*":"-I*",in_imag);
751  else
752  sprintf(out,"(%s%s)",c.imag().sign()>=0?"I*":"-I*",in_imag);
753  }
754  omFree( (void *) in_real );
755  omFree( (void *) in_imag );
756  }
757  else
758  {
759  out= floatToStr( c.real(), oprec );
760  }
761 
762  return out;

◆ cos()

gmp_float cos ( const gmp_float )

Definition at line 338 of file mpr_complex.cc.

340 {
341  gmp_float tmp( cos((double)a) );
342  return tmp;

◆ exp()

gmp_float exp ( const gmp_float )

Definition at line 357 of file mpr_complex.cc.

359 {
360  gmp_float tmp( exp((double)a) );
361  return tmp;

◆ floatToStr()

char* floatToStr ( const gmp_float r,
const unsigned int  oprec 
)

Definition at line 578 of file mpr_complex.cc.

580 {
581 #if 1
582  mp_exp_t exponent;
583  int size,insize;
584  char *nout,*out,*in;
585 
586  insize= (oprec+2) * sizeof(char) + 10;
587  in= (char*)omAlloc( insize );
588 
589  mpf_get_str(in,&exponent,10,oprec,*(r.mpfp()));
590 
591  //if ( (exponent > 0)
592  //&& (exponent < (int)oprec)
593  //&& (strlen(in)-(in[0]=='-'?1:0) == oprec) )
594  //{
595  // omFree( (void *) in );
596  // insize= (exponent+oprec+2) * sizeof(char) + 10;
597  // in= (char*)omAlloc( insize );
598  // int newprec= exponent+oprec;
599  // mpf_get_str(in,&exponent,10,newprec,*(r.mpfp()));
600  //}
601  nout= nicifyFloatStr( in, exponent, oprec, &size, SIGN_EMPTY );
602  omFree( (void *) in );
603  out= (char*)omAlloc( (strlen(nout)+1) * sizeof(char) );
604  strcpy( out, nout );
605  omFree( (void *) nout );
606 
607  return out;
608 #else
609  // for testing purpose...
610  char *out= (char*)omAlloc( (1024) * sizeof(char) );
611  sprintf(out,"% .10f",(double)r);
612  return out;
613 #endif

◆ hypot()

gmp_float hypot ( const gmp_float ,
const gmp_float  
)

Definition at line 348 of file mpr_complex.cc.

350 {
351 #if 1
352  return ( sqrt( (a*a) + (b*b) ) );
353 #else
354  gmp_float tmp( hypot( (double)a, (double)b ) );
355  return tmp;
356 #endif

◆ log()

gmp_float log ( const gmp_float )

Definition at line 343 of file mpr_complex.cc.

345 {
346  gmp_float tmp( log((double)a) );
347  return tmp;

◆ max()

gmp_float max ( const gmp_float ,
const gmp_float  
)

Definition at line 362 of file mpr_complex.cc.

364 {
365  gmp_float tmp;
366  a > b ? tmp= a : tmp= b;
367  return tmp;

◆ numberFieldToFloat()

gmp_float numberFieldToFloat ( number  num,
int  src 
)

Definition at line 438 of file mpr_complex.cc.

440 {
441  gmp_float r;
442 
443  switch (cf)
444  {
445  case QTOF:
446  if ( num != NULL )
447  {
448  if (SR_HDL(num) & SR_INT)
449  {
450  r = gmp_float(SR_TO_INT(num));
451  }
452  else
453  {
454  if ( num->s != 3 )
455  {
456  r= gmp_float(num->z);
457  r/= gmp_float(num->n);
458  }
459  else
460  {
461  r= num->z;
462  }
463  }
464  }
465  else
466  {
467  r= 0.0;
468  }
469  break;
470  case RTOF:
471  r= *(gmp_float*)num;
472  break;
473  case CTOF:
474  WerrorS("Can not map from field C to field R!");
475  break;
476  case ZTOF:
477  default:
478  WerrorS("Ground field not implemented!");
479  } // switch
480 
481  return r;

◆ numberToComplex()

gmp_complex numberToComplex ( number  num,
const coeffs  r 
)
inline

Definition at line 311 of file mpr_complex.h.

313 {
314  if (nCoeff_is_long_C(r))
315  {
316  return *(gmp_complex*)num;
317  }
318  else
319  {
320  return gmp_complex( numberToFloat(num, r) );
321  }

◆ numberToFloat()

gmp_float numberToFloat ( number  num,
const coeffs  src 
)

Definition at line 372 of file mpr_complex.cc.

374 {
375  gmp_float r;
376 
377  if ( nCoeff_is_Q(src) )
378  {
379  if ( num != NULL )
380  {
381  if (SR_HDL(num) & SR_INT)
382  {
383  //n_Print(num, src);printf("\n");
384  int nn = SR_TO_INT(num);
385  if((long)nn == SR_TO_INT(num))
386  r = SR_TO_INT(num);
387  else
388  r = gmp_float(SR_TO_INT(num));
389  //int dd = 20;
390  //gmp_printf("\nr = %.*Ff\n",dd,*r.mpfp());
391  //getchar();
392  }
393  else
394  {
395  if ( num->s == 0 )
396  {
397  nlNormalize( num, src ); // FIXME? TODO? // extern void nlNormalize(number &x, const coeffs r); // FIXME
398  }
399  if (SR_HDL(num) & SR_INT)
400  {
401  r= SR_TO_INT(num);
402  }
403  else
404  {
405  if ( num->s != 3 )
406  {
407  r= num->z;
408  r/= (gmp_float)num->n;
409  }
410  else
411  {
412  r= num->z;
413  }
414  }
415  }
416  }
417  else
418  {
419  r= 0.0;
420  }
421  }
422  else if (nCoeff_is_long_R(src) || nCoeff_is_long_C(src))
423  {
424  r= *(gmp_float*)num;
425  }
426  else if ( nCoeff_is_R(src) )
427  {
428  // Add some code here :-)
429  WerrorS("Ground field not implemented!");
430  }
431  else
432  {
433  WerrorS("Ground field not implemented!");
434  }
435 
436  return r;

◆ operator*()

gmp_complex operator* ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 254 of file mpr_complex.h.

256 {
257  return gmp_complex( a.r * b_d, a.i * b_d );

◆ operator+()

gmp_complex operator+ ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 246 of file mpr_complex.h.

248 {
249  return gmp_complex( a.r + b_d, a.i );

◆ operator-()

gmp_complex operator- ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 250 of file mpr_complex.h.

252 {
253  return gmp_complex( a.r - b_d, a.i );

◆ operator/()

gmp_complex operator/ ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 258 of file mpr_complex.h.

260 {
261  return gmp_complex( a.r / b_d, a.i / b_d );

◆ operator<()

bool operator< ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 272 of file mpr_complex.h.

274 {
275  return ( a.real() < b.real() );

◆ operator<=()

bool operator<= ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 280 of file mpr_complex.h.

282 {
283  return ( a.real() <= b.real() );

◆ operator==()

bool operator== ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 264 of file mpr_complex.h.

266 {
267  return ( b.real() == a.real() ) && ( b.imag() == a.imag() );

◆ operator>()

bool operator> ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 268 of file mpr_complex.h.

270 {
271  return ( a.real() > b.real() );

◆ operator>=()

bool operator>= ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 276 of file mpr_complex.h.

278 {
279  return ( a.real() >= b.real() );

◆ 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 two parts: the "output" part a and the "rest" part b.

According to the GMP-precision digits is recomputed to bits (basis 2). Two numbers a, b are equal if | a - b | < | a | * 0.1^digits . In this case we have a - b = 0 . The epsilon e is e=0.1^(digits+rest) with 1+e != 1, but 1+0.1*e = 1.

Definition at line 60 of file mpr_complex.cc.

62 {
63  size_t bits = 1 + (size_t) ((float)digits * 3.5);
64  size_t rb = 1 + (size_t) ((float)rest * 3.5);
65  size_t db = bits+rb;
66  gmp_output_digits= digits;
67  mpf_set_default_prec( db );
68  if (diff!=NULL) delete diff;
69  diff=new gmp_float(0.0);
70  mpf_set_prec(*diff->_mpfp(),32);
71  if (gmpRel!=NULL) delete gmpRel;
72  gmpRel=new gmp_float(0.0);
73  mpf_set_prec(*gmpRel->_mpfp(),32);
74  mpf_set_d(*gmpRel->_mpfp(),0.1);
75  mpf_pow_ui(*gmpRel->_mpfp(),*gmpRel->_mpfp(),digits);

◆ sin()

gmp_float sin ( const gmp_float )

Definition at line 333 of file mpr_complex.cc.

335 {
336  gmp_float tmp( sin((double)a) );
337  return tmp;

◆ sqrt() [1/2]

gmp_complex sqrt ( const gmp_complex x)

Definition at line 676 of file mpr_complex.cc.

678 {
679  gmp_float r = abs(x);
680  gmp_float nr, ni;
681  if (r == (gmp_float) 0.0)
682  {
683  nr = ni = r;
684  }
685  else if ( x.real() > (gmp_float)0)
686  {
687  nr = sqrt((gmp_float)0.5 * (r + x.real()));
688  ni = x.imag() / nr / (gmp_float)2;
689  }
690  else
691  {
692  ni = sqrt((gmp_float)0.5 * (r - x.real()));
693  if (x.imag() < (gmp_float)0)
694  {
695  ni = - ni;
696  }
697  nr = x.imag() / ni / (gmp_float)2;
698  }
699  gmp_complex tmp(nr, ni);
700  return tmp;

◆ sqrt() [2/2]

gmp_float sqrt ( const gmp_float )

Definition at line 327 of file mpr_complex.cc.

329 {
330  gmp_float tmp;
331  mpf_sqrt( *(tmp._mpfp()), *a.mpfp() );
332  return tmp;
gmp_float::sign
int sign()
Definition: mpr_complex.h:122
floatToStr
char * floatToStr(const gmp_float &r, const unsigned int oprec)
Definition: mpr_complex.cc:578
exponent
int exponent(const CanonicalForm &f, int q)
int exponent ( const CanonicalForm & f, int q )
Definition: gengftables-conway.cc:92
omFree
#define omFree(addr)
Definition: omAllocDecl.h:259
gmp_float::isZero
bool isZero() const
Definition: mpr_complex.cc:252
gmp_complex::r
gmp_float r
Definition: mpr_complex.h:180
nCoeff_is_R
static FORCE_INLINE BOOLEAN nCoeff_is_R(const coeffs r)
Definition: coeffs.h:857
x
Variable x
Definition: cfModGcd.cc:4023
diff
static gmp_float * diff
Definition: mpr_complex.cc:45
num
CanonicalForm num(const CanonicalForm &f)
Definition: canonicalform.h:330
cf
CanonicalForm cf
Definition: cfModGcd.cc:4024
RTOF
#define RTOF
Definition: mpr_complex.h:19
ZTOF
#define ZTOF
Definition: mpr_complex.h:17
sqrt
gmp_float sqrt(const gmp_float &a)
Definition: mpr_complex.cc:327
N
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:48
sin
gmp_float sin(const gmp_float &a)
Definition: mpr_complex.cc:333
gmp_complex::imag
gmp_float imag() const
Definition: mpr_complex.h:234
nCoeff_is_long_C
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:915
b
CanonicalForm b
Definition: cfModGcd.cc:4044
gmp_float::_mpfp
mpf_t * _mpfp()
Definition: mpr_complex.h:133
SR_HDL
#define SR_HDL(A)
Definition: mpr_complex.cc:32
CTOF
#define CTOF
Definition: mpr_complex.h:20
hypot
gmp_float hypot(const gmp_float &, const gmp_float &)
Definition: mpr_complex.cc:348
nCoeff_is_Q
static FORCE_INLINE BOOLEAN nCoeff_is_Q(const coeffs r)
Definition: coeffs.h:827
hypot
gmp_float hypot(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:348
gmp_float::isMOne
bool isMOne() const
Definition: mpr_complex.cc:273
SIGN_EMPTY
#define SIGN_EMPTY
Definition: mpr_complex.cc:37
size
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
nlNormalize
void nlNormalize(number &x, const coeffs r)
Definition: longrat.cc:1343
nCoeff_is_long_R
static FORCE_INLINE BOOLEAN nCoeff_is_long_R(const coeffs r)
Definition: coeffs.h:912
nicifyFloatStr
char * nicifyFloatStr(char *in, mp_exp_t exponent, size_t oprec, int *size, int thesign)
Definition: mpr_complex.cc:485
omAlloc
#define omAlloc(size)
Definition: omAllocDecl.h:208
abs
gmp_float abs(const gmp_float &a)
Definition: mpr_complex.cc:321
gmpRel
static gmp_float * gmpRel
Definition: mpr_complex.cc:44
SR_INT
#define SR_INT
Definition: longrat.h:65
n_ParameterNames
static FORCE_INLINE const char ** n_ParameterNames(const coeffs r)
Returns a (const!) pointer to (const char*) names of parameters.
Definition: coeffs.h:799
exp
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:357
gmp_complex::real
gmp_float real() const
Definition: mpr_complex.h:233
numberToFloat
gmp_float numberToFloat(number num, const coeffs src)
Definition: mpr_complex.cc:372
gmp_complex::SmallToZero
void SmallToZero()
Definition: mpr_complex.cc:784
log
gmp_float log(const gmp_float &a)
Definition: mpr_complex.cc:343
gmp_complex::i
gmp_float i
Definition: mpr_complex.h:180
pow
Rational pow(const Rational &a, int e)
Definition: GMPrat.cc:414
WerrorS
void WerrorS(const char *s)
Definition: feFopen.cc:24
assume
#define assume(x)
Definition: mod2.h:384
NULL
#define NULL
Definition: omList.c:9
SR_TO_INT
#define SR_TO_INT(SR)
Definition: mpr_complex.cc:33
gmp_float::mpfp
const mpf_t * mpfp() const
Definition: mpr_complex.h:132
QTOF
#define QTOF
Definition: mpr_complex.h:18
gmp_output_digits
size_t gmp_output_digits
Definition: mpr_complex.cc:42
gmp_float
Definition: mpr_complex.h:30
gmp_float::isOne
bool isOne() const
Definition: mpr_complex.cc:257
cos
gmp_float cos(const gmp_float &a)
Definition: mpr_complex.cc:338
gmp_complex
gmp_complex numbers based on
Definition: mpr_complex.h:177