My Project
Loading...
Searching...
No Matches
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.
 
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 307 of file mpr_complex.h.

308{
309 return hypot(c.real(),c.imag());
310}
gmp_float imag() const
gmp_float real() const
gmp_float hypot(const gmp_float &, const gmp_float &)

◆ abs() [2/2]

Definition at line 313 of file mpr_complex.cc.

314{
315 gmp_float tmp;
316 mpf_abs( *(tmp._mpfp()), *a.mpfp() );
317 return tmp;
318}
mpf_t * _mpfp()
const mpf_t * mpfp() const

◆ complexNearZero()

bool complexNearZero ( gmp_complex * c,
int digits )

Definition at line 757 of file mpr_complex.cc.

758{
759 gmp_float eps,epsm;
760
761 if ( digits < 1 ) return true;
762
763 eps=pow(10.0,(int)digits);
764 //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
765 eps=(gmp_float)1.0/eps;
766 epsm=-eps;
767
768 //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
769
770 if ( c->real().sign() > 0 ) // +
771 return (c->real() < eps && (c->imag() < eps && c->imag() > epsm));
772 else // -
773 return (c->real() > epsm && (c->imag() < eps && c->imag() > epsm));
774}
Rational pow(const Rational &a, int e)
Definition GMPrat.cc:411

◆ complexToStr()

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

Definition at line 696 of file mpr_complex.cc.

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

◆ cos()

Definition at line 330 of file mpr_complex.cc.

331{
332 gmp_float tmp( cos((double)a) );
333 return tmp;
334}
gmp_float cos(const gmp_float &a)

◆ exp()

Definition at line 349 of file mpr_complex.cc.

350{
351 gmp_float tmp( exp((double)a) );
352 return tmp;
353}
gmp_float exp(const gmp_float &a)

◆ floatToStr()

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

Definition at line 570 of file mpr_complex.cc.

571{
572#if 1
573 mp_exp_t exponent;
574 int size,insize;
575 char *nout,*out,*in;
576
577 insize= (oprec+2) * sizeof(char) + 10;
578 in= (char*)omAlloc( insize );
579
580 mpf_get_str(in,&exponent,10,oprec,*(r.mpfp()));
581
582 //if ( (exponent > 0)
583 //&& (exponent < (int)oprec)
584 //&& (strlen(in)-(in[0]=='-'?1:0) == oprec) )
585 //{
586 // omFree( (void *) in );
587 // insize= (exponent+oprec+2) * sizeof(char) + 10;
588 // in= (char*)omAlloc( insize );
589 // int newprec= exponent+oprec;
590 // mpf_get_str(in,&exponent,10,newprec,*(r.mpfp()));
591 //}
592 nout= nicifyFloatStr( in, exponent, oprec, &size, SIGN_EMPTY );
593 omFree( (void *) in );
594 out= (char*)omAlloc( (strlen(nout)+1) * sizeof(char) );
595 strcpy( out, nout );
596 omFree( (void *) nout );
597
598 return out;
599#else
600 // for testing purpose...
601 char *out= (char*)omAlloc( (1024) * sizeof(char) );
602 sprintf(out,"% .10f",(double)r);
603 return out;
604#endif
605}
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition cf_ops.cc:600
#define exponent
#define SIGN_EMPTY
char * nicifyFloatStr(char *in, mp_exp_t exponent, size_t oprec, int *size, int thesign)

◆ hypot()

gmp_float hypot ( const gmp_float & a,
const gmp_float & b )

Definition at line 340 of file mpr_complex.cc.

341{
342#if 1
343 return ( sqrt( (a*a) + (b*b) ) );
344#else
345 gmp_float tmp( hypot( (double)a, (double)b ) );
346 return tmp;
347#endif
348}
CanonicalForm b
Definition cfModGcd.cc:4111
gmp_float sqrt(const gmp_float &a)
gmp_float hypot(const gmp_float &a, const gmp_float &b)

◆ log()

Definition at line 335 of file mpr_complex.cc.

336{
337 gmp_float tmp( log((double)a) );
338 return tmp;
339}
gmp_float log(const gmp_float &a)

◆ max()

Definition at line 354 of file mpr_complex.cc.

355{
356 gmp_float tmp;
357 a > b ? tmp= a : tmp= b;
358 return tmp;
359}

◆ numberFieldToFloat()

gmp_float numberFieldToFloat ( number num,
int src )

Definition at line 430 of file mpr_complex.cc.

431{
432 gmp_float r;
433
434 switch (cf)
435 {
436 case QTOF:
437 if ( num != NULL )
438 {
439 if (SR_HDL(num) & SR_INT)
440 {
441 r = gmp_float(SR_TO_INT(num));
442 }
443 else
444 {
445 if ( num->s != 3 )
446 {
447 r= gmp_float(num->z);
448 r/= gmp_float(num->n);
449 }
450 else
451 {
452 r= num->z;
453 }
454 }
455 }
456 else
457 {
458 r= 0.0;
459 }
460 break;
461 case RTOF:
462 r= *(gmp_float*)num;
463 break;
464 case CTOF:
465 WerrorS("Can not map from field C to field R!");
466 break;
467 case ZTOF:
468 default:
469 WerrorS("Ground field not implemented!");
470 } // switch
471
472 return r;
473}
CanonicalForm num(const CanonicalForm &f)
CanonicalForm cf
Definition cfModGcd.cc:4091
void WerrorS(const char *s)
Definition feFopen.cc:24
#define SR_INT
Definition longrat.h:67
#define SR_HDL(A)
#define SR_TO_INT(SR)
#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 314 of file mpr_complex.h.

315{
316 if (nCoeff_is_long_C(r))
317 {
318 return *(gmp_complex*)num;
319 }
320 else
321 {
322 return gmp_complex( numberToFloat(num, r) );
323 }
324}
gmp_complex numbers based on
gmp_float numberToFloat(number num, const coeffs src)

◆ numberToFloat()

gmp_float numberToFloat ( number num,
const coeffs src )

Definition at line 364 of file mpr_complex.cc.

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

◆ operator*()

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

Definition at line 257 of file mpr_complex.h.

258{
259 return gmp_complex( a.r * b_d, a.i * b_d );
260}
gmp_float i
gmp_float r

◆ operator+()

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

Definition at line 249 of file mpr_complex.h.

250{
251 return gmp_complex( a.r + b_d, a.i );
252}

◆ operator-()

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

Definition at line 253 of file mpr_complex.h.

254{
255 return gmp_complex( a.r - b_d, a.i );
256}

◆ operator/()

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

Definition at line 261 of file mpr_complex.h.

262{
263 return gmp_complex( a.r / b_d, a.i / b_d );
264}

◆ operator<()

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

Definition at line 275 of file mpr_complex.h.

276{
277 return ( a.real() < b.real() );
278}

◆ operator<=()

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

Definition at line 283 of file mpr_complex.h.

284{
285 return ( a.real() <= b.real() );
286}

◆ operator==()

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

Definition at line 267 of file mpr_complex.h.

268{
269 return ( b.real() == a.real() ) && ( b.imag() == a.imag() );
270}

◆ operator>()

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

Definition at line 271 of file mpr_complex.h.

272{
273 return ( a.real() > b.real() );
274}

◆ operator>=()

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

Definition at line 279 of file mpr_complex.h.

280{
281 return ( a.real() >= b.real() );
282}

◆ 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}
EXTERN_VAR size_t gmp_output_digits
Definition mpr_base.h:115
STATIC_VAR gmp_float * gmpRel
STATIC_VAR gmp_float * diff

◆ sin()

Definition at line 325 of file mpr_complex.cc.

326{
327 gmp_float tmp( sin((double)a) );
328 return tmp;
329}
gmp_float sin(const gmp_float &a)

◆ sqrt() [1/2]

Definition at line 668 of file mpr_complex.cc.

669{
670 gmp_float r = abs(x);
671 gmp_float nr, ni;
672 if (r == (gmp_float) 0.0)
673 {
674 nr = ni = r;
675 }
676 else if ( x.real() > (gmp_float)0)
677 {
678 nr = sqrt((gmp_float)0.5 * (r + x.real()));
679 ni = x.imag() / nr / (gmp_float)2;
680 }
681 else
682 {
683 ni = sqrt((gmp_float)0.5 * (r - x.real()));
684 if (x.imag() < (gmp_float)0)
685 {
686 ni = - ni;
687 }
688 nr = x.imag() / ni / (gmp_float)2;
689 }
690 gmp_complex tmp(nr, ni);
691 return tmp;
692}
Variable x
Definition cfModGcd.cc:4090

◆ sqrt() [2/2]

gmp_float sqrt ( const gmp_float & a)

Definition at line 319 of file mpr_complex.cc.

320{
321 gmp_float tmp;
322 mpf_sqrt( *(tmp._mpfp()), *a.mpfp() );
323 return tmp;
324}