void acb_hypgeom_u(acb_t res, const acb_t a, const acb_t b, const acb_t z, long prec) { acb_t t; acb_init(t); acb_sub(t, a, b, prec); acb_add_ui(t, t, 1, prec); if ((acb_is_int(a) && arf_sgn(arb_midref(acb_realref(a))) <= 0) || (acb_is_int(t) && arf_sgn(arb_midref(acb_realref(t))) <= 0) || acb_hypgeom_u_use_asymp(z, prec)) { acb_neg(t, a); acb_pow(t, z, t, prec); acb_hypgeom_u_asymp(res, a, b, z, -1, prec); acb_mul(res, res, t, prec); } else { acb_hypgeom_u_1f1(res, a, b, z, prec); } acb_clear(t); }
void arb_set_interval_arf(arb_t x, const arf_t a, const arf_t b, slong prec) { arf_t t; int inexact; if (arf_is_inf(a) && arf_equal(a, b)) { /* [-inf, -inf] or [+inf, +inf] */ arf_set(arb_midref(x), a); mag_zero(arb_radref(x)); return; } arf_init(t); arf_sub(t, b, a, MAG_BITS, ARF_RND_UP); if (arf_sgn(t) < 0) { flint_printf("exception: arb_set_interval_arf: endpoints not ordered\n"); abort(); } arf_get_mag(arb_radref(x), t); inexact = arf_add(arb_midref(x), a, b, prec, ARB_RND); if (inexact) arf_mag_add_ulp(arb_radref(x), arb_radref(x), arb_midref(x), prec); arb_mul_2exp_si(x, x, -1); arf_clear(t); }
void arb_sech(arb_t res, const arb_t x, slong prec) { if (arf_cmpabs_2exp_si(arb_midref(x), 0) > 0) { arb_t t; arb_init(t); if (arf_sgn(arb_midref(x)) > 0) { arb_neg(t, x); arb_exp(t, t, prec + 4); } else { arb_exp(t, x, prec + 4); } arb_mul(res, t, t, prec + 4); arb_add_ui(res, res, 1, prec + 4); arb_div(res, t, res, prec); arb_mul_2exp_si(res, res, 1); arb_clear(t); } else { arb_cosh(res, x, prec + 4); arb_inv(res, res, prec); } }
static __inline__ void arb_nonnegative_part(arb_t z, const arb_t x, long prec) { if (arb_contains_negative(x)) { arf_t t; arf_init(t); arf_set_mag(t, arb_radref(x)); arf_add(arb_midref(z), arb_midref(x), t, MAG_BITS, ARF_RND_CEIL); if (arf_sgn(arb_midref(z)) <= 0) { mag_zero(arb_radref(z)); } else { arf_mul_2exp_si(arb_midref(z), arb_midref(z), -1); arf_get_mag(arb_radref(z), arb_midref(z)); /* XXX: needed since arf_get_mag is inexact */ arf_set_mag(arb_midref(z), arb_radref(z)); } arf_clear(t); } else { arb_set(z, x); } }
void acb_lambertw_cleared_cut_fix_small(acb_t res, const acb_t z, const acb_t ez1, const fmpz_t k, int flags, slong prec) { acb_t zz, zmid, zmide1; arf_t eps; acb_init(zz); acb_init(zmid); acb_init(zmide1); arf_init(eps); arf_mul_2exp_si(eps, arb_midref(acb_realref(z)), -prec); acb_set(zz, z); if (arf_sgn(arb_midref(acb_realref(zz))) < 0 && (!fmpz_is_zero(k) || arf_sgn(arb_midref(acb_realref(ez1))) < 0) && arf_cmpabs(arb_midref(acb_imagref(zz)), eps) < 0) { /* now the value must be in [0,2eps] */ arf_get_mag(arb_radref(acb_imagref(zz)), eps); arf_set_mag(arb_midref(acb_imagref(zz)), arb_radref(acb_imagref(zz))); if (arf_sgn(arb_midref(acb_imagref(z))) >= 0) { acb_lambertw_cleared_cut(res, zz, k, flags, prec); } else { fmpz_t kk; fmpz_init(kk); fmpz_neg(kk, k); acb_lambertw_cleared_cut(res, zz, kk, flags, prec); acb_conj(res, res); fmpz_clear(kk); } } else { acb_lambertw_cleared_cut(res, zz, k, flags, prec); } acb_clear(zz); acb_clear(zmid); acb_clear(zmide1); arf_clear(eps); }
void arb_pow(arb_t z, const arb_t x, const arb_t y, slong prec) { if (arb_is_zero(y)) { arb_one(z); return; } if (arb_is_zero(x)) { if (arb_is_positive(y)) arb_zero(z); else arb_indeterminate(z); return; } if (arb_is_exact(y) && !arf_is_special(arb_midref(x))) { const arf_struct * ymid = arb_midref(y); /* 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); arb_pow_fmpz_binexp(z, x, e, prec); } else { arf_get_fmpz_fixed_si(e, ymid, -1); arb_sqrt(z, x, prec + fmpz_bits(e)); arb_pow_fmpz_binexp(z, z, e, prec); } fmpz_clear(e); return; } else if (arf_is_int(ymid) && arf_sgn(arb_midref(x)) < 0) { /* use (-x)^n = (-1)^n * x^n to avoid NaNs at least at high enough precision */ int odd = !arf_is_int_2exp_si(ymid, 1); _arb_pow_exp(z, x, 1, y, prec); if (odd) arb_neg(z, z); return; } } _arb_pow_exp(z, x, 0, y, prec); }
int arb_contains_arf(const arb_t x, const arf_t y) { if (arf_is_nan(y)) { return arf_is_nan(arb_midref(x)); } else if (arf_is_nan(arb_midref(x))) { return 1; } else if (arb_is_exact(x)) { return arf_equal(arb_midref(x), y); } else { arf_t t; arf_struct tmp[3]; int result; arf_init(t); /* y >= xm - xr <=> 0 >= xm - xr - y */ arf_init_set_shallow(tmp + 0, arb_midref(x)); arf_init_neg_mag_shallow(tmp + 1, arb_radref(x)); arf_init_neg_shallow(tmp + 2, y); arf_sum(t, tmp, 3, MAG_BITS, ARF_RND_DOWN); result = (arf_sgn(t) <= 0); if (result) { /* y <= xm + xr <=> 0 <= xm + xr - y */ arf_init_set_mag_shallow(tmp + 1, arb_radref(x)); arf_sum(t, tmp, 3, MAG_BITS, ARF_RND_DOWN); result = (arf_sgn(t) >= 0); } arf_clear(t); return result; } }
void acb_tan_pi(acb_t r, const acb_t z, slong prec) { if (arb_is_zero(acb_imagref(z))) { arb_tan_pi(acb_realref(r), acb_realref(z), prec); arb_zero(acb_imagref(r)); } else if (arb_is_zero(acb_realref(z))) { arb_t t; arb_init(t); arb_const_pi(t, prec + 4); arb_mul(t, acb_imagref(z), t, prec + 4); arb_tanh(acb_imagref(r), t, prec); arb_zero(acb_realref(r)); arb_clear(t); } else { acb_t t; acb_init(t); if (arf_cmpabs_2exp_si(arb_midref(acb_imagref(z)), 0) < 0) { acb_sin_cos_pi(r, t, z, prec + 4); acb_div(r, r, t, prec); } else { acb_mul_2exp_si(t, z, 1); if (arf_sgn(arb_midref(acb_imagref(z))) > 0) { acb_exp_pi_i(t, t, prec + 4); acb_add_ui(r, t, 1, prec + 4); acb_div(r, t, r, prec + 4); acb_mul_2exp_si(r, r, 1); acb_sub_ui(r, r, 1, prec); acb_div_onei(r, r); } else { acb_neg(t, t); acb_exp_pi_i(t, t, prec + 4); acb_add_ui(r, t, 1, prec + 4); acb_div(r, t, r, prec + 4); acb_mul_2exp_si(r, r, 1); acb_sub_ui(r, r, 1, prec); acb_mul_onei(r, r); } } acb_clear(t); } }
void acb_lambertw_principal_d(acb_t res, const acb_t z) { double za, zb, wa, wb, ewa, ewb, t, u, q, r; int k, maxk = 15; za = arf_get_d(arb_midref(acb_realref(z)), ARF_RND_DOWN); zb = arf_get_d(arb_midref(acb_imagref(z)), ARF_RND_DOWN); /* make sure we end up on the right branch */ if (za < -0.367 && zb > -1e-20 && zb <= 0.0 && arf_sgn(arb_midref(acb_imagref(z))) < 0) zb = -1e-20; wa = za; wb = zb; if (fabs(wa) > 2.0 || fabs(wb) > 2.0) { t = atan2(wb, wa); wa = 0.5 * log(wa * wa + wb * wb); wb = t; } else if (fabs(wa) > 0.25 || fabs(wb) > 0.25) { /* We have W(z) ~= -1 + (2(ez+1))^(1/2) near the branch point. Changing the exponent to 1/4 gives a much worse local guess which however does the job on a larger domain. */ wa *= 5.43656365691809; wb *= 5.43656365691809; wa += 2.0; t = atan2(wb, wa); r = pow(wa * wa + wb * wb, 0.125); wa = r * cos(0.25 * t); wb = r * sin(0.25 * t); wa -= 1.0; } for (k = 0; k < maxk; k++) { t = exp(wa); ewa = t * cos(wb); ewb = t * sin(wb); t = (ewa * wa - ewb * wb); q = t + ewa; t -= za; u = (ewb * wa + ewa * wb); r = u + ewb; u -= zb; ewa = q * t + r * u; ewb = q * u - r * t; r = 1.0 / (q * q + r * r); ewa *= r; ewb *= r; if ((ewa*ewa + ewb*ewb) < (wa*wa + wb*wb) * 1e-12) maxk = FLINT_MIN(maxk, k + 2); wa -= ewa; wb -= ewb; } acb_set_d_d(res, wa, wb); }
void acb_hypgeom_m_1f1(acb_t res, const acb_t a, const acb_t b, const acb_t z, int regularized, slong prec) { if (arf_sgn(arb_midref(acb_realref(z))) >= 0 || (acb_is_int(a) && arb_is_nonpositive(acb_realref(a)))) { _acb_hypgeom_m_1f1(res, a, b, z, regularized, prec, prec, 0); } else { _acb_hypgeom_m_1f1(res, a, b, z, regularized, prec, prec, 1); } }
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); }
void arb_hypgeom_infsum(arb_t P, arb_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); arb_hypgeom_sum(P, Q, hyp, n, prec); if (arf_sgn(arb_midref(Q)) < 0) { arb_neg(P, P); arb_neg(Q, Q); } /* We have p/q = s + err i.e. (p + q*err)/q = s */ { mag_t u; mag_init(u); arb_get_mag(u, Q); mag_mul(u, u, err); mag_add(arb_radref(P), arb_radref(P), u); mag_clear(u); } mag_clear(z); mag_clear(err); }
void acb_rising(acb_t y, const acb_t x, const acb_t n, long prec) { if (acb_is_int(n) && arf_sgn(arb_midref(acb_realref(n))) >= 0 && arf_cmpabs_ui(arb_midref(acb_realref(n)), FLINT_MAX(prec, 100)) < 0) { acb_rising_ui_rec(y, x, arf_get_si(arb_midref(acb_realref(n)), ARF_RND_DOWN), prec); } else { acb_t t; acb_init(t); acb_add(t, x, n, prec); acb_gamma(t, t, prec); acb_rgamma(y, x, prec); acb_mul(y, y, t, prec); acb_clear(t); } }
void acb_hypgeom_m_1f1(acb_t res, const acb_t a, const acb_t b, const acb_t z, int regularized, long prec) { acb_t t; if (regularized) { acb_init(t); acb_rgamma(t, b, prec); } if (arf_sgn(arb_midref(acb_realref(z))) >= 0 || (acb_is_int(a) && arb_is_nonpositive(acb_realref(a)))) { _acb_hypgeom_m_1f1(res, a, b, z, prec); } else { /* Kummer's transformation */ acb_t u, v; acb_init(u); acb_init(v); acb_sub(u, b, a, prec); acb_neg(v, z); _acb_hypgeom_m_1f1(u, u, b, v, prec); acb_exp(v, z, prec); acb_mul(res, u, v, prec); acb_clear(u); acb_clear(v); } if (regularized) { acb_mul(res, res, t, prec); acb_clear(t); } }
void arb_sqrtpos(arb_t z, const arb_t x, long prec) { if (!arb_is_finite(x)) { if (mag_is_zero(arb_radref(x)) && arf_is_pos_inf(arb_midref(x))) arb_pos_inf(z); else arb_zero_pm_inf(z); } else if (arb_contains_nonpositive(x)) { arf_t t; arf_init(t); arf_set_mag(t, arb_radref(x)); arf_add(t, arb_midref(x), t, MAG_BITS, ARF_RND_CEIL); if (arf_sgn(t) <= 0) { arb_zero(z); } else { arf_sqrt(t, t, MAG_BITS, ARF_RND_CEIL); arf_mul_2exp_si(t, t, -1); arf_set(arb_midref(z), t); arf_get_mag(arb_radref(z), t); } arf_clear(t); } else { arb_sqrt(z, x, prec); } arb_nonnegative_part(z, z, prec); }
/* atan(x) = pi/2 - eps, eps < 1/x <= 2^(1-mag) */ void arb_atan_inf_eps(arb_t z, const arf_t x, slong prec) { fmpz_t mag; fmpz_init(mag); fmpz_neg(mag, ARF_EXPREF(x)); fmpz_add_ui(mag, mag, 1); if (arf_sgn(x) > 0) { arb_const_pi(z, prec); } else { arb_const_pi(z, prec); arb_neg(z, z); } arb_mul_2exp_si(z, z, -1); arb_add_error_2exp_fmpz(z, mag); fmpz_clear(mag); }
int main() { slong iter; flint_rand_t state; flint_printf("get_mpn_fixed_mod_pi4...."); fflush(stdout); flint_randinit(state); /* _flint_rand_init_gmp(state); */ for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { arf_t x; int octant; fmpz_t q; mp_ptr w; arb_t wb, t, u; mp_size_t wn; slong prec, prec2; int success; mp_limb_t error; prec = 2 + n_randint(state, 10000); wn = 1 + n_randint(state, 200); prec2 = FLINT_MAX(prec, wn * FLINT_BITS) + 100; arf_init(x); arb_init(wb); arb_init(t); arb_init(u); fmpz_init(q); w = flint_malloc(sizeof(mp_limb_t) * wn); arf_randtest(x, state, prec, 14); /* this should generate numbers close to multiples of pi/4 */ if (n_randint(state, 4) == 0) { arb_const_pi(t, prec); arb_mul_2exp_si(t, t, -2); fmpz_randtest(q, state, 200); arb_mul_fmpz(t, t, q, prec); arf_add(x, x, arb_midref(t), prec, ARF_RND_DOWN); } arf_abs(x, x); success = _arb_get_mpn_fixed_mod_pi4(w, q, &octant, &error, x, wn); if (success) { /* could round differently */ if (fmpz_fdiv_ui(q, 8) != octant) { flint_printf("bad octant\n"); abort(); } _arf_set_mpn_fixed(arb_midref(wb), w, wn, wn, 0, FLINT_BITS * wn, ARB_RND); mag_set_ui_2exp_si(arb_radref(wb), error, -FLINT_BITS * wn); arb_const_pi(u, prec2); arb_mul_2exp_si(u, u, -2); arb_set(t, wb); if (octant % 2 == 1) arb_sub(t, u, t, prec2); arb_addmul_fmpz(t, u, q, prec2); if (!arb_contains_arf(t, x)) { flint_printf("FAIL (containment)\n"); flint_printf("x = "); arf_printd(x, 50); flint_printf("\n\n"); flint_printf("q = "); fmpz_print(q); flint_printf("\n\n"); flint_printf("w = "); arb_printd(wb, 50); flint_printf("\n\n"); flint_printf("t = "); arb_printd(t, 50); flint_printf("\n\n"); abort(); } arb_const_pi(t, prec2); arb_mul_2exp_si(t, t, -2); if (arf_sgn(arb_midref(wb)) < 0 || arf_cmp(arb_midref(wb), arb_midref(t)) >= 0) { flint_printf("FAIL (expected 0 <= w < pi/4)\n"); flint_printf("x = "); arf_printd(x, 50); flint_printf("\n\n"); flint_printf("w = "); arb_printd(wb, 50); flint_printf("\n\n"); abort(); } } flint_free(w); fmpz_clear(q); arf_clear(x); arb_clear(wb); arb_clear(t); arb_clear(u); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
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; }
void _acb_poly_zeta_cpx_reflect(acb_ptr t, const acb_t h, const acb_t a, int deflate, slong len, slong prec) { /* use reflection formula */ if (arf_sgn(arb_midref(acb_realref(h))) < 0 && acb_is_one(a)) { /* zeta(s) = (2*pi)**s * sin(pi*s/2) / pi * gamma(1-s) * zeta(1-s) */ acb_t pi, hcopy; acb_ptr f, s1, s2, s3, s4, u; slong i; acb_init(pi); acb_init(hcopy); f = _acb_vec_init(2); s1 = _acb_vec_init(len); s2 = _acb_vec_init(len); s3 = _acb_vec_init(len); s4 = _acb_vec_init(len); u = _acb_vec_init(len); acb_set(hcopy, h); acb_const_pi(pi, prec); /* s1 = (2*pi)**s */ acb_mul_2exp_si(pi, pi, 1); _acb_poly_pow_cpx(s1, pi, h, len, prec); acb_mul_2exp_si(pi, pi, -1); /* s2 = sin(pi*s/2) / pi */ acb_set(f, h); acb_one(f + 1); acb_mul_2exp_si(f, f, -1); acb_mul_2exp_si(f + 1, f + 1, -1); _acb_poly_sin_pi_series(s2, f, 2, len, prec); _acb_vec_scalar_div(s2, s2, len, pi, prec); /* s3 = gamma(1-s) */ acb_sub_ui(f, hcopy, 1, prec); acb_neg(f, f); acb_set_si(f + 1, -1); _acb_poly_gamma_series(s3, f, 2, len, prec); /* s4 = zeta(1-s) */ acb_sub_ui(f, hcopy, 1, prec); acb_neg(f, f); _acb_poly_zeta_cpx_series(s4, f, a, 0, len, prec); for (i = 1; i < len; i += 2) acb_neg(s4 + i, s4 + i); _acb_poly_mullow(u, s1, len, s2, len, len, prec); _acb_poly_mullow(s1, s3, len, s4, len, len, prec); _acb_poly_mullow(t, u, len, s1, len, len, prec); /* add 1/(1-(s+t)) = 1/(1-s) + t/(1-s)^2 + ... */ if (deflate) { acb_sub_ui(u, hcopy, 1, prec); acb_neg(u, u); acb_inv(u, u, prec); for (i = 1; i < len; i++) acb_mul(u + i, u + i - 1, u, prec); _acb_vec_add(t, t, u, len, prec); } acb_clear(pi); acb_clear(hcopy); _acb_vec_clear(f, 2); _acb_vec_clear(s1, len); _acb_vec_clear(s2, len); _acb_vec_clear(s3, len); _acb_vec_clear(s4, len); _acb_vec_clear(u, len); } else { _acb_poly_zeta_cpx_series(t, h, a, deflate, len, prec); } }
int arf_sum(arf_t s, arf_srcptr terms, long len, long prec, arf_rnd_t rnd) { arf_ptr blocks; long i, j, used; int have_merged, res; /* first check if the result is inf or nan */ { int have_pos_inf = 0; int have_neg_inf = 0; for (i = 0; i < len; i++) { if (arf_is_pos_inf(terms + i)) { if (have_neg_inf) { arf_nan(s); return 0; } have_pos_inf = 1; } else if (arf_is_neg_inf(terms + i)) { if (have_pos_inf) { arf_nan(s); return 0; } have_neg_inf = 1; } else if (arf_is_nan(terms + i)) { arf_nan(s); return 0; } } if (have_pos_inf) { arf_pos_inf(s); return 0; } if (have_neg_inf) { arf_neg_inf(s); return 0; } } blocks = flint_malloc(sizeof(arf_struct) * len); for (i = 0; i < len; i++) arf_init(blocks + i); /* put all terms into blocks */ used = 0; for (i = 0; i < len; i++) { if (!arf_is_zero(terms + i)) { arf_set(blocks + used, terms + i); used++; } } /* merge blocks until all are well separated */ have_merged = 1; while (used >= 2 && have_merged) { have_merged = 0; for (i = 0; i < used && !have_merged; i++) { for (j = i + 1; j < used && !have_merged; j++) { if (_arf_are_close(blocks + i, blocks + j, prec)) { arf_add(blocks + i, blocks + i, blocks + j, ARF_PREC_EXACT, ARF_RND_DOWN); /* remove the merged block */ arf_swap(blocks + j, blocks + used - 1); used--; /* remove the updated block if the sum is zero */ if (arf_is_zero(blocks + i)) { arf_swap(blocks + i, blocks + used - 1); used--; } have_merged = 1; } } } } if (used == 0) { arf_zero(s); res = 0; } else if (used == 1) { res = arf_set_round(s, blocks + 0, prec, rnd); } else { /* find the two largest blocks */ for (i = 1; i < used; i++) if (arf_cmpabs(blocks + 0, blocks + i) < 0) arf_swap(blocks + 0, blocks + i); for (i = 2; i < used; i++) if (arf_cmpabs(blocks + 1, blocks + i) < 0) arf_swap(blocks + 1, blocks + i); res = _arf_add_eps(s, blocks + 0, arf_sgn(blocks + 1), prec, rnd); } for (i = 0; i < len; i++) arf_clear(blocks + i); flint_free(blocks); return res; }
int main() { slong iter; flint_rand_t state; flint_printf("elliptic_p...."); fflush(stdout); flint_randinit(state); /* check test values */ for (iter = 0; iter < 100; iter++) { slong i; acb_t z, tau, p1, p2; acb_init(z); acb_init(tau); acb_init(p1); acb_init(p2); for (i = 0; i < NUM_TESTS; i++) { acb_set_dddd(z, testdata[i][0], 0.0, testdata[i][1], 0.0); acb_set_dddd(tau, testdata[i][2], 0.0, testdata[i][3], 0.0); acb_set_dddd(p2, testdata[i][4], EPS, testdata[i][5], EPS); acb_modular_elliptic_p(p1, z, tau, 2 + n_randint(state, 1000)); if (!acb_overlaps(p1, p2)) { flint_printf("FAIL (test value)\n"); flint_printf("tau = "); acb_printd(tau, 15); flint_printf("\n\n"); flint_printf("z = "); acb_printd(z, 15); flint_printf("\n\n"); flint_printf("p1 = "); acb_printd(p1, 15); flint_printf("\n\n"); flint_printf("p2 = "); acb_printd(p2, 15); flint_printf("\n\n"); abort(); } } acb_clear(z); acb_clear(tau); acb_clear(p1); acb_clear(p2); } /* Test periods */ for (iter = 0; iter < 2000; iter++) { acb_t tau, z1, z2, p1, p2; slong m, n, e0, prec0, prec1, prec2; acb_init(tau); acb_init(z1); acb_init(z2); acb_init(p1); acb_init(p2); e0 = 1 + n_randint(state, 10); prec0 = 2 + n_randint(state, 1000); prec1 = 2 + n_randint(state, 1000); prec2 = 2 + n_randint(state, 1000); acb_randtest(tau, state, prec0, e0); if (arf_sgn(arb_midref(acb_imagref(tau))) < 0) acb_neg(tau, tau); acb_randtest(z1, state, prec0, e0); acb_randtest(p1, state, prec0, e0); acb_randtest(p2, state, prec0, e0); /* z2 = z1 + m + n*tau */ m = n_randint(state, 10); n = n_randint(state, 10); acb_add_ui(z2, z1, m, prec0); acb_addmul_ui(z2, tau, n, prec0); acb_modular_elliptic_p(p1, z1, tau, prec1); acb_modular_elliptic_p(p2, z2, tau, prec2); if (!acb_overlaps(p1, p2)) { flint_printf("FAIL (overlap)\n"); flint_printf("tau = "); acb_printd(tau, 15); flint_printf("\n\n"); flint_printf("z1 = "); acb_printd(z1, 15); flint_printf("\n\n"); flint_printf("z2 = "); acb_printd(z2, 15); flint_printf("\n\n"); flint_printf("p1 = "); acb_printd(p1, 15); flint_printf("\n\n"); flint_printf("p2 = "); acb_printd(p2, 15); flint_printf("\n\n"); abort(); } acb_modular_elliptic_p(z1, z1, tau, prec1); if (!acb_overlaps(z1, p1)) { flint_printf("FAIL (aliasing)\n"); flint_printf("tau = "); acb_printd(tau, 15); flint_printf("\n\n"); flint_printf("z1 = "); acb_printd(z1, 15); flint_printf("\n\n"); flint_printf("p1 = "); acb_printd(p1, 15); flint_printf("\n\n"); abort(); } acb_clear(tau); acb_clear(z1); acb_clear(z2); acb_clear(p1); acb_clear(p2); } flint_randclear(state); flint_cleanup(); flint_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); }
static int acb_is_nonpositive_int(const acb_t x) { return acb_is_int(x) && arf_sgn(arb_midref(acb_realref(x))) <= 0; }
int main() { slong iter; flint_rand_t state; flint_printf("log_arf...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 5000 * arb_test_multiplier(); iter++) { arf_t x; arb_t y1, y2; slong prec1, prec2, acc1, acc2; prec1 = 2 + n_randint(state, 9000); prec2 = 2 + n_randint(state, 9000); arf_init(x); arb_init(y1); arb_init(y2); arf_randtest_special(x, state, 1 + n_randint(state, 9000), 200); arb_randtest_special(y1, state, 1 + n_randint(state, 9000), 200); arb_randtest_special(y2, state, 1 + n_randint(state, 9000), 200); if (n_randint(state, 2)) arf_add_ui(x, x, 1, 2 + n_randint(state, 9000), ARF_RND_DOWN); arb_log_arf(y1, x, prec1); arb_log_arf(y2, x, prec2); if (!arb_overlaps(y1, y2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("prec1 = %wd, prec2 = %wd\n\n", prec1, prec2); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y1 = "); arb_print(y1); flint_printf("\n\n"); flint_printf("y2 = "); arb_print(y2); flint_printf("\n\n"); abort(); } acc1 = arb_rel_accuracy_bits(y1); acc2 = arb_rel_accuracy_bits(y2); if (arf_sgn(x) > 0) { if (acc1 < prec1 - 2 || acc2 < prec2 - 2) { flint_printf("FAIL: accuracy\n\n"); flint_printf("prec1 = %wd, prec2 = %wd\n\n", prec1, prec2); flint_printf("acc1 = %wd, acc2 = %wd\n\n", acc1, acc2); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y1 = "); arb_print(y1); flint_printf("\n\n"); flint_printf("y2 = "); arb_print(y2); flint_printf("\n\n"); abort(); } } arf_clear(x); arb_clear(y1); arb_clear(y2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
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 acb_hypgeom_2f1(acb_t res, const acb_t a, const acb_t b, const acb_t c, const acb_t z, int flags, slong prec) { int algorithm, regularized; regularized = flags & ACB_HYPGEOM_2F1_REGULARIZED; if (!acb_is_finite(a) || !acb_is_finite(b) || !acb_is_finite(c) || !acb_is_finite(z)) { acb_indeterminate(res); return; } if (acb_is_zero(z)) { if (regularized) acb_rgamma(res, c, prec); else acb_one(res); return; } if (regularized && acb_is_int(c) && arb_is_nonpositive(acb_realref(c))) { if ((acb_is_int(a) && arb_is_nonpositive(acb_realref(a)) && arf_cmp(arb_midref(acb_realref(a)), arb_midref(acb_realref(c))) >= 0) || (acb_is_int(b) && arb_is_nonpositive(acb_realref(b)) && arf_cmp(arb_midref(acb_realref(b)), arb_midref(acb_realref(c))) >= 0)) { acb_zero(res); return; } } if (regularized && acb_eq(a, c)) { _acb_hypgeom_2f1r_reduced(res, b, c, z, prec); return; } if (regularized && acb_eq(b, c)) { _acb_hypgeom_2f1r_reduced(res, a, c, z, prec); return; } /* polynomial */ if (acb_is_int(a) && arf_sgn(arb_midref(acb_realref(a))) <= 0 && arf_cmpabs_ui(arb_midref(acb_realref(a)), prec) < 0) { acb_hypgeom_2f1_direct(res, a, b, c, z, regularized, prec); return; } /* polynomial */ if (acb_is_int(b) && arf_sgn(arb_midref(acb_realref(b))) <= 0 && arf_cmpabs_ui(arb_midref(acb_realref(b)), prec) < 0) { acb_hypgeom_2f1_direct(res, a, b, c, z, regularized, prec); return; } /* Try to reduce to a polynomial case using the Pfaff transformation */ /* TODO: look at flags for integer c-b, c-a here, even when c is nonexact */ if (acb_is_exact(c)) { acb_t t; acb_init(t); acb_sub(t, c, b, prec); if (acb_is_int(t) && arb_is_nonpositive(acb_realref(t))) { acb_hypgeom_2f1_transform(res, a, b, c, z, flags, 1, prec); acb_clear(t); return; } acb_sub(t, c, a, prec); if (acb_is_int(t) && arb_is_nonpositive(acb_realref(t))) { int f1, f2; /* When swapping a, b, also swap the flags. */ f1 = flags & ACB_HYPGEOM_2F1_AC; f2 = flags & ACB_HYPGEOM_2F1_BC; flags &= ~ACB_HYPGEOM_2F1_AC; flags &= ~ACB_HYPGEOM_2F1_BC; if (f1) flags |= ACB_HYPGEOM_2F1_BC; if (f2) flags |= ACB_HYPGEOM_2F1_AC; acb_hypgeom_2f1_transform(res, b, a, c, z, flags, 1, prec); acb_clear(t); return; } acb_clear(t); } /* special value at z = 1 */ if (acb_is_one(z)) { acb_t t, u, v; acb_init(t); acb_init(u); acb_init(v); acb_sub(t, c, a, prec); acb_sub(u, c, b, prec); acb_sub(v, t, b, prec); if (arb_is_positive(acb_realref(v))) { acb_rgamma(t, t, prec); acb_rgamma(u, u, prec); acb_mul(t, t, u, prec); acb_gamma(v, v, prec); acb_mul(t, t, v, prec); if (!regularized) { acb_gamma(v, c, prec); acb_mul(t, t, v, prec); } acb_set(res, t); } else { acb_indeterminate(res); } acb_clear(t); acb_clear(u); acb_clear(v); return; } algorithm = acb_hypgeom_2f1_choose(z); if (algorithm == 0) { acb_hypgeom_2f1_direct(res, a, b, c, z, regularized, prec); } else if (algorithm >= 1 && algorithm <= 5) { acb_hypgeom_2f1_transform(res, a, b, c, z, flags, algorithm, prec); } else { acb_hypgeom_2f1_corner(res, a, b, c, z, regularized, prec); } }
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); }
/* todo: use euler product for complex s, and check efficiency for large negative integers */ void acb_dirichlet_zeta(acb_t res, const acb_t s, slong prec) { acb_t a; double cutoff; if (acb_is_int(s) && arf_cmpabs_2exp_si(arb_midref(acb_realref(s)), FLINT_BITS - 1) < 0) { acb_zeta_si(res, arf_get_si(arb_midref(acb_realref(s)), ARF_RND_DOWN), prec); return; } cutoff = 24.0 * prec * sqrt(prec); if (arf_cmpabs_d(arb_midref(acb_imagref(s)), cutoff) >= 0 && arf_cmpabs_d(arb_midref(acb_realref(s)), 10 + prec * 0.1) <= 0) { acb_dirichlet_zeta_rs(res, s, 0, prec); return; } acb_init(a); acb_one(a); if (arf_sgn(arb_midref(acb_realref(s))) < 0) { acb_t t, u, v; slong wp = prec + 6; acb_init(t); acb_init(u); acb_init(v); acb_sub_ui(t, s, 1, wp); /* 2 * (2pi)^(s-1) */ arb_const_pi(acb_realref(u), wp); acb_mul_2exp_si(u, u, 1); acb_pow(u, u, t, wp); acb_mul_2exp_si(u, u, 1); /* sin(pi*s/2) */ acb_mul_2exp_si(v, s, -1); acb_sin_pi(v, v, wp); acb_mul(u, u, v, wp); /* gamma(1-s) zeta(1-s) */ acb_neg(t, t); acb_gamma(v, t, wp); acb_mul(u, u, v, wp); acb_hurwitz_zeta(v, t, a, wp); acb_mul(res, u, v, prec); acb_clear(t); acb_clear(u); acb_clear(v); } else { acb_hurwitz_zeta(res, s, a, prec); } acb_clear(a); }
int main() { slong iter; flint_rand_t state; flint_printf("floor...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000; iter++) { arf_t x, y; int result; arf_init(x); arf_init(y); arf_randtest_special(x, state, 2000, 100); arf_randtest_special(y, state, 2000, 100); arf_floor(y, x); result = 1; if (arf_is_int(x) || !arf_is_finite(x)) { result = arf_equal(y, x); } else if (!arf_is_int(y)) { result = 0; } else if (arf_cmp(y, x) >= 0) { result = 0; } else { arf_t s, t[3]; /* check floor(x) - x + 1 > 0 */ arf_init(s); arf_init(t[0]); arf_init(t[1]); arf_init(t[2]); arf_set(t[0], y); arf_neg(t[1], x); arf_one(t[2]); arf_sum(s, (arf_ptr) t, 3, 32, ARF_RND_DOWN); result = arf_sgn(s) > 0; arf_clear(s); arf_clear(t[0]); arf_clear(t[1]); arf_clear(t[2]); } if (!result) { flint_printf("FAIL!\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y = "); arf_print(y); flint_printf("\n\n"); abort(); } arf_floor(x, x); if (!arf_equal(x, y)) { flint_printf("FAIL (aliasing)!\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y = "); arf_print(y); flint_printf("\n\n"); abort(); } arf_clear(x); arf_clear(y); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }