void renf_elem_check_embedding(const renf_elem_t a, const renf_t nf, slong prec) { arb_t emb; arb_init(emb); if (nf->nf->flag & NF_LINEAR) { arb_fmpz_div_fmpz(emb, LNF_ELEM_NUMREF(a->elem), LNF_ELEM_DENREF(a->elem), prec); } else if (nf->nf->flag & NF_QUADRATIC) { arb_mul_fmpz(emb, nf->emb, QNF_ELEM_NUMREF(a->elem) + 1, prec); arb_add_fmpz(emb, emb, QNF_ELEM_NUMREF(a->elem), prec); arb_div_fmpz(emb, emb, QNF_ELEM_DENREF(a->elem), prec); } else { fmpq_poly_evaluate_arb(emb, NF_ELEM(a->elem), nf->emb, prec); } if (!arb_overlaps(a->emb, emb)) { fprintf(stderr, "embedding set to "); arb_fprint(stderr, a->emb); fprintf(stderr, " but got "); arb_fprint(stderr, emb); fprintf(stderr, "\n"); arb_clear(emb); abort(); } arb_clear(emb); }
int main() { slong iter; flint_rand_t state; flint_printf("sin_pi_fmpq...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t s1, s2; fmpq_t x; slong prec; prec = 2 + n_randint(state, 5000); arb_init(s1); arb_init(s2); fmpq_init(x); fmpq_randtest(x, state, 1 + n_randint(state, 200)); arb_sin_pi_fmpq(s1, x, prec); arb_const_pi(s2, prec); arb_mul_fmpz(s2, s2, fmpq_numref(x), prec); arb_div_fmpz(s2, s2, fmpq_denref(x), prec); arb_sin(s2, s2, prec); if (!arb_overlaps(s1, s2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("s1 = "); arb_printd(s1, 15); flint_printf("\n\n"); flint_printf("s2 = "); arb_printd(s2, 15); flint_printf("\n\n"); abort(); } arb_clear(s1); arb_clear(s2); fmpq_clear(x); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void _arb_cos_pi_fmpq_oct(arb_t c, const fmpz_t v, const fmpz_t w, slong prec) { if (use_algebraic(v, w, prec)) { _arb_cos_pi_fmpq_algebraic(c, *v, *w, prec); } else { arb_const_pi(c, prec); arb_mul_fmpz(c, c, v, prec); arb_div_fmpz(c, c, w, prec); arb_cos(c, c, prec); } }
void _arb_sin_pi_fmpq_oct(arb_t s, const fmpz_t v, const fmpz_t w, slong prec) { if (use_algebraic(v, w, prec)) { _arb_sin_pi_fmpq_algebraic(s, *v, *w, prec); } else { arb_const_pi(s, prec); arb_mul_fmpz(s, s, v, prec); arb_div_fmpz(s, s, w, prec); arb_sin(s, s, prec); } }
void arb_pow_fmpq(arb_t y, const arb_t x, const fmpq_t a, long prec) { if (fmpz_is_one(fmpq_denref(a))) { arb_pow_fmpz(y, x, fmpq_numref(a), prec); } else if (fmpz_cmp_ui(fmpq_denref(a), 36) <= 0) { arb_root(y, x, *fmpq_denref(a), prec); arb_pow_fmpz(y, y, fmpq_numref(a), prec); } else { long wp; wp = prec + 10; arb_log(y, x, wp); arb_mul_fmpz(y, y, fmpq_numref(a), wp); arb_div_fmpz(y, y, fmpq_denref(a), wp); arb_exp(y, y, prec); } }
int main() { long iter; flint_rand_t state; printf("const_glaisher...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 250; iter++) { arb_t r, s, t; fmpz_t v; long accuracy, prec; prec = 2 + n_randint(state, 2000); arb_init(r); arb_init(s); arb_init(t); fmpz_init(v); arb_const_glaisher(r, prec); arb_const_glaisher(s, prec + 100); if (!arb_overlaps(r, s)) { printf("FAIL: containment\n\n"); printf("prec = %ld\n", prec); printf("r = "); arb_printd(r, prec / 3.33); printf("\n\n"); abort(); } accuracy = arb_rel_accuracy_bits(r); if (accuracy < prec - 4) { printf("FAIL: poor accuracy\n\n"); printf("prec = %ld\n", prec); printf("r = "); arb_printd(r, prec / 3.33); printf("\n\n"); abort(); } if (n_randint(state, 30) == 0) { flint_cleanup(); } fmpz_set_str(v, "128242712910062263687534256886979172776768892732500", 10); arb_set_fmpz(t, v); mag_one(arb_radref(t)); fmpz_ui_pow_ui(v, 10, 50); arb_div_fmpz(t, t, v, 170); if (!arb_overlaps(r, t)) { printf("FAIL: reference value\n\n"); printf("prec = %ld\n", prec); printf("r = "); arb_printd(r, prec / 3.33); printf("\n\n"); abort(); } arb_clear(r); arb_clear(s); arb_clear(t); fmpz_clear(v); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("legendre_p_ui_root...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100 * arb_test_multiplier(); iter++) { ulong n, k; slong prec; arb_ptr roots, weights; arb_poly_t pol; arb_t s; fmpq_poly_t pol2; n = 1 + n_randint(state, 100); prec = 20 + n_randint(state, 500); roots = _arb_vec_init(n); weights = _arb_vec_init(n); arb_poly_init(pol); fmpq_poly_init(pol2); arb_init(s); for (k = 0; k < n; k++) { if (k > n / 2 && n_randint(state, 2)) { arb_neg(roots + k, roots + n - k - 1); arb_set(weights + k, weights + n - k - 1); } else { arb_hypgeom_legendre_p_ui_root(roots + k, weights + k, n, k, prec); } } arb_poly_product_roots(pol, roots, n, prec); /* fmpq_poly_legendre_p(pol2, n); */ arith_legendre_polynomial(pol2, n); arb_set_fmpz(s, pol2->coeffs + n); arb_div_fmpz(s, s, pol2->den, prec); arb_poly_scalar_mul(pol, pol, s, prec); if (!arb_poly_contains_fmpq_poly(pol, pol2)) { flint_printf("FAIL: polynomial containment\n\n"); flint_printf("n = %wu, prec = %wd\n\n", n, prec); flint_printf("pol = "); arb_poly_printd(pol, 30); flint_printf("\n\n"); flint_printf("pol2 = "); fmpq_poly_print(pol2); flint_printf("\n\n"); flint_abort(); } arb_zero(s); for (k = 0; k < n; k++) { arb_add(s, s, weights + k, prec); } if (!arb_contains_si(s, 2)) { flint_printf("FAIL: sum of weights\n\n"); flint_printf("n = %wu, prec = %wd\n\n", n, prec); flint_printf("s = "); arb_printn(s, 30, 0); flint_printf("\n\n"); flint_abort(); } _arb_vec_clear(roots, n); _arb_vec_clear(weights, n); arb_poly_clear(pol); fmpq_poly_clear(pol2); arb_clear(s); } for (iter = 0; iter < 500 * arb_test_multiplier(); iter++) { arb_t x1, x2, w1, w2; ulong n, k; slong prec1, prec2; arb_init(x1); arb_init(x2); arb_init(w1); arb_init(w2); n = 1 + n_randtest(state) % 100000; if (n_randint(state, 2) || n == 1) k = n_randtest(state) % n; else k = n / 2 - (n_randtest(state) % (n / 2)); prec1 = 2 + n_randtest(state) % 2000; prec2 = 2 + n_randtest(state) % 2000; arb_hypgeom_legendre_p_ui_root(x1, w1, n, k, prec1); if (n_randint(state, 10) == 0) arb_hypgeom_legendre_p_ui_root(x1, NULL, n, k, prec1); arb_hypgeom_legendre_p_ui_root(x2, w2, n, k, prec2); if (!arb_overlaps(x1, x2) || !arb_overlaps(w1, w2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("n = %wu, k = %wu, prec1 = %wd, prec2 = %wd\n\n", n, k, prec1, prec2); flint_printf("x1 = "); arb_printn(x1, 100, 0); flint_printf("\n\n"); flint_printf("x2 = "); arb_printn(x2, 100, 0); flint_printf("\n\n"); flint_printf("w1 = "); arb_printn(w1, 100, 0); flint_printf("\n\n"); flint_printf("w2 = "); arb_printn(w2, 100, 0); flint_printf("\n\n"); flint_abort(); } if (arb_rel_accuracy_bits(x1) < prec1 - 3 || arb_rel_accuracy_bits(w1) < prec1 - 3) { flint_printf("FAIL: accuracy\n\n"); flint_printf("n = %wu, k = %wu, prec1 = %wd\n\n", n, k, prec1); flint_printf("acc(x1) = %wd, acc(w1) = %wd\n\n", arb_rel_accuracy_bits(x1), arb_rel_accuracy_bits(w1)); flint_printf("x1 = "); arb_printn(x1, prec1, ARB_STR_CONDENSE * 30); flint_printf("\n\n"); flint_printf("w1 = "); arb_printn(w1, prec1, ARB_STR_CONDENSE * 30); flint_printf("\n\n"); flint_abort(); } if (arb_rel_accuracy_bits(x2) < prec2 - 3 || arb_rel_accuracy_bits(w2) < prec2 - 3) { flint_printf("FAIL: accuracy 2\n\n"); flint_printf("n = %wu, k = %wu, prec2 = %wd\n\n", n, k, prec2); flint_printf("acc(x2) = %wd, acc(w2) = %wd\n\n", arb_rel_accuracy_bits(x2), arb_rel_accuracy_bits(w2)); flint_printf("x2 = "); arb_printn(x2, prec2, ARB_STR_CONDENSE * 30); flint_printf("\n\n"); flint_printf("w2 = "); arb_printn(w2, prec2, ARB_STR_CONDENSE * 30); flint_printf("\n\n"); flint_abort(); } arb_clear(x1); arb_clear(x2); arb_clear(w1); arb_clear(w2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void _acb_poly_zeta_em_tail_naive(acb_ptr sum, const acb_t s, const acb_t Na, acb_srcptr Nasx, slong M, slong d, slong prec) { acb_ptr u, term; acb_t Na2, splus, rec; arb_t x; fmpz_t c; int aint; slong r; BERNOULLI_ENSURE_CACHED(2 * M); u = _acb_vec_init(d); term = _acb_vec_init(d); acb_init(splus); acb_init(rec); acb_init(Na2); arb_init(x); fmpz_init(c); _acb_vec_zero(sum, d); /* u = 1/2 * Nasx */ _acb_vec_scalar_mul_2exp_si(u, Nasx, d, -WORD(1)); /* term = u * (s+x) / (N+a) */ _acb_poly_mullow_cpx(u, u, d, s, d, prec); _acb_vec_scalar_div(term, u, d, Na, prec); /* (N+a)^2 or 1/(N+a)^2 */ acb_mul(Na2, Na, Na, prec); aint = acb_is_int(Na2); if (!aint) acb_inv(Na2, Na2, prec); for (r = 1; r <= M; r++) { /* flint_printf("sum 2: %wd %wd\n", r, M); */ /* sum += bernoulli number * term */ arb_set_round_fmpz(x, fmpq_numref(bernoulli_cache + 2 * r), prec); arb_div_fmpz(x, x, fmpq_denref(bernoulli_cache + 2 * r), prec); _acb_vec_scalar_mul_arb(u, term, d, x, prec); _acb_vec_add(sum, sum, u, d, prec); /* multiply term by ((s+x)+2r-1)((s+x)+2r) / ((N+a)^2 * (2*r+1)*(2*r+2)) */ acb_set(splus, s); arb_add_ui(acb_realref(splus), acb_realref(splus), 2*r-1, prec); _acb_poly_mullow_cpx(term, term, d, splus, d, prec); arb_add_ui(acb_realref(splus), acb_realref(splus), 1, prec); _acb_poly_mullow_cpx(term, term, d, splus, d, prec); /* TODO: combine with previous multiplication? */ if (aint) { arb_mul_ui(x, acb_realref(Na2), 2*r+1, prec); arb_mul_ui(x, x, 2*r+2, prec); _acb_vec_scalar_div_arb(term, term, d, x, prec); } else { fmpz_set_ui(c, 2*r+1); fmpz_mul_ui(c, c, 2*r+2); acb_div_fmpz(rec, Na2, c, prec); _acb_vec_scalar_mul(term, term, d, rec, prec); } } _acb_vec_clear(u, d); _acb_vec_clear(term, d); acb_clear(splus); acb_clear(rec); acb_clear(Na2); arb_clear(x); fmpz_clear(c); }
int main() { slong iter; flint_rand_t state; flint_printf("div_fmpz...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d; fmpz_t x; slong prec; arb_init(a); arb_init(b); arb_init(c); arb_init(d); fmpz_init(x); arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100); arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100); arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100); fmpz_randtest(x, state, 1 + n_randint(state, 2000)); prec = 2 + n_randint(state, 2000); arb_set_fmpz(b, x); arb_div_fmpz(c, a, x, prec); arb_div(d, a, b, prec); if (!arb_equal(c, d)) { flint_printf("FAIL\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_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); fmpz_clear(x); } /* aliasing */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b, c; fmpz_t x; slong prec; arb_init(a); arb_init(b); arb_init(c); fmpz_init(x); arb_randtest_special(a, state, 1 + n_randint(state, 2000), 100); arb_randtest_special(b, state, 1 + n_randint(state, 2000), 100); arb_randtest_special(c, state, 1 + n_randint(state, 2000), 100); fmpz_randtest(x, state, 1 + n_randint(state, 2000)); prec = 2 + n_randint(state, 2000); arb_set_fmpz(b, x); arb_div_fmpz(c, a, x, prec); arb_div_fmpz(a, a, x, prec); if (!arb_equal(a, c)) { 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"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); fmpz_clear(x); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void Lib_Arb_Div_Fmpz(ArbPtr x, ArbPtr y, FmpzPtr z, int32_t prec) { arb_div_fmpz( (arb_ptr) x, (arb_ptr) y, (fmpz*) z, prec); }
void _arb_bell_sum_taylor(arb_t res, const fmpz_t n, const fmpz_t a, const fmpz_t b, const fmpz_t mmag, long tol) { fmpz_t m, r, R, tmp; mag_t B, C, D, bound; arb_t t, u; long wp, k, N; if (_fmpz_sub_small(b, a) < 5) { arb_bell_sum_bsplit(res, n, a, b, mmag, tol); return; } fmpz_init(m); fmpz_init(r); fmpz_init(R); fmpz_init(tmp); /* r = max(m - a, b - m) */ /* m = a + (b - a) / 2 */ fmpz_sub(r, b, a); fmpz_cdiv_q_2exp(r, r, 1); fmpz_add(m, a, r); fmpz_mul_2exp(R, r, RADIUS_BITS); mag_init(B); mag_init(C); mag_init(D); mag_init(bound); arb_init(t); arb_init(u); if (fmpz_cmp(R, m) >= 0) { mag_inf(C); mag_inf(D); } else { /* C = exp(R * |F'(m)| + (1/2) R^2 * (n/(m-R)^2 + 1/(m-R))) */ /* C = exp(R * (|F'(m)| + (1/2) R * (n/(m-R) + 1)/(m-R))) */ /* D = (1/2) R * (n/(m-R) + 1)/(m-R) */ fmpz_sub(tmp, m, R); mag_set_fmpz(D, n); mag_div_fmpz(D, D, tmp); mag_one(C); mag_add(D, D, C); mag_div_fmpz(D, D, tmp); mag_mul_fmpz(D, D, R); mag_mul_2exp_si(D, D, -1); /* C = |F'(m)| */ wp = 20 + 1.05 * fmpz_bits(n); arb_set_fmpz(t, n); arb_div_fmpz(t, t, m, wp); fmpz_add_ui(tmp, m, 1); arb_set_fmpz(u, tmp); arb_digamma(u, u, wp); arb_sub(t, t, u, wp); arb_get_mag(C, t); /* C = exp(R * (C + D)) */ mag_add(C, C, D); mag_mul_fmpz(C, C, R); mag_exp(C, C); } if (mag_cmp_2exp_si(C, tol / 4 + 2) > 0) { _arb_bell_sum_taylor(res, n, a, m, mmag, tol); _arb_bell_sum_taylor(t, n, m, b, mmag, tol); arb_add(res, res, t, 2 * tol); } else { arb_ptr mx, ser1, ser2, ser3; /* D = T(m) */ wp = 20 + 1.05 * fmpz_bits(n); arb_set_fmpz(t, m); arb_pow_fmpz(t, t, n, wp); fmpz_add_ui(tmp, m, 1); arb_gamma_fmpz(u, tmp, wp); arb_div(t, t, u, wp); arb_get_mag(D, t); /* error bound: (b-a) * C * D * B^N / (1 - B), B = r/R */ /* ((b-a) * C * D * 2) * 2^(-N*RADIUS_BITS) */ /* ((b-a) * C * D * 2) */ mag_mul(bound, C, D); mag_mul_2exp_si(bound, bound, 1); fmpz_sub(tmp, b, a); mag_mul_fmpz(bound, bound, tmp); /* N = (tol + log2((b-a)*C*D*2) - mmag) / RADIUS_BITS */ if (mmag == NULL) { /* estimate D ~= 2^mmag */ fmpz_add_ui(tmp, MAG_EXPREF(C), tol); fmpz_cdiv_q_ui(tmp, tmp, RADIUS_BITS); } else { fmpz_sub(tmp, MAG_EXPREF(bound), mmag); fmpz_add_ui(tmp, tmp, tol); fmpz_cdiv_q_ui(tmp, tmp, RADIUS_BITS); } if (fmpz_cmp_ui(tmp, 5 * tol / 4) > 0) N = 5 * tol / 4; else if (fmpz_cmp_ui(tmp, 2) < 0) N = 2; else N = fmpz_get_ui(tmp); /* multiply by 2^(-N*RADIUS_BITS) */ mag_mul_2exp_si(bound, bound, -N * RADIUS_BITS); mx = _arb_vec_init(2); ser1 = _arb_vec_init(N); ser2 = _arb_vec_init(N); ser3 = _arb_vec_init(N); /* estimate (this should work for moderate n and tol) */ wp = 1.1 * tol + 1.05 * fmpz_bits(n) + 5; /* increase precision until convergence */ while (1) { /* (m+x)^n / gamma(m+1+x) */ arb_set_fmpz(mx, m); arb_one(mx + 1); _arb_poly_log_series(ser1, mx, 2, N, wp); for (k = 0; k < N; k++) arb_mul_fmpz(ser1 + k, ser1 + k, n, wp); arb_add_ui(mx, mx, 1, wp); _arb_poly_lgamma_series(ser2, mx, 2, N, wp); _arb_vec_sub(ser1, ser1, ser2, N, wp); _arb_poly_exp_series(ser3, ser1, N, N, wp); /* t = a - m, u = b - m */ arb_set_fmpz(t, a); arb_sub_fmpz(t, t, m, wp); arb_set_fmpz(u, b); arb_sub_fmpz(u, u, m, wp); arb_power_sum_vec(ser1, t, u, N, wp); arb_zero(res); for (k = 0; k < N; k++) arb_addmul(res, ser3 + k, ser1 + k, wp); if (mmag != NULL) { if (_fmpz_sub_small(MAG_EXPREF(arb_radref(res)), mmag) <= -tol) break; } else { if (arb_rel_accuracy_bits(res) >= tol) break; } wp = 2 * wp; } /* add the series truncation bound */ arb_add_error_mag(res, bound); _arb_vec_clear(mx, 2); _arb_vec_clear(ser1, N); _arb_vec_clear(ser2, N); _arb_vec_clear(ser3, N); } mag_clear(B); mag_clear(C); mag_clear(D); mag_clear(bound); arb_clear(t); arb_clear(u); fmpz_clear(m); fmpz_clear(r); fmpz_clear(R); fmpz_clear(tmp); }