Changeset f543de1 in git
- Timestamp:
- Jun 24, 1999, 9:46:51 AM (24 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
- Children:
- e1eec6fcd177391b1ba1d2a6030f298f404ae5f9
- Parents:
- 6b9e9c662b8c35931dfedb7053c0c6d2597e5f68
- Location:
- Singular
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/gnumpc.h
r6b9e9c rf543de1 4 4 * Computer Algebra System SINGULAR * 5 5 ****************************************/ 6 /* $Id: gnumpc.h,v 1. 1 1999-05-11 15:42:37 SingularExp $ */6 /* $Id: gnumpc.h,v 1.2 1999-06-24 07:46:49 wenk Exp $ */ 7 7 /* 8 8 * ABSTRACT: computations with GMP floating-point numbers -
Singular/gnumpfl.h
r6b9e9c rf543de1 4 4 * Computer Algebra System SINGULAR * 5 5 ****************************************/ 6 /* $Id: gnumpfl.h,v 1. 2 1999-05-10 15:10:50 SingularExp $ */6 /* $Id: gnumpfl.h,v 1.3 1999-06-24 07:46:49 wenk Exp $ */ 7 7 /* 8 8 * ABSTRACT: computations with GMP floating-point numbers … … 40 40 BOOLEAN ngfSetMap(int c, char ** par, int nop, number minpol); 41 41 42 42 void setGMPFloatDigits( size_t digits ); 43 43 void setGMPFloatPrecBytes( unsigned long int bytes ); 44 44 #endif -
Singular/mpr_complex.cc
r6b9e9c rf543de1 2 2 * Computer Algebra System SINGULAR * 3 3 ****************************************/ 4 /* $Id: mpr_complex.cc,v 1. 4 1999-05-17 11:31:47 SingularExp $ */4 /* $Id: mpr_complex.cc,v 1.5 1999-06-24 07:46:50 wenk Exp $ */ 5 5 6 6 /* … … 9 9 * 10 10 */ 11 12 // WARNING! ALWAYS use AllocL and FreeL when alloc. memory for some char* !! 11 13 12 14 #include "mod2.h" … … 20 22 #include "mpr_complex.h" 21 23 24 //%s 22 25 // this was copied form longrat0.cc 26 // and will be used in numberToFloat. 27 // Make sure that it is up to date!! 23 28 #define SR_HDL(A) ((long)(A)) 24 29 #define SR_INT 1 … … 33 38 size_t gmp_output_digits= DEFPREC; 34 39 35 const gmp_float gmpOne= 1; 36 const gmp_float gmpMOne= -1; 37 const gmp_float gmpZero= 0; 38 39 40 //-> setGMPFloat* 40 41 /** Set size of mantissa to <bytes> bytes and guess the number of 41 42 * output digits. … … 63 64 } 64 65 66 // Sets the lenght of the mantissa to <digits> digits 65 67 void setGMPFloatDigits( size_t digits ) 66 68 { 67 69 size_t bits= 1 + (size_t) (digits / (log(2)/log(10))); 68 70 bits= bits>64?bits:64; 69 gmp_float::setPrecision( bits+EXTRABYTES*8 ); 71 // gmp_float::setPrecision( bits+EXTRABYTES*8 ); 72 gmp_float::setPrecision( bits+(bits/2) ); 70 73 gmp_float::setEqualBits( bits ); 71 74 gmp_output_digits= digits; … … 76 79 return gmp_output_digits; 77 80 } 78 79 //------------------------------------- gmp_float ---------------------------------------------- 81 //<- 80 82 81 83 //-> gmp_float::* … … 83 85 unsigned long int gmp_float::gmp_needequal_bits= GMP_NEEDEQUAL_BITS; 84 86 85 gmp_float::gmp_float( const int v )86 {87 mpf_init2( t, gmp_default_prec_bits );88 mpf_set_si( t, (signed long int) v );89 }90 gmp_float::gmp_float( const long v )91 {92 mpf_init2( t, gmp_default_prec_bits );93 mpf_set_si( t, (signed long int) v );94 }95 gmp_float::gmp_float( const mprfloat v )96 {97 mpf_init2( t, gmp_default_prec_bits );98 mpf_set_d( t, (double) v );99 }100 gmp_float::gmp_float( const mpf_t v )101 {102 mpf_init2( t, gmp_default_prec_bits );103 mpf_set( t, v );104 }105 gmp_float::gmp_float( const mpz_t v )106 {107 mpf_init2( t, gmp_default_prec_bits );108 mpf_set_z( t, v );109 }110 gmp_float::gmp_float( const gmp_float & v )111 {112 //mpf_init2( t, mpf_get_prec( v.t ) );113 mpf_init2( t, gmp_default_prec_bits );114 mpf_set( t, v.t );115 }116 117 gmp_float::~gmp_float()118 {119 mpf_clear( t );120 }121 122 87 // <gmp_float> = <gmp_float> operator <gmp_float> 123 88 gmp_float operator + ( const gmp_float & a, const gmp_float & b ) … … 146 111 } 147 112 148 // <gmp_float> operator <gmp_float>149 gmp_float & gmp_float::operator += ( const gmp_float & a )150 {151 mpf_add( t, t, a.t );152 return *this;153 }154 gmp_float & gmp_float::operator -= ( const gmp_float & a )155 {156 mpf_sub( t, t, a.t );157 return *this;158 }159 gmp_float & gmp_float::operator *= ( const gmp_float & a )160 {161 mpf_mul( t, t, a.t );162 return *this;163 }164 gmp_float & gmp_float::operator /= ( const gmp_float & a )165 {166 mpf_div( t, t, a.t );167 return *this;168 }169 170 113 // <gmp_float> == <gmp_float> ?? up to the first gmp_float::gmp_needequal_bits bits 171 114 bool operator == ( const gmp_float & a, const gmp_float & b ) … … 199 142 } 200 143 201 // <gmp_float> = <*>202 gmp_float & gmp_float::operator = ( const gmp_float & a )203 {204 mpf_set( t, a.t );205 return *this;206 }207 gmp_float & gmp_float::operator = ( const mpz_t & a )208 {209 mpf_set_z( t, a );210 return *this;211 }212 gmp_float & gmp_float::operator = ( const mprfloat a )213 {214 mpf_set_d( t, (double) a );215 return *this;216 }217 gmp_float & gmp_float::operator = ( const long a )218 {219 mpf_set_si( t, (signed long int) a );220 return *this;221 }222 223 // cast to double224 gmp_float::operator double()225 {226 return mpf_get_d( t );227 }228 gmp_float::operator double() const229 {230 return mpf_get_d( t );231 }232 233 // cast to int234 gmp_float::operator int()235 {236 return (int)mpf_get_d( t );237 }238 gmp_float::operator int() const239 {240 return (int)mpf_get_d( t );241 }242 243 // get sign of real number ( -1: t < 0; 0: t==0; 1: t > 0 )244 int gmp_float::sign()245 {246 return mpf_sgn( t );247 }248 // t == 0 ?249 bool gmp_float::isZero()250 {251 #ifdef VARIANTE_1252 return (mpf_sgn( t ) == 0);253 #else254 return mpf_eq( t , gmpZero.t , gmp_float::gmp_needequal_bits );255 #endif256 }257 // t == 1 ?258 bool gmp_float::isOne()259 {260 #ifdef VARIANTE_1261 return (mpf_cmp_ui( t , 1 ) == 0);262 #else263 return mpf_eq( t , gmpOne.t , gmp_float::gmp_needequal_bits );264 #endif265 }266 // t == -1 ?267 bool gmp_float::isMOne()268 {269 #ifdef VARIANTE_1270 return (mpf_cmp_si( t , -1 ) == 0);271 #else272 return mpf_eq( t , gmpMOne.t , gmp_float::gmp_needequal_bits );273 #endif274 }275 276 bool gmp_float::setFromStr( char * in )277 {278 return ( mpf_set_str( t, in, 10 ) == 0 );279 }280 281 // access pointer282 const mpf_t *gmp_float::mpfp() const283 {284 return &t;285 }286 287 144 gmp_float abs( const gmp_float & a ) 288 145 { … … 335 192 } 336 193 // 337 // WARNING:338 // supports only Q to float !!!194 // number to float, number = Q, R, C 195 // makes a COPY of num! (Ist das gut?) 339 196 // 340 197 gmp_float numberToFloat( number num ) … … 342 199 gmp_float r; 343 200 344 //Print("numberToFloat: ");nPrint(num); 345 346 if ( num != NULL ) 347 { 348 if (SR_HDL(num) & SR_INT) 349 { 350 r= SR_TO_INT(num); 351 } 201 if ( rField_is_Q() ) { 202 if ( num != NULL ) 203 { 204 if (SR_HDL(num) & SR_INT) 205 { 206 r= SR_TO_INT(num); 207 } 208 else 209 { 210 if ( num->s == 0 ) 211 { 212 nlNormalize( num ); 213 } 214 if (SR_HDL(num) & SR_INT) 215 { 216 r= SR_TO_INT(num); 217 } 218 else 219 { 220 if ( num->s != 3 ) 221 { 222 r= &num->z; 223 r/= (gmp_float)&num->n; 224 } 225 else 226 { 227 r= &num->z; 228 } 229 } 230 } 231 } 352 232 else 353 {354 if ( num->s == 0 )355 233 { 356 nlNormalize( num );234 r= 0.0; 357 235 } 358 if (SR_HDL(num) & SR_INT) 359 { 360 r= SR_TO_INT(num); 361 } 362 else 363 { 364 if ( num->s != 3 ) 365 { 366 r= &num->z; 367 r/= (mprfloat_g)&num->n; 368 } 369 else 370 { 371 r= &num->z; 372 } 373 } 374 } 375 } 376 else 377 { 378 r= 0.0; 379 } 380 381 //Print(" --> %s ",floatToStr(r,10));PrintLn(); 236 } else if (rField_is_long_R() || rField_is_long_C()) { 237 r= *(gmp_float*)num; 238 } else if ( rField_is_R() ) { 239 // Add some code here :-) 240 Werror("Wrong field!"); 241 } else { 242 Werror("Wrong field!"); 243 } 382 244 383 245 return r; … … 396 258 { 397 259 case SIGN_PLUS: 398 sign ? strcpy(csign,"-") : strcpy(csign,"+"); 260 sign ? strcpy(csign,"-") : strcpy(csign,"+"); //+123, -123 399 261 break; 400 262 case SIGN_SPACE: 401 sign ? strcpy(csign,"-") : strcpy(csign," "); 263 sign ? strcpy(csign,"-") : strcpy(csign," "); // 123, -123 402 264 break; 403 265 case SIGN_EMPTY: 404 266 default: 405 sign ? strcpy(csign,"-") : strcpy(csign,""); 267 sign ? strcpy(csign,"-") : strcpy(csign,""); //123, -123 406 268 break; 407 269 } … … 409 271 if ( strlen(in) == 0 ) 410 272 { 411 out= (char*)Alloc0( 2*sizeof(char) );412 *size= 2*sizeof(char);273 *size= 2*sizeof(char)+10; 274 out= (char*)AllocL( *size ); 413 275 strcpy(out,"0"); 414 276 return out; … … 418 280 /*|| (exponent+sign >= (int)strlen(in))*/ ) 419 281 { 420 #ifdef mprDEBUG_ALL421 Print(" no exponent %d %d\n",abs(exponent),oprec);422 #endif423 282 if ( exponent+sign < (int)strlen(in) ) 424 283 { 425 284 int eexponent= (exponent >= 0) ? 0 : -exponent; 426 285 int eeexponent= (exponent >= 0) ? exponent : 0; 427 *size= (strlen(in)+5+eexponent) * sizeof(char); 428 out= (char*)Alloc0(*size); 286 *size= (strlen(in)+5+eexponent) * sizeof(char) + 10; 287 out= (char*)AllocL(*size); 288 memset(out,'\0',*size); 429 289 430 290 strcpy(out,csign); … … 444 304 else if ( exponent+sign > (int)strlen(in) ) 445 305 { 446 *size= (strlen(in)+exponent+2)*sizeof(char) ;447 out= (char*)Alloc 0(*size);306 *size= (strlen(in)+exponent+2)*sizeof(char)+10; 307 out= (char*)AllocL(*size); 448 308 sprintf(out,"%s%s",csign,in+sign); 449 309 memset(out+strlen(out),'0',exponent-strlen(in)+sign); … … 451 311 else 452 312 { 453 *size= (strlen(in)+2) * sizeof(char) ;454 out= (char*)Alloc 0(*size);313 *size= (strlen(in)+2) * sizeof(char) + 10; 314 out= (char*)AllocL(*size); 455 315 sprintf(out,"%s%s",csign,in+sign); 456 316 } … … 458 318 else 459 319 { 460 #ifdef mprDEBUG_ALL 461 Print(" exponent %d %d\n",exponent,oprec); 462 #endif 463 if ( exponent > 0 ) 464 { 320 // if ( exponent > 0 ) 321 // { 465 322 int c=1,d=10; 466 323 while ( exponent / d > 0 ) … … 469 326 c++; 470 327 } 471 *size= (strlen(in)+12+c) * sizeof(char) ;472 out= (char*)Alloc 0(*size);328 *size= (strlen(in)+12+c) * sizeof(char) + 10; 329 out= (char*)AllocL(*size); 473 330 sprintf(out,"%s0.%se%d",csign,in+sign,(unsigned int)exponent); 474 }475 else476 {477 *size=2;478 out= (char*)Alloc0(*size);479 strcpy(out,"0");480 }331 // } 332 // else 333 // { 334 // *size=2; 335 // out= (char*)AllocL(*size); 336 // strcpy(out,"0"); 337 // } 481 338 } 482 339 return out; … … 490 347 char *nout,*out,*in; 491 348 492 insize= (oprec+2) * sizeof(char) ;493 in= (char*)Alloc 0( insize );349 insize= (oprec+2) * sizeof(char) + 10; 350 in= (char*)AllocL( insize ); 494 351 495 352 mpf_get_str(in,&exponent,10,oprec,*(r.mpfp())); 353 496 354 if ( (exponent > 0) 497 355 && (exponent < (int)oprec) 498 356 && (strlen(in)-(in[0]=='-'?1:0) == oprec) ) 499 357 { 500 in= (char*)ReAlloc( in, insize, (exponent+oprec+2) * sizeof(char) ); 501 insize= (exponent+oprec+2) * sizeof(char); 358 FreeL( (ADDRESS) in ); 359 insize= (exponent+oprec+2) * sizeof(char) + 10; 360 in= (char*)AllocL( insize ); 502 361 int newprec= exponent+oprec; 503 362 mpf_get_str(in,&exponent,10,newprec,*(r.mpfp())); 504 363 } 505 364 nout= nicifyFloatStr( in, exponent, oprec, &size, SIGN_EMPTY ); 506 Free ( (ADDRESS) in, insize);507 out= (char*)Alloc 0( (strlen(nout)+1) * sizeof(char) );365 FreeL( (ADDRESS) in ); 366 out= (char*)AllocL( (strlen(nout)+1) * sizeof(char) ); 508 367 strcpy( out, nout ); 509 Free( (ADDRESS) nout, size ); 368 FreeL( (ADDRESS) nout ); 369 510 370 return out; 511 371 #else 512 char *out= (char*)Alloc0( (1024) * sizeof(char) ); 372 // for testing purpose... 373 char *out= (char*)AllocL( (1024) * sizeof(char) ); 513 374 sprintf(out,"% .10f",(double)r); 514 375 return out; … … 517 378 //<- 518 379 519 //------------------------------------- complex ------------------------------------------------ 520 521 //-> complex::* 522 // constructors 380 //-> gmp_complex::* 381 // <gmp_complex> = <gmp_complex> operator <gmp_complex> 523 382 // 524 complex::complex( const mprfloat_g re, const mprfloat_g im ) 525 { 526 r= re; 527 i= im; 528 } 529 complex::complex( const mprfloat re, const mprfloat im ) 530 { 531 r= re; 532 i= im; 533 } 534 complex::complex( const long re, const long im ) 535 { 536 r= re; 537 i= im; 538 } 539 complex::complex( const complex & v ) 540 { 541 r= v.r; 542 i= v.i; 543 } 544 complex::~complex() 545 { 546 } 547 548 // <complex> = <complex> operator <complex> 549 // 550 complex operator + ( const complex & a, const complex & b ) 551 { 552 return complex( a.r + b.r, a.i + b.i ); 553 } 554 complex operator - ( const complex & a, const complex & b ) 555 { 556 return complex( a.r - b.r, a.i - b.i ); 557 } 558 complex operator * ( const complex & a, const complex & b ) 559 { 560 return complex( a.real() * b.real() - a.imag() * b.imag(), 383 gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b ) 384 { 385 return gmp_complex( a.r + b.r, a.i + b.i ); 386 } 387 gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b ) 388 { 389 return gmp_complex( a.r - b.r, a.i - b.i ); 390 } 391 gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b ) 392 { 393 return gmp_complex( a.real() * b.real() - a.imag() * b.imag(), 561 394 a.real() * b.imag() + a.imag() * b.real()); 562 395 } 563 complex operator / ( const complex & a, constcomplex & b )564 { 565 mprfloat_gar = abs(b.real());566 mprfloat_gai = abs(b.imag());567 mprfloat_gnr, ni, t, d;396 gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b ) 397 { 398 gmp_float ar = abs(b.real()); 399 gmp_float ai = abs(b.imag()); 400 gmp_float nr, ni, t, d; 568 401 if (ar <= ai) 569 402 { 570 403 t = b.real() / b.imag(); 571 d = b.imag() * (( mprfloat_g)1 + t*t);404 d = b.imag() * ((gmp_float)1 + t*t); 572 405 nr = (a.real() * t + a.imag()) / d; 573 406 ni = (a.imag() * t - a.real()) / d; … … 576 409 { 577 410 t = b.imag() / b.real(); 578 d = b.real() * (( mprfloat_g)1 + t*t);411 d = b.real() * ((gmp_float)1 + t*t); 579 412 nr = (a.real() + a.imag() * t) / d; 580 413 ni = (a.imag() - a.real() * t) / d; 581 414 } 582 return complex( nr, ni );583 } 584 585 // < complex> = <complex> operator <mprfloat_g>415 return gmp_complex( nr, ni ); 416 } 417 418 // <gmp_complex> operator <gmp_complex> 586 419 // 587 complex operator + ( const complex & a, const mprfloat_g b_d ) 588 { 589 return complex( a.r + b_d, a.i ); 590 } 591 complex operator - ( const complex & a, const mprfloat_g b_d ) 592 { 593 return complex( a.r - b_d, a.i ); 594 } 595 complex operator * ( const complex & a, const mprfloat_g b_d ) 596 { 597 return complex( a.r * b_d, a.i * b_d ); 598 } 599 complex operator / ( const complex & a, const mprfloat_g b_d ) 600 { 601 return complex( a.r / b_d, a.i / b_d ); 602 } 603 604 // <complex> operator <complex> 605 // 606 complex & complex::operator += ( const complex & b ) 420 gmp_complex & gmp_complex::operator += ( const gmp_complex & b ) 607 421 { 608 422 r+=b.r; … … 610 424 return *this; 611 425 } 612 complex & complex::operator -= ( constcomplex & b )426 gmp_complex & gmp_complex::operator -= ( const gmp_complex & b ) 613 427 { 614 428 r-=b.r; … … 616 430 return *this; 617 431 } 618 complex & complex::operator *= ( constcomplex & b )619 { 620 mprfloat_gf = r * b.r - i * b.i;432 gmp_complex & gmp_complex::operator *= ( const gmp_complex & b ) 433 { 434 gmp_float f = r * b.r - i * b.i; 621 435 i = r * b.i + i * b.r; 622 436 r = f; 623 437 return *this; 624 438 } 625 complex & complex::operator /= ( constcomplex & b )626 { 627 mprfloat_gar = abs(b.r);628 mprfloat_gai = abs(b.i);629 mprfloat_gnr, ni, t, d;439 gmp_complex & gmp_complex::operator /= ( const gmp_complex & b ) 440 { 441 gmp_float ar = abs(b.r); 442 gmp_float ai = abs(b.i); 443 gmp_float nr, ni, t, d; 630 444 if (ar <= ai) 631 445 { 632 446 t = b.r / b.i; 633 d = b.i * (( mprfloat_g)1 + t*t);447 d = b.i * ((gmp_float)1 + t*t); 634 448 nr = (r * t + i) / d; 635 449 ni = (i * t - r) / d; … … 638 452 { 639 453 t = b.i / b.r; 640 d = b.r * (( mprfloat_g)1 + t*t);454 d = b.r * ((gmp_float)1 + t*t); 641 455 nr = (r + i * t) / d; 642 456 ni = (i - r * t) / d; … … 647 461 } 648 462 649 // <complex> == <complex> ? 650 bool operator == ( const complex & a, const complex & b ) 651 { 652 return ( b.real() == a.real() ) && ( b.imag() == a.imag() ); 653 } 654 655 // <complex> = <complex> 656 complex & complex::operator = ( const complex & a ) 657 { 658 r= a.r; 659 i= a.i; 660 return *this; 661 } 662 663 // Returns absolute value of a complex number 463 // Returns square root of gmp_complex number 664 464 // 665 mprfloat_g abs( const complex & c ) 666 { 667 return hypot(c.real(),c.imag()); 668 } 669 670 // Returns square root of complex number 671 // 672 complex sqrt( const complex & x ) 673 { 674 mprfloat_g r = abs(x); 675 mprfloat_g nr, ni; 676 if (r == (mprfloat_g) 0.0) 465 gmp_complex sqrt( const gmp_complex & x ) 466 { 467 gmp_float r = abs(x); 468 gmp_float nr, ni; 469 if (r == (gmp_float) 0.0) 677 470 { 678 471 nr = ni = r; 679 472 } 680 else if ( x.real() > ( mprfloat_g)0)681 { 682 nr = sqrt(( mprfloat_g)0.5 * (r + x.real()));683 ni = x.imag() / nr / ( mprfloat_g)2;473 else if ( x.real() > (gmp_float)0) 474 { 475 nr = sqrt((gmp_float)0.5 * (r + x.real())); 476 ni = x.imag() / nr / (gmp_float)2; 684 477 } 685 478 else 686 479 { 687 ni = sqrt(( mprfloat_g)0.5 * (r - x.real()));688 if (x.imag() < ( mprfloat_g)0)480 ni = sqrt((gmp_float)0.5 * (r - x.real())); 481 if (x.imag() < (gmp_float)0) 689 482 { 690 483 ni = - ni; 691 484 } 692 nr = x.imag() / ni / ( mprfloat_g)2;693 } 694 complex *tmp= newcomplex(nr, ni);695 return *tmp; 696 } 697 698 // converts a number to a complex485 nr = x.imag() / ni / (gmp_float)2; 486 } 487 gmp_complex *tmp= new gmp_complex(nr, ni); 488 return *tmp; 489 } 490 491 // converts a gmp_complex to a string ( <real part> + I * <imaginary part> ) 699 492 // 700 char *complexToStr( const complex & c, const size_t oprec )493 char *complexToStr( const gmp_complex & c, const size_t oprec ) 701 494 { 702 495 char *out,*in_imag,*in_real; … … 706 499 in_real=floatToStr( c.real(), oprec ); // get real part 707 500 in_imag=floatToStr( abs(c.imag()), oprec ); // get imaginary part 708 out= (char*)Alloc0( (strlen(in_real)+strlen(in_imag)+6) * sizeof(char) ); 709 sprintf(out,"%s%s%s",in_real,c.imag().sign() >= 0 ? " + i ":" - i ",in_imag); 710 Free( in_real, (strlen(in_real)+1)*sizeof(char) ); 711 Free( in_imag, (strlen(in_imag)+1)*sizeof(char) ); 501 502 if (rField_is_long_C()) { 503 out=(char*)AllocL((strlen(in_real)+strlen(in_imag)+5+strlen(currRing->parameter[0]))*sizeof(char)); 504 sprintf(out,"%s%s%s*%s",in_real,c.imag().sign()>=0?"+":"-",currRing->parameter[0],in_imag); 505 } else { 506 out=(char*)AllocL( (strlen(in_real)+strlen(in_imag)+8) * sizeof(char)); 507 sprintf(out,"%s%s%s",in_real,c.imag().sign()>=0?" + I ":" - I ",in_imag); 508 } 509 FreeL( (ADDRESS) in_real ); 510 FreeL( (ADDRESS) in_imag ); 712 511 } 713 512 else … … 718 517 } 719 518 //<- 519 //%e 720 520 721 521 //#endif // HAVE_MPR -
Singular/mpr_complex.h
r6b9e9c rf543de1 4 4 * Computer Algebra System SINGULAR * 5 5 ****************************************/ 6 /* $Id: mpr_complex.h,v 1. 3 1999-06-15 16:39:00 SingularExp $ */6 /* $Id: mpr_complex.h,v 1.4 1999-06-24 07:46:51 wenk Exp $ */ 7 7 8 8 /* … … 18 18 } 19 19 #include "numbers.h" 20 #include "ring.h" 20 21 #include "mpr_global.h" 21 22 … … 28 29 void setGMPFloatPrecBytes( unsigned long int bytes ); 29 30 unsigned long int getGMPFloatPrecBytes(); 31 30 32 void setGMPFloatDigits( size_t digits ); 31 33 size_t getGMPFloatDigits(); … … 38 40 { 39 41 public: 40 gmp_float( const mprfloat v = 0.0 ); 41 gmp_float( const int v ); 42 gmp_float( const long v ); 43 gmp_float( const mpf_t v ); 44 gmp_float( const mpz_t v ); 45 gmp_float( const gmp_float & v ); 46 47 ~gmp_float(); 42 gmp_float( const int v = 0 ) 43 { 44 mpf_init2( t, gmp_default_prec_bits ); 45 mpf_set_si( t, (signed long int) v ); 46 } 47 gmp_float( const long v ) 48 { 49 mpf_init2( t, gmp_default_prec_bits ); 50 mpf_set_si( t, (signed long int) v ); 51 } 52 gmp_float( const mprfloat v ) // double 53 { 54 mpf_init2( t, gmp_default_prec_bits ); 55 mpf_set_d( t, (double) v ); 56 } 57 gmp_float( const mpf_t v ) 58 { 59 mpf_init2( t, gmp_default_prec_bits ); 60 mpf_set( t, v ); 61 } 62 gmp_float( const mpz_t v ) // gnu mp Z 63 { 64 mpf_init2( t, gmp_default_prec_bits ); 65 mpf_set_z( t, v ); 66 } 67 gmp_float( const gmp_float & v ) // copy constructor 68 { 69 //mpf_init2( t, mpf_get_prec( v.t ) ); 70 mpf_init2( t, gmp_default_prec_bits ); 71 mpf_set( t, v.t ); 72 } 73 74 ~gmp_float() 75 { 76 mpf_clear( t ); 77 } 48 78 49 79 friend gmp_float operator + ( const gmp_float & a, const gmp_float & b ); … … 52 82 friend gmp_float operator / ( const gmp_float & a, const gmp_float & b ); 53 83 54 gmp_float & operator += ( const gmp_float & a );55 gmp_float & operator -= ( const gmp_float & a );56 gmp_float & operator *= ( const gmp_float & a );57 gmp_float & operator /= ( const gmp_float & a );84 inline gmp_float & operator += ( const gmp_float & a ); 85 inline gmp_float & operator -= ( const gmp_float & a ); 86 inline gmp_float & operator *= ( const gmp_float & a ); 87 inline gmp_float & operator /= ( const gmp_float & a ); 58 88 59 89 friend bool operator == ( const gmp_float & a, const gmp_float & b ); … … 70 100 gmp_float & operator = ( const long a ); 71 101 72 in t sign(); // t>0:+1, t==0:0, t<0:-173 bool isZero(); // t == 0 ?74 bool isOne(); // t == 1 ?75 bool isMOne(); // t == -1 ?76 77 bool setFromStr( char * in );102 inline int sign(); // t>0:+1, t==0:0, t<0:-1 103 inline bool isZero(); // t == 0 ? 104 inline bool isOne(); // t == 1 ? 105 inline bool isMOne(); // t == -1 ? 106 107 inline bool setFromStr( char * in ); 78 108 79 109 // access 80 110 inline const mpf_t *mpfp() const; 81 111 82 operator double();83 operator double() const;84 85 operator int();86 operator int() const;112 inline operator double(); 113 inline operator double() const; 114 115 inline operator int(); 116 inline operator int() const; 87 117 88 118 public: … … 104 134 }; 105 135 136 static const gmp_float gmpOne= 1; 137 static const gmp_float gmpMOne= -1; 138 static const gmp_float gmpZero= 0; 139 140 // <gmp_float> operator <gmp_float> 141 inline gmp_float & gmp_float::operator += ( const gmp_float & a ) 142 { 143 mpf_add( t, t, a.t ); 144 return *this; 145 } 146 inline gmp_float & gmp_float::operator -= ( const gmp_float & a ) 147 { 148 mpf_sub( t, t, a.t ); 149 return *this; 150 } 151 inline gmp_float & gmp_float::operator *= ( const gmp_float & a ) 152 { 153 mpf_mul( t, t, a.t ); 154 return *this; 155 } 156 inline gmp_float & gmp_float::operator /= ( const gmp_float & a ) 157 { 158 mpf_div( t, t, a.t ); 159 return *this; 160 } 161 162 // <gmp_float> = <*> 163 inline gmp_float & gmp_float::operator = ( const gmp_float & a ) 164 { 165 mpf_set( t, a.t ); 166 return *this; 167 } 168 inline gmp_float & gmp_float::operator = ( const mpz_t & a ) 169 { 170 mpf_set_z( t, a ); 171 return *this; 172 } 173 inline gmp_float & gmp_float::operator = ( const mprfloat a ) 174 { 175 mpf_set_d( t, (double) a ); 176 return *this; 177 } 178 inline gmp_float & gmp_float::operator = ( const long a ) 179 { 180 mpf_set_si( t, (signed long int) a ); 181 return *this; 182 } 183 184 // cast to double 185 inline gmp_float::operator double() 186 { 187 return mpf_get_d( t ); 188 } 189 inline gmp_float::operator double() const 190 { 191 return mpf_get_d( t ); 192 } 193 194 // cast to int 195 inline gmp_float::operator int() 196 { 197 return (int)mpf_get_d( t ); 198 } 199 inline gmp_float::operator int() const 200 { 201 return (int)mpf_get_d( t ); 202 } 203 204 // get sign of real number ( -1: t < 0; 0: t==0; 1: t > 0 ) 205 inline int gmp_float::sign() 206 { 207 return mpf_sgn( t ); 208 } 209 // t == 0 ? 210 inline bool gmp_float::isZero() 211 { 212 #ifdef VARIANTE_1 213 return (mpf_sgn( t ) == 0); 214 #else 215 return mpf_eq( t , gmpZero.t , gmp_float::gmp_needequal_bits ); 216 #endif 217 } 218 // t == 1 ? 219 inline bool gmp_float::isOne() 220 { 221 #ifdef VARIANTE_1 222 return (mpf_cmp_ui( t , 1 ) == 0); 223 #else 224 return mpf_eq( t , gmpOne.t , gmp_float::gmp_needequal_bits ); 225 #endif 226 } 227 // t == -1 ? 228 inline bool gmp_float::isMOne() 229 { 230 #ifdef VARIANTE_1 231 return (mpf_cmp_si( t , -1 ) == 0); 232 #else 233 return mpf_eq( t , gmpMOne.t , gmp_float::gmp_needequal_bits ); 234 #endif 235 } 236 237 inline bool gmp_float::setFromStr( char * in ) 238 { 239 return ( mpf_set_str( t, in, 10 ) == 0 ); 240 } 241 242 // access pointer 243 inline const mpf_t *gmp_float::mpfp() const 244 { 245 return &t; 246 } 247 106 248 // built-in functions of GMP 107 249 gmp_float abs( const gmp_float & ); … … 118 260 119 261 gmp_float numberToFloat( number num ); 120 char *floatToStr( const gmp_float & r, const size_t oprec ); 121 122 typedef gmp_float mprfloat_g; 262 char *floatToStr( const gmp_float & r, const unsigned int oprec ); 123 263 //<- 124 264 125 //-> class complex265 //-> class gmp_complex 126 266 /** 127 * @short complex numbers based on267 * @short gmp_complex numbers based on 128 268 */ 129 class complex269 class gmp_complex 130 270 { 131 271 private: 132 mprfloat_gr, i;272 gmp_float r, i; 133 273 134 274 public: 135 complex( const mprfloat_g re= 0.0, const mprfloat_g im= 0.0 ); 136 complex( const mprfloat re, const mprfloat im = 0.0 ); 137 complex( const long re, const long im ); 138 complex( const complex & v ); 139 140 ~complex(); 141 142 friend complex operator + ( const complex & a, const complex & b ); 143 friend complex operator - ( const complex & a, const complex & b ); 144 friend complex operator * ( const complex & a, const complex & b ); 145 friend complex operator / ( const complex & a, const complex & b ); 146 147 friend complex operator + ( const complex & a, const mprfloat_g b_d ); 148 friend complex operator - ( const complex & a, const mprfloat_g b_d ); 149 friend complex operator * ( const complex & a, const mprfloat_g b_d ); 150 friend complex operator / ( const complex & a, const mprfloat_g b_d ); 151 152 complex & operator += ( const complex & a ); 153 complex & operator -= ( const complex & a ); 154 complex & operator *= ( const complex & a ); 155 complex & operator /= ( const complex & a ); 156 157 friend bool operator == ( const complex & a, const complex & b ); 158 159 complex & operator = ( const complex & a ); 275 gmp_complex( const gmp_float re= 0.0, const gmp_float im= 0.0 ) 276 { 277 r= re; 278 i= im; 279 } 280 gmp_complex( const mprfloat re, const mprfloat im = 0.0 ) 281 { 282 r= re; 283 i= im; 284 } 285 gmp_complex( const long re, const long im ) 286 { 287 r= re; 288 i= im; 289 } 290 gmp_complex( const gmp_complex & v ) 291 { 292 r= v.r; 293 i= v.i; 294 } 295 ~gmp_complex() {} 296 297 friend gmp_complex operator + ( const gmp_complex & a, const gmp_complex & b ); 298 friend gmp_complex operator - ( const gmp_complex & a, const gmp_complex & b ); 299 friend gmp_complex operator * ( const gmp_complex & a, const gmp_complex & b ); 300 friend gmp_complex operator / ( const gmp_complex & a, const gmp_complex & b ); 301 302 // gmp_complex <operator> real 303 inline friend gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ); 304 inline friend gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ); 305 inline friend gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ); 306 inline friend gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ); 307 308 gmp_complex & operator += ( const gmp_complex & a ); 309 gmp_complex & operator -= ( const gmp_complex & a ); 310 gmp_complex & operator *= ( const gmp_complex & a ); 311 gmp_complex & operator /= ( const gmp_complex & a ); 312 313 inline friend bool operator == ( const gmp_complex & a, const gmp_complex & b ); 314 315 inline gmp_complex & operator = ( const gmp_complex & a ); 160 316 161 317 // access to real and imaginary part 162 inline mprfloat_greal() const { return r; }163 inline mprfloat_gimag() const { return i; }164 165 inline void real( mprfloat_gval ) { r = val; }166 inline void imag( mprfloat_gval ) { i = val; }318 inline gmp_float real() const { return r; } 319 inline gmp_float imag() const { return i; } 320 321 inline void real( gmp_float val ) { r = val; } 322 inline void imag( gmp_float val ) { i = val; } 167 323 }; 168 324 169 mprfloat_g abs( const complex & c ); 170 complex sqrt( const complex & x ); 171 172 inline complex numberToComplex( number num ) 173 { 174 return complex( numberToFloat(num) ); 175 } 176 char *complexToStr( const complex & c, const size_t oprec ); 325 // <gmp_complex> = <gmp_complex> operator <gmp_float> 326 // 327 inline gmp_complex operator + ( const gmp_complex & a, const gmp_float b_d ) 328 { 329 return gmp_complex( a.r + b_d, a.i ); 330 } 331 inline gmp_complex operator - ( const gmp_complex & a, const gmp_float b_d ) 332 { 333 return gmp_complex( a.r - b_d, a.i ); 334 } 335 inline gmp_complex operator * ( const gmp_complex & a, const gmp_float b_d ) 336 { 337 return gmp_complex( a.r * b_d, a.i * b_d ); 338 } 339 inline gmp_complex operator / ( const gmp_complex & a, const gmp_float b_d ) 340 { 341 return gmp_complex( a.r / b_d, a.i / b_d ); 342 } 343 344 // <gmp_complex> == <gmp_complex> ? 345 inline bool operator == ( const gmp_complex & a, const gmp_complex & b ) 346 { 347 return ( b.real() == a.real() ) && ( b.imag() == a.imag() ); 348 } 349 350 // <gmp_complex> = <gmp_complex> 351 inline gmp_complex & gmp_complex::operator = ( const gmp_complex & a ) 352 { 353 r= a.r; 354 i= a.i; 355 return *this; 356 } 357 358 // Returns absolute value of a gmp_complex number 359 // 360 inline gmp_float abs( const gmp_complex & c ) 361 { 362 return hypot(c.real(),c.imag()); 363 } 364 365 gmp_complex sqrt( const gmp_complex & x ); 366 367 inline gmp_complex numberToGmp_Complex( number num ) 368 { 369 if (rField_is_long_C()) { 370 return *(gmp_complex*)num; 371 } else { 372 return gmp_complex( numberToFloat(num) ); 373 } 374 } 375 376 char *complexToStr( const gmp_complex & c, const unsigned int oprec ); 177 377 //<- 178 378 … … 184 384 // compile-command-2: "make install" *** 185 385 // End: *** 386 387 388 389 390 -
Singular/mpr_global.h
r6b9e9c rf543de1 4 4 * Computer Algebra System SINGULAR * 5 5 ****************************************/ 6 /* $Id: mpr_global.h,v 1. 1 1999-04-29 11:38:51 SingularExp $ */6 /* $Id: mpr_global.h,v 1.2 1999-06-24 07:46:51 wenk Exp $ */ 7 7 8 8 /* … … 11 11 */ 12 12 13 // Define WITH_SUBDIV to compute mixed polyhedral subdivision 14 //#define WITH_SUBDIV 15 16 // <matrix representation>_<determinant algorithm> 17 #define DENSE_FACTORY 1 18 #define SPARSE_BAREISS 2 19 #define SPARSE_GAUSS 3 20 21 #define USE_MATRIX_REP SPARSE_BAREISS 13 // to get detailed timigs, define MPR_TIMING 14 #define MPR_TIMING 22 15 23 16 // Set to double or long double. double is recomended. 24 // Sets the global floating point type used in mpr_ roots.cc and mpr_nric.cc.17 // Sets the global floating point type used in mpr_numeric.cc. 25 18 typedef double mprfloat; 26 19 27 20 // --------------------------- debugging stuff ---------------------------- 21 #if !defined(NDEBUG) 28 22 //#define mprDEBUG_ALL 23 #endif 24 25 #if !defined(NDEBUG) || defined(mprDEBUG_ALL) 29 26 #define mprDEBUG_PROT 27 #endif 28 29 #if !defined(NDEBUG) && !defined(MPR_TIMING) 30 #define MPR_TIMING 31 #endif 32 30 33 #define mprDEBUG_STICKY 31 34 … … 51 54 52 55 #ifdef mprDEBUG_STICKY 53 #define mprSTICKYPROT(msg) Print(msg) 54 //if (BTEST1(OPT_PROT)) Print(msg) 56 // call 'option(prot);' to get status informations 57 #define mprSTICKYPROT(msg) if (BTEST1(OPT_PROT)) Print(msg) 58 #define mprSTICKYPROT2(msg,arg) if (BTEST1(OPT_PROT)) Print(msg,arg) 55 59 #else 56 60 #define mprSTICKYPROT(msg) 61 #define mprSTICKYPROT2(msg,arg) 57 62 #endif 58 63 … … 89 94 // compile-command-2: "make install" *** 90 95 // End: *** 96 97 98
Note: See TracChangeset
for help on using the changeset viewer.