void fmprb_mul_main_naive(fmprb_t z, const fmprb_t x, const fmprb_t y, slong prec) { if (fmpr_is_pos_inf(fmprb_radref(x)) || fmpr_is_pos_inf(fmprb_radref(y))) { fmpr_mul(fmprb_midref(z), fmprb_midref(x), fmprb_midref(y), prec, FMPR_RND_DOWN); fmpr_pos_inf(fmprb_radref(z)); } else { fmpr_t t, u; slong r; fmpr_init(t); fmpr_init(u); /* (x+a)*(y+b) = x*y + x*b + y*a + a*b*/ fmpr_mul(t, fmprb_midref(x), fmprb_radref(y), FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_abs(t, t); fmpr_mul(u, fmprb_midref(y), fmprb_radref(x), FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_abs(u, u); fmpr_add(t, t, u, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_addmul(t, fmprb_radref(x), fmprb_radref(y), FMPRB_RAD_PREC, FMPR_RND_UP); r = fmpr_mul(fmprb_midref(z), fmprb_midref(x), fmprb_midref(y), prec, FMPR_RND_DOWN); fmpr_add_error_result(fmprb_radref(z), t, fmprb_midref(z), r, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_clear(t); fmpr_clear(u); } }
void fmprb_mul_naive(fmprb_t z, const fmprb_t x, const fmprb_t y, slong prec) { if (fmprb_is_exact(x)) fmprb_mul_fmpr_naive(z, y, fmprb_midref(x), prec); else if (fmprb_is_exact(y)) fmprb_mul_fmpr_naive(z, x, fmprb_midref(y), prec); else fmprb_mul_main_naive(z, x, y, prec); fmprb_adjust(z); }
void fmprb_add(fmprb_t z, const fmprb_t x, const fmprb_t y, slong prec) { slong r; fmpr_add(fmprb_radref(z), fmprb_radref(x), fmprb_radref(y), FMPRB_RAD_PREC, FMPR_RND_UP); r = fmpr_add(fmprb_midref(z), fmprb_midref(x), fmprb_midref(y), prec, FMPR_RND_DOWN); fmpr_add_error_result(fmprb_radref(z), fmprb_radref(z), fmprb_midref(z), r, FMPRB_RAD_PREC, FMPR_RND_UP); fmprb_adjust(z); }
void fmprb_mul_fmpr_naive(fmprb_t z, const fmprb_t x, const fmpr_t y, slong prec) { /* (x+a) * y = x*y + y*a */ if (fmpr_is_pos_inf(fmprb_radref(x))) { fmpr_mul(fmprb_midref(z), fmprb_midref(x), y, prec, FMPR_RND_DOWN); fmpr_pos_inf(fmprb_radref(z)); } else if (fmpr_is_zero(fmprb_radref(x))) { slong r; r = fmpr_mul(fmprb_midref(z), fmprb_midref(x), y, prec, FMPR_RND_DOWN); fmpr_set_error_result(fmprb_radref(z), fmprb_midref(z), r); } else { slong r; fmpr_mul(fmprb_radref(z), fmprb_radref(x), y, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_abs(fmprb_radref(z), fmprb_radref(z)); r = fmpr_mul(fmprb_midref(z), fmprb_midref(x), y, prec, FMPR_RND_DOWN); fmpr_add_error_result(fmprb_radref(z), fmprb_radref(z), fmprb_midref(z), r, FMPRB_RAD_PREC, FMPR_RND_UP); } }
void fmprb_sqrt(fmprb_t z, const fmprb_t x, slong prec) { slong r; if (fmprb_contains_negative(x)) { fmpr_nan(fmprb_midref(z)); fmpr_pos_inf(fmprb_radref(z)); return; } if (fmprb_is_exact(x)) { r = fmpr_sqrt(fmprb_midref(z), fmprb_midref(x), prec, FMPR_RND_DOWN); fmpr_set_error_result(fmprb_radref(z), fmprb_midref(z), r); } else { fmpr_t err; fmpr_init(err); fmpr_sub(err, fmprb_midref(x), fmprb_radref(x), FMPRB_RAD_PREC, FMPR_RND_DOWN); fmpr_rsqrt(err, err, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_mul(err, fmprb_radref(x), err, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_mul_2exp_si(err, err, -1); r = fmpr_sqrt(fmprb_midref(z), fmprb_midref(x), prec, FMPR_RND_DOWN); fmpr_add_error_result(fmprb_radref(z), err, fmprb_midref(z), r, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_clear(err); } fmprb_adjust(z); }
void fmprb_tanh(fmprb_t y, const fmprb_t x, long prec) { fmprb_t t, u; fmprb_init(t); fmprb_init(u); fmprb_mul_2exp_si(t, x, 1); if (fmpr_sgn(fmprb_midref(x)) >= 0) { fmprb_neg(t, t); fmprb_expm1(t, t, prec + 4); fmprb_add_ui(y, t, 2, prec + 4); fmprb_div(y, t, y, prec); fmprb_neg(y, y); } else { fmprb_expm1(t, t, prec + 4); fmprb_add_ui(y, t, 2, prec + 4); fmprb_div(y, t, y, prec); } fmprb_clear(t); fmprb_clear(u); }
void fmprb_hypgeom_infsum(fmprb_t P, fmprb_t Q, hypgeom_t hyp, long target_prec, long prec) { mag_t err, z; long n; mag_init(err); mag_init(z); mag_set_fmpz(z, hyp->P->coeffs + hyp->P->length - 1); mag_div_fmpz(z, z, hyp->Q->coeffs + hyp->Q->length - 1); if (!hyp->have_precomputed) { hypgeom_precompute(hyp); hyp->have_precomputed = 1; } n = hypgeom_bound(err, hyp->r, hyp->boundC, hyp->boundD, hyp->boundK, hyp->MK, z, target_prec); fmprb_hypgeom_sum(P, Q, hyp, n, prec); if (fmpr_sgn(fmprb_midref(Q)) < 0) { fmprb_neg(P, P); fmprb_neg(Q, Q); } /* We have p/q = s + err i.e. (p + q*err)/q = s */ { fmpr_t u, v; fmpr_init(u); fmpr_init(v); mag_get_fmpr(v, err); fmpr_add(u, fmprb_midref(Q), fmprb_radref(Q), FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_mul(u, u, v, FMPRB_RAD_PREC, FMPR_RND_UP); fmprb_add_error_fmpr(P, u); fmpr_clear(u); fmpr_clear(v); } mag_clear(z); mag_clear(err); }
void _fmpcb_poly_refine_roots_durand_kerner(fmpcb_ptr roots, fmpcb_srcptr poly, long len, long prec) { long i, j; fmpcb_t x, y, t; fmpcb_init(x); fmpcb_init(y); fmpcb_init(t); for (i = 0; i < len - 1; i++) { _fmpcb_poly_evaluate_mid(x, poly, len, roots + i, prec); fmpcb_set(y, poly + len - 1); for (j = 0; j < len - 1; j++) { if (i != j) { fmpcb_sub_mid(t, roots + i, roots + j, prec); fmpcb_mul_mid(y, y, t, prec); } } fmpr_zero(fmprb_radref(fmpcb_realref(y))); fmpr_zero(fmprb_radref(fmpcb_imagref(y))); fmpcb_inv_mid(t, y, prec); fmpcb_mul_mid(t, t, x, prec); fmpcb_sub_mid(roots + i, roots + i, t, prec); fmpr_set_round(fmprb_radref(fmpcb_realref(roots + i)), fmprb_midref(fmpcb_realref(t)), FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_set_round(fmprb_radref(fmpcb_imagref(roots + i)), fmprb_midref(fmpcb_imagref(t)), FMPRB_RAD_PREC, FMPR_RND_UP); } fmpcb_clear(x); fmpcb_clear(y); fmpcb_clear(t); }
void bernoulli_rev_init(bernoulli_rev_t iter, ulong nmax) { long j; fmpz_t t; fmprb_t x; int round1, round2; long wp; nmax -= (nmax % 2); iter->n = nmax; iter->alloc = 0; if (nmax < BERNOULLI_REV_MIN) return; iter->prec = wp = global_prec(nmax); iter->max_power = zeta_terms(nmax, iter->prec); iter->alloc = iter->max_power + 1; iter->powers = _fmpz_vec_init(iter->alloc); fmpz_init(iter->pow_error); fmprb_init(iter->prefactor); fmprb_init(iter->two_pi_squared); fmprb_init(x); fmpz_init(t); /* precompute powers */ for (j = 3; j <= iter->max_power; j += 2) { fmprb_ui_pow_ui(x, j, nmax, power_prec(j, nmax, wp)); fmprb_ui_div(x, 1UL, x, power_prec(j, nmax, wp)); round1 = fmpr_get_fmpz_fixed_si(t, fmprb_midref(x), -wp); fmpz_set(iter->powers + j, t); /* error: the radius, plus two roundings */ round2 = fmpr_get_fmpz_fixed_si(t, fmprb_radref(x), -wp); fmpz_add_ui(t, t, (round1 != 0) + (round2 != 0)); if (fmpz_cmp(iter->pow_error, t) < 0) fmpz_set(iter->pow_error, t); } /* precompute (2pi)^2 and 2*(n!)/(2pi)^n */ fmprb_fac_ui(iter->prefactor, nmax, wp); fmprb_mul_2exp_si(iter->prefactor, iter->prefactor, 1); fmprb_const_pi(x, wp); fmprb_mul_2exp_si(x, x, 1); fmprb_mul(iter->two_pi_squared, x, x, wp); fmprb_pow_ui(x, iter->two_pi_squared, nmax / 2, wp); fmprb_div(iter->prefactor, iter->prefactor, x, wp); fmpz_clear(t); fmprb_clear(x); }
void fmprb_get_interval_fmpz_2exp(fmpz_t a, fmpz_t b, fmpz_t exp, const fmprb_t x) { if (fmprb_is_exact(x)) { fmpr_get_fmpz_2exp(a, exp, fmprb_midref(x)); fmpz_set(b, a); } else { fmpr_t t; fmpz_t exp2; slong s; fmpr_init(t); fmpz_init(exp2); fmpr_sub(t, fmprb_midref(x), fmprb_radref(x), FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_get_fmpz_2exp(a, exp, t); fmpr_add(t, fmprb_midref(x), fmprb_radref(x), FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_get_fmpz_2exp(b, exp2, t); s = _fmpz_sub_small(exp, exp2); if (s <= 0) { fmpz_mul_2exp(b, b, -s); } else { fmpz_mul_2exp(a, a, s); fmpz_set(exp, exp2); } fmpr_clear(t); fmpz_clear(exp2); } }
int main() { long iter; flint_rand_t state; printf("cauchy_bound...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100; iter++) { fmprb_t b, radius, ans; fmpcb_t x; long r, prec, maxdepth; fmprb_init(b); fmprb_init(radius); fmpcb_init(x); fmpcb_set_ui(x, 5); r = 1 + n_randint(state, 10); fmprb_set_ui(radius, r); prec = 2 + n_randint(state, 100); maxdepth = n_randint(state, 10); fmpcb_calc_cauchy_bound(b, sin_x, NULL, x, radius, maxdepth, prec); fmpr_set_d(fmprb_midref(ans), answers[r-1]); fmpr_set_d(fmprb_radref(ans), 1e-8); if (!fmprb_overlaps(b, ans)) { printf("FAIL\n"); printf("r = %ld, prec = %ld, maxdepth = %ld\n\n", r, prec, maxdepth); fmprb_printd(b, 15); printf("\n\n"); fmprb_printd(ans, 15); printf("\n\n"); abort(); } fmprb_clear(b); fmprb_clear(radius); fmpcb_clear(x); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
long gamma_taylor_bound_mag(long n) { fmprb_t t, u; long v; fmprb_init(t); fmprb_init(u); /* (pi-1) n */ fmprb_const_pi(t, FMPRB_RAD_PREC); fmprb_sub_ui(t, t, 1, FMPRB_RAD_PREC); fmprb_mul_ui(t, t, n, FMPRB_RAD_PREC); /* (3-5n) log(n/6) */ fmprb_set_ui(u, n); fmprb_div_ui(u, u, 6, FMPRB_RAD_PREC); fmprb_log(u, u, FMPRB_RAD_PREC); fmprb_mul_si(u, u, 3 - 5*n, FMPRB_RAD_PREC); fmprb_add(t, t, u, FMPRB_RAD_PREC); /* divide by 6 log(2) */ fmprb_log_ui(u, 2, FMPRB_RAD_PREC); fmprb_mul_ui(u, u, 6, FMPRB_RAD_PREC); fmprb_div(t, t, u, FMPRB_RAD_PREC); /* upper bound */ fmpr_add(fmprb_midref(t), fmprb_midref(t), fmprb_radref(t), FMPRB_RAD_PREC, FMPR_RND_CEIL); v = fmpr_get_si(fmprb_midref(t), FMPR_RND_CEIL); fmprb_clear(t); fmprb_clear(u); return v; }
void fmprb_add_error(fmprb_t x, const fmprb_t error) { fmpr_t high; fmpr_init(high); fmpr_add(high, fmprb_midref(error), fmprb_radref(error), FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_add(fmprb_radref(x), fmprb_radref(x), high, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_clear(high); }
int fmpcb_cmpabs_approx(const fmpcb_t x, const fmpcb_t y) { const fmpr_struct *xm, *ym; if (fmpr_cmpabs(fmprb_midref(fmpcb_realref(x)), fmprb_midref(fmpcb_imagref(x))) >= 0) xm = fmprb_midref(fmpcb_realref(x)); else xm = fmprb_midref(fmpcb_imagref(x)); if (fmpr_cmpabs(fmprb_midref(fmpcb_realref(y)), fmprb_midref(fmpcb_imagref(y))) >= 0) ym = fmprb_midref(fmpcb_realref(y)); else ym = fmprb_midref(fmpcb_imagref(y)); return fmpr_cmpabs(xm, ym); }
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 fmpcb_add_mid(fmpcb_t z, const fmpcb_t x, const fmpcb_t y, long prec) { fmpr_add(fmprb_midref(fmpcb_realref(z)), fmprb_midref(fmpcb_realref(x)), fmprb_midref(fmpcb_realref(y)), prec, FMPR_RND_DOWN); fmpr_add(fmprb_midref(fmpcb_imagref(z)), fmprb_midref(fmpcb_imagref(x)), fmprb_midref(fmpcb_imagref(y)), prec, FMPR_RND_DOWN); }
void zeta_ui_borwein_bsplit(fmprb_t x, ulong s, long prec) { zeta_bsplit_t sum; fmpr_t err; long wp, n; /* zeta(0) = -1/2 */ if (s == 0) { fmpr_set_si_2exp_si(fmprb_midref(x), -1, -1); fmpr_zero(fmprb_radref(x)); return; } if (s == 1) { printf("zeta_ui_borwein_bsplit: zeta(1)"); abort(); } n = prec / ERROR_B + 2; wp = prec + 30; zeta_bsplit_init(sum); zeta_bsplit(sum, 0, n + 1, n, s, 0, wp); /* A/Q3 - B/Q3 / (C/Q1) = (A*C - B*Q1) / (Q3*C) */ fmprb_mul(sum->A, sum->A, sum->C, wp); fmprb_mul(sum->B, sum->B, sum->Q1, wp); fmprb_sub(sum->A, sum->A, sum->B, wp); fmprb_mul(sum->Q3, sum->Q3, sum->C, wp); fmprb_div(sum->C, sum->A, sum->Q3, wp); fmpr_init(err); borwein_error(err, n); fmprb_add_error_fmpr(sum->C, err); fmpr_clear(err); /* convert from eta(s) to zeta(s) */ fmprb_div_2expm1_ui(x, sum->C, s - 1, wp); fmprb_mul_2exp_si(x, x, s - 1); zeta_bsplit_clear(sum); }
void fmprb_get_rand_fmpq(fmpq_t q, flint_rand_t state, const fmprb_t x, long bits) { /* there is only one rational */ if (fmprb_is_exact(x)) { fmpr_get_fmpq(q, fmprb_midref(x)); return; } /* pick a denominator */ fmpz_randbits(fmpq_denref(q), state, n_randint(state, bits + 1)); fmpz_abs(fmpq_denref(q), fmpq_denref(q)); if (fmpz_is_zero(fmpq_denref(q))) fmpz_one(fmpq_denref(q)); _fmprb_get_rand_fmpq(fmpq_numref(q), fmpq_denref(q), state, fmpq_denref(q), x); fmpq_canonicalise(q); }
void bound_rfac(fmprb_ptr F, const fmpcb_t s, ulong n, long len, long wp) { if (len == 1) { fmpcb_rfac_abs_ubound2(fmprb_midref(F + 0), s, n, wp); fmpr_zero(fmprb_radref(F + 0)); } else { fmprb_struct sx[2]; fmprb_init(sx + 0); fmprb_init(sx + 1); fmpcb_abs(sx + 0, s, wp); fmprb_one(sx + 1); _fmprb_vec_zero(F, len); _fmprb_poly_rising_ui_series(F, sx, 2, n, len, wp); fmprb_clear(sx + 0); fmprb_clear(sx + 1); } }
/* Absolute value of rising factorial (could speed up once complex gamma is available). */ void fmpcb_rfac_abs_ubound2(fmpr_t bound, const fmpcb_t s, ulong n, long prec) { fmpr_t term, t; ulong k; /* M(k) = (a+k)^2 + b^2 M(0) = a^2 + b^2 M(k+1) = M(k) + 2*a + (2*k+1) */ fmpr_init(t); fmpr_init(term); fmpr_one(bound); /* M(0) = a^2 + b^2 */ fmprb_get_abs_ubound_fmpr(t, fmpcb_realref(s), prec); fmpr_mul(term, t, t, prec, FMPR_RND_UP); fmprb_get_abs_ubound_fmpr(t, fmpcb_imagref(s), prec); fmpr_mul(t, t, t, prec, FMPR_RND_UP); fmpr_add(term, term, t, prec, FMPR_RND_UP); /* we add t = 2*a to each term. note that this can be signed; we always want the most positive value */ fmpr_add(t, fmprb_midref(fmpcb_realref(s)), fmprb_radref(fmpcb_realref(s)), prec, FMPR_RND_CEIL); fmpr_mul_2exp_si(t, t, 1); for (k = 0; k < n; k++) { fmpr_mul(bound, bound, term, prec, FMPR_RND_UP); fmpr_add_ui(term, term, 2 * k + 1, prec, FMPR_RND_UP); fmpr_add(term, term, t, prec, FMPR_RND_UP); } fmpr_sqrt(bound, bound, prec, FMPR_RND_UP); fmpr_clear(t); fmpr_clear(term); }
int _fmprb_poly_mid_get_hull(fmpz_t bot_exp, fmpz_t top_exp, fmprb_srcptr A, long lenA) { long i; fmpz_t t; int have_nonzero = 0; fmpz_init(t); fmpz_zero(bot_exp); fmpz_zero(top_exp); for (i = 0; i < lenA; i++) { if (fmpr_is_normal(fmprb_midref(A + i))) { if (!have_nonzero) { have_nonzero = 1; fmpr_get_bot_exp(bot_exp, fmprb_midref(A + i)); fmpr_get_top_exp(top_exp, fmprb_midref(A + i)); } else { fmpr_get_bot_exp(t, fmprb_midref(A + i)); if (fmpz_cmp(t, bot_exp) < 0) fmpz_swap(t, bot_exp); fmpr_get_top_exp(t, fmprb_midref(A + i)); if (fmpz_cmp(t, top_exp) > 0) fmpz_swap(t, top_exp); } } else if (!fmpr_is_zero(fmprb_midref(A + i))) { printf("exception: inf or nan encountered in polynomial\n"); abort(); } } fmpz_clear(t); return have_nonzero; }
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); } }
void _fmprb_poly_rgamma_series(fmprb_ptr res, fmprb_srcptr h, long hlen, long len, long prec) { int reflect; long i, rflen, r, n, wp; fmprb_ptr t, u, v; fmprb_struct f[2]; hlen = FLINT_MIN(hlen, len); wp = prec + FLINT_BIT_COUNT(prec); t = _fmprb_vec_init(len); u = _fmprb_vec_init(len); v = _fmprb_vec_init(len); fmprb_init(f); fmprb_init(f + 1); /* use zeta values at small integers */ if (fmprb_is_int(h) && (fmpr_cmpabs_ui(fmprb_midref(h), prec / 2) < 0)) { r = fmpr_get_si(fmprb_midref(h), FMPR_RND_DOWN); gamma_lgamma_series_at_one(u, len, wp); _fmprb_vec_neg(u, u, len); _fmprb_poly_exp_series(t, u, len, len, wp); if (r == 1) { _fmprb_vec_swap(v, t, len); } else if (r <= 0) { fmprb_set(f, h); fmprb_one(f + 1); rflen = FLINT_MIN(len, 2 - r); _fmprb_poly_rising_ui_series(u, f, FLINT_MIN(2, len), 1 - r, rflen, wp); _fmprb_poly_mullow(v, t, len, u, rflen, len, wp); } else { fmprb_one(f); fmprb_one(f + 1); rflen = FLINT_MIN(len, r); _fmprb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r - 1, rflen, wp); /* TODO: use div_series? */ _fmprb_poly_inv_series(u, v, rflen, len, wp); _fmprb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* otherwise use Stirling series */ gamma_stirling_choose_param_fmprb(&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) */ fmprb_sub_ui(f, h, r + 1, wp); fmprb_neg(f, f); gamma_stirling_eval_fmprb_series(t, f, n, len, wp); _fmprb_poly_exp_series(u, t, len, len, wp); for (i = 1; i < len; i += 2) fmprb_neg(u + i, u + i); /* v = sin(pi x) */ fmprb_const_pi(f + 1, wp); fmprb_mul(f, h, f + 1, wp); _fmprb_poly_sin_series(v, f, 2, len, wp); _fmprb_poly_mullow(t, u, len, v, len, len, wp); /* rf(1-h,r) * pi */ if (r == 0) { fmprb_const_pi(u, wp); _fmprb_vec_scalar_div(v, t, len, u, wp); } else { fmprb_sub_ui(f, h, 1, wp); fmprb_neg(f, f); fmprb_set_si(f + 1, -1); rflen = FLINT_MIN(len, r + 1); _fmprb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r, rflen, wp); fmprb_const_pi(u, wp); _fmprb_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 */ _fmprb_poly_inv_series(u, v, rflen, len, wp); _fmprb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* rgamma(h) = rgamma(h+r) rf(h,r) */ if (r == 0) { fmprb_add_ui(f, h, r, wp); gamma_stirling_eval_fmprb_series(t, f, n, len, wp); _fmprb_vec_neg(t, t, len); _fmprb_poly_exp_series(v, t, len, len, wp); } else { fmprb_set(f, h); fmprb_one(f + 1); rflen = FLINT_MIN(len, r + 1); _fmprb_poly_rising_ui_series(t, f, FLINT_MIN(2, len), r, rflen, wp); fmprb_add_ui(f, h, r, wp); gamma_stirling_eval_fmprb_series(v, f, n, len, wp); _fmprb_vec_neg(v, v, len); _fmprb_poly_exp_series(u, v, len, len, wp); _fmprb_poly_mullow(v, u, len, t, rflen, len, wp); } } } /* compose with nonconstant part */ fmprb_zero(t); _fmprb_vec_set(t + 1, h + 1, hlen - 1); _fmprb_poly_compose_series(res, v, len, t, hlen, len, prec); fmprb_clear(f); fmprb_clear(f + 1); _fmprb_vec_clear(t, len); _fmprb_vec_clear(u, len); _fmprb_vec_clear(v, len); }
/* 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); }
void gamma_stirling_eval_fmprb(fmprb_t s, const fmprb_t z, long nterms, int digamma, long prec) { fmprb_t b, t, logz, zinv, zinv2; fmpr_t err; long k, term_prec; double z_mag, term_mag; fmprb_init(b); fmprb_init(t); fmprb_init(logz); fmprb_init(zinv); fmprb_init(zinv2); fmprb_log(logz, z, prec); fmprb_ui_div(zinv, 1UL, z, prec); nterms = FLINT_MAX(nterms, 1); fmprb_zero(s); if (nterms > 1) { fmprb_mul(zinv2, zinv, zinv, prec); z_mag = fmpr_get_d(fmprb_midref(logz), FMPR_RND_UP) * 1.44269504088896; for (k = nterms - 1; k >= 1; k--) { term_mag = bernoulli_bound_2exp_si(2 * k); term_mag -= (2 * k - 1) * z_mag; term_prec = prec + term_mag; term_prec = FLINT_MIN(term_prec, prec); term_prec = FLINT_MAX(term_prec, 10); if (prec > 2000) { fmprb_set_round(t, zinv2, term_prec); fmprb_mul(s, s, t, term_prec); } else fmprb_mul(s, s, zinv2, term_prec); gamma_stirling_coeff(b, k, digamma, term_prec); fmprb_add(s, s, b, term_prec); } if (digamma) fmprb_mul(s, s, zinv2, prec); else fmprb_mul(s, s, zinv, prec); } /* remainder bound */ fmpr_init(err); gamma_stirling_bound_fmprb(err, z, digamma ? 1 : 0, 1, nterms); fmprb_add_error_fmpr(s, err); fmpr_clear(err); if (digamma) { fmprb_neg(s, s); fmprb_mul_2exp_si(zinv, zinv, -1); fmprb_sub(s, s, zinv, prec); fmprb_add(s, s, logz, prec); } else { /* (z-0.5)*log(z) - z + log(2*pi)/2 */ fmprb_one(t); fmprb_mul_2exp_si(t, t, -1); fmprb_sub(t, z, t, prec); fmprb_mul(t, logz, t, prec); fmprb_add(s, s, t, prec); fmprb_sub(s, s, z, prec); fmprb_const_log_sqrt2pi(t, prec); fmprb_add(s, s, t, prec); } fmprb_clear(t); fmprb_clear(b); fmprb_clear(zinv); fmprb_clear(zinv2); fmprb_clear(logz); }
void zeta_series_em_vec_bound(fmprb_ptr bound, const fmpcb_t s, const fmpcb_t a, ulong N, ulong M, long len, long wp) { fmprb_t K, C, AN, S2M; fmprb_ptr F, R; long k; fmprb_srcptr alpha = fmpcb_realref(a); fmprb_srcptr beta = fmpcb_imagref(a); fmprb_srcptr sigma = fmpcb_realref(s); fmprb_srcptr tau = fmpcb_imagref(s); fmprb_init(AN); fmprb_init(S2M); /* require alpha + N > 1, sigma + 2M > 1 */ fmprb_add_ui(AN, alpha, N - 1, wp); fmprb_add_ui(S2M, sigma, 2*M - 1, wp); if (!fmprb_is_positive(AN) || !fmprb_is_positive(S2M) || N < 1 || M < 1) { fmprb_clear(AN); fmprb_clear(S2M); for (k = 0; k < len; k++) { fmpr_pos_inf(fmprb_midref(bound + k)); fmpr_zero(fmprb_radref(bound + k)); } return; } /* alpha + N, sigma + 2M */ fmprb_add_ui(AN, AN, 1, wp); fmprb_add_ui(S2M, S2M, 1, wp); R = _fmprb_vec_init(len); F = _fmprb_vec_init(len); fmprb_init(K); fmprb_init(C); /* bound for power integral */ bound_C(C, AN, beta, wp); bound_K(K, AN, beta, tau, wp); bound_I(R, AN, S2M, C, len, wp); for (k = 0; k < len; k++) { fmprb_mul(R + k, R + k, K, wp); fmprb_div_ui(K, K, k + 1, wp); } /* bound for rising factorial */ bound_rfac(F, s, 2*M, len, wp); /* product (TODO: only need upper bound; write a function for this) */ _fmprb_poly_mullow(bound, F, len, R, len, len, wp); /* bound for bernoulli polynomials, 4 / (2pi)^(2M) */ fmprb_const_pi(C, wp); fmprb_mul_2exp_si(C, C, 1); fmprb_pow_ui(C, C, 2 * M, wp); fmprb_ui_div(C, 4, C, wp); _fmprb_vec_scalar_mul(bound, bound, len, C, wp); fmprb_clear(K); fmprb_clear(C); fmprb_clear(AN); fmprb_clear(S2M); _fmprb_vec_clear(R, len); _fmprb_vec_clear(F, len); }
void _fmprb_poly_zeta_series(fmprb_ptr res, fmprb_srcptr h, long hlen, const fmprb_t a, int deflate, long len, long prec) { long i; fmpcb_t cs, ca; fmpcb_ptr z; fmprb_ptr t, u; if (fmprb_contains_nonpositive(a)) { _fmprb_vec_indeterminate(res, len); return; } hlen = FLINT_MIN(hlen, len); z = _fmpcb_vec_init(len); t = _fmprb_vec_init(len); u = _fmprb_vec_init(len); fmpcb_init(cs); fmpcb_init(ca); /* use reflection formula */ if (fmpr_sgn(fmprb_midref(h)) < 0 && fmprb_is_one(a)) { /* zeta(s) = (2*pi)**s * sin(pi*s/2) / pi * gamma(1-s) * zeta(1-s) */ fmprb_t pi; fmprb_ptr f, s1, s2, s3, s4; fmprb_init(pi); f = _fmprb_vec_init(2); s1 = _fmprb_vec_init(len); s2 = _fmprb_vec_init(len); s3 = _fmprb_vec_init(len); s4 = _fmprb_vec_init(len); fmprb_const_pi(pi, prec); /* s1 = (2*pi)**s */ fmprb_mul_2exp_si(pi, pi, 1); _fmprb_poly_pow_cpx(s1, pi, h, len, prec); fmprb_mul_2exp_si(pi, pi, -1); /* s2 = sin(pi*s/2) / pi */ fmprb_mul_2exp_si(pi, pi, -1); fmprb_mul(f, pi, h, prec); fmprb_set(f + 1, pi); fmprb_mul_2exp_si(pi, pi, 1); _fmprb_poly_sin_series(s2, f, 2, len, prec); _fmprb_vec_scalar_div(s2, s2, len, pi, prec); /* s3 = gamma(1-s) */ fmprb_sub_ui(f, h, 1, prec); fmprb_neg(f, f); fmprb_set_si(f + 1, -1); _fmprb_poly_gamma_series(s3, f, 2, len, prec); /* s4 = zeta(1-s) */ fmprb_sub_ui(f, h, 1, prec); fmprb_neg(f, f); fmpcb_set_fmprb(cs, f); fmpcb_one(ca); zeta_series(z, cs, ca, 0, len, prec); for (i = 0; i < len; i++) fmprb_set(s4 + i, fmpcb_realref(z + i)); for (i = 1; i < len; i += 2) fmprb_neg(s4 + i, s4 + i); _fmprb_poly_mullow(u, s1, len, s2, len, len, prec); _fmprb_poly_mullow(s1, s3, len, s4, len, len, prec); _fmprb_poly_mullow(t, u, len, s1, len, len, prec); /* add 1/(1-(s+t)) = 1/(1-s) + t/(1-s)^2 + ... */ if (deflate) { fmprb_sub_ui(u, h, 1, prec); fmprb_neg(u, u); fmprb_ui_div(u, 1, u, prec); for (i = 1; i < len; i++) fmprb_mul(u + i, u + i - 1, u, prec); _fmprb_vec_add(t, t, u, len, prec); } fmprb_clear(pi); _fmprb_vec_clear(f, 2); _fmprb_vec_clear(s1, len); _fmprb_vec_clear(s2, len); _fmprb_vec_clear(s3, len); _fmprb_vec_clear(s4, len); } else { fmpcb_set_fmprb(cs, h); fmpcb_set_fmprb(ca, a); zeta_series(z, cs, ca, deflate, len, prec); for (i = 0; i < len; i++) fmprb_set(t + i, fmpcb_realref(z + i)); } /* compose with nonconstant part */ fmprb_zero(u); _fmprb_vec_set(u + 1, h + 1, hlen - 1); _fmprb_poly_compose_series(res, t, len, u, hlen, len, prec); _fmpcb_vec_clear(z, len); _fmprb_vec_clear(t, len); _fmprb_vec_clear(u, len); fmpcb_init(cs); fmpcb_init(ca); }
void fmpcb_calc_cauchy_bound(fmprb_t bound, fmpcb_calc_func_t func, void * param, const fmpcb_t x, const fmprb_t radius, long maxdepth, long prec) { long i, n, depth, wp; fmprb_t pi, theta, v, s1, c1, s2, c2, st, ct; fmpcb_t t, u; fmprb_t b; fmprb_init(pi); fmprb_init(theta); fmprb_init(v); fmprb_init(s1); fmprb_init(c1); fmprb_init(s2); fmprb_init(c2); fmprb_init(st); fmprb_init(ct); fmpcb_init(t); fmpcb_init(u); fmprb_init(b); wp = prec + 20; fmprb_const_pi(pi, wp); fmprb_zero_pm_inf(b); for (depth = 0, n = 16; depth < maxdepth; n *= 2, depth++) { fmprb_zero(b); /* theta = 2 pi / n */ fmprb_div_ui(theta, pi, n, wp); fmprb_mul_2exp_si(theta, theta, 1); /* sine and cosine of i*theta and (i+1)*theta */ fmprb_zero(s1); fmprb_one(c1); fmprb_sin_cos(st, ct, theta, wp); fmprb_set(s2, st); fmprb_set(c2, ct); for (i = 0; i < n; i++) { /* sine and cosine of 2 pi ([i,i+1]/n) */ /* since we use power of two subdivision points, the sine and cosine are monotone on each subinterval */ fmprb_union(fmpcb_realref(t), c1, c2, wp); fmprb_union(fmpcb_imagref(t), s1, s2, wp); fmpcb_mul_fmprb(t, t, radius, wp); fmpcb_add(t, t, x, prec); /* next angle */ fmprb_mul(v, c2, ct, wp); fmprb_mul(c1, s2, st, wp); fmprb_sub(c1, v, c1, wp); fmprb_mul(v, c2, st, wp); fmprb_mul(s1, s2, ct, wp); fmprb_add(s1, v, s1, wp); fmprb_swap(c1, c2); fmprb_swap(s1, s2); func(u, t, param, 1, prec); fmpcb_abs(v, u, prec); fmprb_add(b, b, v, prec); } fmprb_div_ui(b, b, n, prec); if (fmprb_is_exact(b) || fmpr_cmp(fmprb_radref(b), fmprb_midref(b)) < 0) break; } fmprb_set(bound, b); fmprb_clear(pi); fmprb_clear(theta); fmprb_clear(v); fmpcb_clear(t); fmpcb_clear(u); fmprb_clear(b); fmprb_clear(s1); fmprb_clear(c1); fmprb_clear(s2); fmprb_clear(c2); fmprb_clear(st); fmprb_clear(ct); }
static void _fmprb_gamma(fmprb_t y, const fmprb_t x, long prec, int inverse) { int reflect; long r, n, wp; fmprb_t t, u, v; if (fmprb_is_exact(x)) { const fmpr_struct * mid = fmprb_midref(x); if (fmpr_is_special(mid)) { if (!inverse && fmpr_is_pos_inf(mid)) { fmprb_set(y, x); } else if (fmpr_is_nan(mid) || fmpr_is_neg_inf(mid) || !inverse) { fmpr_nan(fmprb_midref(y)); fmpr_pos_inf(fmprb_radref(y)); } else { fmprb_zero(y); } return; } else { const fmpz exp = *fmpr_expref(mid); const fmpz man = *fmpr_manref(mid); /* fast gamma(n), gamma(n/2) or gamma(n/4) */ if (!COEFF_IS_MPZ(exp) && (exp >= -2) && ((double) fmpz_bits(&man) + exp < prec)) { fmpq_t a; fmpq_init(a); fmpr_get_fmpq(a, mid); fmprb_gamma_fmpq(y, a, prec + 2 * inverse); if (inverse) fmprb_ui_div(y, 1, y, prec); fmpq_clear(a); return; } } } wp = prec + FLINT_BIT_COUNT(prec); gamma_stirling_choose_param_fmprb(&reflect, &r, &n, x, 1, 0, wp); fmprb_init(t); fmprb_init(u); fmprb_init(v); if (reflect) { /* gamma(x) = (rf(1-x, r) * pi) / (gamma(1-x+r) sin(pi x)) */ fmprb_sub_ui(t, x, 1, wp); fmprb_neg(t, t); gamma_rising_fmprb_ui_bsplit(u, t, r, wp); fmprb_const_pi(v, wp); fmprb_mul(u, u, v, wp); fmprb_add_ui(t, t, r, wp); gamma_stirling_eval_fmprb(v, t, n, 0, wp); fmprb_exp(v, v, wp); fmprb_sin_pi(t, x, wp); fmprb_mul(v, v, t, wp); } else { /* gamma(x) = gamma(x+r) / rf(x,r) */ fmprb_add_ui(t, x, r, wp); gamma_stirling_eval_fmprb(u, t, n, 0, wp); fmprb_exp(u, u, prec); gamma_rising_fmprb_ui_bsplit(v, x, r, wp); } if (inverse) fmprb_div(y, v, u, prec); else fmprb_div(y, u, v, prec); fmprb_clear(t); fmprb_clear(u); fmprb_clear(v); }
int main() { slong iter; flint_rand_t state; flint_printf("overlaps...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { fmprb_t a, b; fmpq_t am, ar, bm, br, t, u; int c1, c2; fmprb_init(a); fmprb_init(b); fmpq_init(am); fmpq_init(ar); fmpq_init(bm); fmpq_init(br); fmpq_init(t); fmpq_init(u); fmprb_randtest(a, state, 1 + n_randint(state, 500), 14); fmprb_randtest(b, state, 1 + n_randint(state, 500), 14); fmpr_get_fmpq(am, fmprb_midref(a)); fmpr_get_fmpq(ar, fmprb_radref(a)); fmpr_get_fmpq(bm, fmprb_midref(b)); fmpr_get_fmpq(br, fmprb_radref(b)); fmpq_sub(t, am, bm); fmpz_abs(fmpq_numref(t), fmpq_numref(t)); fmpq_add(u, ar, br); c1 = fmprb_overlaps(a, b); c2 = (fmpq_cmp(t, u) <= 0); if (c1 != c2) { flint_printf("FAIL:\n\n"); flint_printf("a = "); fmprb_print(a); flint_printf("\n\n"); flint_printf("b = "); fmprb_print(b); flint_printf("\n\n"); flint_printf("am = "); fmpq_print(am); flint_printf("\n\n"); flint_printf("ar = "); fmpq_print(ar); flint_printf("\n\n"); flint_printf("bm = "); fmpq_print(bm); flint_printf("\n\n"); flint_printf("br = "); fmpq_print(br); flint_printf("\n\n"); flint_printf("t = "); fmpq_print(t); flint_printf("\n\n"); flint_printf("u = "); fmpq_print(u); flint_printf("\n\n"); flint_printf("c1 = %d, c2 = %d\n\n", c1, c2); abort(); } fmprb_clear(a); fmprb_clear(b); fmpq_clear(am); fmpq_clear(ar); fmpq_clear(bm); fmpq_clear(br); fmpq_clear(t); fmpq_clear(u); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }