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 k, const coeffs 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

#define CTOF   4

Definition at line 21 of file mpr_complex.h.

#define QTOF   2

Definition at line 19 of file mpr_complex.h.

#define RTOF   3

Definition at line 20 of file mpr_complex.h.

#define ZTOF   1

Definition at line 18 of file mpr_complex.h.

Function Documentation

gmp_float abs ( const gmp_float )

Definition at line 323 of file mpr_complex.cc.

324 {
325  gmp_float tmp;
326  mpf_abs( *(tmp._mpfp()), *a.mpfp() );
327  return tmp;
328 }
const poly a
Definition: syzextra.cc:212
mpf_t * _mpfp()
Definition: mpr_complex.h:134
gmp_float abs ( const gmp_complex c)
inline

Definition at line 305 of file mpr_complex.h.

306 {
307  return hypot(c.real(),c.imag());
308 }
gmp_float hypot(const gmp_float &, const gmp_float &)
Definition: mpr_complex.cc:350
gmp_float imag() const
Definition: mpr_complex.h:235
gmp_float real() const
Definition: mpr_complex.h:234
bool complexNearZero ( gmp_complex c,
int  digits 
)

Definition at line 778 of file mpr_complex.cc.

779 {
780  gmp_float eps,epsm;
781 
782  if ( digits < 1 ) return true;
783 
784  eps=pow(10.0,(int)digits);
785  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
786  eps=(gmp_float)1.0/eps;
787  epsm=-eps;
788 
789  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
790 
791  if ( c->real().sign() > 0 ) // +
792  return (c->real() < eps && (c->imag() < eps && c->imag() > epsm));
793  else // -
794  return (c->real() > epsm && (c->imag() < eps && c->imag() > epsm));
795 }
int sign()
Definition: mpr_complex.h:123
gmp_float imag() const
Definition: mpr_complex.h:235
Rational pow(const Rational &a, int e)
Definition: GMPrat.cc:418
gmp_float real() const
Definition: mpr_complex.h:234
char* complexToStr ( gmp_complex c,
const unsigned int  oprec,
const coeffs  src 
)

Definition at line 717 of file mpr_complex.cc.

718 {
719  const char * complex_parameter = "I";
720  int N = 1; // strlen(complex_parameter);
721 
722  if (nCoeff_is_long_C(src))
723  {
724  complex_parameter = n_ParameterNames(src)[0];
725  N = strlen(complex_parameter);
726  }
727 
728  assume( complex_parameter != NULL && N > 0);
729 
730  char *out,*in_imag,*in_real;
731 
732  c.SmallToZero();
733  if ( !c.imag().isZero() )
734  {
735 
736  in_real=floatToStr( c.real(), oprec ); // get real part
737  in_imag=floatToStr( abs(c.imag()), oprec ); // get imaginary part
738 
739  if (nCoeff_is_long_C(src))
740  {
741  int len=(strlen(in_real)+strlen(in_imag)+7+N)*sizeof(char);
742  out=(char*)omAlloc(len);
743  memset(out,0,len);
744  if ( !c.real().isZero() ) // (-23-i*5.43) or (15.1+i*5.3)
745  sprintf(out,"(%s%s%s*%s)",in_real,c.imag().sign()>=0?"+":"-",complex_parameter,in_imag);
746  else // (-i*43) or (i*34)
747  {
748  if (c.imag().isOne())
749  sprintf(out,"%s", complex_parameter);
750  else if (c.imag().isMOne())
751  sprintf(out,"-%s", complex_parameter);
752  else
753  sprintf(out,"(%s%s*%s)",c.imag().sign()>=0?"":"-", complex_parameter,in_imag);
754  }
755  }
756  else
757  {
758  int len=(strlen(in_real)+strlen(in_imag)+9) * sizeof(char);
759  out=(char*)omAlloc( len );
760  memset(out,0,len);
761  if ( !c.real().isZero() )
762  sprintf(out,"(%s%s%s)",in_real,c.imag().sign()>=0?"+I*":"-I*",in_imag);
763  else
764  sprintf(out,"(%s%s)",c.imag().sign()>=0?"I*":"-I*",in_imag);
765  }
766  omFree( (void *) in_real );
767  omFree( (void *) in_imag );
768  }
769  else
770  {
771  out= floatToStr( c.real(), oprec );
772  }
773 
774  return out;
775 }
void SmallToZero()
Definition: mpr_complex.cc:797
static FORCE_INLINE char const ** n_ParameterNames(const coeffs r)
Returns a (const!) pointer to (const char*) names of parameters.
Definition: coeffs.h:796
bool isZero() const
Definition: mpr_complex.cc:254
bool isMOne() const
Definition: mpr_complex.cc:275
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:895
#define omAlloc(size)
Definition: omAllocDecl.h:210
int sign()
Definition: mpr_complex.h:123
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
#define omFree(addr)
Definition: omAllocDecl.h:261
#define assume(x)
Definition: mod2.h:405
bool isOne() const
Definition: mpr_complex.cc:259
gmp_float abs(const gmp_float &a)
Definition: mpr_complex.cc:323
gmp_float imag() const
Definition: mpr_complex.h:235
#define NULL
Definition: omList.c:10
char * floatToStr(const gmp_float &r, const unsigned int oprec)
Definition: mpr_complex.cc:591
gmp_float real() const
Definition: mpr_complex.h:234
gmp_float cos ( const gmp_float )

Definition at line 340 of file mpr_complex.cc.

341 {
342  gmp_float tmp( cos((double)a) );
343  return tmp;
344 }
const poly a
Definition: syzextra.cc:212
gmp_float cos(const gmp_float &a)
Definition: mpr_complex.cc:340
gmp_float exp ( const gmp_float )

Definition at line 359 of file mpr_complex.cc.

360 {
361  gmp_float tmp( exp((double)a) );
362  return tmp;
363 }
const poly a
Definition: syzextra.cc:212
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:359
char* floatToStr ( const gmp_float r,
const unsigned int  oprec 
)

Definition at line 591 of file mpr_complex.cc.

592 {
593 #if 1
594  mp_exp_t exponent;
595  int size,insize;
596  char *nout,*out,*in;
597 
598  insize= (oprec+2) * sizeof(char) + 10;
599  in= (char*)omAlloc( insize );
600 
601  mpf_get_str(in,&exponent,10,oprec,*(r.mpfp()));
602 
603  if ( (exponent > 0)
604  && (exponent < (int)oprec)
605  && (strlen(in)-(in[0]=='-'?1:0) == oprec) )
606  {
607  omFree( (void *) in );
608  insize= (exponent+oprec+2) * sizeof(char) + 10;
609  in= (char*)omAlloc( insize );
610  int newprec= exponent+oprec;
611  mpf_get_str(in,&exponent,10,newprec,*(r.mpfp()));
612  }
613  nout= nicifyFloatStr( in, exponent, oprec, &size, SIGN_EMPTY );
614  omFree( (void *) in );
615  out= (char*)omAlloc( (strlen(nout)+1) * sizeof(char) );
616  strcpy( out, nout );
617  omFree( (void *) nout );
618 
619  return out;
620 #else
621  // for testing purpose...
622  char *out= (char*)omAlloc( (1024) * sizeof(char) );
623  sprintf(out,"% .10f",(double)r);
624  return out;
625 #endif
626 }
const mpf_t * mpfp() const
Definition: mpr_complex.h:133
char * nicifyFloatStr(char *in, mp_exp_t exponent, size_t oprec, int *size, int thesign)
Definition: mpr_complex.cc:498
#define SIGN_EMPTY
Definition: mpr_complex.cc:39
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omFree(addr)
Definition: omAllocDecl.h:261
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition: cf_ops.cc:600
int exponent(const CanonicalForm &f, int q)
int exponent ( const CanonicalForm & f, int q )
gmp_float hypot ( const gmp_float ,
const gmp_float  
)

Definition at line 350 of file mpr_complex.cc.

351 {
352 #if 1
353  return ( sqrt( (a*a) + (b*b) ) );
354 #else
355  gmp_float tmp( hypot( (double)a, (double)b ) );
356  return tmp;
357 #endif
358 }
const poly a
Definition: syzextra.cc:212
gmp_float hypot(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:350
gmp_float sqrt(const gmp_float &a)
Definition: mpr_complex.cc:329
const poly b
Definition: syzextra.cc:213
gmp_float log ( const gmp_float )

Definition at line 345 of file mpr_complex.cc.

346 {
347  gmp_float tmp( log((double)a) );
348  return tmp;
349 }
const poly a
Definition: syzextra.cc:212
gmp_float log(const gmp_float &a)
Definition: mpr_complex.cc:345
gmp_float max ( const gmp_float ,
const gmp_float  
)

Definition at line 364 of file mpr_complex.cc.

365 {
366  gmp_float tmp;
367  a > b ? tmp= a : tmp= b;
368  return tmp;
369 }
const poly a
Definition: syzextra.cc:212
const poly b
Definition: syzextra.cc:213
gmp_float numberFieldToFloat ( number  num,
int  k,
const coeffs  src 
)

Definition at line 440 of file mpr_complex.cc.

441 {
442  gmp_float r;
443 
444  switch (k)
445  {
446  case QTOF:
447  if ( num != NULL )
448  {
449  if (SR_HDL(num) & SR_INT)
450  {
451  r = gmp_float(SR_TO_INT(num));
452  }
453  else
454  {
455  if ( num->s == 0 )
456  {
457  nlNormalize( num, src ); // FIXME? TODO? // extern void nlNormalize(number &x, const coeffs r); // FIXME
458  }
459  if (SR_HDL(num) & SR_INT)
460  {
461  r = gmp_float(SR_TO_INT(num));
462  }
463  else
464  {
465  if ( num->s != 3 )
466  {
467  r= num->z;
468  r/= (gmp_float)num->n;
469  }
470  else
471  {
472  r= num->z;
473  }
474  }
475  }
476  }
477  else
478  {
479  r= 0.0;
480  }
481  break;
482  case RTOF:
483  r= *(gmp_float*)num;
484  break;
485  case CTOF:
486  WerrorS("Can not map from field C to field R!");
487  break;
488  case ZTOF:
489  default:
490  WerrorS("Ground field not implemented!");
491  } // switch
492 
493  return r;
494 }
#define ZTOF
Definition: mpr_complex.h:18
CanonicalForm num(const CanonicalForm &f)
void WerrorS(const char *s)
Definition: feFopen.cc:24
int k
Definition: cfEzgcd.cc:93
#define CTOF
Definition: mpr_complex.h:21
const ring r
Definition: syzextra.cc:208
#define QTOF
Definition: mpr_complex.h:19
#define SR_TO_INT(SR)
Definition: mpr_complex.cc:35
void nlNormalize(number &x, const coeffs r)
Definition: longrat.cc:1277
#define NULL
Definition: omList.c:10
#define SR_HDL(A)
Definition: mpr_complex.cc:34
#define SR_INT
Definition: longrat.h:65
#define RTOF
Definition: mpr_complex.h:20
gmp_complex numberToComplex ( number  num,
const coeffs  r 
)
inline

Definition at line 312 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  }
322 }
gmp_float numberToFloat(number num, const coeffs src)
Definition: mpr_complex.cc:374
CanonicalForm num(const CanonicalForm &f)
gmp_complex numbers based on
Definition: mpr_complex.h:178
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:895
gmp_float numberToFloat ( number  num,
const coeffs  src 
)

Definition at line 374 of file mpr_complex.cc.

375 {
376  gmp_float r;
377 
378  if ( nCoeff_is_Q(src) )
379  {
380  if ( num != NULL )
381  {
382  if (SR_HDL(num) & SR_INT)
383  {
384  //n_Print(num, src);printf("\n");
385  int nn = SR_TO_INT(num);
386  if((long)nn == SR_TO_INT(num))
387  r = SR_TO_INT(num);
388  else
389  r = gmp_float(SR_TO_INT(num));
390  //int dd = 20;
391  //gmp_printf("\nr = %.*Ff\n",dd,*r.mpfp());
392  //getchar();
393  }
394  else
395  {
396  if ( num->s == 0 )
397  {
398  nlNormalize( num, src ); // FIXME? TODO? // extern void nlNormalize(number &x, const coeffs r); // FIXME
399  }
400  if (SR_HDL(num) & SR_INT)
401  {
402  r= SR_TO_INT(num);
403  }
404  else
405  {
406  if ( num->s != 3 )
407  {
408  r= num->z;
409  r/= (gmp_float)num->n;
410  }
411  else
412  {
413  r= num->z;
414  }
415  }
416  }
417  }
418  else
419  {
420  r= 0.0;
421  }
422  }
423  else if (nCoeff_is_long_R(src) || nCoeff_is_long_C(src))
424  {
425  r= *(gmp_float*)num;
426  }
427  else if ( nCoeff_is_R(src) )
428  {
429  // Add some code here :-)
430  WerrorS("Ground field not implemented!");
431  }
432  else
433  {
434  WerrorS("Ground field not implemented!");
435  }
436 
437  return r;
438 }
CanonicalForm num(const CanonicalForm &f)
static FORCE_INLINE BOOLEAN nCoeff_is_long_R(const coeffs r)
Definition: coeffs.h:892
static FORCE_INLINE BOOLEAN nCoeff_is_R(const coeffs r)
Definition: coeffs.h:834
void WerrorS(const char *s)
Definition: feFopen.cc:24
static FORCE_INLINE BOOLEAN nCoeff_is_Q(const coeffs r)
Definition: coeffs.h:824
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:895
const ring r
Definition: syzextra.cc:208
#define SR_TO_INT(SR)
Definition: mpr_complex.cc:35
void nlNormalize(number &x, const coeffs r)
Definition: longrat.cc:1277
#define NULL
Definition: omList.c:10
#define SR_HDL(A)
Definition: mpr_complex.cc:34
#define SR_INT
Definition: longrat.h:65
gmp_complex operator* ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 255 of file mpr_complex.h.

256 {
257  return gmp_complex( a.r * b_d, a.i * b_d );
258 }
gmp_float i
Definition: mpr_complex.h:181
gmp_complex numbers based on
Definition: mpr_complex.h:178
gmp_float r
Definition: mpr_complex.h:181
gmp_complex operator+ ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 247 of file mpr_complex.h.

248 {
249  return gmp_complex( a.r + b_d, a.i );
250 }
gmp_float i
Definition: mpr_complex.h:181
gmp_complex numbers based on
Definition: mpr_complex.h:178
gmp_float r
Definition: mpr_complex.h:181
gmp_complex operator- ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 251 of file mpr_complex.h.

252 {
253  return gmp_complex( a.r - b_d, a.i );
254 }
gmp_float i
Definition: mpr_complex.h:181
gmp_complex numbers based on
Definition: mpr_complex.h:178
gmp_float r
Definition: mpr_complex.h:181
gmp_complex operator/ ( const gmp_complex a,
const gmp_float  b_d 
)
inline

Definition at line 259 of file mpr_complex.h.

260 {
261  return gmp_complex( a.r / b_d, a.i / b_d );
262 }
gmp_float i
Definition: mpr_complex.h:181
gmp_complex numbers based on
Definition: mpr_complex.h:178
gmp_float r
Definition: mpr_complex.h:181
bool operator< ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 273 of file mpr_complex.h.

274 {
275  return ( a.real() < b.real() );
276 }
gmp_float real() const
Definition: mpr_complex.h:234
bool operator<= ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 281 of file mpr_complex.h.

282 {
283  return ( a.real() <= b.real() );
284 }
gmp_float real() const
Definition: mpr_complex.h:234
bool operator== ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 265 of file mpr_complex.h.

266 {
267  return ( b.real() == a.real() ) && ( b.imag() == a.imag() );
268 }
gmp_float imag() const
Definition: mpr_complex.h:235
gmp_float real() const
Definition: mpr_complex.h:234
bool operator> ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 269 of file mpr_complex.h.

270 {
271  return ( a.real() > b.real() );
272 }
gmp_float real() const
Definition: mpr_complex.h:234
bool operator>= ( const gmp_complex a,
const gmp_complex b 
)
inline

Definition at line 277 of file mpr_complex.h.

278 {
279  return ( a.real() >= b.real() );
280 }
gmp_float real() const
Definition: mpr_complex.h:234
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 62 of file mpr_complex.cc.

63 {
64  size_t bits = 1 + (size_t) ((float)digits * 3.5);
65  size_t rb = 1 + (size_t) ((float)rest * 3.5);
66  size_t db = bits+rb;
67  gmp_output_digits= digits;
68  mpf_set_default_prec( db );
69  if (diff!=NULL) delete diff;
70  diff=new gmp_float(0.0);
71  mpf_set_prec(*diff->_mpfp(),32);
72  if (gmpRel!=NULL) delete gmpRel;
73  gmpRel=new gmp_float(0.0);
74  mpf_set_prec(*gmpRel->_mpfp(),32);
75  mpf_set_d(*gmpRel->_mpfp(),0.1);
76  mpf_pow_ui(*gmpRel->_mpfp(),*gmpRel->_mpfp(),digits);
77 }
static gmp_float * diff
Definition: mpr_complex.cc:47
mpf_t * _mpfp()
Definition: mpr_complex.h:134
#define NULL
Definition: omList.c:10
static gmp_float * gmpRel
Definition: mpr_complex.cc:46
size_t gmp_output_digits
Definition: mpr_complex.cc:44
gmp_float sin ( const gmp_float )

Definition at line 335 of file mpr_complex.cc.

336 {
337  gmp_float tmp( sin((double)a) );
338  return tmp;
339 }
const poly a
Definition: syzextra.cc:212
gmp_float sin(const gmp_float &a)
Definition: mpr_complex.cc:335
gmp_float sqrt ( const gmp_float )

Definition at line 329 of file mpr_complex.cc.

330 {
331  gmp_float tmp;
332  mpf_sqrt( *(tmp._mpfp()), *a.mpfp() );
333  return tmp;
334 }
const poly a
Definition: syzextra.cc:212
mpf_t * _mpfp()
Definition: mpr_complex.h:134
gmp_complex sqrt ( const gmp_complex x)

Definition at line 689 of file mpr_complex.cc.

690 {
691  gmp_float r = abs(x);
692  gmp_float nr, ni;
693  if (r == (gmp_float) 0.0)
694  {
695  nr = ni = r;
696  }
697  else if ( x.real() > (gmp_float)0)
698  {
699  nr = sqrt((gmp_float)0.5 * (r + x.real()));
700  ni = x.imag() / nr / (gmp_float)2;
701  }
702  else
703  {
704  ni = sqrt((gmp_float)0.5 * (r - x.real()));
705  if (x.imag() < (gmp_float)0)
706  {
707  ni = - ni;
708  }
709  nr = x.imag() / ni / (gmp_float)2;
710  }
711  gmp_complex tmp(nr, ni);
712  return tmp;
713 }
gmp_complex numbers based on
Definition: mpr_complex.h:178
const ring r
Definition: syzextra.cc:208
gmp_float sqrt(const gmp_float &a)
Definition: mpr_complex.cc:329
gmp_float abs(const gmp_float &a)
Definition: mpr_complex.cc:323
gmp_float imag() const
Definition: mpr_complex.h:235
gmp_float real() const
Definition: mpr_complex.h:234