source: git/Singular/mpr_complex.cc @ 28e0ac8

fieker-DuValspielwiese
Last change on this file since 28e0ac8 was a3bc95e, checked in by Hans Schönemann <hannes@…>, 23 years ago
*hannes: namespaces ->ns git-svn-id: file:///usr/local/Singular/svn/trunk@5651 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.32 2001-10-09 16:36:10 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 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;
257  mpf_abs( *(tmp._mpfp()), *a.mpfp() );
258  return tmp;
259}
260gmp_float sqrt( const gmp_float & a )
261{
262  gmp_float tmp;
263  mpf_sqrt( *(tmp._mpfp()), *a.mpfp() );
264  return tmp;
265}
266gmp_float sin( const gmp_float & a )
267{
268  gmp_float tmp( sin((double)a) );
269  return tmp;
270}
271gmp_float cos( const gmp_float & a )
272{
273  gmp_float tmp( cos((double)a) );
274  return tmp;
275}
276gmp_float log( const gmp_float & a )
277{
278  gmp_float tmp( log((double)a) );
279  return tmp;
280}
281gmp_float hypot( const gmp_float & a, const gmp_float & b )
282{
283#if 1
284  return ( sqrt( (a*a) + (b*b) ) );
285#else
286  gmp_float tmp( hypot( (double)a, (double)b ) );
287  return tmp;
288#endif
289}
290gmp_float exp( const gmp_float & a )
291{
292  gmp_float tmp( exp((double)a) );
293  return tmp;
294}
295gmp_float max( const gmp_float & a, const gmp_float & b )
296{
297  gmp_float tmp;
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(*ar._mpfp(), *ar._mpfp());
709  mpf_abs(*ai._mpfp(), *ai._mpfp());
710  mpf_set_prec(*ar._mpfp(), 32);
711  mpf_set_prec(*ai._mpfp(), 32);
712  if (ar > ai)
713  {
714    mpf_div(*ai._mpfp(), *ai._mpfp(), *ar._mpfp());
715    if (ai < gmpRel) this->imag(0.0);
716  }
717  else
718  {
719    mpf_div(*ar._mpfp(), *ar._mpfp(), *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.