void zeta_series_em_choose_param(fmpr_t bound, ulong * N, ulong * M, const fmpcb_t s, const fmpcb_t a, long d, long target, long prec) { ulong A, B, C; fmpr_t Abound, Bbound, Cbound, tol; fmpr_init(Abound); fmpr_init(Bbound); fmpr_init(Cbound); fmpr_init(tol); fmpr_set_si_2exp_si(tol, 1, -target); A = 1; B = 2; zeta_series_em_bound(Bbound, s, a, B, choose_M(B, target), d, prec); if (fmpr_cmp(Bbound, tol) > 0) { while (fmpr_cmp(Bbound, tol) > 0) { fmpr_set(Abound, Bbound); A *= 2; B *= 2; if (B == 0) abort(); zeta_series_em_bound(Bbound, s, a, B, choose_M(B, target), d, prec); } /* bisect (-A, B] */ while (B > A + 4) { C = A + (B - A) / 2; zeta_series_em_bound(Cbound, s, a, C, choose_M(C, target), d, prec); if (fmpr_cmp(Cbound, tol) < 0) { B = C; fmpr_set(Bbound, Cbound); } else { A = C; fmpr_set(Abound, Cbound); } } } fmpr_set(bound, Bbound); *N = B; *M = choose_M(B, target); fmpr_clear(Abound); fmpr_clear(Bbound); fmpr_clear(Cbound); fmpr_clear(tol); }
static slong _fmpr_add_special(fmpr_t z, const fmpr_t x, const fmpr_t y, slong prec, fmpr_rnd_t rnd) { if (fmpr_is_zero(x)) { if (fmpr_is_zero(y)) { fmpr_zero(z); return FMPR_RESULT_EXACT; } else return fmpr_set_round(z, y, prec, rnd); } else if (fmpr_is_zero(y)) { return fmpr_set_round(z, x, prec, rnd); } else if (fmpr_is_nan(x) || fmpr_is_nan(y) || (fmpr_is_pos_inf(x) && fmpr_is_neg_inf(y)) || (fmpr_is_neg_inf(x) && fmpr_is_pos_inf(y))) { fmpr_nan(z); return FMPR_RESULT_EXACT; } else if (fmpr_is_special(x)) { fmpr_set(z, x); return FMPR_RESULT_EXACT; } else { fmpr_set(z, y); return FMPR_RESULT_EXACT; } }
void fmpr_pow_sloppy_fmpz(fmpr_t y, const fmpr_t b, const fmpz_t e, long prec, fmpr_rnd_t rnd) { long i, wp, bits; if (fmpz_is_zero(e)) { fmpr_set_ui(y, 1UL); return; } if (fmpz_sgn(e) < 0) { fmpz_t f; fmpz_init(f); fmpz_neg(f, e); fmpr_pow_sloppy_fmpz(y, b, f, prec + 2, (rnd == FMPR_RND_FLOOR || rnd == FMPR_RND_DOWN) ? FMPR_RND_UP : FMPR_RND_DOWN); fmpr_ui_div(y, 1UL, y, prec, rnd); fmpz_clear(f); } if (y == b) { fmpr_t t; fmpr_init(t); fmpr_set(t, b); fmpr_pow_sloppy_fmpz(y, t, e, prec, rnd); fmpr_clear(t); return; } fmpr_set(y, b); bits = fmpz_bits(e); wp = FMPR_PREC_ADD(prec, bits); for (i = bits - 2; i >= 0; i--) { fmpr_mul(y, y, y, wp, rnd); if (fmpz_tstbit(e, i)) fmpr_mul(y, y, b, wp, rnd); } }
int main() { slong iter; flint_rand_t state; flint_printf("set_d...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { fmpr_t a, b, c; mag_t m; double x; fmpr_init(a); fmpr_init(b); fmpr_init(c); mag_init(m); x = d_randtest2(state); x = ldexp(x, 100 - n_randint(state, 200)); if (n_randint(state, 100) == 0) x = 0.0; fmpr_set_d(a, x); mag_set_d(m, x); mag_get_fmpr(b, m); fmpr_set(c, a); fmpr_mul_ui(c, c, 1025, MAG_BITS, FMPR_RND_UP); fmpr_mul_2exp_si(c, c, -10); MAG_CHECK_BITS(m) if (!(fmpr_cmpabs(a, b) <= 0 && fmpr_cmpabs(b, c) <= 0)) { flint_printf("FAIL\n\n"); flint_printf("a = "); fmpr_print(a); flint_printf("\n\n"); flint_printf("b = "); fmpr_print(b); flint_printf("\n\n"); flint_printf("c = "); fmpr_print(c); flint_printf("\n\n"); abort(); } fmpr_clear(a); fmpr_clear(b); fmpr_clear(c); mag_clear(m); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void fmprb_sqrtpos(fmprb_t z, const fmprb_t x, slong prec) { if (!fmprb_is_finite(x)) { if (fmpr_is_zero(fmprb_radref(x)) && fmpr_is_pos_inf(fmprb_midref(x))) fmprb_pos_inf(z); else fmprb_zero_pm_inf(z); } else if (fmprb_contains_nonpositive(x)) { fmpr_t t; fmpr_init(t); fmpr_add(t, fmprb_midref(x), fmprb_radref(x), FMPRB_RAD_PREC, FMPR_RND_CEIL); if (fmpr_sgn(t) <= 0) { fmprb_zero(z); } else { fmpr_sqrt(t, t, FMPRB_RAD_PREC, FMPR_RND_CEIL); fmpr_mul_2exp_si(t, t, -1); fmpr_set(fmprb_midref(z), t); fmpr_set(fmprb_radref(z), t); } fmpr_clear(t); } else { fmprb_sqrt(z, x, prec); } fmprb_nonnegative_part(z, z, prec); }
static __inline__ void fmprb_nonnegative_part(fmprb_t z, const fmprb_t x, slong prec) { if (fmprb_contains_negative(x)) { fmpr_add(fmprb_midref(z), fmprb_midref(x), fmprb_radref(x), prec, FMPR_RND_CEIL); if (fmpr_sgn(fmprb_midref(z)) <= 0) { fmpr_zero(fmprb_radref(z)); } else { fmpr_mul_2exp_si(fmprb_midref(z), fmprb_midref(z), -1); fmpr_set(fmprb_midref(z), fmprb_radref(z)); } } else { fmprb_set(z, x); } }
/* convert to an fmpz poly with a common exponent and coefficients at most prec bits, also bounding input error plus rounding error */ void _fmprb_poly_get_fmpz_poly_2exp(fmpr_t error, fmpz_t exp, fmpz * coeffs, fmprb_srcptr A, long lenA, long prec) { fmpz_t top_exp, bot_exp; long shift; long i; int rounding; fmpz_init(top_exp); fmpz_init(bot_exp); if (!_fmprb_poly_mid_get_hull(bot_exp, top_exp, A, lenA)) { fmpz_zero(exp); _fmpz_vec_zero(coeffs, lenA); fmpr_zero(error); for (i = 0; i < lenA; i++) { if (fmpr_cmp(fmprb_radref(A + i), error) > 0) fmpr_set(error, fmprb_radref(A + i)); } return; /* no need to clear fmpzs */ } /* only take as much precision as necessary */ shift = _fmpz_sub_small(top_exp, bot_exp); prec = FLINT_MIN(prec, shift); fmpz_sub_ui(exp, top_exp, prec); /* extract integer polynomial */ rounding = 0; for (i = 0; i < lenA; i++) rounding |= fmpr_get_fmpz_fixed_fmpz(coeffs + i, fmprb_midref(A + i), exp); fmpr_zero(error); /* compute maximum of input errors */ for (i = 0; i < lenA; i++) { if (fmpr_cmp(fmprb_radref(A + i), error) > 0) fmpr_set(error, fmprb_radref(A + i)); } /* add rounding error */ if (rounding) { fmpr_t t; fmpr_init(t); fmpz_set_ui(fmpr_manref(t), 1UL); fmpz_set(fmpr_expref(t), exp); fmpr_add(error, error, t, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_clear(t); } fmpz_clear(top_exp); }
int main() { slong iter, iter2; flint_rand_t state; flint_printf("mul_si...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 3000; iter++) { fmpr_t x, z, v; slong y; slong prec, r1, r2; fmpr_rnd_t rnd; fmpr_init(x); fmpr_init(z); fmpr_init(v); for (iter2 = 0; iter2 < 30; iter2++) { fmpr_randtest_special(x, state, 2000, 200); y = z_randtest(state); prec = 2 + n_randint(state, 2000); switch (n_randint(state, 4)) { case 0: rnd = FMPR_RND_DOWN; break; case 1: rnd = FMPR_RND_UP; break; case 2: rnd = FMPR_RND_FLOOR; break; default: rnd = FMPR_RND_CEIL; break; } switch (n_randint(state, 2)) { case 0: r1 = fmpr_mul_si(z, x, y, prec, rnd); r2 = fmpr_mul_si_naive(v, x, y, prec, rnd); if (!fmpr_equal(z, v) || r1 != r2 || !fmpr_check_ulp(z, r1, prec)) { flint_printf("FAIL!\n"); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = %wd\n\n", y); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("v = "); fmpr_print(v); flint_printf("\n\n"); flint_printf("r1 = %wd, r2 = %wd\n", r1, r2); abort(); } break; default: fmpr_set(v, x); fmpr_set(z, x); r1 = fmpr_mul_si(z, z, y, prec, rnd); r2 = fmpr_mul_si_naive(v, v, y, prec, rnd); if (!fmpr_equal(z, v) || r1 != r2 || !fmpr_check_ulp(z, r1, prec)) { flint_printf("FAIL (aliasing 1)!\n"); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = %wd\n\n", y); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("v = "); fmpr_print(v); flint_printf("\n\n"); flint_printf("r1 = %wd, r2 = %wd\n", r1, r2); abort(); } break; } } fmpr_clear(x); fmpr_clear(z); fmpr_clear(v); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int fmprb_contains(const fmprb_t x, const fmprb_t y) { fmpr_t t; fmpr_t u; fmpr_struct tmp[4]; int left_ok, right_ok; if (fmprb_is_exact(y)) return fmprb_contains_fmpr(x, fmprb_midref(y)); if (fmpr_is_nan(fmprb_midref(y))) return fmpr_is_nan(fmprb_midref(x)); fmpr_init(t); fmpr_init(u); /* fast check */ fmpr_sub(t, fmprb_midref(x), fmprb_radref(x), 30, FMPR_RND_CEIL); fmpr_sub(u, fmprb_midref(y), fmprb_radref(y), 30, FMPR_RND_FLOOR); left_ok = fmpr_cmp(t, u) <= 0; /* exact check */ if (!left_ok) { fmpr_init(tmp + 0); fmpr_init(tmp + 1); fmpr_init(tmp + 2); fmpr_init(tmp + 3); fmpr_set(tmp + 0, fmprb_midref(x)); fmpr_neg(tmp + 1, fmprb_radref(x)); fmpr_neg(tmp + 2, fmprb_midref(y)); fmpr_set(tmp + 3, fmprb_radref(y)); fmpr_sum(t, tmp, 4, 30, FMPR_RND_DOWN); left_ok = fmpr_sgn(t) <= 0; fmpr_clear(tmp + 0); fmpr_clear(tmp + 1); fmpr_clear(tmp + 2); fmpr_clear(tmp + 3); } /* fast check */ fmpr_add(t, fmprb_midref(x), fmprb_radref(x), 30, FMPR_RND_FLOOR); fmpr_add(u, fmprb_midref(y), fmprb_radref(y), 30, FMPR_RND_CEIL); right_ok = (fmpr_cmp(t, u) >= 0); /* exact check */ if (!right_ok) { fmpr_init(tmp + 0); fmpr_init(tmp + 1); fmpr_init(tmp + 2); fmpr_init(tmp + 3); fmpr_set(tmp + 0, fmprb_midref(x)); fmpr_set(tmp + 1, fmprb_radref(x)); fmpr_neg(tmp + 2, fmprb_midref(y)); fmpr_neg(tmp + 3, fmprb_radref(y)); fmpr_sum(t, tmp, 4, 30, FMPR_RND_DOWN); right_ok = fmpr_sgn(t) >= 0; fmpr_clear(tmp + 0); fmpr_clear(tmp + 1); fmpr_clear(tmp + 2); fmpr_clear(tmp + 3); } fmpr_clear(t); fmpr_clear(u); return left_ok && right_ok; }