My Project  debian-1:4.1.2-p1+ds-2
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 21 of file mpr_complex.h.

◆ QTOF

#define QTOF   2

Definition at line 19 of file mpr_complex.h.

◆ RTOF

#define RTOF   3

Definition at line 20 of file mpr_complex.h.

◆ ZTOF

#define ZTOF   1

Definition at line 18 of file mpr_complex.h.

Function Documentation

◆ abs() [1/2]

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 imag() const
Definition: mpr_complex.h:235
gmp_float real() const
Definition: mpr_complex.h:234
gmp_float hypot(const gmp_float &, const gmp_float &)
Definition: mpr_complex.cc:348

◆ abs() [2/2]

gmp_float abs ( const gmp_float a)

Definition at line 321 of file mpr_complex.cc.

322 {
323  gmp_float tmp;
324  mpf_abs( *(tmp._mpfp()), *a.mpfp() );
325  return tmp;
326 }
const mpf_t * mpfp() const
Definition: mpr_complex.h:133
mpf_t * _mpfp()
Definition: mpr_complex.h:134

◆ complexNearZero()

bool complexNearZero ( gmp_complex c,
int  digits 
)

Definition at line 765 of file mpr_complex.cc.

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

◆ complexToStr()

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

Definition at line 704 of file mpr_complex.cc.

705 {
706  const char * complex_parameter = "I";
707  int N = 1; // strlen(complex_parameter);
708 
709  if (nCoeff_is_long_C(src))
710  {
711  complex_parameter = n_ParameterNames(src)[0];
712  N = strlen(complex_parameter);
713  }
714 
715  assume( complex_parameter != NULL && N > 0);
716 
717  char *out,*in_imag,*in_real;
718 
719  c.SmallToZero();
720  if ( !c.imag().isZero() )
721  {
722 
723  in_real=floatToStr( c.real(), oprec ); // get real part
724  in_imag=floatToStr( abs(c.imag()), oprec ); // get imaginary part
725 
726  if (nCoeff_is_long_C(src))
727  {
728  int len=(strlen(in_real)+strlen(in_imag)+7+N)*sizeof(char);
729  out=(char*)omAlloc(len);
730  memset(out,0,len);
731  if ( !c.real().isZero() ) // (-23-i*5.43) or (15.1+i*5.3)
732  sprintf(out,"(%s%s%s*%s)",in_real,c.imag().sign()>=0?"+":"-",complex_parameter,in_imag);
733  else // (-i*43) or (i*34)
734  {
735  if (c.imag().isOne())
736  sprintf(out,"%s", complex_parameter);
737  else if (c.imag().isMOne())
738  sprintf(out,"-%s", complex_parameter);
739  else
740  sprintf(out,"(%s%s*%s)",c.imag().sign()>=0?"":"-", complex_parameter,in_imag);
741  }
742  }
743  else
744  {
745  int len=(strlen(in_real)+strlen(in_imag)+9) * sizeof(char);
746  out=(char*)omAlloc( len );
747  memset(out,0,len);
748  if ( !c.real().isZero() )
749  sprintf(out,"(%s%s%s)",in_real,c.imag().sign()>=0?"+I*":"-I*",in_imag);
750  else
751  sprintf(out,"(%s%s)",c.imag().sign()>=0?"I*":"-I*",in_imag);
752  }
753  omFree( (void *) in_real );
754  omFree( (void *) in_imag );
755  }
756  else
757  {
758  out= floatToStr( c.real(), oprec );
759  }
760 
761  return out;
762 }
const CanonicalForm CFMap CFMap & N
Definition: cfEzgcd.cc:49
void SmallToZero()
Definition: mpr_complex.cc:784
bool isOne() const
Definition: mpr_complex.cc:257
bool isMOne() const
Definition: mpr_complex.cc:273
bool isZero() const
Definition: mpr_complex.cc:252
static FORCE_INLINE char const ** n_ParameterNames(const coeffs r)
Returns a (const!) pointer to (const char*) names of parameters.
Definition: coeffs.h:800
static FORCE_INLINE BOOLEAN nCoeff_is_long_C(const coeffs r)
Definition: coeffs.h:916
#define assume(x)
Definition: mod2.h:390
char * floatToStr(const gmp_float &r, const unsigned int oprec)
Definition: mpr_complex.cc:578
gmp_float abs(const gmp_float &a)
Definition: mpr_complex.cc:321
#define omAlloc(size)
Definition: omAllocDecl.h:210
#define omFree(addr)
Definition: omAllocDecl.h:261
#define NULL
Definition: omList.c:12

◆ cos()

gmp_float cos ( const gmp_float a)

Definition at line 338 of file mpr_complex.cc.

339 {
340  gmp_float tmp( cos((double)a) );
341  return tmp;
342 }
gmp_float cos(const gmp_float &a)
Definition: mpr_complex.cc:338

◆ exp()

gmp_float exp ( const gmp_float a)

Definition at line 357 of file mpr_complex.cc.

358 {
359  gmp_float tmp( exp((double)a) );
360  return tmp;
361 }
gmp_float exp(const gmp_float &a)
Definition: mpr_complex.cc:357

◆ floatToStr()

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

Definition at line 578 of file mpr_complex.cc.

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

◆ hypot()

gmp_float hypot ( const gmp_float a,
const gmp_float b 
)

Definition at line 348 of file mpr_complex.cc.

349 {
350 #if 1
351  return ( sqrt( (a*a) + (b*b) ) );
352 #else
353  gmp_float tmp( hypot( (double)a, (double)b ) );
354  return tmp;
355 #endif
356 }
CanonicalForm b
Definition: cfModGcd.cc:4044
gmp_float sqrt(const gmp_float &a)
Definition: mpr_complex.cc:327
gmp_float hypot(const gmp_float &a, const gmp_float &b)
Definition: mpr_complex.cc:348

◆ log()

gmp_float log ( const gmp_float a)

Definition at line 343 of file mpr_complex.cc.

344 {
345  gmp_float tmp( log((double)a) );
346  return tmp;
347 }
gmp_float log(const gmp_float &a)
Definition: mpr_complex.cc:343

◆ max()

gmp_float max ( const gmp_float a,
const gmp_float b 
)

Definition at line 362 of file mpr_complex.cc.

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

◆ numberFieldToFloat()

gmp_float numberFieldToFloat ( number  num,
int  src 
)

Definition at line 438 of file mpr_complex.cc.

439 {
440  gmp_float r;
441 
442  switch (cf)
443  {
444  case QTOF:
445  if ( num != NULL )
446  {
447  if (SR_HDL(num) & SR_INT)
448  {
449  r = gmp_float(SR_TO_INT(num));
450  }
451  else
452  {
453  if ( num->s != 3 )
454  {
455  r= gmp_float(num->z);
456  r/= gmp_float(num->n);
457  }
458  else
459  {
460  r= num->z;
461  }
462  }
463  }
464  else
465  {
466  r= 0.0;
467  }
468  break;
469  case RTOF:
470  r= *(gmp_float*)num;
471  break;
472  case CTOF:
473  WerrorS("Can not map from field C to field R!");
474  break;
475  case ZTOF:
476  default:
477  WerrorS("Ground field not implemented!");
478  } // switch
479 
480  return r;
481 }
CanonicalForm num(const CanonicalForm &f)
CanonicalForm cf
Definition: cfModGcd.cc:4024
void WerrorS(const char *s)
Definition: feFopen.cc:24
#define SR_INT
Definition: longrat.h:66
#define SR_HDL(A)
Definition: mpr_complex.cc:32
#define SR_TO_INT(SR)
Definition: mpr_complex.cc:33
#define RTOF
Definition: mpr_complex.h:20
#define QTOF
Definition: mpr_complex.h:19
#define ZTOF
Definition: mpr_complex.h:18
#define CTOF
Definition: mpr_complex.h:21

◆ numberToComplex()

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_complex numbers based on
Definition: mpr_complex.h:179
gmp_float numberToFloat(number num, const coeffs src)
Definition: mpr_complex.cc:372

◆ numberToFloat()

gmp_float numberToFloat ( number  num,
const coeffs  src 
)

Definition at line 372 of file mpr_complex.cc.

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

◆ operator*()

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_float r
Definition: mpr_complex.h:181

◆ operator+()

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 }

◆ operator-()

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 }

◆ operator/()

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 }

◆ operator<()

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 }

◆ operator<=()

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 }

◆ operator==()

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 }

◆ operator>()

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 }

◆ operator>=()

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 }

◆ 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.

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

◆ sin()

gmp_float sin ( const gmp_float a)

Definition at line 333 of file mpr_complex.cc.

334 {
335  gmp_float tmp( sin((double)a) );
336  return tmp;
337 }
gmp_float sin(const gmp_float &a)
Definition: mpr_complex.cc:333

◆ sqrt() [1/2]

gmp_complex sqrt ( const gmp_complex x)

Definition at line 676 of file mpr_complex.cc.

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

◆ sqrt() [2/2]

gmp_float sqrt ( const gmp_float a)

Definition at line 327 of file mpr_complex.cc.

328 {
329  gmp_float tmp;
330  mpf_sqrt( *(tmp._mpfp()), *a.mpfp() );
331  return tmp;
332 }