source: git/Singular/mpr_complex.cc @ 9bffb9c

spielwiese
Last change on this file since 9bffb9c was 9bffb9c, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: SOLARIS/gcc 2.95.2 changes git-svn-id: file:///usr/local/Singular/svn/trunk@4752 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 16.0 KB
Line 
1/****************************************
2*  Computer Algebra System SINGULAR     *
3****************************************/
4/* $Id: mpr_complex.cc,v 1.28 2000-11-21 15:35:44 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 "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 and the "zero" part.
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 */
58void setGMPFloatDigits( size_t digits )
59{
60  size_t bits = 1 + (size_t) ((float)digits * 3.5);
61  size_t db = bits+bits;
62  bits= bits>64?bits:64;
63  gmp_output_digits= digits;
64  mpf_set_default_prec( db );
65  mpf_set_prec((mpf_ptr)*(diff.mpfp()),32);
66  mpf_set_prec((mpf_ptr)*gmpRel.mpfp(),32);
67  mpf_set_d((mpf_ptr)*gmpRel.mpfp(),0.1);
68  mpf_pow_ui((mpf_ptr)*gmpRel.mpfp(),(mpf_ptr)*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= new gmp_float();
255  mpf_abs( *tmp->_mpfp(), *a.mpfp() );
256  return *tmp;
257}
258gmp_float sqrt( const gmp_float & a )
259{
260  gmp_float *tmp= new gmp_float();
261  mpf_sqrt( *tmp->_mpfp(), *a.mpfp() );
262  return *tmp;
263}
264gmp_float sin( const gmp_float & a )
265{
266  gmp_float *tmp= new gmp_float( sin((double)a) );
267  return *tmp;
268}
269gmp_float cos( const gmp_float & a )
270{
271  gmp_float *tmp= new gmp_float( cos((double)a) );
272  return *tmp;
273}
274gmp_float log( const gmp_float & a )
275{
276  gmp_float *tmp= new gmp_float( log((double)a) );
277  return *tmp;
278}
279gmp_float hypot( const gmp_float & a, const gmp_float & b )
280{
281#if 1
282  gmp_float *tmp= new gmp_float();
283  *tmp= sqrt( (a*a) + (b*b) );
284  return *tmp;
285#else
286  gmp_float *tmp= new gmp_float( hypot( (double)a, (double)b ) );
287  return *tmp;
288#endif
289}
290gmp_float exp( const gmp_float & a )
291{
292  gmp_float *tmp= new gmp_float( exp((double)a) );
293  return *tmp;
294}
295gmp_float max( const gmp_float & a, const gmp_float & b )
296{
297  gmp_float *tmp= new gmp_float();
298  a > b ? *tmp= a : *tmp= b;
299  return *tmp;
300}
301//
302// number to float, number = Q, R, C
303// makes a COPY of num! (Ist das gut?)
304//
305gmp_float numberToFloat( number num )
306{
307  gmp_float r;
308
309  if ( rField_is_Q() )
310  {
311    if ( num != NULL )
312    {
313      if (SR_HDL(num) & SR_INT)
314      {
315        r= SR_TO_INT(num);
316      }
317      else
318      {
319        if ( num->s == 0 )
320        {
321          nlNormalize( num );
322        }
323        if (SR_HDL(num) & SR_INT)
324        {
325          r= SR_TO_INT(num);
326        }
327        else
328        {
329          if ( num->s != 3 )
330          {
331            r= &num->z;
332            r/= (gmp_float)&num->n;
333          }
334          else
335          {
336            r= &num->z;
337          }
338        }
339      }
340    }
341    else
342    {
343      r= 0.0;
344    }
345  }
346  else if (rField_is_long_R() || rField_is_long_C())
347  {
348    r= *(gmp_float*)num;
349  }
350  else if ( rField_is_R() )
351  {
352    // Add some code here :-)
353    WerrorS("Ground field not implemented!");
354  }
355  else
356  {
357    WerrorS("Ground field not implemented!");
358  }
359
360  return r;
361}
362
363gmp_float numberFieldToFloat( number num, int k )
364{
365  gmp_float r;
366
367  switch (k) 
368  {
369  case QTOF:
370    if ( num != NULL )
371    {
372      if (SR_HDL(num) & SR_INT)
373      {
374        r= SR_TO_INT(num);
375      }
376      else
377      {
378        if ( num->s == 0 )
379        {
380          nlNormalize( num );
381        }
382        if (SR_HDL(num) & SR_INT)
383        {
384          r= SR_TO_INT(num);
385        }
386        else
387        {
388          if ( num->s != 3 )
389          {
390            r= &num->z;
391            r/= (gmp_float)&num->n;
392          }
393          else
394          {
395            r= &num->z;
396          }
397        }
398      }
399    }
400    else
401    {
402      r= 0.0;
403    }
404    break;
405  case RTOF:
406    r= *(gmp_float*)num;
407    break;
408  case CTOF:
409    WerrorS("Can not map from field C to field R!");
410    break;
411  case ZTOF:
412  default:
413    WerrorS("Ground field not implemented!");
414  } // switch
415
416  return r;
417}
418
419// Do some strange things with the mantissa string and the exponent
420// to get some nice output string.
421char *nicifyFloatStr( char * in, mp_exp_t exponent, size_t oprec, int *size, int thesign )
422{
423  char *out;
424
425  int sign= (in[0] == '-') ? 1 : 0;
426  char csign[2];
427
428  switch (thesign)
429  {
430    case SIGN_PLUS:
431      sign ? strcpy(csign,"-") : strcpy(csign,"+");  //+123, -123
432      break;
433    case SIGN_SPACE:
434      sign ? strcpy(csign,"-") : strcpy(csign," ");  // 123, -123
435      break;
436    case SIGN_EMPTY:
437    default:
438      sign ? strcpy(csign,"-") : strcpy(csign,"");   //123, -123
439      break;
440  }
441
442  if ( strlen(in) == 0 )
443  {
444    *size= 2*sizeof(char);
445    return omStrDup("0");
446  }
447
448  if ( ((unsigned int)ABS(exponent) <= oprec)
449       /*|| (exponent+sign >= (int)strlen(in))*/ )
450  {
451    if ( exponent+sign < (int)strlen(in) )
452    {
453      int eexponent= (exponent >= 0) ? 0 : -exponent;
454      int eeexponent= (exponent >= 0) ? exponent : 0;
455      *size= (strlen(in)+15+eexponent) * sizeof(char);
456      out= (char*)omAlloc(*size);
457      memset(out,0,*size);
458
459      strcpy(out,csign);
460      strncat(out,in+sign,eeexponent);
461
462      if (exponent == 0)
463        strcat(out,"0.");
464      else if ( exponent > 0 )
465        strcat(out,".");
466      else
467      {
468        strcat(out,"0.");
469        memset(out+strlen(out),'0',eexponent);
470      }
471      strcat(out,in+sign+eeexponent);
472    }
473    else if ( exponent+sign > (int)strlen(in) )
474    {
475      *size= (strlen(in)+exponent+12)*sizeof(char);
476      out= (char*)omAlloc(*size);
477      memset(out,0,*size);
478      sprintf(out,"%s%s",csign,in+sign);
479      memset(out+strlen(out),'0',exponent-strlen(in)+sign);
480    }
481    else
482    {
483      *size= (strlen(in)+2) * sizeof(char) + 10;
484      out= (char*)omAlloc(*size);
485      memset(out,0,*size);
486      sprintf(out,"%s%s",csign,in+sign);
487    }
488  }
489  else
490  {
491//      if ( exponent > 0 )
492//      {
493      int c=1,d=10;
494      while ( exponent / d > 0 )
495      { // count digits
496        d*=10;
497        c++;
498      }
499      *size= (strlen(in)+12+c) * sizeof(char) + 10;
500      out= (char*)omAlloc(*size);
501      memset(out,0,*size);
502      sprintf(out,"%s0.%se%s%d",csign,in+sign,exponent>=0?"+":"",(int)exponent);
503//      }
504//      else
505//      {
506//        *size=2;
507//        out= (char*)omAlloc(*size);
508//        strcpy(out,"0");
509//      }
510  }
511  return out;
512}
513
514char *floatToStr( const gmp_float & r, const unsigned int oprec )
515{
516#if 1
517  mp_exp_t exponent;
518  int size,insize;
519  char *nout,*out,*in;
520
521  insize= (oprec+2) * sizeof(char) + 10;
522  in= (char*)omAlloc( insize );
523
524  mpf_get_str(in,&exponent,10,oprec,*(r.mpfp()));
525
526  if ( (exponent > 0)
527  && (exponent < (int)oprec)
528  && (strlen(in)-(in[0]=='-'?1:0) == oprec) )
529  {
530    omFree( (ADDRESS) in );
531    insize= (exponent+oprec+2) * sizeof(char) + 10;
532    in= (char*)omAlloc( insize );
533    int newprec= exponent+oprec;
534    mpf_get_str(in,&exponent,10,newprec,*(r.mpfp()));
535  }
536  nout= nicifyFloatStr( in, exponent, oprec, &size, SIGN_EMPTY );
537  omFree( (ADDRESS) in );
538  out= (char*)omAlloc( (strlen(nout)+1) * sizeof(char) );
539  strcpy( out, nout );
540  omFree( (ADDRESS) nout );
541
542  return out;
543#else
544  // for testing purpose...
545  char *out= (char*)omAlloc( (1024) * sizeof(char) );
546  sprintf(out,"% .10f",(double)r);
547  return out;
548#endif
549}
550//<-
551
552//-> gmp_complex::*
553// <gmp_complex> = <gmp_complex> operator <gmp_complex>
554//
555gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b )
556{
557  return gmp_complex( a.r + b.r, a.i + b.i );
558}
559gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b )
560{
561  return gmp_complex( a.r - b.r, a.i - b.i );
562}
563gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b )
564{
565  return gmp_complex( a.r * b.r - a.i * b.i,
566                  a.r * b.i + a.i * b.r);
567}
568gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b )
569{
570  gmp_float d = b.r*b.r + b.i*b.i;
571  return gmp_complex( (a.r * b.r + a.i * b.i) / d,
572                (a.i * b.r - a.r * b.i) / d);
573}
574
575// <gmp_complex> operator <gmp_complex>
576//
577gmp_complex & gmp_complex::operator += ( const gmp_complex & b )
578{
579  r+=b.r;
580  i+=b.i;
581  return *this;
582}
583gmp_complex & gmp_complex::operator -= ( const gmp_complex & b )
584{
585  r-=b.r;
586  i-=b.i;
587  return *this;
588}
589gmp_complex & gmp_complex::operator *= ( const gmp_complex & b )
590{
591  gmp_float f = r * b.r - i * b.i;
592  i = r * b.i + i * b.r;
593  r = f;
594  return *this;
595}
596gmp_complex & gmp_complex::operator /= ( const gmp_complex & b )
597{
598  gmp_float d = b.r*b.r + b.i*b.i;
599  r = (r * b.r + i * b.i) / d;
600  i = (i * b.r - r * b.i) / d;
601  return *this;
602}
603
604// Returns square root of gmp_complex number
605//
606gmp_complex sqrt( const gmp_complex & x )
607{
608  gmp_float r = abs(x);
609  gmp_float nr, ni;
610  if (r == (gmp_float) 0.0)
611  {
612    nr = ni = r;
613  }
614  else if ( x.real() > (gmp_float)0)
615  {
616    nr = sqrt((gmp_float)0.5 * (r + x.real()));
617    ni = x.imag() / nr / (gmp_float)2;
618  }
619  else
620  {
621    ni = sqrt((gmp_float)0.5 * (r - x.real()));
622    if (x.imag() < (gmp_float)0)
623    {
624      ni = - ni;
625    }
626    nr = x.imag() / ni / (gmp_float)2;
627  }
628  gmp_complex *tmp= new gmp_complex(nr, ni);
629  return *tmp;
630}
631
632// converts a gmp_complex to a string ( <real part> + I * <imaginary part> )
633//
634char *complexToStr( gmp_complex & c, const unsigned int oprec )
635{
636  char *out,*in_imag,*in_real;
637
638  c.SmallToZero();
639  if ( !c.imag().isZero() )
640  {
641
642    in_real=floatToStr( c.real(), oprec );         // get real part
643    in_imag=floatToStr( abs(c.imag()), oprec );    // get imaginary part
644
645    if (rField_is_long_C())
646    {
647      int len=(strlen(in_real)+strlen(in_imag)+7+strlen(currRing->parameter[0]))*sizeof(char);
648      out=(char*)omAlloc(len);
649      memset(out,0,len);
650      if (  !c.real().isZero() )  // (-23-i*5.43) or (15.1+i*5.3)
651        sprintf(out,"(%s%s%s*%s)",in_real,c.imag().sign()>=0?"+":"-",currRing->parameter[0],in_imag);
652      else // (-i*43) or (i*34)
653      {
654        if (c.imag().isOne())
655          sprintf(out,currRing->parameter[0]);
656        else if (c.imag().isMOne())
657          sprintf(out,"-%s",currRing->parameter[0]);
658        else
659          sprintf(out,"(%s%s*%s)",c.imag().sign()>=0?"":"-",currRing->parameter[0],in_imag);
660      }
661    }
662    else
663    {
664      int len=(strlen(in_real)+strlen(in_imag)+9) * sizeof(char);
665      out=(char*)omAlloc( len );
666      memset(out,0,len);
667      if ( !c.real().isZero() ) 
668        sprintf(out,"(%s%s%s)",in_real,c.imag().sign()>=0?"+I*":"-I*",in_imag);
669      else
670        sprintf(out,"(%s%s)",c.imag().sign()>=0?"I*":"-I*",in_imag);
671    }
672    omFree( (ADDRESS) in_real );
673    omFree( (ADDRESS) in_imag );
674  }
675  else 
676  {
677    out= floatToStr( c.real(), oprec );
678  }
679
680  return out;
681}
682//<-
683
684bool complexNearZero( gmp_complex * c, int digits )
685{
686  gmp_float eps,epsm;
687
688  if ( digits < 1 ) return true;
689
690  eps=pow(10.0,(int)digits);
691  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
692  eps=(gmp_float)1.0/eps;
693  epsm=-eps;
694
695  //Print("eps: %s\n",floatToStr(eps,gmp_output_digits));
696
697  if ( c->real().sign() > 0 ) // +
698    return (c->real() < eps && (c->imag() < eps && c->imag() > epsm));
699  else // -
700    return (c->real() > epsm && (c->imag() < eps && c->imag() > epsm));
701}
702
703void gmp_complex::SmallToZero()
704{
705  gmp_float ar=this->real();
706  gmp_float ai=this->imag();
707  if (ar.isZero() || ai.isZero()) return;
708  mpf_abs((mpf_ptr)*ar.mpfp(), (mpf_ptr)*ar.mpfp());
709  mpf_abs((mpf_ptr)*ai.mpfp(), (mpf_ptr)*ai.mpfp());
710  mpf_set_prec((mpf_ptr)*ar.mpfp(), 32);
711  mpf_set_prec((mpf_ptr)*ai.mpfp(), 32);
712  if (ar > ai)
713  {
714    mpf_div((mpf_ptr)*ai.mpfp(), (mpf_ptr)*ai.mpfp(), (mpf_ptr)*ar.mpfp());
715    if (ai < gmpRel) this->imag(0.0);
716  }
717  else
718  {
719    mpf_div((mpf_ptr)*ar.mpfp(), (mpf_ptr)*ar.mpfp(), (mpf_ptr)*ai.mpfp());
720    if (ar < gmpRel) this->real(0.0);
721  }
722}
723
724//%e
725
726//#endif // HAVE_MPR
727
728// local Variables: ***
729// folded-file: t ***
730// compile-command-1: "make installg" ***
731// compile-command-2: "make install" ***
732// End: ***
Note: See TracBrowser for help on using the repository browser.