source: git/Singular/mpr_complex.cc @ bad9f3

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