void arb_sqrt1pm1(arb_t r, const arb_t z, slong prec) { slong magz, wp; if (arb_is_zero(z)) { arb_zero(r); return; } magz = arf_abs_bound_lt_2exp_si(arb_midref(z)); if (magz < -prec) { arb_sqrt1pm1_tiny(r, z, prec); } else { if (magz < 0) wp = prec + (-magz) + 4; else wp = prec + 4; arb_add_ui(r, z, 1, wp); arb_sqrt(r, r, wp); arb_sub_ui(r, r, 1, wp); } }
void arb_div_2expm1_ui(arb_t y, const arb_t x, ulong n, long prec) { if (n < FLINT_BITS) { arb_div_ui(y, x, (1UL << n) - 1, prec); } else if (n < 1024 + prec / 32 || n > LONG_MAX / 4) { arb_t t; fmpz_t e; arb_init(t); fmpz_init_set_ui(e, n); arb_one(t); arb_mul_2exp_fmpz(t, t, e); arb_sub_ui(t, t, 1, prec); arb_div(y, x, t, prec); arb_clear(t); fmpz_clear(e); } else { arb_t s, t; long i, b; arb_init(s); arb_init(t); /* x / (2^n - 1) = sum_{k>=1} x * 2^(-k*n)*/ arb_mul_2exp_si(s, x, -n); arb_set(t, s); b = 1; for (i = 2; i <= prec / n + 1; i++) { arb_mul_2exp_si(t, t, -n); arb_add(s, s, t, prec); b = i; } /* error bound: sum_{k>b} x * 2^(-k*n) <= x * 2^(-b*n - (n-1)) */ arb_mul_2exp_si(t, x, -b*n - (n-1)); arb_abs(t, t); arb_add_error(s, t); arb_set(y, s); arb_clear(s); arb_clear(t); } }
static void bound_I(arb_ptr I, const arb_t A, const arb_t B, const arb_t C, slong len, slong wp) { slong k; arb_t D, Dk, L, T, Bm1; arb_init(D); arb_init(Dk); arb_init(Bm1); arb_init(T); arb_init(L); arb_sub_ui(Bm1, B, 1, wp); arb_one(L); /* T = 1 / (A^Bm1 * Bm1) */ arb_inv(T, A, wp); arb_pow(T, T, Bm1, wp); arb_div(T, T, Bm1, wp); if (len > 1) { arb_log(D, A, wp); arb_add(D, D, C, wp); arb_mul(D, D, Bm1, wp); arb_set(Dk, D); } for (k = 0; k < len; k++) { if (k > 0) { arb_mul_ui(L, L, k, wp); arb_add(L, L, Dk, wp); arb_mul(Dk, Dk, D, wp); } arb_mul(I + k, L, T, wp); arb_div(T, T, Bm1, wp); } arb_clear(D); arb_clear(Dk); arb_clear(Bm1); arb_clear(T); arb_clear(L); }
int acb_modular_is_in_fundamental_domain(const acb_t z, const arf_t tol, long prec) { arb_t t; arb_init(t); /* require re(w) + 1/2 >= 0 */ arb_set_ui(t, 1); arb_mul_2exp_si(t, t, -1); arb_add(t, t, acb_realref(z), prec); arb_add_arf(t, t, tol, prec); if (!arb_is_nonnegative(t)) { arb_clear(t); return 0; } /* require re(w) - 1/2 <= 0 */ arb_set_ui(t, 1); arb_mul_2exp_si(t, t, -1); arb_sub(t, acb_realref(z), t, prec); arb_sub_arf(t, t, tol, prec); if (!arb_is_nonpositive(t)) { arb_clear(t); return 0; } /* require |w| >= 1 - tol, i.e. |w| - 1 + tol >= 0 */ acb_abs(t, z, prec); arb_sub_ui(t, t, 1, prec); arb_add_arf(t, t, tol, prec); if (!arb_is_nonnegative(t)) { arb_clear(t); return 0; } arb_clear(t); return 1; }
void arb_acosh(arb_t z, const arb_t x, slong prec) { if (arb_is_one(x)) { arb_zero(z); } else { arb_t t; arb_init(t); arb_mul(t, x, x, prec + 4); arb_sub_ui(t, t, 1, prec + 4); arb_sqrt(t, t, prec + 4); arb_add(t, t, x, prec + 4); arb_log(z, t, prec); arb_clear(t); } }
void _arb_poly_asin_series(arb_ptr g, arb_srcptr h, slong hlen, slong n, slong prec) { arb_t c; arb_init(c); arb_asin(c, h, prec); hlen = FLINT_MIN(hlen, n); if (hlen == 1) { _arb_vec_zero(g + 1, n - 1); } else { arb_ptr t, u; slong ulen; t = _arb_vec_init(n); u = _arb_vec_init(n); /* asin(h(x)) = integral(h'(x)/sqrt(1-h(x)^2)) */ ulen = FLINT_MIN(n, 2 * hlen - 1); _arb_poly_mullow(u, h, hlen, h, hlen, ulen, prec); arb_sub_ui(u, u, 1, prec); _arb_vec_neg(u, u, ulen); _arb_poly_rsqrt_series(t, u, ulen, n, prec); _arb_poly_derivative(u, h, hlen, prec); _arb_poly_mullow(g, t, n, u, hlen - 1, n, prec); _arb_poly_integral(g, g, n, prec); _arb_vec_clear(t, n); _arb_vec_clear(u, n); } arb_swap(g, c); arb_clear(c); }
void arb_atanh(arb_t z, const arb_t x, slong prec) { if (arb_is_zero(x)) { arb_zero(z); } else { arb_t t; arb_init(t); arb_sub_ui(t, x, 1, prec + 4); arb_div(t, x, t, prec + 4); arb_mul_2exp_si(t, t, 1); arb_neg(t, t); arb_log1p(z, t, prec); arb_mul_2exp_si(z, z, -1); arb_clear(t); } }
void acb_dirichlet_zeta_rs_mid(acb_t res, const acb_t s, slong K, slong prec) { acb_t R1, R2, X, t; slong wp; if (arf_sgn(arb_midref(acb_imagref(s))) < 0) { acb_init(t); acb_conj(t, s); acb_dirichlet_zeta_rs(res, t, K, prec); acb_conj(res, res); acb_clear(t); return; } acb_init(R1); acb_init(R2); acb_init(X); acb_init(t); /* rs_r increases the precision internally */ wp = prec; acb_dirichlet_zeta_rs_r(R1, s, K, wp); if (arb_is_exact(acb_realref(s)) && (arf_cmp_2exp_si(arb_midref(acb_realref(s)), -1) == 0)) { acb_conj(R2, R1); } else { /* conj(R(conj(1-s))) */ arb_sub_ui(acb_realref(t), acb_realref(s), 1, 10 * wp); arb_neg(acb_realref(t), acb_realref(t)); arb_set(acb_imagref(t), acb_imagref(s)); acb_dirichlet_zeta_rs_r(R2, t, K, wp); acb_conj(R2, R2); } if (acb_is_finite(R1) && acb_is_finite(R2)) { wp += 10 + arf_abs_bound_lt_2exp_si(arb_midref(acb_imagref(s))); wp = FLINT_MAX(wp, 10); /* X = pi^(s-1/2) gamma((1-s)/2) rgamma(s/2) = (2 pi)^s rgamma(s) / (2 cos(pi s / 2)) */ acb_rgamma(X, s, wp); acb_const_pi(t, wp); acb_mul_2exp_si(t, t, 1); acb_pow(t, t, s, wp); acb_mul(X, X, t, wp); acb_mul_2exp_si(t, s, -1); acb_cos_pi(t, t, wp); acb_mul_2exp_si(t, t, 1); acb_div(X, X, t, wp); acb_mul(R2, R2, X, wp); } /* R1 + X * R2 */ acb_add(res, R1, R2, prec); acb_clear(R1); acb_clear(R2); acb_clear(X); acb_clear(t); }
void _arb_poly_rgamma_series(arb_ptr res, arb_srcptr h, long hlen, long len, long prec) { int reflect; long i, rflen, r, n, wp; arb_ptr t, u, v; arb_struct f[2]; hlen = FLINT_MIN(hlen, len); wp = prec + FLINT_BIT_COUNT(prec); t = _arb_vec_init(len); u = _arb_vec_init(len); v = _arb_vec_init(len); arb_init(f); arb_init(f + 1); /* use zeta values at small integers */ if (arb_is_int(h) && (arf_cmpabs_ui(arb_midref(h), prec / 2) < 0)) { r = arf_get_si(arb_midref(h), ARF_RND_DOWN); _arb_poly_lgamma_series_at_one(u, len, wp); _arb_vec_neg(u, u, len); _arb_poly_exp_series(t, u, len, len, wp); if (r == 1) { _arb_vec_swap(v, t, len); } else if (r <= 0) { arb_set(f, h); arb_one(f + 1); rflen = FLINT_MIN(len, 2 - r); _arb_poly_rising_ui_series(u, f, FLINT_MIN(2, len), 1 - r, rflen, wp); _arb_poly_mullow(v, t, len, u, rflen, len, wp); } else { arb_one(f); arb_one(f + 1); rflen = FLINT_MIN(len, r); _arb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r - 1, rflen, wp); /* TODO: use div_series? */ _arb_poly_inv_series(u, v, rflen, len, wp); _arb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* otherwise use Stirling series */ arb_gamma_stirling_choose_param(&reflect, &r, &n, h, 1, 0, wp); /* rgamma(h) = (gamma(1-h+r) sin(pi h)) / (rf(1-h, r) * pi), h = h0 + t*/ if (reflect) { /* u = gamma(r+1-h) */ arb_sub_ui(f, h, r + 1, wp); arb_neg(f, f); _arb_poly_gamma_stirling_eval(t, f, n, len, wp); _arb_poly_exp_series(u, t, len, len, wp); for (i = 1; i < len; i += 2) arb_neg(u + i, u + i); /* v = sin(pi x) */ arb_const_pi(f + 1, wp); arb_mul(f, h, f + 1, wp); _arb_poly_sin_series(v, f, 2, len, wp); _arb_poly_mullow(t, u, len, v, len, len, wp); /* rf(1-h,r) * pi */ if (r == 0) { arb_const_pi(u, wp); _arb_vec_scalar_div(v, t, len, u, wp); } else { arb_sub_ui(f, h, 1, wp); arb_neg(f, f); arb_set_si(f + 1, -1); rflen = FLINT_MIN(len, r + 1); _arb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r, rflen, wp); arb_const_pi(u, wp); _arb_vec_scalar_mul(v, v, rflen, u, wp); /* divide by rising factorial */ /* TODO: might better to use div_series, when it has a good basecase */ _arb_poly_inv_series(u, v, rflen, len, wp); _arb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* rgamma(h) = rgamma(h+r) rf(h,r) */ if (r == 0) { arb_add_ui(f, h, r, wp); _arb_poly_gamma_stirling_eval(t, f, n, len, wp); _arb_vec_neg(t, t, len); _arb_poly_exp_series(v, t, len, len, wp); } else { arb_set(f, h); arb_one(f + 1); rflen = FLINT_MIN(len, r + 1); _arb_poly_rising_ui_series(t, f, FLINT_MIN(2, len), r, rflen, wp); arb_add_ui(f, h, r, wp); _arb_poly_gamma_stirling_eval(v, f, n, len, wp); _arb_vec_neg(v, v, len); _arb_poly_exp_series(u, v, len, len, wp); _arb_poly_mullow(v, u, len, t, rflen, len, wp); } } } /* compose with nonconstant part */ arb_zero(t); _arb_vec_set(t + 1, h + 1, hlen - 1); _arb_poly_compose_series(res, v, len, t, hlen, len, prec); arb_clear(f); arb_clear(f + 1); _arb_vec_clear(t, len); _arb_vec_clear(u, len); _arb_vec_clear(v, len); }
/* todo: use log(1-z) when this is better? would also need to adjust strategy in the main function */ void acb_hypgeom_dilog_bernoulli(acb_t res, const acb_t z, slong prec) { acb_t s, w, w2; slong n, k; fmpz_t c, d; mag_t m, err; double lm; int real; acb_init(s); acb_init(w); acb_init(w2); fmpz_init(c); fmpz_init(d); mag_init(m); mag_init(err); real = 0; if (acb_is_real(z)) { arb_sub_ui(acb_realref(w), acb_realref(z), 1, 30); real = arb_is_nonpositive(acb_realref(w)); } acb_log(w, z, prec); acb_get_mag(m, w); /* for k >= 4, the terms are bounded by (|w| / (2 pi))^k */ mag_set_ui_2exp_si(err, 2670177, -24); /* upper bound for 1/(2pi) */ mag_mul(err, err, m); lm = mag_get_d_log2_approx(err); if (lm < -0.25) { n = prec / (-lm) + 1; n = FLINT_MAX(n, 4); mag_geom_series(err, err, n); BERNOULLI_ENSURE_CACHED(n) acb_mul(w2, w, w, prec); for (k = n - (n % 2 == 0); k >= 3; k -= 2) { fmpz_mul_ui(c, fmpq_denref(bernoulli_cache + k - 1), k - 1); fmpz_mul_ui(d, c, (k + 1) * (k + 2)); acb_mul(s, s, w2, prec); acb_mul_fmpz(s, s, c, prec); fmpz_mul_ui(c, fmpq_numref(bernoulli_cache + k - 1), (k + 1) * (k + 2)); acb_sub_fmpz(s, s, c, prec); acb_div_fmpz(s, s, d, prec); } acb_mul(s, s, w, prec); acb_mul_2exp_si(s, s, 1); acb_sub_ui(s, s, 3, prec); acb_mul(s, s, w2, prec); acb_mul_2exp_si(s, s, -1); acb_const_pi(w2, prec); acb_addmul(s, w2, w2, prec); acb_div_ui(s, s, 6, prec); acb_neg(w2, w); acb_log(w2, w2, prec); acb_submul(s, w2, w, prec); acb_add(res, s, w, prec); acb_add_error_mag(res, err); if (real) arb_zero(acb_imagref(res)); } else { acb_indeterminate(res); } acb_clear(s); acb_clear(w); acb_clear(w2); fmpz_clear(c); fmpz_clear(d); mag_clear(m); mag_clear(err); }
void _arb_poly_zeta_series(arb_ptr res, arb_srcptr h, long hlen, const arb_t a, int deflate, long len, long prec) { long i; acb_t cs, ca; acb_ptr z; arb_ptr t, u; if (arb_contains_nonpositive(a)) { _arb_vec_indeterminate(res, len); return; } hlen = FLINT_MIN(hlen, len); z = _acb_vec_init(len); t = _arb_vec_init(len); u = _arb_vec_init(len); acb_init(cs); acb_init(ca); /* use reflection formula */ if (arf_sgn(arb_midref(h)) < 0 && arb_is_one(a)) { /* zeta(s) = (2*pi)**s * sin(pi*s/2) / pi * gamma(1-s) * zeta(1-s) */ arb_t pi; arb_ptr f, s1, s2, s3, s4; arb_init(pi); f = _arb_vec_init(2); s1 = _arb_vec_init(len); s2 = _arb_vec_init(len); s3 = _arb_vec_init(len); s4 = _arb_vec_init(len); arb_const_pi(pi, prec); /* s1 = (2*pi)**s */ arb_mul_2exp_si(pi, pi, 1); _arb_poly_pow_cpx(s1, pi, h, len, prec); arb_mul_2exp_si(pi, pi, -1); /* s2 = sin(pi*s/2) / pi */ arb_set(f, h); arb_one(f + 1); arb_mul_2exp_si(f, f, -1); arb_mul_2exp_si(f + 1, f + 1, -1); _arb_poly_sin_pi_series(s2, f, 2, len, prec); _arb_vec_scalar_div(s2, s2, len, pi, prec); /* s3 = gamma(1-s) */ arb_sub_ui(f, h, 1, prec); arb_neg(f, f); arb_set_si(f + 1, -1); _arb_poly_gamma_series(s3, f, 2, len, prec); /* s4 = zeta(1-s) */ arb_sub_ui(f, h, 1, prec); arb_neg(f, f); acb_set_arb(cs, f); acb_one(ca); _acb_poly_zeta_cpx_series(z, cs, ca, 0, len, prec); for (i = 0; i < len; i++) arb_set(s4 + i, acb_realref(z + i)); for (i = 1; i < len; i += 2) arb_neg(s4 + i, s4 + i); _arb_poly_mullow(u, s1, len, s2, len, len, prec); _arb_poly_mullow(s1, s3, len, s4, len, len, prec); _arb_poly_mullow(t, u, len, s1, len, len, prec); /* add 1/(1-(s+t)) = 1/(1-s) + t/(1-s)^2 + ... */ if (deflate) { arb_sub_ui(u, h, 1, prec); arb_neg(u, u); arb_inv(u, u, prec); for (i = 1; i < len; i++) arb_mul(u + i, u + i - 1, u, prec); _arb_vec_add(t, t, u, len, prec); } arb_clear(pi); _arb_vec_clear(f, 2); _arb_vec_clear(s1, len); _arb_vec_clear(s2, len); _arb_vec_clear(s3, len); _arb_vec_clear(s4, len); } else { acb_set_arb(cs, h); acb_set_arb(ca, a); _acb_poly_zeta_cpx_series(z, cs, ca, deflate, len, prec); for (i = 0; i < len; i++) arb_set(t + i, acb_realref(z + i)); } /* compose with nonconstant part */ arb_zero(u); _arb_vec_set(u + 1, h + 1, hlen - 1); _arb_poly_compose_series(res, t, len, u, hlen, len, prec); _acb_vec_clear(z, len); _arb_vec_clear(t, len); _arb_vec_clear(u, len); acb_init(cs); acb_init(ca); }
void Lib_Arb_Sub_Ui(ArbPtr f, ArbPtr g, uint32_t x, int32_t prec) { arb_sub_ui( (arb_ptr) f, (arb_ptr) g, x, prec); }
void _arb_poly_sin_cos_series_tangent(arb_ptr s, arb_ptr c, arb_srcptr h, slong hlen, slong len, slong prec, int times_pi) { arb_ptr t, u, v; arb_t s0, c0; hlen = FLINT_MIN(hlen, len); if (hlen == 1) { if (times_pi) arb_sin_cos_pi(s, c, h, prec); else arb_sin_cos(s, c, h, prec); _arb_vec_zero(s + 1, len - 1); _arb_vec_zero(c + 1, len - 1); return; } /* sin(x) = 2*tan(x/2)/(1+tan(x/2)^2) cos(x) = (1-tan(x/2)^2)/(1+tan(x/2)^2) */ arb_init(s0); arb_init(c0); t = _arb_vec_init(3 * len); u = t + len; v = u + len; /* sin, cos of h0 */ if (times_pi) arb_sin_cos_pi(s0, c0, h, prec); else arb_sin_cos(s0, c0, h, prec); /* t = tan((h-h0)/2) */ arb_zero(u); _arb_vec_scalar_mul_2exp_si(u + 1, h + 1, hlen - 1, -1); if (times_pi) { arb_const_pi(t, prec); _arb_vec_scalar_mul(u + 1, u + 1, hlen - 1, t, prec); } _arb_poly_tan_series(t, u, hlen, len, prec); /* v = 1 + t^2 */ _arb_poly_mullow(v, t, len, t, len, len, prec); arb_add_ui(v, v, 1, prec); /* u = 1/(1+t^2) */ _arb_poly_inv_series(u, v, len, len, prec); /* sine */ _arb_poly_mullow(s, t, len, u, len, len, prec); _arb_vec_scalar_mul_2exp_si(s, s, len, 1); /* cosine */ arb_sub_ui(v, v, 2, prec); _arb_vec_neg(v, v, len); _arb_poly_mullow(c, v, len, u, len, len, prec); /* sin(h0 + h1) = cos(h0) sin(h1) + sin(h0) cos(h1) cos(h0 + h1) = cos(h0) cos(h1) - sin(h0) sin(h1) */ if (!arb_is_zero(s0)) { _arb_vec_scalar_mul(t, s, len, c0, prec); _arb_vec_scalar_mul(u, c, len, s0, prec); _arb_vec_scalar_mul(v, s, len, s0, prec); _arb_vec_add(s, t, u, len, prec); _arb_vec_scalar_mul(t, c, len, c0, prec); _arb_vec_sub(c, t, v, len, prec); } _arb_vec_clear(t, 3 * len); arb_clear(s0); arb_clear(c0); }
int main() { slong iter; flint_rand_t state; flint_printf("sqrt1pm1...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 20000; iter++) { arb_t a, b, c, d; slong prec0, prec1, prec2; if (iter % 10 == 0) prec0 = 10000; else prec0 = 1000; prec1 = 2 + n_randint(state, prec0); prec2 = 2 + n_randint(state, prec0); arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_special(a, state, 1 + n_randint(state, prec0), 100); arb_randtest_special(b, state, 1 + n_randint(state, prec0), 100); arb_randtest_special(c, state, 1 + n_randint(state, prec0), 100); arb_sqrt1pm1(b, a, prec1); arb_sqrt1pm1(c, a, prec2); if (!arb_overlaps(b, c)) { flint_printf("FAIL: overlap\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); abort(); } /* compare with sqrt */ arb_add_ui(d, a, 1, prec2); arb_sqrt(d, d, prec2); arb_sub_ui(d, d, 1, prec2); if (!arb_overlaps(c, d)) { flint_printf("FAIL: comparison with log\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("d = "); arb_print(d); flint_printf("\n\n"); abort(); } arb_sqrt1pm1(a, a, prec1); if (!arb_overlaps(a, b)) { flint_printf("FAIL: aliasing\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { long iter; flint_rand_t state; printf("cos...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { arb_t a, b; fmpq_t q; mpfr_t t; long prec0, prec; prec0 = 400; if (iter % 100 == 0) prec0 = 8000; prec = 2 + n_randint(state, prec0); arb_init(a); arb_init(b); fmpq_init(q); mpfr_init2(t, prec0 + 100); arb_randtest(a, state, 1 + n_randint(state, prec0), 6); arb_randtest(b, state, 1 + n_randint(state, prec0), 6); arb_get_rand_fmpq(q, state, a, 1 + n_randint(state, prec0)); fmpq_get_mpfr(t, q, MPFR_RNDN); mpfr_cos(t, t, MPFR_RNDN); arb_cos(b, a, prec); if (!arb_contains_mpfr(b, t)) { printf("FAIL: containment\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); abort(); } arb_cos(a, a, prec); if (!arb_equal(a, b)) { printf("FAIL: aliasing\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(q); mpfr_clear(t); } /* check large arguments */ for (iter = 0; iter < 1000000; iter++) { arb_t a, b, c, d; long prec0, prec1, prec2; if (iter % 10 == 0) prec0 = 10000; else prec0 = 1000; prec1 = 2 + n_randint(state, prec0); prec2 = 2 + n_randint(state, prec0); arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_special(a, state, 1 + n_randint(state, prec0), prec0); arb_randtest_special(b, state, 1 + n_randint(state, prec0), 100); arb_randtest_special(c, state, 1 + n_randint(state, prec0), 100); arb_randtest_special(d, state, 1 + n_randint(state, prec0), 100); arb_cos(b, a, prec1); arb_cos(c, a, prec2); if (!arb_overlaps(b, c)) { printf("FAIL: overlap\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); abort(); } /* check cos(2a) = 2cos(a)^2-1 */ arb_mul_2exp_si(c, a, 1); arb_cos(c, c, prec1); arb_mul(b, b, b, prec1); arb_mul_2exp_si(b, b, 1); arb_sub_ui(b, b, 1, prec1); if (!arb_overlaps(b, c)) { printf("FAIL: functional equation\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main() { long iter; flint_rand_t state; printf("div_2expm1_ui...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { arb_t a, b, c; ulong n; long prec, acc1, acc2; fmpz_t t; arb_init(a); arb_init(b); arb_init(c); fmpz_init(t); prec = 2 + n_randint(state, 10000); arb_randtest(a, state, 1 + n_randint(state, 10000), 10); if (n_randint(state, 2)) n = 1 + (n_randtest(state) % (10 * prec)); else n = n_randtest(state); arb_div_2expm1_ui(b, a, n, prec); arb_one(c); if (n >= (1UL << (FLINT_BITS-1))) { arb_mul_2exp_si(c, c, (1UL << (FLINT_BITS-2))); arb_mul_2exp_si(c, c, (1UL << (FLINT_BITS-2))); arb_mul_2exp_si(c, c, n - (1UL << (FLINT_BITS-1))); } else { arb_mul_2exp_si(c, c, n); } arb_sub_ui(c, c, 1, prec); arb_div(c, a, c, prec); acc1 = arb_rel_accuracy_bits(a); acc2 = arb_rel_accuracy_bits(b); if (!arb_overlaps(b, c)) { printf("FAIL: containment\n\n"); printf("n = %lu\n", n); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); abort(); } if (n > 0 && (acc2 < FLINT_MIN(prec, acc1) - 10) && !(acc1 == -ARF_PREC_EXACT && acc2 == -ARF_PREC_EXACT)) { printf("FAIL: poor accuracy\n\n"); printf("prec=%ld, acc1=%ld, acc2=%ld\n\n", prec, acc1, acc2); printf("n = %lu\n\n", n); printf("a = "); arb_print(a); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); fmpz_clear(t); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void arb_exp_arf_bb(arb_t z, const arf_t x, slong prec, int minus_one) { slong k, iter, bits, r, mag, q, wp, N; slong argred_bits, start_bits; mp_bitcnt_t Qexp[1]; int inexact; fmpz_t t, u, T, Q; arb_t w; if (arf_is_zero(x)) { if (minus_one) arb_zero(z); else arb_one(z); return; } if (arf_is_special(x)) { abort(); } mag = arf_abs_bound_lt_2exp_si(x); /* We assume that this function only gets called with something reasonable as input (huge/tiny input will be handled by the main exp wrapper). */ if (mag > 200 || mag < -2 * prec - 100) { flint_printf("arb_exp_arf_bb: unexpectedly large/small input\n"); abort(); } if (prec < 100000000) { argred_bits = 16; start_bits = 32; } else { argred_bits = 32; start_bits = 64; } /* Argument reduction: exp(x) -> exp(x/2^q). This improves efficiency of the first iteration in the bit-burst algorithm. */ q = FLINT_MAX(0, mag + argred_bits); /* Determine working precision. */ wp = prec + 10 + 2 * q + 2 * FLINT_BIT_COUNT(prec); if (minus_one && mag < 0) wp += (-mag); fmpz_init(t); fmpz_init(u); fmpz_init(Q); fmpz_init(T); arb_init(w); /* Convert x/2^q to a fixed-point number. */ inexact = arf_get_fmpz_fixed_si(t, x, -wp + q); /* Aliasing of z and x is safe now that only use t. */ /* Start with z = 1. */ arb_one(z); /* Bit-burst loop. */ for (iter = 0, bits = start_bits; !fmpz_is_zero(t); iter++, bits *= 2) { /* Extract bits. */ r = FLINT_MIN(bits, wp); fmpz_tdiv_q_2exp(u, t, wp - r); /* Binary splitting (+1 fixed-point ulp truncation error). */ mag = fmpz_bits(u) - r; N = bs_num_terms(mag, wp); _arb_exp_sum_bs_powtab(T, Q, Qexp, u, r, N); /* T = T / Q (+1 fixed-point ulp error). */ if (*Qexp >= wp) { fmpz_tdiv_q_2exp(T, T, *Qexp - wp); fmpz_tdiv_q(T, T, Q); } else { fmpz_mul_2exp(T, T, wp - *Qexp); fmpz_tdiv_q(T, T, Q); } /* T = 1 + T */ fmpz_one(Q); fmpz_mul_2exp(Q, Q, wp); fmpz_add(T, T, Q); /* Now T = exp(u) with at most 2 fixed-point ulp error. */ /* Set z = z * T. */ arf_set_fmpz(arb_midref(w), T); arf_mul_2exp_si(arb_midref(w), arb_midref(w), -wp); mag_set_ui_2exp_si(arb_radref(w), 2, -wp); arb_mul(z, z, w, wp); /* Remove used bits. */ fmpz_mul_2exp(u, u, wp - r); fmpz_sub(t, t, u); } /* We have exp(x + eps) - exp(x) < 2*eps (by assumption that the argument reduction is large enough). */ if (inexact) arb_add_error_2exp_si(z, -wp + 1); fmpz_clear(t); fmpz_clear(u); fmpz_clear(Q); fmpz_clear(T); arb_clear(w); /* exp(x) = exp(x/2^q)^(2^q) */ for (k = 0; k < q; k++) arb_mul(z, z, z, wp); if (minus_one) arb_sub_ui(z, z, 1, wp); arb_set_round(z, z, prec); }