int arb_mat_lu(long * P, arb_mat_t LU, const arb_mat_t A, long prec) { arb_t d, e; arb_ptr * a; long i, j, m, n, r, row, col; int result; m = arb_mat_nrows(A); n = arb_mat_ncols(A); result = 1; if (m == 0 || n == 0) return result; arb_mat_set(LU, A); a = LU->rows; row = col = 0; for (i = 0; i < m; i++) P[i] = i; arb_init(d); arb_init(e); while (row < m && col < n) { r = arb_mat_find_pivot_partial(LU, row, m, col); if (r == -1) { result = 0; break; } else if (r != row) arb_mat_swap_rows(LU, P, row, r); arb_set(d, a[row] + col); for (j = row + 1; j < m; j++) { arb_div(e, a[j] + col, d, prec); arb_neg(e, e); _arb_vec_scalar_addmul(a[j] + col, a[row] + col, n - col, e, prec); arb_zero(a[j] + col); arb_neg(a[j] + row, e); } row++; col++; } arb_clear(d); arb_clear(e); return result; }
void _arb_poly_product_roots(arb_ptr poly, arb_srcptr xs, slong n, slong prec) { if (n == 0) { arb_one(poly); } else if (n == 1) { arb_neg(poly, xs); arb_one(poly + 1); } else if (n == 2) { arb_mul(poly, xs + 0, xs + 1, prec); arb_add(poly + 1, xs + 0, xs + 1, prec); arb_neg(poly + 1, poly + 1); arb_one(poly + 2); } else { const slong m = (n + 1) / 2; arb_ptr tmp; tmp = _arb_vec_init(n + 2); _arb_poly_product_roots(tmp, xs, m, prec); _arb_poly_product_roots(tmp + m + 1, xs + m, n - m, prec); _arb_poly_mul_monic(poly, tmp, m + 1, tmp + m + 1, n - m + 1, prec); _arb_vec_clear(tmp, n + 2); } }
void arb_sin_cos_pi(arb_t s, arb_t c, const arb_t x, long prec) { arb_t t; arb_t u; fmpz_t v; if (arf_cmpabs_2exp_si(arb_midref(x), FLINT_MAX(65536, (4*prec))) > 0) { arf_zero(arb_midref(s)); mag_one(arb_radref(s)); arf_zero(arb_midref(c)); mag_one(arb_radref(c)); return; } arb_init(t); arb_init(u); fmpz_init(v); arb_mul_2exp_si(t, x, 1); arf_get_fmpz(v, arb_midref(t), ARF_RND_NEAR); arb_sub_fmpz(t, t, v, prec); arb_const_pi(u, prec); arb_mul(t, t, u, prec); arb_mul_2exp_si(t, t, -1); switch (fmpz_fdiv_ui(v, 4)) { case 0: arb_sin_cos(s, c, t, prec); break; case 1: arb_sin_cos(c, s, t, prec); arb_neg(c, c); break; case 2: arb_sin_cos(s, c, t, prec); arb_neg(s, s); arb_neg(c, c); break; default: arb_sin_cos(c, s, t, prec); arb_neg(s, s); break; } fmpz_clear(v); arb_clear(t); arb_clear(u); }
static void bsplit(arb_poly_t pol, const arb_t sqrtD, const slong * qbf, slong a, slong b, slong prec) { if (b - a == 0) { arb_poly_one(pol); } else if (b - a == 1) { acb_t z; acb_init(z); /* j((-b+sqrt(-D))/(2a)) */ arb_set_si(acb_realref(z), -FLINT_ABS(qbf[3 * a + 1])); arb_set(acb_imagref(z), sqrtD); acb_div_si(z, z, 2 * qbf[3 * a], prec); acb_modular_j(z, z, prec); if (qbf[3 * a + 1] < 0) { /* (x^2 - 2re(j) x + |j|^2) */ arb_poly_fit_length(pol, 3); arb_mul(pol->coeffs, acb_realref(z), acb_realref(z), prec); arb_addmul(pol->coeffs, acb_imagref(z), acb_imagref(z), prec); arb_mul_2exp_si(pol->coeffs + 1, acb_realref(z), 1); arb_neg(pol->coeffs + 1, pol->coeffs + 1); arb_one(pol->coeffs + 2); _arb_poly_set_length(pol, 3); } else { /* (x-j) */ arb_poly_fit_length(pol, 2); arb_neg(pol->coeffs, acb_realref(z)); arb_one(pol->coeffs + 1); _arb_poly_set_length(pol, 2); } acb_clear(z); } else { arb_poly_t tmp; arb_poly_init(tmp); bsplit(pol, sqrtD, qbf, a, a + (b - a) / 2, prec); bsplit(tmp, sqrtD, qbf, a + (b - a) / 2, b, prec); arb_poly_mul(pol, pol, tmp, prec); arb_poly_clear(tmp); } }
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); } }
void acb_dirichlet_theta_arb(acb_t res, const dirichlet_group_t G, const dirichlet_char_t chi, const arb_t t, slong prec) { slong len; ulong order; arb_t xt; mag_t e; len = acb_dirichlet_theta_length(G->q, t, prec); arb_init(xt); _acb_dirichlet_theta_argument_at_arb(xt, G->q, t, prec); mag_init(e); mag_tail_kexpk2_arb(e, xt, len); arb_neg(xt, xt); arb_exp(xt, xt, prec); /* TODO: tune this limit */ order = dirichlet_order_char(G, chi); if (order < 30) _acb_dirichlet_theta_arb_smallorder(res, G, chi, xt, len, prec); else _acb_dirichlet_theta_arb_naive(res, G, chi, xt, len, prec); arb_add_error_mag(acb_realref(res), e); arb_add_error_mag(acb_imagref(res), e); mag_clear(e); acb_mul_2exp_si(res, res, 1); arb_clear(xt); }
void _arb_poly_sin_cos_pi_series(arb_ptr s, arb_ptr c, arb_srcptr h, slong hlen, slong n, slong prec) { hlen = FLINT_MIN(hlen, n); if (hlen == 1) { arb_sin_cos_pi(s, c, h, prec); _arb_vec_zero(s + 1, n - 1); _arb_vec_zero(c + 1, n - 1); } else if (n == 2) { arb_t t; arb_init(t); arb_const_pi(t, prec); arb_mul(t, t, h + 1, prec); arb_sin_cos_pi(s, c, h, prec); arb_mul(s + 1, c, t, prec); arb_neg(t, t); arb_mul(c + 1, s, t, prec); arb_clear(t); } else if (hlen < TANGENT_CUTOFF) _arb_poly_sin_cos_series_basecase(s, c, h, hlen, n, prec, 1); else _arb_poly_sin_cos_series_tangent(s, c, h, hlen, n, prec, 1); }
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); }
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_cot(acb_t r, const acb_t z, slong prec) { if (arb_is_zero(acb_imagref(z))) { arb_cot(acb_realref(r), acb_realref(z), prec); arb_zero(acb_imagref(r)); } else if (arb_is_zero(acb_realref(z))) { arb_coth(acb_imagref(r), acb_imagref(z), prec); arb_neg(acb_imagref(r), acb_imagref(r)); arb_zero(acb_realref(r)); } else { acb_t t; acb_init(t); if (arf_cmpabs_2exp_si(arb_midref(acb_imagref(z)), 0) < 0) { acb_sin_cos(r, t, z, prec + 4); acb_div(r, t, r, prec); } else { acb_mul_2exp_si(t, z, 1); if (arf_sgn(arb_midref(acb_imagref(z))) > 0) { acb_mul_onei(t, t); acb_exp(t, t, prec + 4); acb_sub_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); } else { acb_div_onei(t, t); acb_exp(t, t, prec + 4); acb_sub_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); } } acb_clear(t); } }
void acb_sin_cos_pi(acb_t s, acb_t c, const acb_t z, slong prec) { #define a acb_realref(z) #define b acb_imagref(z) if (arb_is_zero(b)) { arb_sin_cos_pi(acb_realref(s), acb_realref(c), a, prec); arb_zero(acb_imagref(s)); arb_zero(acb_imagref(c)); } else if (arb_is_zero(a)) { arb_t t; arb_init(t); arb_const_pi(t, prec); arb_mul(t, t, b, prec); arb_sinh_cosh(acb_imagref(s), acb_realref(c), t, prec); arb_zero(acb_realref(s)); arb_zero(acb_imagref(c)); arb_clear(t); } else { arb_t sa, ca, sb, cb; arb_init(sa); arb_init(ca); arb_init(sb); arb_init(cb); arb_const_pi(sb, prec); arb_mul(sb, sb, b, prec); arb_sin_cos_pi(sa, ca, a, prec); arb_sinh_cosh(sb, cb, sb, prec); arb_mul(acb_realref(s), sa, cb, prec); arb_mul(acb_imagref(s), sb, ca, prec); arb_mul(acb_realref(c), ca, cb, prec); arb_mul(acb_imagref(c), sa, sb, prec); arb_neg(acb_imagref(c), acb_imagref(c)); arb_clear(sa); arb_clear(ca); arb_clear(sb); arb_clear(cb); } #undef a #undef b }
/* compose by poly2 = a*x^n + c, no aliasing; n >= 1 */ void _arb_poly_compose_axnc(arb_ptr res, arb_srcptr poly1, slong len1, const arb_t c, const arb_t a, slong n, slong prec) { slong i; _arb_vec_set_round(res, poly1, len1, prec); /* shift by c (c = 0 case will be fast) */ _arb_poly_taylor_shift(res, c, len1, prec); /* multiply by powers of a */ if (!arb_is_one(a)) { if (arb_equal_si(a, -1)) { for (i = 1; i < len1; i += 2) arb_neg(res + i, res + i); } else if (len1 == 2) { arb_mul(res + 1, res + 1, a, prec); } else { arb_t t; arb_init(t); arb_set(t, a); for (i = 1; i < len1; i++) { arb_mul(res + i, res + i, t, prec); if (i + 1 < len1) arb_mul(t, t, a, prec); } arb_clear(t); } } /* stretch */ for (i = len1 - 1; i >= 1 && n > 1; i--) { arb_swap(res + i * n, res + i); _arb_vec_zero(res + (i - 1) * n + 1, n - 1); } }
void acb_zeta_si(acb_t z, slong s, slong prec) { if (s >= 0) { arb_zeta_ui(acb_realref(z), s, prec); } else { arb_bernoulli_ui(acb_realref(z), 1-s, prec); arb_div_ui(acb_realref(z), acb_realref(z), 1-s, prec); arb_neg(acb_realref(z), acb_realref(z)); } arb_zero(acb_imagref(z)); return; }
void arb_bernoulli_fmpz(arb_t res, const fmpz_t n, slong prec) { if (fmpz_cmp_ui(n, UWORD_MAX) <= 0) { if (fmpz_sgn(n) >= 0) arb_bernoulli_ui(res, fmpz_get_ui(n), prec); else arb_zero(res); } else if (fmpz_is_odd(n)) { arb_zero(res); } else { arb_t t; slong wp; arb_init(t); wp = prec + 2 * fmpz_bits(n); /* zeta(n) ~= 1 */ arf_one(arb_midref(res)); mag_one(arb_radref(res)); mag_mul_2exp_si(arb_radref(res), arb_radref(res), WORD_MIN); /* |B_n| = 2 * n! / (2*pi)^n * zeta(n) */ arb_gamma_fmpz(t, n, wp); arb_mul_fmpz(t, t, n, wp); arb_mul(res, res, t, wp); arb_const_pi(t, wp); arb_mul_2exp_si(t, t, 1); arb_pow_fmpz(t, t, n, wp); arb_div(res, res, t, prec); arb_mul_2exp_si(res, res, 1); if (fmpz_fdiv_ui(n, 4) == 0) arb_neg(res, res); arb_clear(t); } }
void _arb_pow_exp(arb_t z, const arb_t x, int negx, const arb_t y, long prec) { arb_t t; arb_init(t); if (negx) { arb_neg(t, x); arb_log(t, t, prec); } else arb_log(t, x, prec); arb_mul(t, t, y, prec); arb_exp(z, t, prec); arb_clear(t); }
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); }
/* 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 acb_dirichlet_vec_mellin_arb(acb_ptr res, const dirichlet_group_t G, const dirichlet_char_t chi, slong len, const arb_t t, slong n, slong prec) { slong k; arb_t tk, xt, stk, st; acb_ptr a; mag_t e; a = _acb_vec_init(len); acb_dirichlet_chi_vec(a, G, chi, len, prec); if (dirichlet_parity_char(G, chi)) { for (k = 2; k < len; k++) acb_mul_si(a + k, a + k, k, prec); } arb_init(tk); arb_init(xt); arb_init(st); arb_init(stk); mag_init(e); arb_sqrt(st, t, prec); arb_one(tk); arb_one(stk); for (k = 0; k < n; k++) { _acb_dirichlet_theta_argument_at_arb(xt, G->q, tk, prec); mag_tail_kexpk2_arb(e, xt, len); arb_neg(xt, xt); arb_exp(xt, xt, prec); /* TODO: reduce len */ acb_dirichlet_qseries_arb(res + k, a, xt, len, prec); acb_add_error_mag(res + k, e); acb_mul_arb(res + k, res + k, stk, prec); arb_mul(tk, tk, t, prec); arb_mul(stk, stk, st, prec); } mag_clear(e); arb_clear(xt); arb_clear(tk); arb_clear(stk); arb_clear(st); _acb_vec_clear(a, len); }
void _arb_poly_sin_cos_series(arb_ptr s, arb_ptr c, arb_srcptr h, slong hlen, slong n, slong prec) { hlen = FLINT_MIN(hlen, n); if (hlen == 1) { arb_sin_cos(s, c, h, prec); _arb_vec_zero(s + 1, n - 1); _arb_vec_zero(c + 1, n - 1); } else if (n == 2) { arb_t t; arb_init(t); arb_set(t, h + 1); arb_sin_cos(s, c, h, prec); arb_mul(s + 1, c, t, prec); arb_neg(t, t); arb_mul(c + 1, s, t, prec); arb_clear(t); } else { slong cutoff; if (prec <= 128) { cutoff = 1400; } else { cutoff = 100000 / pow(log(prec), 3); cutoff = FLINT_MIN(cutoff, 700); } if (hlen < cutoff) _arb_poly_sin_cos_series_basecase(s, c, h, hlen, n, prec, 0); else _arb_poly_sin_cos_series_tangent(s, c, h, hlen, n, prec, 0); } }
void _arb_poly_inv_series(arb_ptr Qinv, arb_srcptr Q, slong Qlen, slong len, slong prec) { arb_inv(Qinv, Q, prec); if (Qlen == 1) { _arb_vec_zero(Qinv + 1, len - 1); } else if (len == 2) { arb_div(Qinv + 1, Qinv, Q, prec); arb_mul(Qinv + 1, Qinv + 1, Q + 1, prec); arb_neg(Qinv + 1, Qinv + 1); } else { slong Qnlen, Wlen, W2len; arb_ptr W; W = _arb_vec_init(len); NEWTON_INIT(1, len) NEWTON_LOOP(m, n) Qnlen = FLINT_MIN(Qlen, n); Wlen = FLINT_MIN(Qnlen + m - 1, n); W2len = Wlen - m; MULLOW(W, Q, Qnlen, Qinv, m, Wlen, prec); MULLOW(Qinv + m, Qinv, m, W + m, W2len, n - m, prec); _arb_vec_neg(Qinv + m, Qinv + m, n - m); NEWTON_END_LOOP NEWTON_END _arb_vec_clear(W, len); } }
void arb_atanh(arb_t z, const arb_t x, slong prec) { if (arb_is_zero(x)) { arb_zero(z); } else { arb_t t; arb_init(t); arb_sub_ui(t, x, 1, prec + 4); arb_div(t, x, t, prec + 4); arb_mul_2exp_si(t, t, 1); arb_neg(t, t); arb_log1p(z, t, prec); arb_mul_2exp_si(z, z, -1); arb_clear(t); } }
void _arb_poly_div_series(arb_ptr Q, arb_srcptr A, long Alen, arb_srcptr B, long Blen, long n, long prec) { Alen = FLINT_MIN(Alen, n); Blen = FLINT_MIN(Blen, n); if (Blen == 1) { _arb_vec_scalar_div(Q, A, Alen, B, prec); _arb_vec_zero(Q + Alen, n - Alen); } else if (n == 2) { if (Alen == 1) { arb_div(Q, A, B, prec); arb_div(Q + 1, Q, B, prec); arb_mul(Q + 1, Q + 1, B + 1, prec); arb_neg(Q + 1, Q + 1); } else { arb_div(Q, A, B, prec); arb_mul(Q + 1, Q, B + 1, prec); arb_sub(Q + 1, A + 1, Q + 1, prec); arb_div(Q + 1, Q + 1, B, prec); } } else { arb_ptr Binv; Binv = _arb_vec_init(n); _arb_poly_inv_series(Binv, B, Blen, n, prec); _arb_poly_mullow(Q, Binv, n, A, Alen, n, prec); _arb_vec_clear(Binv, n); } }
static void _acb_print(const acb_t z, slong n) { arb_printn(acb_realref(z), n, ARB_STR_NO_RADIUS); if (!arb_is_zero(acb_imagref(z))) { if (arb_is_negative(acb_imagref(z))) { arb_t t; arb_init(t); arb_neg(t, acb_imagref(z)); printf(" - "); arb_printn(t, n, ARB_STR_NO_RADIUS); } else { printf(" + "); arb_printn(acb_imagref(z), n, ARB_STR_NO_RADIUS); } printf("i"); } }
/* 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); }
void acb_dirichlet_zeta_rs_mid(acb_t res, const acb_t s, slong K, slong prec) { acb_t R1, R2, X, t; slong wp; if (arf_sgn(arb_midref(acb_imagref(s))) < 0) { acb_init(t); acb_conj(t, s); acb_dirichlet_zeta_rs(res, t, K, prec); acb_conj(res, res); acb_clear(t); return; } acb_init(R1); acb_init(R2); acb_init(X); acb_init(t); /* rs_r increases the precision internally */ wp = prec; acb_dirichlet_zeta_rs_r(R1, s, K, wp); if (arb_is_exact(acb_realref(s)) && (arf_cmp_2exp_si(arb_midref(acb_realref(s)), -1) == 0)) { acb_conj(R2, R1); } else { /* conj(R(conj(1-s))) */ arb_sub_ui(acb_realref(t), acb_realref(s), 1, 10 * wp); arb_neg(acb_realref(t), acb_realref(t)); arb_set(acb_imagref(t), acb_imagref(s)); acb_dirichlet_zeta_rs_r(R2, t, K, wp); acb_conj(R2, R2); } if (acb_is_finite(R1) && acb_is_finite(R2)) { wp += 10 + arf_abs_bound_lt_2exp_si(arb_midref(acb_imagref(s))); wp = FLINT_MAX(wp, 10); /* X = pi^(s-1/2) gamma((1-s)/2) rgamma(s/2) = (2 pi)^s rgamma(s) / (2 cos(pi s / 2)) */ acb_rgamma(X, s, wp); acb_const_pi(t, wp); acb_mul_2exp_si(t, t, 1); acb_pow(t, t, s, wp); acb_mul(X, X, t, wp); acb_mul_2exp_si(t, s, -1); acb_cos_pi(t, t, wp); acb_mul_2exp_si(t, t, 1); acb_div(X, X, t, wp); acb_mul(R2, R2, X, wp); } /* R1 + X * R2 */ acb_add(res, R1, R2, prec); acb_clear(R1); acb_clear(R2); acb_clear(X); acb_clear(t); }
void acb_hypgeom_ci_asymp(acb_t res, const acb_t z, slong prec) { acb_t t, u, w, v, one; acb_init(t); acb_init(u); acb_init(w); acb_init(v); acb_init(one); acb_one(one); acb_mul_onei(w, z); /* u = U(1,1,iz) */ acb_hypgeom_u_asymp(u, one, one, w, -1, prec); /* v = e^(-iz) */ acb_neg(v, w); acb_exp(v, v, prec); acb_mul(t, u, v, prec); if (acb_is_real(z)) { arb_div(acb_realref(t), acb_imagref(t), acb_realref(z), prec); arb_zero(acb_imagref(t)); acb_neg(t, t); } else { /* u = U(1,1,-iz) */ acb_neg(w, w); acb_hypgeom_u_asymp(u, one, one, w, -1, prec); acb_inv(v, v, prec); acb_submul(t, u, v, prec); acb_div(t, t, w, prec); acb_mul_2exp_si(t, t, -1); } if (arb_is_zero(acb_realref(z))) { if (arb_is_positive(acb_imagref(z))) { arb_const_pi(acb_imagref(t), prec); arb_mul_2exp_si(acb_imagref(t), acb_imagref(t), -1); } else if (arb_is_negative(acb_imagref(z))) { arb_const_pi(acb_imagref(t), prec); arb_mul_2exp_si(acb_imagref(t), acb_imagref(t), -1); arb_neg(acb_imagref(t), acb_imagref(t)); } else { acb_const_pi(u, prec); acb_mul_2exp_si(u, u, -1); arb_zero(acb_imagref(t)); arb_add_error(acb_imagref(t), acb_realref(u)); } } else { /* 0 if positive or positive imaginary pi if upper left quadrant (including negative real axis) -pi if lower left quadrant (including negative imaginary axis) */ if (arb_is_positive(acb_realref(z))) { /* do nothing */ } else if (arb_is_negative(acb_realref(z)) && arb_is_nonnegative(acb_imagref(z))) { acb_const_pi(u, prec); arb_add(acb_imagref(t), acb_imagref(t), acb_realref(u), prec); } else if (arb_is_nonpositive(acb_realref(z)) && arb_is_negative(acb_imagref(z))) { acb_const_pi(u, prec); arb_sub(acb_imagref(t), acb_imagref(t), acb_realref(u), prec); } else { /* add [-pi,pi] */ acb_const_pi(u, prec); arb_add_error(acb_imagref(t), acb_realref(u)); } } acb_swap(res, t); acb_clear(t); acb_clear(u); acb_clear(w); acb_clear(v); acb_clear(one); }
void _arb_poly_rgamma_series(arb_ptr res, arb_srcptr h, long hlen, long len, long prec) { int reflect; long i, rflen, r, n, wp; arb_ptr t, u, v; arb_struct f[2]; hlen = FLINT_MIN(hlen, len); wp = prec + FLINT_BIT_COUNT(prec); t = _arb_vec_init(len); u = _arb_vec_init(len); v = _arb_vec_init(len); arb_init(f); arb_init(f + 1); /* use zeta values at small integers */ if (arb_is_int(h) && (arf_cmpabs_ui(arb_midref(h), prec / 2) < 0)) { r = arf_get_si(arb_midref(h), ARF_RND_DOWN); _arb_poly_lgamma_series_at_one(u, len, wp); _arb_vec_neg(u, u, len); _arb_poly_exp_series(t, u, len, len, wp); if (r == 1) { _arb_vec_swap(v, t, len); } else if (r <= 0) { arb_set(f, h); arb_one(f + 1); rflen = FLINT_MIN(len, 2 - r); _arb_poly_rising_ui_series(u, f, FLINT_MIN(2, len), 1 - r, rflen, wp); _arb_poly_mullow(v, t, len, u, rflen, len, wp); } else { arb_one(f); arb_one(f + 1); rflen = FLINT_MIN(len, r); _arb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r - 1, rflen, wp); /* TODO: use div_series? */ _arb_poly_inv_series(u, v, rflen, len, wp); _arb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* otherwise use Stirling series */ arb_gamma_stirling_choose_param(&reflect, &r, &n, h, 1, 0, wp); /* rgamma(h) = (gamma(1-h+r) sin(pi h)) / (rf(1-h, r) * pi), h = h0 + t*/ if (reflect) { /* u = gamma(r+1-h) */ arb_sub_ui(f, h, r + 1, wp); arb_neg(f, f); _arb_poly_gamma_stirling_eval(t, f, n, len, wp); _arb_poly_exp_series(u, t, len, len, wp); for (i = 1; i < len; i += 2) arb_neg(u + i, u + i); /* v = sin(pi x) */ arb_const_pi(f + 1, wp); arb_mul(f, h, f + 1, wp); _arb_poly_sin_series(v, f, 2, len, wp); _arb_poly_mullow(t, u, len, v, len, len, wp); /* rf(1-h,r) * pi */ if (r == 0) { arb_const_pi(u, wp); _arb_vec_scalar_div(v, t, len, u, wp); } else { arb_sub_ui(f, h, 1, wp); arb_neg(f, f); arb_set_si(f + 1, -1); rflen = FLINT_MIN(len, r + 1); _arb_poly_rising_ui_series(v, f, FLINT_MIN(2, len), r, rflen, wp); arb_const_pi(u, wp); _arb_vec_scalar_mul(v, v, rflen, u, wp); /* divide by rising factorial */ /* TODO: might better to use div_series, when it has a good basecase */ _arb_poly_inv_series(u, v, rflen, len, wp); _arb_poly_mullow(v, t, len, u, len, len, wp); } } else { /* rgamma(h) = rgamma(h+r) rf(h,r) */ if (r == 0) { arb_add_ui(f, h, r, wp); _arb_poly_gamma_stirling_eval(t, f, n, len, wp); _arb_vec_neg(t, t, len); _arb_poly_exp_series(v, t, len, len, wp); } else { arb_set(f, h); arb_one(f + 1); rflen = FLINT_MIN(len, r + 1); _arb_poly_rising_ui_series(t, f, FLINT_MIN(2, len), r, rflen, wp); arb_add_ui(f, h, r, wp); _arb_poly_gamma_stirling_eval(v, f, n, len, wp); _arb_vec_neg(v, v, len); _arb_poly_exp_series(u, v, len, len, wp); _arb_poly_mullow(v, u, len, t, rflen, len, wp); } } } /* compose with nonconstant part */ arb_zero(t); _arb_vec_set(t + 1, h + 1, hlen - 1); _arb_poly_compose_series(res, v, len, t, hlen, len, prec); arb_clear(f); arb_clear(f + 1); _arb_vec_clear(t, len); _arb_vec_clear(u, len); _arb_vec_clear(v, len); }
void gc_integrals_precomp(acb_ptr res, acb_srcptr u, slong d1, slong d, slong g, const gc_int_t gc, int flag, slong prec) { slong l; arb_t w, x; acb_t y, yxi; void (*sqrt_pol) (acb_t y, acb_srcptr u, slong d1, slong d, const arb_t x, slong prec); arb_init(w); arb_init(x); acb_init(y); acb_init(yxi); #if DEBUG flint_printf("\ngc integral : d1 = %ld, d = %ld, g = %ld, n = %ld, prec = %ld", d1, d, g, gc->n, prec); #endif sqrt_pol = &sqrt_pol_turn; if (flag & AJ_ROOT_DEF) sqrt_pol = &sqrt_pol_def; else if (flag & AJ_ROOT_TURN) sqrt_pol = &sqrt_pol_turn; /* compute integral */ _acb_vec_zero(res, g); for (l = 0; l < gc->len; l++) { /* compute 1/y(x) */ sqrt_pol(y, u, d1, d, gc->x + l, prec); acb_inv(y, y, prec); /* differentials */ acb_vec_add_geom_arb(res, g, y, gc->x + l, prec); /* now on -x */ arb_neg(x, gc->x + l); sqrt_pol(y, u, d1, d, x, prec); acb_inv(y, y, prec); acb_vec_add_geom_arb(res, g, y, x, prec); } if (gc->n % 2) { arb_zero(x); /* FIXME: pb with turn */ sqrt_pol_def(y, u, d1, d, x, prec); #if DEBUG > 1 flint_printf("\nend integration sum"); _acb_vec_printd(res, g, 30, "\n"); flint_printf("\nroots (d1=%ld, d=%ld)\n",d1,d); _acb_vec_printd(u, d, 30, "\n"); flint_printf("\n -> y = "); acb_printd(y, 30); #endif acb_inv(y, y, prec); acb_add(res + 0, res + 0, y, prec); } /* multiply by weight = Pi / n */ arb_const_pi(w, prec); arb_div_ui(w, w, gc->n, prec); _acb_vec_scalar_mul_arb(res, res, g, w, prec); #if DEBUG > 1 flint_printf("\nend integration "); _acb_vec_printd(res, g, 30, "\n"); #endif arb_clear(x); arb_clear(w); acb_clear(y); acb_clear(yxi); }
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 _arb_poly_inv_series(arb_ptr Qinv, arb_srcptr Q, slong Qlen, slong len, slong prec) { Qlen = FLINT_MIN(Qlen, len); arb_inv(Qinv, Q, prec); if (Qlen == 1) { _arb_vec_zero(Qinv + 1, len - 1); } else if (len == 2) { arb_mul(Qinv + 1, Qinv, Qinv, prec); arb_mul(Qinv + 1, Qinv + 1, Q + 1, prec); arb_neg(Qinv + 1, Qinv + 1); } else { slong i, j, blen; /* The basecase algorithm is faster for much larger Qlen or len than this, but unfortunately also much less numerically stable. */ if (Qlen == 2 || len <= 8) blen = len; else blen = FLINT_MIN(len, 4); for (i = 1; i < blen; i++) { arb_mul(Qinv + i, Q + 1, Qinv + i - 1, prec); for (j = 2; j < FLINT_MIN(i + 1, Qlen); j++) arb_addmul(Qinv + i, Q + j, Qinv + i - j, prec); if (!arb_is_one(Qinv)) arb_mul(Qinv + i, Qinv + i, Qinv, prec); arb_neg(Qinv + i, Qinv + i); } if (len > blen) { slong Qnlen, Wlen, W2len; arb_ptr W; W = _arb_vec_init(len); NEWTON_INIT(blen, len) NEWTON_LOOP(m, n) Qnlen = FLINT_MIN(Qlen, n); Wlen = FLINT_MIN(Qnlen + m - 1, n); W2len = Wlen - m; MULLOW(W, Q, Qnlen, Qinv, m, Wlen, prec); MULLOW(Qinv + m, Qinv, m, W + m, W2len, n - m, prec); _arb_vec_neg(Qinv + m, Qinv + m, n - m); NEWTON_END_LOOP NEWTON_END _arb_vec_clear(W, len); } } }