int acb_cmp_pretty(const acb_t a, const acb_t b) { arb_t t, u, v; int res; arb_init(t); arb_init(u); arb_init(v); arb_abs(u, acb_imagref(a)); arb_abs(v, acb_imagref(b)); arb_sub(t, u, v, MAG_BITS); res = 0; if (arb_contains_zero(t)) { arb_sub(t, acb_realref(a), acb_realref(b), MAG_BITS); res = arb_is_positive(t) ? 1 : -1; } else { res = arb_is_positive(t) ? 1 : -1; } arb_clear(t); arb_clear(u); arb_clear(v); return res; }
void check_renf(renf_t nf) { arb_ptr a; arb_t b; a = nf->emb; if (fmpq_poly_length(nf->nf->pol) != fmpz_poly_length(nf->der) + 1) { printf("FAIL:\n"); printf("uninitalized derivative"); fflush(stdout); abort(); } arb_init(b); fmpq_poly_evaluate_arb(b, nf->nf->pol, a, nf->prec); if (!arb_contains_zero(b)) { printf("FAIL:\n"); printf("evaluation does not contain zero\n"); printf("pol = "); fmpq_poly_print_pretty(nf->nf->pol, "x"); printf("\n"); printf("a = "); arb_printd(a, 10); printf("\n"); printf("b = "); arb_printd(b, 10); printf("\n"); fflush(stdout); abort(); } arb_clear(b); }
void arb_poly_revert_series(arb_poly_t Qinv, const arb_poly_t Q, slong n, slong prec) { slong Qlen = Q->length; if (Qlen < 2 || !arb_is_zero(Q->coeffs) || arb_contains_zero(Q->coeffs + 1)) { flint_printf("Exception (arb_poly_revert_series). Input must \n" "have zero constant term and nonzero coefficient of x^1.\n"); abort(); } if (Qinv != Q) { arb_poly_fit_length(Qinv, n); _arb_poly_revert_series(Qinv->coeffs, Q->coeffs, Qlen, n, prec); } else { arb_poly_t t; arb_poly_init2(t, n); _arb_poly_revert_series(t->coeffs, Q->coeffs, Qlen, n, prec); arb_poly_swap(Qinv, t); arb_poly_clear(t); } _arb_poly_set_length(Qinv, n); _arb_poly_normalise(Qinv); }
int f_pol(arb_t max, const arb_t t, params_t * p, slong prec) { slong k; acb_t z; arb_t tmp; acb_init(z); arb_init(tmp); arb_one(max); for (k = 0; k < p->len; k++) { acb_sub_arb(z, p->z + k, t, prec); acb_abs(tmp, z, prec); if (arb_contains_zero(tmp)) return 0; arb_mul(max, max, tmp, prec); } arb_inv(max, max, prec); arb_clear(tmp); acb_clear(z); return 1; }
int acb_contains_int(const acb_t x) { if (!arb_contains_zero(acb_imagref(x))) return 0; return arb_contains_int(acb_realref(x)); }
int renf_elem_sgn(renf_elem_t a, renf_t nf) { slong prec; slong cond; if (nf_elem_is_rational(a->elem, nf->nf)) { if (nf->nf->flag & NF_LINEAR) return fmpz_sgn(LNF_ELEM_NUMREF(a->elem)); else if (nf->nf->flag & NF_QUADRATIC) return fmpz_sgn(QNF_ELEM_NUMREF(a->elem)); else if (NF_ELEM(a->elem)->length == 0) return 0; else return fmpz_sgn(NF_ELEM_NUMREF(a->elem)); } if (!arb_contains_zero(a->emb)) return arf_sgn(arb_midref(a->emb)); renf_elem_relative_condition_number_2exp(&cond, a, nf); prec = FLINT_MAX(nf->prec, arb_rel_accuracy_bits(nf->emb)); renf_elem_set_evaluation(a, nf, prec + cond); do { if (!arb_contains_zero(a->emb)) return arf_sgn(arb_midref(a->emb)); prec *= 2; renf_refine_embedding(nf, prec); renf_elem_set_evaluation(a, nf, prec + cond); } while(1); /* we should not get here */ abort(); return -3; }
void acb_hypgeom_erf_asymp(acb_t res, const acb_t z, slong prec, slong prec2) { acb_t a, t, u; acb_init(a); acb_init(t); acb_init(u); acb_one(a); acb_mul_2exp_si(a, a, -1); acb_mul(t, z, z, prec2); acb_hypgeom_u_asymp(u, a, a, t, -1, prec2); acb_neg(t, t); acb_exp(t, t, prec2); acb_mul(u, u, t, prec2); acb_const_pi(t, prec2); acb_sqrt(t, t, prec2); acb_mul(t, t, z, prec2); acb_div(u, u, t, prec2); /* branch cut term: -1 or 1 */ if (arb_contains_zero(acb_realref(z))) { arb_zero(acb_imagref(t)); arf_zero(arb_midref(acb_realref(t))); mag_one(arb_radref(acb_realref(t))); } else { acb_set_si(t, arf_sgn(arb_midref(acb_realref(z)))); } acb_sub(t, t, u, prec); if (arb_is_zero(acb_imagref(z))) arb_zero(acb_imagref(t)); else if (arb_is_zero(acb_realref(z))) arb_zero(acb_realref(t)); acb_set(res, t); acb_clear(a); acb_clear(t); acb_clear(u); }
int arb_poly_divrem(arb_poly_t Q, arb_poly_t R, const arb_poly_t A, const arb_poly_t B, slong prec) { const slong lenA = A->length, lenB = B->length; if (lenB == 0 || arb_contains_zero(B->coeffs + lenB - 1)) { return 0; } if (lenA < lenB) { arb_poly_set(R, A); arb_poly_zero(Q); return 1; } if (Q == A || Q == B) { arb_poly_t T; arb_poly_init(T); arb_poly_divrem(T, R, A, B, prec); arb_poly_swap(Q, T); arb_poly_clear(T); return 1; } if (R == A || R == B) { arb_poly_t U; arb_poly_init(U); arb_poly_divrem(Q, U, A, B, prec); arb_poly_swap(R, U); arb_poly_clear(U); return 1; } arb_poly_fit_length(Q, lenA - lenB + 1); arb_poly_fit_length(R, lenB - 1); _arb_poly_divrem(Q->coeffs, R->coeffs, A->coeffs, lenA, B->coeffs, lenB, prec); _arb_poly_set_length(Q, lenA - lenB + 1); _arb_poly_set_length(R, lenB - 1); _arb_poly_normalise(R); return 1; }
void acb_pow_arb(acb_t z, const acb_t x, const arb_t y, long prec) { const arf_struct * ymid = arb_midref(y); const mag_struct * yrad = arb_radref(y); if (arb_is_zero(y)) { acb_one(z); return; } if (mag_is_zero(yrad)) { /* small half-integer or integer */ if (arf_cmpabs_2exp_si(ymid, BINEXP_LIMIT) < 0 && arf_is_int_2exp_si(ymid, -1)) { fmpz_t e; fmpz_init(e); if (arf_is_int(ymid)) { arf_get_fmpz_fixed_si(e, ymid, 0); acb_pow_fmpz_binexp(z, x, e, prec); } else { /* hack: give something finite here (should fix sqrt/rsqrt etc) */ if (arb_contains_zero(acb_imagref(x)) && arb_contains_nonpositive(acb_realref(x))) { _acb_pow_arb_exp(z, x, y, prec); fmpz_clear(e); return; } arf_get_fmpz_fixed_si(e, ymid, -1); acb_sqrt(z, x, prec + fmpz_bits(e)); acb_pow_fmpz_binexp(z, z, e, prec); } fmpz_clear(e); return; } } _acb_pow_arb_exp(z, x, y, prec); }
/* invalid in (-1,0) */ int _acb_hypgeom_legendre_q_single_valid(const acb_t z) { arb_t t; int ok; if (!arb_contains_zero(acb_imagref(z))) return 1; if (arb_is_positive(acb_imagref(z))) return 1; arb_init(t); arb_one(t); arb_neg(t, t); ok = arb_lt(acb_realref(z), t); arb_clear(t); return ok; }
void arb_twobytwo_diag(arb_t u1, arb_t u2, const arb_t a, const arb_t b, const arb_t d, slong prec) { // Compute the orthogonal matrix that diagonalizes // // A = [a b] // [b d] // // This matrix will have the form // // U = [cos x , -sin x] // [sin x, cos x] // // where the diagonal matrix is U^t A U. // We set u1 = cos x, u2 = -sin x. if(arb_contains_zero(b)) { // this is not quite right (doesn't set error intervals) arb_set_ui(u1, 1); arb_set_ui(u2, 0); return; } arb_t x; arb_init(x); arb_mul(u1, b, b, prec); // u1 = b^2 arb_sub(u2, a, d, prec); // u2 = a - d arb_mul_2exp_si(u2, u2, -1); // u2 = (a - d)/2 arb_mul(u2, u2, u2, prec); // u2 = ( (a - d)/2 )^2 arb_add(u1, u1, u2, prec); // u1 = b^2 + ( (a-d)/2 )^2 arb_sqrt(u1, u1, prec); // u1 = sqrt(above) arb_mul_2exp_si(u1, u1, 1); // u1 = 2 (sqrt (above) ) arb_add(u1, u1, d, prec); // u1 += d arb_sub(u1, u1, a, prec); // u1 -= a arb_mul_2exp_si(u1, u1, -1); // u1 = (d - a)/2 + sqrt(b^2 + ( (a-d)/2 )^2) arb_mul(x, u1, u1, prec); arb_addmul(x, b, b, prec); // x = u1^2 + b^2 arb_sqrt(x, x, prec); // x = sqrt(u1^2 + b^2) arb_div(u2, u1, x, prec); arb_div(u1, b, x, prec); arb_neg(u1, u1); arb_clear(x); }
int f_lambertw(acb_ptr res, const acb_t z, void * param, slong order, slong prec) { acb_t t; if (order > 1) flint_abort(); /* Would be needed for Taylor method. */ acb_init(t); prec = FLINT_MIN(prec, acb_rel_accuracy_bits(z) + 10); if (order != 0) { /* check for branch cut */ arb_const_e(acb_realref(t), prec); acb_inv(t, t, prec); acb_add(t, t, z, prec); if (arb_contains_zero(acb_imagref(t)) && arb_contains_nonpositive(acb_realref(t))) { acb_indeterminate(t); } } if (acb_is_finite(t)) { fmpz_t k; fmpz_init(k); acb_lambertw(res, z, k, 0, prec); fmpz_clear(k); } else { acb_indeterminate(res); } acb_clear(t); return 0; }
/* Check if z crosses a branch cut. */ int acb_lambertw_branch_crossing(const acb_t z, const acb_t ez1, const fmpz_t k) { if (arb_contains_zero(acb_imagref(z)) && !arb_is_nonnegative(acb_imagref(z))) { if (fmpz_is_zero(k)) { if (!arb_is_positive(acb_realref(ez1))) { return 1; } } else if (!arb_is_positive(acb_realref(z))) { return 1; } } return 0; }
int arb_poly_contains(const arb_poly_t poly1, const arb_poly_t poly2) { slong i; if (poly2->length > poly1->length) return 0; for (i = 0; i < poly2->length; i++) { if (!arb_contains(poly1->coeffs + i, poly2->coeffs + i)) return 0; } for (i = poly2->length; i < poly1->length; i++) if (!arb_contains_zero(poly1->coeffs + i)) return 0; return 1; }
/* f(z) = sin(1/z), assume on real interval */ int f_essing(acb_ptr res, const acb_t z, void * param, slong order, slong prec) { if (order > 1) flint_abort(); /* Would be needed for Taylor method. */ if ((order == 0) && acb_is_real(z) && arb_contains_zero(acb_realref(z))) { /* todo: arb_zero_pm_one, arb_unit_interval? */ acb_zero(res); mag_one(arb_radref(acb_realref(res))); } else { acb_inv(res, z, prec); acb_sin(res, res, prec); } return 0; }
static int check_block(arb_calc_func_t func, void * param, const arf_interval_t block, int asign, int bsign, slong prec) { arb_struct t[2]; arb_t x; int result; arb_init(t + 0); arb_init(t + 1); arb_init(x); arf_interval_get_arb(x, block, prec); func(t, x, param, 1, prec); result = BLOCK_UNKNOWN; if (arb_is_positive(t) || arb_is_negative(t)) { result = BLOCK_NO_ZERO; } else { if ((asign < 0 && bsign > 0) || (asign > 0 && bsign < 0)) { func(t, x, param, 2, prec); if (arb_is_finite(t + 1) && !arb_contains_zero(t + 1)) { result = BLOCK_ISOLATED_ZERO; } } } arb_clear(t + 0); arb_clear(t + 1); arb_clear(x); return result; }
int renf_elem_cmp_fmpq(renf_elem_t a, const fmpq_t b, renf_t nf) { int s; slong prec, cond; arb_t diffball; renf_elem_t diffnf; if (fmpq_is_zero(b)) return renf_elem_sgn(a, nf); if (nf_elem_is_rational(a->elem, nf->nf)) { if (nf->nf->flag & NF_LINEAR) return _fmpq_cmp(LNF_ELEM_NUMREF(a->elem), LNF_ELEM_DENREF(a->elem), fmpq_numref(b), fmpq_denref(b)); else if (nf->nf->flag & NF_QUADRATIC) return _fmpq_cmp(QNF_ELEM_NUMREF(a->elem), QNF_ELEM_DENREF(a->elem), fmpq_numref(b), fmpq_denref(b)); else return _fmpq_cmp(NF_ELEM_NUMREF(a->elem), NF_ELEM_DENREF(a->elem), fmpq_numref(b), fmpq_denref(b)); } arb_init(diffball); arb_set_fmpq(diffball, b, nf->prec); arb_sub(diffball, a->emb, diffball, nf->prec); if (!arb_contains_zero(diffball)) { s = arf_sgn(arb_midref(diffball)); arb_clear(diffball); return s; } renf_elem_relative_condition_number_2exp(&cond, a, nf); prec = FLINT_MAX(nf->prec, arb_rel_accuracy_bits(nf->emb)); renf_elem_set_evaluation(a, nf, prec + cond); arb_set_fmpq(diffball, b, prec); arb_sub(diffball, a->emb, diffball, prec); if (!arb_contains_zero(diffball)) { s = arf_sgn(arb_midref(diffball)); arb_clear(diffball); return s; } arb_clear(diffball); renf_elem_init(diffnf, nf); renf_elem_set(diffnf, a, nf); renf_elem_sub_fmpq(diffnf, diffnf, b, nf); s = renf_elem_sgn(diffnf, nf); renf_elem_clear(diffnf, nf); return s; }
int main() { long iter; flint_rand_t state; printf("eta...."); fflush(stdout); flint_randinit(state); /* Test functional equation */ for (iter = 0; iter < 10000; iter++) { acb_t tau1, tau2, z1, z2, z3, t; fmpq_t arg; long e0, prec0, prec1, prec2; psl2z_t g; psl2z_init(g); fmpq_init(arg); acb_init(tau1); acb_init(tau2); acb_init(z1); acb_init(z2); acb_init(z3); acb_init(t); e0 = 1 + n_randint(state, 200); prec0 = 2 + n_randint(state, 2000); prec1 = 2 + n_randint(state, 2000); prec2 = 2 + n_randint(state, 2000); acb_randtest(tau1, state, prec0, e0); acb_randtest(tau2, state, prec0, e0); acb_randtest(z1, state, prec0, e0); acb_randtest(z2, state, prec0, e0); psl2z_randtest(g, state, 1 + n_randint(state, 200)); acb_modular_transform(tau2, g, tau1, prec0); acb_modular_eta(z1, tau1, prec1); acb_modular_eta(z2, tau2, prec2); /* apply transformation */ fmpq_set_si(arg, acb_modular_epsilon_arg(g), 12); arb_sin_cos_pi_fmpq(acb_imagref(t), acb_realref(t), arg, prec1); acb_mul(z3, z1, t, prec1); acb_mul_fmpz(t, tau1, &g->c, prec1); acb_add_fmpz(t, t, &g->d, prec1); acb_sqrt(t, t, prec1); acb_mul(z3, z3, t, prec1); if (!acb_overlaps(z3, z2)) { printf("FAIL (overlap)\n"); printf("tau1 = "); acb_printd(tau1, 15); printf("\n\n"); printf("tau2 = "); acb_printd(tau2, 15); printf("\n\n"); printf("g = "); psl2z_print(g); printf("\n\n"); printf("z1 = "); acb_printd(z1, 15); printf("\n\n"); printf("z2 = "); acb_printd(z2, 15); printf("\n\n"); printf("z3 = "); acb_printd(z3, 15); printf("\n\n"); abort(); } acb_modular_eta(tau1, tau1, prec2); if (!acb_overlaps(z1, tau1)) { printf("FAIL (aliasing)\n"); printf("tau1 = "); acb_print(tau1); printf("\n\n"); printf("tau2 = "); acb_print(tau2); printf("\n\n"); printf("z1 = "); acb_print(z1); printf("\n\n"); printf("z2 = "); acb_print(z2); printf("\n\n"); abort(); } acb_clear(tau1); acb_clear(tau2); acb_clear(z1); acb_clear(z2); acb_clear(z3); acb_clear(t); psl2z_clear(g); fmpq_clear(arg); } /* Test special values */ for (iter = 0; iter < 100; iter++) { acb_t tau, z; arb_t t, u; long prec; acb_init(tau); acb_init(z); arb_init(t); arb_init(u); prec = 2 + n_randint(state, 2000); acb_randtest(z, state, prec, 10); acb_onei(tau); acb_modular_eta(z, tau, prec); arb_one(t); arb_mul_2exp_si(t, t, -2); arb_gamma(t, t, prec); arb_const_pi(u, prec); arb_root(u, u, 4, prec); arb_pow_ui(u, u, 3, prec); arb_div(t, t, u, prec); arb_mul_2exp_si(t, t, -1); if (!arb_overlaps(acb_realref(z), t) || !arb_contains_zero(acb_imagref(z))) { printf("FAIL (value 1)\n"); printf("tau = "); acb_print(tau); printf("\n\n"); printf("z = "); acb_print(z); printf("\n\n"); abort(); } acb_clear(tau); acb_clear(z); arb_clear(t); arb_clear(u); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void fmpz_poly_complex_roots_squarefree(const fmpz_poly_t poly, slong initial_prec, slong target_prec, slong print_digits) { slong i, j, prec, deg, deg_deflated, isolated, maxiter, deflation; acb_poly_t cpoly, cpoly_deflated; fmpz_poly_t poly_deflated; acb_ptr roots, roots_deflated; int removed_zero; if (fmpz_poly_degree(poly) < 1) return; fmpz_poly_init(poly_deflated); acb_poly_init(cpoly); acb_poly_init(cpoly_deflated); /* try to write poly as poly_deflated(x^deflation), possibly multiplied by x */ removed_zero = fmpz_is_zero(poly->coeffs); if (removed_zero) fmpz_poly_shift_right(poly_deflated, poly, 1); else fmpz_poly_set(poly_deflated, poly); deflation = fmpz_poly_deflation(poly_deflated); fmpz_poly_deflate(poly_deflated, poly_deflated, deflation); deg = fmpz_poly_degree(poly); deg_deflated = fmpz_poly_degree(poly_deflated); flint_printf("searching for %wd roots, %wd deflated\n", deg, deg_deflated); roots = _acb_vec_init(deg); roots_deflated = _acb_vec_init(deg_deflated); for (prec = initial_prec; ; prec *= 2) { acb_poly_set_fmpz_poly(cpoly_deflated, poly_deflated, prec); maxiter = FLINT_MIN(FLINT_MAX(deg_deflated, 32), prec); TIMEIT_ONCE_START flint_printf("prec=%wd: ", prec); isolated = acb_poly_find_roots(roots_deflated, cpoly_deflated, prec == initial_prec ? NULL : roots_deflated, maxiter, prec); flint_printf("%wd isolated roots | ", isolated); TIMEIT_ONCE_STOP if (isolated == deg_deflated) { if (!check_accuracy(roots_deflated, deg_deflated, target_prec)) continue; if (deflation == 1) { _acb_vec_set(roots, roots_deflated, deg_deflated); } else /* compute all nth roots */ { acb_t w, w2; acb_init(w); acb_init(w2); acb_unit_root(w, deflation, prec); acb_unit_root(w2, 2 * deflation, prec); for (i = 0; i < deg_deflated; i++) { if (arf_sgn(arb_midref(acb_realref(roots_deflated + i))) > 0) { acb_root_ui(roots + i * deflation, roots_deflated + i, deflation, prec); } else { acb_neg(roots + i * deflation, roots_deflated + i); acb_root_ui(roots + i * deflation, roots + i * deflation, deflation, prec); acb_mul(roots + i * deflation, roots + i * deflation, w2, prec); } for (j = 1; j < deflation; j++) { acb_mul(roots + i * deflation + j, roots + i * deflation + j - 1, w, prec); } } acb_clear(w); acb_clear(w2); } /* by assumption that poly is squarefree, must be just one */ if (removed_zero) acb_zero(roots + deg_deflated * deflation); if (!check_accuracy(roots, deg, target_prec)) continue; acb_poly_set_fmpz_poly(cpoly, poly, prec); if (!acb_poly_validate_real_roots(roots, cpoly, prec)) continue; for (i = 0; i < deg; i++) { if (arb_contains_zero(acb_imagref(roots + i))) arb_zero(acb_imagref(roots + i)); } flint_printf("done!\n"); break; } } if (print_digits != 0) { _acb_vec_sort_pretty(roots, deg); for (i = 0; i < deg; i++) { acb_printn(roots + i, print_digits, 0); flint_printf("\n"); } } fmpz_poly_clear(poly_deflated); acb_poly_clear(cpoly); acb_poly_clear(cpoly_deflated); _acb_vec_clear(roots, deg); _acb_vec_clear(roots_deflated, deg_deflated); }
void acb_hypgeom_2f1_transform_limit(acb_t res, const acb_t a, const acb_t b, const acb_t c, const acb_t z, int regularized, int which, slong prec) { acb_poly_t aa, bb, cc, zz; acb_t t; if (acb_contains_zero(z) || !acb_is_finite(z)) { acb_indeterminate(res); return; } if (arb_contains_si(acb_realref(z), 1) && arb_contains_zero(acb_imagref(z))) { acb_indeterminate(res); return; } if (!regularized) { acb_init(t); acb_gamma(t, c, prec); acb_hypgeom_2f1_transform_limit(res, a, b, c, z, 1, which, prec); acb_mul(res, res, t, prec); acb_clear(t); return; } acb_poly_init(aa); acb_poly_init(bb); acb_poly_init(cc); acb_poly_init(zz); acb_init(t); acb_poly_set_acb(aa, a); acb_poly_set_acb(bb, b); acb_poly_set_acb(cc, c); acb_poly_set_acb(zz, z); if (which == 2 || which == 3) { acb_sub(t, b, a, prec); acb_poly_set_coeff_si(aa, 1, 1); /* prefer b-a nonnegative (either is correct) to avoid expensive operations in the hypergeometric series */ if (arb_is_nonnegative(acb_realref(t))) _acb_hypgeom_2f1_transform_limit(res, aa, bb, cc, zz, which, prec); else _acb_hypgeom_2f1_transform_limit(res, bb, aa, cc, zz, which, prec); } else { acb_poly_set_coeff_si(aa, 1, 1); _acb_hypgeom_2f1_transform_limit(res, aa, bb, cc, zz, which, prec); } acb_poly_clear(aa); acb_poly_clear(bb); acb_poly_clear(cc); acb_poly_clear(zz); acb_clear(t); }
void acb_hypgeom_2f1_transform_nolimit(acb_t res, const acb_t a, const acb_t b, const acb_t c, const acb_t z, int regularized, int which, slong prec) { acb_t ba, ca, cb, cab, ac1, bc1, ab1, ba1, w, t, u, v, s; if (acb_contains_zero(z) || !acb_is_finite(z)) { acb_indeterminate(res); return; } if (arb_contains_si(acb_realref(z), 1) && arb_contains_zero(acb_imagref(z))) { acb_indeterminate(res); return; } if (!regularized) { acb_init(t); acb_gamma(t, c, prec); acb_hypgeom_2f1_transform_nolimit(res, a, b, c, z, 1, which, prec); acb_mul(res, res, t, prec); acb_clear(t); return; } acb_init(ba); acb_init(ca); acb_init(cb); acb_init(cab); acb_init(ac1); acb_init(bc1); acb_init(ab1); acb_init(ba1); acb_init(w); acb_init(t); acb_init(u); acb_init(v); acb_init(s); acb_add_si(s, z, -1, prec); /* s = 1 - z */ acb_neg(s, s); acb_sub(ba, b, a, prec); /* ba = b - a */ acb_sub(ca, c, a, prec); /* ca = c - a */ acb_sub(cb, c, b, prec); /* cb = c - b */ acb_sub(cab, ca, b, prec); /* cab = c - a - b */ acb_add_si(ac1, ca, -1, prec); acb_neg(ac1, ac1); /* ac1 = a - c + 1 */ acb_add_si(bc1, cb, -1, prec); acb_neg(bc1, bc1); /* bc1 = b - c + 1 */ acb_add_si(ab1, ba, -1, prec); acb_neg(ab1, ab1); /* ab1 = a - b + 1 */ acb_add_si(ba1, ba, 1, prec); /* ba1 = b - a + 1 */ /* t = left term, u = right term (DLMF 15.8.1 - 15.8.5) */ if (which == 2) { acb_inv(w, z, prec); /* w = 1/z */ acb_hypgeom_2f1_direct(t, a, ac1, ab1, w, 1, prec); acb_hypgeom_2f1_direct(u, b, bc1, ba1, w, 1, prec); } else if (which == 3) { acb_inv(w, s, prec); /* w = 1/(1-z) */ acb_hypgeom_2f1_direct(t, a, cb, ab1, w, 1, prec); acb_hypgeom_2f1_direct(u, b, ca, ba1, w, 1, prec); } else if (which == 4) { acb_set(w, s); /* w = 1-z */ acb_add(v, ac1, b, prec); /* v = a+b-c+1 */ acb_hypgeom_2f1_direct(t, a, b, v, w, 1, prec); acb_add_si(v, cab, 1, prec); /* v = c-a-b+1 */ acb_hypgeom_2f1_direct(u, ca, cb, v, w, 1, prec); } else if (which == 5) { acb_inv(w, z, prec); /* w = 1-1/z */ acb_neg(w, w); acb_add_si(w, w, 1, prec); acb_add(v, ac1, b, prec); /* v = a+b-c+1 */ acb_hypgeom_2f1_direct(t, a, ac1, v, w, 1, prec); acb_add_si(v, cab, 1, prec); /* v = c-a-b+1 */ acb_add_si(u, a, -1, prec); /* u = 1-a */ acb_neg(u, u); acb_hypgeom_2f1_direct(u, ca, u, v, w, 1, prec); } else { flint_printf("invalid transformation!\n"); flint_abort(); } /* gamma factors */ acb_rgamma(v, a, prec); acb_mul(u, u, v, prec); acb_rgamma(v, ca, prec); acb_mul(t, t, v, prec); acb_rgamma(v, b, prec); if (which == 2 || which == 3) acb_mul(t, t, v, prec); else acb_mul(u, u, v, prec); acb_rgamma(v, cb, prec); if (which == 2 || which == 3) acb_mul(u, u, v, prec); else acb_mul(t, t, v, prec); if (which == 2 || which == 3) { if (which == 2) acb_neg(s, z); /* -z, otherwise 1-z since before */ acb_neg(v, a); acb_pow(v, s, v, prec); acb_mul(t, t, v, prec); acb_neg(v, b); acb_pow(v, s, v, prec); acb_mul(u, u, v, prec); } else { acb_pow(v, s, cab, prec); acb_mul(u, u, v, prec); if (which == 5) { acb_neg(v, a); acb_pow(v, z, v, prec); acb_mul(t, t, v, prec); acb_neg(v, ca); acb_pow(v, z, v, prec); acb_mul(u, u, v, prec); } } acb_sub(t, t, u, prec); if (which == 2 || which == 3) acb_sin_pi(v, ba, prec); else acb_sin_pi(v, cab, prec); acb_div(t, t, v, prec); acb_const_pi(v, prec); acb_mul(t, t, v, prec); acb_set(res, t); acb_clear(ba); acb_clear(ca); acb_clear(cb); acb_clear(cab); acb_clear(ac1); acb_clear(bc1); acb_clear(ab1); acb_clear(ba1); acb_clear(w); acb_clear(t); acb_clear(u); acb_clear(v); acb_clear(s); }
int main() { slong iter; flint_rand_t state; flint_printf("hardy_z...."); fflush(stdout); flint_randinit(state); /* test self-consistency */ for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++) { acb_t s, s2; dirichlet_group_t G; dirichlet_char_t chi; acb_ptr vec1, vec2; slong len1, len2; slong prec1, prec2; ulong q, k; slong i; len1 = n_randint(state, 6); len2 = n_randint(state, 6); prec1 = 2 + n_randint(state, 100); prec2 = 2 + n_randint(state, 100); do { q = 1 + n_randint(state, 30); } while (q % 4 == 2); dirichlet_group_init(G, q); dirichlet_char_init(chi, G); do { k = n_randint(state, n_euler_phi(q)); dirichlet_char_index(chi, G, k); } while (dirichlet_conductor_char(G, chi) != q); acb_init(s); acb_init(s2); vec1 = _acb_vec_init(len1); vec2 = _acb_vec_init(len2); acb_randtest(s, state, 2 + n_randint(state, 200), 2); acb_randtest(s2, state, 2 + n_randint(state, 200), 2); acb_sub(s2, s2, s2, 200); acb_add(s2, s, s2, 200); acb_dirichlet_hardy_z(vec1, s, G, chi, len1, prec1); acb_dirichlet_hardy_z(vec2, s2, G, chi, len2, prec2); for (i = 0; i < FLINT_MIN(len1, len2); i++) { if (!acb_overlaps(vec1 + i, vec2 + i)) { flint_printf("FAIL: overlap\n\n"); flint_printf("iter = %wd q = %wu k = %wu i = %wd\n\n", iter, q, k, i); flint_printf("s = "); acb_printn(s, 50, 0); flint_printf("\n\n"); flint_printf("r1 = "); acb_printn(vec1 + i, 50, 0); flint_printf("\n\n"); flint_printf("r2 = "); acb_printn(vec2 + i, 50, 0); flint_printf("\n\n"); flint_abort(); } } if (arb_contains_zero(acb_imagref(s))) { for (i = 0; i < len1; i++) { if (!arb_contains_zero(acb_imagref(vec1 + i))) { flint_printf("FAIL: real 1\n\n"); flint_printf("iter = %wd q = %wu k = %wu i = %wd\n\n", iter, q, k, i); flint_printf("s = "); acb_printn(s, 50, 0); flint_printf("\n\n"); flint_printf("r1 = "); acb_printn(vec1 + i, 50, 0); flint_printf("\n\n"); flint_abort(); } } } if (arb_contains_zero(acb_imagref(s2))) { for (i = 0; i < len2; i++) { if (!arb_contains_zero(acb_imagref(vec2 + i))) { flint_printf("FAIL: real 1\n\n"); flint_printf("iter = %wd q = %wu k = %wu i = %wd\n\n", iter, q, k, i); flint_printf("s = "); acb_printn(s, 50, 0); flint_printf("\n\n"); flint_printf("r1 = "); acb_printn(vec2 + i, 50, 0); flint_printf("\n\n"); flint_abort(); } } } dirichlet_char_clear(chi); dirichlet_group_clear(G); acb_clear(s); acb_clear(s2); _acb_vec_clear(vec1, len1); _acb_vec_clear(vec2, len2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int fmpq_poly_check_unique_real_root(const fmpq_poly_t pol, const arb_t a, slong prec) { if (pol->length < 2) return 0; else if (pol->length == 2) { /* linear polynomial */ fmpq_t root; int ans; fmpq_init(root); fmpq_set_fmpz_frac(root, fmpq_poly_numref(pol), fmpq_poly_numref(pol) + 1); fmpq_neg(root, root); ans = arb_contains_fmpq(a, root); fmpq_clear(root); return ans; } else { arb_t b, c; arf_t l, r; fmpz * der; int lsign, rsign; fmpz_poly_t pol2; slong n; /* 1 - cheap test: */ /* - sign(left) * sign(right) = -1 */ /* - no zero of the derivative */ arb_init(b); arb_init(c); arf_init(l); arf_init(r); arb_get_interval_arf(l, r, a, prec); arb_set_arf(b, l); _fmpz_poly_evaluate_arb(c, pol->coeffs, pol->length, b, 2*prec); lsign = arb_sgn2(c); arb_set_arf(b, r); _fmpz_poly_evaluate_arb(c, pol->coeffs, pol->length, b, 2*prec); rsign = arb_sgn2(c); arb_clear(c); if (lsign * rsign == -1) { der = _fmpz_vec_init(pol->length - 1); _fmpz_poly_derivative(der, pol->coeffs, pol->length); _fmpz_poly_evaluate_arb(b, der, pol->length - 1, a, prec); _fmpz_vec_clear(der, pol->length - 1); if (!arb_contains_zero(b)) { arf_clear(l); arf_clear(r); arb_clear(b); return 1; } } else return 0; arb_clear(b); /* 2 - expensive testing */ fmpq_t ql, qr; fmpq_init(ql); fmpq_init(qr); arf_get_fmpq(ql, l); arf_get_fmpq(qr, r); fmpz_poly_init(pol2); fmpz_poly_fit_length(pol2, pol->length); _fmpz_vec_set(pol2->coeffs, pol->coeffs, pol->length); pol2->length = pol->length; _fmpz_poly_scale_0_1_fmpq(pol2->coeffs, pol2->length, ql, qr); n = fmpz_poly_num_real_roots_0_1(pol2); fmpz_poly_clear(pol2); fmpq_clear(ql); fmpq_clear(qr); return (n == 1); } }
int main() { slong iter; flint_rand_t state; flint_printf("hurwitz_zeta...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++) { arb_t a, b, c; acb_t d, e, f; slong prec; prec = 2 + n_randint(state, 300); arb_init(a); arb_init(b); arb_init(c); acb_init(d); acb_init(e); acb_init(f); arb_randtest_precise(a, state, 1 + n_randint(state, 300), 5); arb_randtest_precise(b, state, 1 + n_randint(state, 300), 5); arb_randtest_precise(c, state, 1 + n_randint(state, 300), 5); acb_set_arb(d, a); acb_set_arb(e, b); arb_hurwitz_zeta(c, a, b, prec); acb_hurwitz_zeta(f, d, e, prec); if (!arb_overlaps(c, acb_realref(f)) || (arb_is_finite(c) && !arb_contains_zero(acb_imagref(f)))) { flint_printf("FAIL: overlap\n\n"); flint_printf("a = "); arb_printd(a, 15); flint_printf("\n\n"); flint_printf("b = "); arb_printd(b, 15); flint_printf("\n\n"); flint_printf("c = "); arb_printd(c, 15); flint_printf("\n\n"); flint_printf("d = "); acb_printd(d, 15); flint_printf("\n\n"); flint_printf("e = "); acb_printd(e, 15); flint_printf("\n\n"); flint_printf("f = "); acb_printd(f, 15); flint_printf("\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); arb_clear(c); acb_clear(d); acb_clear(e); acb_clear(f); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int _acb_poly_validate_real_roots(acb_srcptr roots, acb_srcptr poly, long len, long prec) { long i, deg, num_real; arb_ptr real; int result; deg = len - 1; num_real = 0; result = 1; if (deg <= 1) return 1; real = _arb_vec_init(deg); /* pick out the candidate real roots */ for (i = 0; i < deg; i++) { if (arb_contains_zero(acb_imagref(roots + i))) { arb_set(real + num_real, acb_realref(roots + i)); num_real++; } } /* number of real roots must be even if the polynomial is even, and odd if the polynomial is odd (unless there are repeated roots... in which case the input is invalid) */ if ((num_real % 2) != (deg % 2)) { result = 0; } else if (num_real > 0) { int sign_neg_inf, sign_pos_inf, prev_sign; acb_t t; acb_init(t); /* by assumption that the roots are real and isolated, the lead coefficient really must be known to be either positive or negative */ sign_pos_inf = arb_is_positive(acb_realref(poly + deg)) ? 1 : -1; sign_neg_inf = (deg % 2) ? -sign_pos_inf : sign_pos_inf; /* now we check that there's a sign change between each root */ _arb_vec_sort_mid(real, num_real); prev_sign = sign_neg_inf; for (i = 0; i < num_real - 1; i++) { /* set t to the midpoint between the midpoints */ arb_zero(acb_imagref(t)); arf_add(arb_midref(acb_realref(t)), arb_midref(real + i), arb_midref(real + i + 1), prec, ARF_RND_DOWN); arf_mul_2exp_si(arb_midref(acb_realref(t)), arb_midref(acb_realref(t)), -1); mag_zero(arb_radref(acb_realref(t))); /* check that this point really is between both intervals (one interval could be much wider than the other */ if (arb_lt(real + i, acb_realref(t)) && arb_lt(acb_realref(t), real + i + 1)) { /* check sign change */ _acb_poly_evaluate(t, poly, len, t, prec); if (prev_sign == 1) result = arb_is_negative(acb_realref(t)); else result = arb_is_positive(acb_realref(t)); if (!result) break; prev_sign = -prev_sign; } else { result = 0; break; } } acb_clear(t); } _arb_vec_clear(real, deg); return result; }
int main() { slong iter; flint_rand_t state; flint_printf("chi...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { acb_t zn1, zn2, zn1n2, zn1zn2; dirichlet_group_t G; dirichlet_char_t chi; ulong q, m, n1, n2, iter2; int res; q = 1 + n_randint(state, 1000); dirichlet_group_init(G, q); dirichlet_char_init(chi, G); acb_init(zn1); acb_init(zn2); acb_init(zn1n2); acb_init(zn1zn2); /* check chi(n1) chi(n2) = chi(n1 n2) */ for (iter2 = 0; iter2 < 10; iter2++) { do { m = 1 + n_randint(state, q); } while (n_gcd(q, m) != 1); dirichlet_char_log(chi, G, m); n1 = n_randint(state, 1000); n2 = n_randint(state, 1000); acb_dirichlet_chi(zn1, G, chi, n1, 53); acb_dirichlet_pairing(zn2, G, m, n1, 53); if (!acb_overlaps(zn1, zn2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("m = %wu\n\n", m); flint_printf("n = %wu\n\n", n1); flint_printf("char = "); acb_printd(zn1, 15); flint_printf("\n\n"); flint_printf("pairing = "); acb_printd(zn2, 15); flint_printf("\n\n"); dirichlet_char_print(G, chi); dirichlet_char_log(chi, G, m); flint_printf("log(m) = "); dirichlet_char_print(G, chi); dirichlet_char_log(chi, G, n1); flint_printf("log(n1) = "); dirichlet_char_print(G, chi); flint_abort(); } acb_dirichlet_pairing(zn2, G, m, n2, 53); acb_dirichlet_pairing(zn1n2, G, m, n1 * n2, 53); acb_mul(zn1zn2, zn1, zn2, 53); if (!acb_overlaps(zn1n2, zn1zn2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("m = %wu\n\n", m); flint_printf("n1 = %wu\n\n", n1); flint_printf("n2 = %wu\n\n", n2); flint_printf("zn1 = "); acb_printd(zn1, 15); flint_printf("\n\n"); flint_printf("zn2 = "); acb_printd(zn2, 15); flint_printf("\n\n"); flint_printf("zn1n2 = "); acb_printd(zn1n2, 15); flint_printf("\n\n"); flint_printf("zn1zn2 = "); acb_printd(zn1zn2, 15); flint_printf("\n\n"); flint_abort(); } } if (iter % 10 == 0) { /* check orthogonality */ acb_zero(zn1); n1 = n_randint(state, 1000); for (m = 1; m <= q; m++) { if (n_gcd(q, m) == 1) { acb_dirichlet_pairing(zn2, G, m, n1, 53); acb_add(zn1, zn1, zn2, 53); } } if (n1 % q == 1 % q) res = arb_contains_si(acb_realref(zn1), n_euler_phi(q)) && arb_contains_zero(acb_imagref(zn1)); else res = acb_contains_zero(zn1); if (!res) { flint_printf("FAIL: orthogonality\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("phi = %wu\n\n", n_euler_phi(q)); flint_printf("n1 = %wu\n\n", n1); flint_printf("zn1 = "); acb_printd(zn1, 15); flint_printf("\n\n"); flint_abort(); } } dirichlet_group_clear(G); dirichlet_char_clear(chi); acb_clear(zn1); acb_clear(zn2); acb_clear(zn1n2); acb_clear(zn1zn2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }