Changeset 5538:35d7e6dac538 for sage/combinat/partitions_c.cc
- Timestamp:
- 07/31/07 19:52:12 (6 years ago)
- Branch:
- default
- File:
-
- 1 edited
-
sage/combinat/partitions_c.cc (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
-
sage/combinat/partitions_c.cc
r5529 r5538 171 171 mp_rnd_t round_mode = GMP_RNDN; 172 172 173 mpfr_t tempa1, tempa2, tempf1, tempf2, temps1, temps2, tempt1, tempt2; // temp variables for different functions, with precision set and cleared by mp_set_precision173 mpfr_t tempa1, tempa2, tempf1, tempf2, temps1, temps2, tempt1, tempt2; // temp variables for different functions, with precision set and cleared by initialize_mpfr_variables 174 174 mpfr_t tempc1, tempc2; // temp variable used by cospi() 175 175 … … 330 330 331 331 332 void mp_set_precision(unsigned int prec) {332 void initialize_mpfr_variables(unsigned int prec) { 333 333 // 334 334 // Clear and initialize some "temp" variables that are used in the computation of various functions. … … 347 347 // 348 348 // -etc... 349 350 static bool init = false; 351 if(init) { 352 mpfr_clear(tempa1); 353 mpfr_clear(tempa2); 354 mpfr_clear(tempf1); 355 mpfr_clear(tempf2); 356 mpfr_clear(tempt1); 357 mpfr_clear(tempt2); 358 mpfr_clear(temps1); 359 mpfr_clear(temps2); 360 361 mpfr_clear(tempc1); 362 mpfr_clear(tempc2); 363 } 349 // 350 // NOTE: Calls to this function must be paired with calls to clear_mpfr_variables() 351 364 352 mpfr_init2(tempa1, prec); 365 353 mpfr_init2(tempa2, prec); … … 372 360 mpfr_init2(tempc1, prec); 373 361 mpfr_init2(tempc2, prec); 374 375 376 init = true; 362 } 363 364 void clear_mpfr_variables() { 365 mpfr_clear(tempa1); 366 mpfr_clear(tempa2); 367 mpfr_clear(tempf1); 368 mpfr_clear(tempf2); 369 mpfr_clear(tempt1); 370 mpfr_clear(tempt2); 371 mpfr_clear(temps1); 372 mpfr_clear(temps2); 373 374 mpfr_clear(tempc1); 375 mpfr_clear(tempc2); 377 376 } 378 377 … … 387 386 // Also, we precompute some extra constants that we use a lot, such as 388 387 // sqrt2, sqrt3, pi, 1/24, 1/12, etc. 388 // 389 // NOTE: Calls to this function must be paired with calls to clear_constants() 389 390 static bool init = false; 390 391 mp_precision = prec; 391 392 mp_prec_t p = mp_precision; 392 393 393 if(init) {394 mpfr_clear(mp_one_over_12); mpfr_clear(mp_one_over_24); mpfr_clear(mp_sqrt2); mpfr_clear(mp_sqrt3); mpfr_clear(mp_pi);395 mpfr_clear(mp_A); mpfr_clear(mp_B); mpfr_clear(mp_C); mpfr_clear(mp_D); mpfr_clear(half); mpfr_clear(fourth);396 397 mpz_clear(ztemp1);398 mpz_clear(ztemp2);399 mpz_clear(ztemp3);400 401 mpq_clear(qtemp1);402 mpq_clear(qtemp2);403 mpq_clear(qtemp3);404 }405 394 mpfr_init2(mp_one_over_12,p); mpfr_init2(mp_one_over_24,p); mpfr_init2(mp_sqrt2,p); mpfr_init2(mp_sqrt3,p); mpfr_init2(mp_pi,p); 406 395 mpfr_init2(mp_A,p); mpfr_init2(mp_B,p); mpfr_init2(mp_C,p); mpfr_init2(mp_D,p); mpfr_init2(fourth, p); mpfr_init2(half, p); 407 mpz_init(ztemp1);408 mpz_init(ztemp2);409 mpz_init(ztemp3);410 411 mpq_init(qtemp1);412 mpq_init(qtemp2);413 mpq_init(qtemp3);414 396 415 397 init = true; … … 477 459 d_D = 2.0 * (n - 1.0/24.0) * sqrt(n - 1.0/24.0); 478 460 461 } 462 463 void clear_constants() { 464 mpfr_clear(mp_one_over_12); mpfr_clear(mp_one_over_24); mpfr_clear(mp_sqrt2); mpfr_clear(mp_sqrt3); mpfr_clear(mp_pi); 465 mpfr_clear(mp_A); mpfr_clear(mp_B); mpfr_clear(mp_C); mpfr_clear(mp_D); mpfr_clear(half); mpfr_clear(fourth); 466 } 467 468 void initialize_mpz_and_mpq_variables() { 469 /* 470 * We use a few mpz_t and mpq_t variables which need to be initialized 471 * before they can be used. Initialization and clearing take some 472 * time, so we initialize just once in this function, and clear in another. 473 */ 474 mpz_init(ztemp1); 475 mpz_init(ztemp2); 476 mpz_init(ztemp3); 477 478 mpq_init(qtemp1); 479 mpq_init(qtemp2); 480 mpq_init(qtemp3); 481 } 482 483 void clear_mpz_and_mpq_variables() { 484 mpz_clear(ztemp1); 485 mpz_clear(ztemp2); 486 mpz_clear(ztemp3); 487 488 mpq_clear(qtemp1); 489 mpq_clear(qtemp2); 490 mpq_clear(qtemp3); 479 491 } 480 492 … … 890 902 mpfr_set_ui(result, 0, round_mode); // 891 903 904 initialize_mpz_and_mpq_variables(); 892 905 initialize_constants(initial_precision, n); // Now that we have the precision information, we initialize some constants 893 906 // that will be used throughout, and also 894 907 // 895 mp_set_precision(initial_precision); // set the precision of the "temp" variables that are used in individual functions.908 initialize_mpfr_variables(initial_precision); // set the precision of the "temp" variables that are used in individual functions. 896 909 897 910 unsigned int current_precision = initial_precision; … … 967 980 if(new_precision != current_precision) { // If the precision changes, we need to clear 968 981 current_precision = new_precision; // and reinitialize all "temp" variables to 969 mp_set_precision(current_precision); // use lower precision. 982 clear_mpfr_variables(); // use lower precision. 983 initialize_mpfr_variables(current_precision); // 970 984 mpfr_clear(t1); mpfr_clear(t2); // 971 985 mpfr_init2(t1, current_precision); // … … 988 1002 mpfr_div(result, result, mp_pi, round_mode); // The actual result is the sum that we have computed 989 1003 mpfr_div(result, result, mp_sqrt2, round_mode); // divided by pi*sqrt(2). 1004 1005 clear_constants(); 1006 clear_mpz_and_mpq_variables(); 1007 clear_mpfr_variables(); 1008 mpfr_clear(t1); 1009 mpfr_clear(t2); // 990 1010 } 991 1011 … … 1299 1319 mpfr_get_z(answer, result, round_mode); 1300 1320 1321 mpfr_clear(result); 1301 1322 return 0; 1302 1323 }
Note: See TracChangeset
for help on using the changeset viewer.
