source: git/kernel/mpr_complex.cc @ 8627ad

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