source: git/kernel/mpr_complex.cc @ 977f94

fieker-DuValspielwiese
Last change on this file since 977f94 was 7447d8, checked in by Hans Schönemann <hannes@…>, 19 years ago
*hannes: gcc 4 and 64bit git-svn-id: file:///usr/local/Singular/svn/trunk@8463 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 15.7 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mpr_complex.cc,v 1.3 2005-07-27 15:48:29 Singular Exp $ */
5
6/*
7* ABSTRACT - multipolynomial resultants - real floating-point numbers using gmp
8*            and complex numbers based on pairs of real floating-point numbers
9*
10*/
11
12// WARNING! ALWAYS use omAlloc and FreeL when alloc. memory for some char* !!
13
14#include "mod2.h"
15//#ifdef HAVE_MPR
16#include "structs.h"
17#include "febase.h"
18#include "omalloc.h"
19#include "numbers.h"
20#include "longrat.h"
21#include <math.h>
22#include "mpr_complex.h"
23
24//%s
25// this was copied form longrat0.cc
26// and will be used in numberToFloat.
27// Make sure that it is up to date!!
28#define SR_HDL(A) ((long)(A))
29#define SR_TO_INT(SR) (((long)SR) >> 2)
30
31#define SIGN_PLUS  1
32#define SIGN_SPACE 2
33#define SIGN_EMPTY 4
34
35#define EXTRABYTES 4
36
37#define DEFPREC        20         // minimum number of digits (output operations)
38size_t gmp_output_digits= DEFPREC;
39
40extern int mmInit(void);
41int dummy=mmInit();
42static gmp_float gmpRel(0.0);
43static gmp_float diff(0.0);
44
45
46/** Set size of mantissa
47 *  digits - the number of output digits (basis 10)
48 *  the size of mantissa consists of two parts:
49 *    the "output" part a and the "rest" part b.
50 *  According to the GMP-precision digits is
51 *  recomputed to bits (basis 2).
52 *  Two numbers a, b are equal if
53 *    | a - b | < | a | * 0.1^digits .
54 *  In this case we have a - b = 0 .
55 *  The epsilon e is e=0.1^(digits+rest) with
56 *  1+e != 1, but 1+0.1*e = 1.
57 */
58void setGMPFloatDigits( size_t digits, size_t rest )
59{
60  size_t bits = 1 + (size_t) ((float)digits * 3.5);
61  size_t rb = 1 + (size_t) ((float)rest * 3.5);
62  size_t db = bits+rb;
63  gmp_output_digits= digits;
64  mpf_set_default_prec( db );
65  mpf_set_prec(*diff._mpfp(),32);
66  mpf_set_prec(*gmpRel._mpfp(),32);
67  mpf_set_d(*gmpRel._mpfp(),0.1);
68  mpf_pow_ui(*gmpRel._mpfp(),*gmpRel._mpfp(),digits);
69}
70
71size_t getGMPFloatDigits()
72{
73  return gmp_output_digits;
74}
75
76void gmp_float::setFromStr( char * in )
77{
78  // gmp doesn't understand number which begin with "." -- it needs 0.
79  // so, insert the zero
80  if (*in == '.')
81  {
82    int len = strlen(in)+2;
83    char* c_in = (char*) omAlloc(len);
84    *c_in = '0';
85    strcpy(&(c_in[1]), in);
86
87    mpf_set_str( t, c_in, 10 );
88    omFreeSize((void*)c_in, len);
89  }
90  else
91  {
92    mpf_set_str( t, in, 10 );
93  }
94}
95
96
97// <gmp_float> = <gmp_float> operator <gmp_float>
98gmp_float operator + ( const gmp_float & a, const gmp_float & b )
99{
100  gmp_float tmp( a );
101  tmp += b;
102  return tmp;
103}
104gmp_float operator - ( const gmp_float & a, const gmp_float & b )
105{
106  gmp_float tmp( a );
107  tmp -= b;
108  return tmp;
109}
110gmp_float operator * ( const gmp_float & a, const gmp_float & b )
111{
112  gmp_float tmp( a );
113  tmp *= b;
114  return tmp;
115}
116gmp_float operator / ( const gmp_float & a, const gmp_float & b )
117{
118  gmp_float tmp( a );
119  tmp /= b;
120  return tmp;
121}
122
123// <gmp_float> operator <gmp_float>
124gmp_float & gmp_float::operator += ( const gmp_float & a )
125{
126  if (mpf_sgn(t) != -(mpf_sgn(a.t)))
127  {
128    mpf_add( t, t, a.t);
129    return *this;
130  }
131  if((mpf_sgn(a.t)==0) && (mpf_sgn(t)==0))
132  {
133    mpf_set_d( t, 0.0);
134    return *this;
135  }
136  mpf_add( t, t, a.t );
137  mpf_set(diff.t, t);
138  mpf_set_prec(diff.t, 32);
139  mpf_div(diff.t, diff.t, a.t);
140  mpf_abs(diff.t, diff.t);
141  if(mpf_cmp(diff.t, gmpRel.t) < 0)
142    mpf_set_d( t, 0.0);
143  return *this;
144}
145gmp_float & gmp_float::operator -= ( const gmp_float & a )
146{
147  if (mpf_sgn(t) != mpf_sgn(a.t))
148  {
149    mpf_sub( t, t, a.t);
150    return *this;
151  }
152  if((mpf_sgn(a.t)==0) && (mpf_sgn(t)==0))
153  {
154    mpf_set_d( t, 0.0);
155    return *this;
156  }
157  mpf_sub( t, t, a.t );
158  mpf_set(diff.t, t);
159  mpf_set_prec(diff.t, 32);
160  mpf_div(diff.t, diff.t, a.t);
161  mpf_abs(diff.t, diff.t);
162  if(mpf_cmp(diff.t, gmpRel.t) < 0)
163    mpf_set_d( t, 0.0);
164  return *this;
165}
166
167// <gmp_float> == <gmp_float> ??
168bool operator == ( const gmp_float & a, const gmp_float & b )
169{
170  if(mpf_sgn(a.t) != mpf_sgn(b.t))
171    return false;
172  if((mpf_sgn(a.t)==0) && (mpf_sgn(b.t)==0))
173    return true;
174  mpf_sub(diff.t, a.t, b.t);
175  mpf_div(diff.t, diff.t, a.t);
176  mpf_abs(diff.t, diff.t);
177  if(mpf_cmp(diff.t, gmpRel.t) < 0)
178    return true;
179  else
180    return false;
181}
182// t == 0 ?
183bool gmp_float::isZero()
184{
185  return (mpf_sgn( t ) == 0);
186}
187// t == 1 ?
188bool gmp_float::isOne()
189{
190#ifdef  VARIANTE_1
191  return (mpf_cmp_ui( t , 1 ) == 0);
192#else
193  if (mpf_sgn(t) <= 0)
194    return false;
195  mpf_sub_ui(diff.t, t, 1);
196  mpf_abs(diff.t, diff.t);
197  if(mpf_cmp(diff.t, gmpRel.t) < 0)
198    return true;
199  else
200    return false;
201#endif
202}
203// t == -1 ?
204bool gmp_float::isMOne()
205{
206#ifdef VARIANTE_1
207  return (mpf_cmp_si( t , -1 ) == 0);
208#else
209  if (mpf_sgn(t) >= 0)
210    return false;
211  mpf_add_ui(diff.t, t, 1);
212  mpf_abs(diff.t, diff.t);
213  if(mpf_cmp(diff.t, gmpRel.t) < 0)
214    return true;
215  else
216    return false;
217#endif
218}
219bool operator > ( const gmp_float & a, const gmp_float & b )
220{
221  if (a.t == b.t)
222    return false;
223  return mpf_cmp( a.t, b.t ) > 0;
224}
225bool operator < ( const gmp_float & a, const gmp_float & b )
226{
227  if (a.t == b.t)
228    return false;
229  return mpf_cmp( a.t, b.t ) < 0;
230}
231bool operator >= ( const gmp_float & a, const gmp_float & b )
232{
233  if (a.t == b.t)
234    return true;
235  return mpf_cmp( a.t, b.t ) >= 0;
236}
237bool operator <= ( const gmp_float & a, const gmp_float & b )
238{
239  if (a.t == b.t)
240    return true;
241  return mpf_cmp( a.t, b.t ) <= 0;
242}
243
244// unary -
245gmp_float operator - ( const gmp_float & a )
246{
247  gmp_float tmp;
248  mpf_neg( *(tmp._mpfp()), *(a.mpfp()) );
249  return tmp;
250}
251
252gmp_float abs( const gmp_float & a )
253{
254  gmp_float tmp;
255  mpf_abs( *(tmp._mpfp()), *a.mpfp() );
256  return tmp;
257}
258gmp_float sqrt( const gmp_float & a )
259{
260  gmp_float tmp;
261  mpf_sqrt( *(tmp._mpfp()), *a.mpfp() );
262  return tmp;
263}
264gmp_float sin( const gmp_float & a )
265{
266  gmp_float tmp( sin((double)a) );
267  return tmp;
268}
269gmp_float cos( const gmp_float & a )
270{
271  gmp_float tmp( cos((double)a) );
272  return tmp;
273}
274gmp_float log( const gmp_float & a )
275{
276  gmp_float tmp( log((double)a) );
277  return tmp;
278}
279gmp_float hypot( const gmp_float & a, const gmp_float & b )
280{
281#if 1
282  return ( sqrt( (a*a) + (b*b) ) );
283#else
284  gmp_float tmp( hypot( (double)a, (double)b ) );
285  return tmp;
286#endif
287}
288gmp_float exp( const gmp_float & a )
289{
290  gmp_float tmp( exp((double)a) );
291  return tmp;
292}
293gmp_float max( const gmp_float & a, const gmp_float & b )
294{
295  gmp_float tmp;
296  a > b ? tmp= a : tmp= b;
297  return tmp;
298}
299//
300// number to float, number = Q, R, C
301// makes a COPY of num! (Ist das gut?)
302//
303gmp_float numberToFloat( number num )
304{
305  gmp_float r;
306
307  if ( rField_is_Q() )
308  {
309    if ( num != NULL )
310    {
311      if (SR_HDL(num) & SR_INT)
312      {
313        r= SR_TO_INT(num);
314      }
315      else
316      {
317        if ( num->s == 0 )
318        {
319          nlNormalize( num );
320        }
321        if (SR_HDL(num) & SR_INT)
322        {
323          r= SR_TO_INT(num);
324        }
325        else
326        {
327          if ( num->s != 3 )
328          {
329            r= &num->z;
330            r/= (gmp_float)&num->n;
331          }
332          else
333          {
334            r= &num->z;
335          }
336        }
337      }
338    }
339    else
340    {
341      r= 0.0;
342    }
343  }
344  else if (rField_is_long_R() || rField_is_long_C())
345  {
346    r= *(gmp_float*)num;
347  }
348  else if ( rField_is_R() )
349  {
350    // Add some code here :-)
351    WerrorS("Ground field not implemented!");
352  }
353  else
354  {
355    WerrorS("Ground field not implemented!");
356  }
357
358  return r;
359}
360
361gmp_float numberFieldToFloat( number num, int k )
362{
363  gmp_float r;
364
365  switch (k)
366  {
367  case QTOF:
368    if ( num != NULL )
369    {
370      if (SR_HDL(num) & SR_INT)
371      {
372        r= SR_TO_INT(num);
373      }
374      else
375      {
376        if ( num->s == 0 )
377        {
378          nlNormalize( num );
379        }
380        if (SR_HDL(num) & SR_INT)
381        {
382          r= SR_TO_INT(num);
383        }
384        else
385        {
386          if ( num->s != 3 )
387          {
388            r= &num->z;
389            r/= (gmp_float)&num->n;
390          }
391          else
392          {
393            r= &num->z;
394          }
395        }
396      }
397    }
398    else
399    {
400      r= 0.0;
401    }
402    break;
403  case RTOF:
404    r= *(gmp_float*)num;
405    break;
406  case CTOF:
407    WerrorS("Can not map from field C to field R!");
408    break;
409  case ZTOF:
410  default:
411    WerrorS("Ground field not implemented!");
412  } // switch
413
414  return r;
415}
416
417// Do some strange things with the mantissa string and the exponent
418// to get some nice output string.
419char *nicifyFloatStr( char * in, mp_exp_t exponent, size_t oprec, int *size, int thesign )
420{
421  char *out;
422
423  int sign= (in[0] == '-') ? 1 : 0;
424  char csign[2];
425
426  switch (thesign)
427  {
428    case SIGN_PLUS:
429      sign ? strcpy(csign,"-") : strcpy(csign,"+");  //+123, -123
430      break;
431    case SIGN_SPACE:
432      sign ? strcpy(csign,"-") : strcpy(csign," ");  // 123, -123
433      break;
434    case SIGN_EMPTY:
435    default:
436      sign ? strcpy(csign,"-") : strcpy(csign,"");   //123, -123
437      break;
438  }
439
440  if ( strlen(in) == 0 )
441  {
442    *size= 2*sizeof(char);
443    return omStrDup("0");
444  }
445
446  if ( ((unsigned int)ABS(exponent) <= oprec)
447       /*|| (exponent+sign >= (int)strlen(in))*/ )
448  {
449    if ( exponent+sign < (int)strlen(in) )
450    {
451      int eexponent= (exponent >= 0) ? 0 : -exponent;
452      int eeexponent= (exponent >= 0) ? exponent : 0;
453      *size= (strlen(in)+15+eexponent) * sizeof(char);
454      out= (char*)omAlloc(*size);
455      memset(out,0,*size);
456
457      strcpy(out,csign);
458      strncat(out,in+sign,eeexponent);
459
460      if (exponent == 0)
461        strcat(out,"0.");
462      else if ( exponent > 0 )
463        strcat(out,".");
464      else
465      {
466        strcat(out,"0.");
467        memset(out+strlen(out),'0',eexponent);
468      }
469      strcat(out,in+sign+eeexponent);
470    }
471    else if ( exponent+sign > (int)strlen(in) )
472    {
473      *size= (strlen(in)+exponent+12)*sizeof(char);
474      out= (char*)omAlloc(*size);
475      memset(out,0,*size);
476      sprintf(out,"%s%s",csign,in+sign);
477      memset(out+strlen(out),'0',exponent-strlen(in)+sign);
478    }
479    else
480    {
481      *size= (strlen(in)+2) * sizeof(char) + 10;
482      out= (char*)omAlloc(*size);
483      memset(out,0,*size);
484      sprintf(out,"%s%s",csign,in+sign);
485    }
486  }
487  else
488  {
489//      if ( exponent > 0 )
490//      {
491      int c=1,d=10;
492      while ( exponent / d > 0 )
493      { // count digits
494        d*=10;
495        c++;
496      }
497      *size= (strlen(in)+12+c) * sizeof(char) + 10;
498      out= (char*)omAlloc(*size);
499      memset(out,0,*size);
500      sprintf(out,"%s0.%se%s%d",csign,in+sign,exponent>=0?"+":"",(int)exponent);
501//      }
502//      else
503//      {
504//        *size=2;
505//        out= (char*)omAlloc(*size);
506//        strcpy(out,"0");
507//      }
508  }
509  return out;
510}
511
512char *floatToStr( const gmp_float & r, const unsigned int oprec )
513{
514#if 1
515  mp_exp_t exponent;
516  int size,insize;
517  char *nout,*out,*in;
518
519  insize= (oprec+2) * sizeof(char) + 10;
520  in= (char*)omAlloc( insize );
521
522  mpf_get_str(in,&exponent,10,oprec,*(r.mpfp()));
523
524  if ( (exponent > 0)
525  && (exponent < (int)oprec)
526  && (strlen(in)-(in[0]=='-'?1:0) == oprec) )
527  {
528    omFree( (ADDRESS) in );
529    insize= (exponent+oprec+2) * sizeof(char) + 10;
530    in= (char*)omAlloc( insize );
531    int newprec= exponent+oprec;
532    mpf_get_str(in,&exponent,10,newprec,*(r.mpfp()));
533  }
534  nout= nicifyFloatStr( in, exponent, oprec, &size, SIGN_EMPTY );
535  omFree( (ADDRESS) in );
536  out= (char*)omAlloc( (strlen(nout)+1) * sizeof(char) );
537  strcpy( out, nout );
538  omFree( (ADDRESS) nout );
539
540  return out;
541#else
542  // for testing purpose...
543  char *out= (char*)omAlloc( (1024) * sizeof(char) );
544  sprintf(out,"% .10f",(double)r);
545  return out;
546#endif
547}
548//<-
549
550//-> gmp_complex::*
551// <gmp_complex> = <gmp_complex> operator <gmp_complex>
552//
553gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b )
554{
555  return gmp_complex( a.r + b.r, a.i + b.i );
556}
557gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b )
558{
559  return gmp_complex( a.r - b.r, a.i - b.i );
560}
561gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b )
562{
563  return gmp_complex( a.r * b.r - a.i * b.i,
564                  a.r * b.i + a.i * b.r);
565}
566gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b )
567{
568  gmp_float d = b.r*b.r + b.i*b.i;
569  return gmp_complex( (a.r * b.r + a.i * b.i) / d,
570                (a.i * b.r - a.r * b.i) / d);
571}
572
573// <gmp_complex> operator <gmp_complex>
574//
575gmp_complex & gmp_complex::operator += ( const gmp_complex & b )
576{
577  r+=b.r;
578  i+=b.i;
579  return *this;
580}
581gmp_complex & gmp_complex::operator -= ( const gmp_complex & b )
582{
583  r-=b.r;
584  i-=b.i;
585  return *this;
586}
587gmp_complex & gmp_complex::operator *= ( const gmp_complex & b )
588{
589  gmp_float f = r * b.r - i * b.i;
590  i = r * b.i + i * b.r;
591  r = f;
592  return *this;
593}
594gmp_complex & gmp_complex::operator /= ( const gmp_complex & b )
595{
596  gmp_float d = b.r*b.r + b.i*b.i;
597  r = (r * b.r + i * b.i) / d;
598  i = (i * b.r - r * b.i) / d;
599  return *this;
600}
601
602// Returns square root of gmp_complex number
603//
604gmp_complex sqrt( const gmp_complex & x )
605{
606  gmp_float r = abs(x);
607  gmp_float nr, ni;
608  if (r == (gmp_float) 0.0)
609  {
610    nr = ni = r;
611  }
612  else if ( x.real() > (gmp_float)0)
613  {
614    nr = sqrt((gmp_float)0.5 * (r + x.real()));
615    ni = x.imag() / nr / (gmp_float)2;
616  }
617  else
618  {
619    ni = sqrt((gmp_float)0.5 * (r - x.real()));
620    if (x.imag() < (gmp_float)0)
621    {
622      ni = - ni;
623    }
624    nr = x.imag() / ni / (gmp_float)2;
625  }
626  gmp_complex tmp(nr, ni);
627  return tmp;
628}
629
630// converts a gmp_complex to a string ( <real part> + I * <imaginary part> )
631//
632char *complexToStr( gmp_complex & c, const unsigned int oprec )
633{
634  char *out,*in_imag,*in_real;
635
636  c.SmallToZero();
637  if ( !c.imag().isZero() )
638  {
639
640    in_real=floatToStr( c.real(), oprec );         // get real part
641    in_imag=floatToStr( abs(c.imag()), oprec );    // get imaginary part
642
643    if (rField_is_long_C())
644    {
645      int len=(strlen(in_real)+strlen(in_imag)+7+strlen(currRing->parameter[0]))*sizeof(char);
646      out=(char*)omAlloc(len);
647      memset(out,0,len);
648      if (  !c.real().isZero() )  // (-23-i*5.43) or (15.1+i*5.3)
649        sprintf(out,"(%s%s%s*%s)",in_real,c.imag().sign()>=0?"+":"-",currRing->parameter[0],in_imag);
650      else // (-i*43) or (i*34)
651      {
652        if (c.imag().isOne())
653          sprintf(out,currRing->parameter[0]);
654        else if (c.imag().isMOne())
655          sprintf(out,"-%s",currRing->parameter[0]);
656        else
657          sprintf(out,"(%s%s*%s)",c.imag().sign()>=0?"":"-",currRing->parameter[0],in_imag);
658      }
659    }
660    else
661    {
662      int len=(strlen(in_real)+strlen(in_imag)+9) * sizeof(char);
663      out=(char*)omAlloc( len );
664      memset(out,0,len);
665      if ( !c.real().isZero() )
666        sprintf(out,"(%s%s%s)",in_real,c.imag().sign()>=0?"+I*":"-I*",in_imag);
667      else
668        sprintf(out,"(%s%s)",c.imag().sign()>=0?"I*":"-I*",in_imag);
669    }
670    omFree( (ADDRESS) in_real );
671    omFree( (ADDRESS) in_imag );
672  }
673  else
674  {
675    out= floatToStr( c.real(), oprec );
676  }
677
678  return out;
679}
680//<-
681
682bool complexNearZero( gmp_complex * c, int digits )
683{
684  gmp_float eps,epsm;
685
686  if ( digits < 1 ) return true;
687
688  eps=pow(10.0,(int)digits);
689  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
690  eps=(gmp_float)1.0/eps;
691  epsm=-eps;
692
693  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
694
695  if ( c->real().sign() > 0 ) // +
696    return (c->real() < eps && (c->imag() < eps && c->imag() > epsm));
697  else // -
698    return (c->real() > epsm && (c->imag() < eps && c->imag() > epsm));
699}
700
701void gmp_complex::SmallToZero()
702{
703  gmp_float ar=this->real();
704  gmp_float ai=this->imag();
705  if (ar.isZero() || ai.isZero()) return;
706  mpf_abs(*ar._mpfp(), *ar._mpfp());
707  mpf_abs(*ai._mpfp(), *ai._mpfp());
708  mpf_set_prec(*ar._mpfp(), 32);
709  mpf_set_prec(*ai._mpfp(), 32);
710  if (ar > ai)
711  {
712    mpf_div(*ai._mpfp(), *ai._mpfp(), *ar._mpfp());
713    if (ai < gmpRel) this->imag(0.0);
714  }
715  else
716  {
717    mpf_div(*ar._mpfp(), *ar._mpfp(), *ai._mpfp());
718    if (ar < gmpRel) this->real(0.0);
719  }
720}
721
722//%e
723
724//#endif // HAVE_MPR
725
726// local Variables: ***
727// folded-file: t ***
728// compile-command-1: "make installg" ***
729// compile-command-2: "make install" ***
730// End: ***
Note: See TracBrowser for help on using the repository browser.