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); }
void arb_tan(arb_t y, const arb_t x, long prec) { arb_t u; arb_init(u); arb_sin_cos(y, u, x, prec + 4); arb_div(y, y, u, prec); arb_clear(u); }
int main() { slong iter; flint_rand_t state; flint_printf("sin_cos_pi...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d, e; slong prec = 2 + n_randint(state, 200); arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_init(e); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_randtest(c, state, 1 + n_randint(state, 200), 10); arb_randtest(d, state, 1 + n_randint(state, 200), 10); arb_randtest(e, state, 1 + n_randint(state, 200), 10); arb_const_pi(b, prec); arb_mul(b, b, a, prec); arb_sin_cos(b, d, b, prec); arb_sin_cos_pi(c, e, a, prec); if (!arb_overlaps(b, c) || !arb_overlaps(d, e)) { flint_printf("FAIL: overlap\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("d = "); arb_print(d); flint_printf("\n\n"); flint_printf("e = "); arb_print(e); flint_printf("\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); arb_clear(e); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
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_sin_cos_pi_fmpq_oct(arb_t s, arb_t c, const fmpz_t v, const fmpz_t w, slong prec) { if (use_algebraic(v, w, prec)) { _arb_sin_cos_pi_fmpq_algebraic(s, c, *v, *w, prec); } else { arb_const_pi(s, prec); arb_mul_fmpz(s, s, v, prec); arb_div_fmpz(s, s, w, prec); arb_sin_cos(s, c, s, prec); } }
void acb_calc_cauchy_bound(arb_t bound, acb_calc_func_t func, void * param, const acb_t x, const arb_t radius, slong maxdepth, slong prec) { slong i, n, depth, wp; arb_t pi, theta, v, s1, c1, s2, c2, st, ct; acb_t t, u; arb_t b; arb_init(pi); arb_init(theta); arb_init(v); arb_init(s1); arb_init(c1); arb_init(s2); arb_init(c2); arb_init(st); arb_init(ct); acb_init(t); acb_init(u); arb_init(b); wp = prec + 20; arb_const_pi(pi, wp); arb_zero_pm_inf(b); for (depth = 0, n = 16; depth < maxdepth; n *= 2, depth++) { arb_zero(b); /* theta = 2 pi / n */ arb_div_ui(theta, pi, n, wp); arb_mul_2exp_si(theta, theta, 1); /* sine and cosine of i*theta and (i+1)*theta */ arb_zero(s1); arb_one(c1); arb_sin_cos(st, ct, theta, wp); arb_set(s2, st); arb_set(c2, ct); for (i = 0; i < n; i++) { /* sine and cosine of 2 pi ([i,i+1]/n) */ /* since we use power of two subdivision points, the sine and cosine are monotone on each subinterval */ arb_union(acb_realref(t), c1, c2, wp); arb_union(acb_imagref(t), s1, s2, wp); acb_mul_arb(t, t, radius, wp); acb_add(t, t, x, prec); /* next angle */ arb_mul(v, c2, ct, wp); arb_mul(c1, s2, st, wp); arb_sub(c1, v, c1, wp); arb_mul(v, c2, st, wp); arb_mul(s1, s2, ct, wp); arb_add(s1, v, s1, wp); arb_swap(c1, c2); arb_swap(s1, s2); func(u, t, param, 1, prec); acb_abs(v, u, prec); arb_add(b, b, v, prec); } arb_div_ui(b, b, n, prec); if (arb_is_positive(b)) break; } arb_set(bound, b); arb_clear(pi); arb_clear(theta); arb_clear(v); acb_clear(t); acb_clear(u); arb_clear(b); arb_clear(s1); arb_clear(c1); arb_clear(s2); arb_clear(c2); arb_clear(st); arb_clear(ct); }
int main() { slong iter; flint_rand_t state; flint_printf("sin_cos_pi_fmpq_algebraic...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000; iter++) { arb_t s1, s2, c1, c2; ulong p, q, g; slong prec; prec = 2 + n_randint(state, 5000); q = 1 + n_randint(state, 500); p = n_randint(state, q / 2 + 1); g = n_gcd(q, p); q /= g; p /= g; arb_init(s1); arb_init(s2); arb_init(c1); arb_init(c2); _arb_sin_cos_pi_fmpq_algebraic(s1, c1, p, q, prec); arb_const_pi(s2, prec); arb_mul_ui(s2, s2, p, prec); arb_div_ui(s2, s2, q, prec); arb_sin_cos(s2, c2, s2, prec); if (!arb_overlaps(s1, s2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("p/q = %wu/%wu", p, q); flint_printf("\n\n"); flint_printf("s1 = "); arb_printd(s1, 15); flint_printf("\n\n"); flint_printf("s2 = "); arb_printd(s2, 15); flint_printf("\n\n"); abort(); } if (!arb_overlaps(c1, c2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("p/q = %wu/%wu", p, q); flint_printf("\n\n"); flint_printf("c1 = "); arb_printd(c1, 15); flint_printf("\n\n"); flint_printf("c2 = "); arb_printd(c2, 15); flint_printf("\n\n"); abort(); } if (arb_rel_accuracy_bits(s1) < prec - 2) { flint_printf("FAIL: accuracy\n\n"); flint_printf("p/q = %wu/%wu", p, q); flint_printf("\n\n"); flint_printf("prec=%wd eff=%wd\n", prec, arb_rel_accuracy_bits(s1)); flint_printf("s1 = "); arb_printd(s1, 15); flint_printf("\n\n"); flint_printf("s2 = "); arb_printd(s2, 15); flint_printf("\n\n"); abort(); } if (arb_rel_accuracy_bits(c1) < prec - 2) { flint_printf("FAIL: accuracy\n\n"); flint_printf("p/q = %wu/%wu", p, q); flint_printf("\n\n"); flint_printf("prec=%wd eff=%wd\n", prec, arb_rel_accuracy_bits(c1)); flint_printf("c1 = "); arb_printd(c1, 15); flint_printf("\n\n"); flint_printf("c2 = "); arb_printd(c2, 15); flint_printf("\n\n"); abort(); } arb_clear(s1); arb_clear(s2); arb_clear(c1); arb_clear(c2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void _arb_poly_sin_cos_series_tangent(arb_ptr s, arb_ptr c, arb_srcptr h, slong hlen, slong len, slong prec, int times_pi) { arb_ptr t, u, v; arb_t s0, c0; hlen = FLINT_MIN(hlen, len); if (hlen == 1) { if (times_pi) arb_sin_cos_pi(s, c, h, prec); else arb_sin_cos(s, c, h, prec); _arb_vec_zero(s + 1, len - 1); _arb_vec_zero(c + 1, len - 1); return; } /* sin(x) = 2*tan(x/2)/(1+tan(x/2)^2) cos(x) = (1-tan(x/2)^2)/(1+tan(x/2)^2) */ arb_init(s0); arb_init(c0); t = _arb_vec_init(3 * len); u = t + len; v = u + len; /* sin, cos of h0 */ if (times_pi) arb_sin_cos_pi(s0, c0, h, prec); else arb_sin_cos(s0, c0, h, prec); /* t = tan((h-h0)/2) */ arb_zero(u); _arb_vec_scalar_mul_2exp_si(u + 1, h + 1, hlen - 1, -1); if (times_pi) { arb_const_pi(t, prec); _arb_vec_scalar_mul(u + 1, u + 1, hlen - 1, t, prec); } _arb_poly_tan_series(t, u, hlen, len, prec); /* v = 1 + t^2 */ _arb_poly_mullow(v, t, len, t, len, len, prec); arb_add_ui(v, v, 1, prec); /* u = 1/(1+t^2) */ _arb_poly_inv_series(u, v, len, len, prec); /* sine */ _arb_poly_mullow(s, t, len, u, len, len, prec); _arb_vec_scalar_mul_2exp_si(s, s, len, 1); /* cosine */ arb_sub_ui(v, v, 2, prec); _arb_vec_neg(v, v, len); _arb_poly_mullow(c, v, len, u, len, len, prec); /* sin(h0 + h1) = cos(h0) sin(h1) + sin(h0) cos(h1) cos(h0 + h1) = cos(h0) cos(h1) - sin(h0) sin(h1) */ if (!arb_is_zero(s0)) { _arb_vec_scalar_mul(t, s, len, c0, prec); _arb_vec_scalar_mul(u, c, len, s0, prec); _arb_vec_scalar_mul(v, s, len, s0, prec); _arb_vec_add(s, t, u, len, prec); _arb_vec_scalar_mul(t, c, len, c0, prec); _arb_vec_sub(c, t, v, len, prec); } _arb_vec_clear(t, 3 * len); arb_clear(s0); arb_clear(c0); }
int main() { slong iter; flint_rand_t state; flint_printf("atan...."); fflush(stdout); flint_randinit(state); /* Compare with MPFR */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b; fmpq_t q; mpfr_t t; slong prec = 2 + n_randint(state, 200); arb_init(a); arb_init(b); fmpq_init(q); mpfr_init2(t, prec + 100); arb_randtest(a, state, 1 + n_randint(state, 200), 3); arb_randtest(b, state, 1 + n_randint(state, 200), 3); arb_get_rand_fmpq(q, state, a, 1 + n_randint(state, 200)); fmpq_get_mpfr(t, q, MPFR_RNDN); mpfr_atan(t, t, MPFR_RNDN); arb_atan(b, a, prec); if (!arb_contains_mpfr(b, t)) { flint_printf("FAIL: containment\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_abort(); } arb_atan(a, a, prec); if (!arb_equal(a, b)) { flint_printf("FAIL: aliasing\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); fmpq_clear(q); mpfr_clear(t); } /* Check large arguments. */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d; slong prec1, prec2; prec1 = 2 + n_randint(state, 1000); prec2 = prec1 + 30; arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_precise(a, state, 1 + n_randint(state, 1000), 100); arb_atan(b, a, prec1); arb_atan(c, a, prec2); if (!arb_overlaps(b, c)) { flint_printf("FAIL: overlap\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_abort(); } /* check tan(atan(x)) = x */ arb_sin_cos(c, d, b, prec1); arb_div(c, c, d, prec1); if (!arb_contains(c, a)) { flint_printf("FAIL: functional equation\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("d = "); arb_print(d); flint_printf("\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } /* Compare with MPFR, higher precision. */ for (iter = 0; iter < 200 * arb_test_multiplier(); iter++) { arb_t a, b; fmpq_t q; mpfr_t t; slong prec = 2 + n_randint(state, 5000); arb_init(a); arb_init(b); fmpq_init(q); mpfr_init2(t, prec + 100); arb_randtest(a, state, 1 + n_randint(state, 5000), 8); arb_randtest(b, state, 1 + n_randint(state, 5000), 8); arb_get_rand_fmpq(q, state, a, 1 + n_randint(state, 200)); fmpq_get_mpfr(t, q, MPFR_RNDN); mpfr_atan(t, t, MPFR_RNDN); arb_atan(b, a, prec); if (!arb_contains_mpfr(b, t)) { flint_printf("FAIL: containment\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("a = "); arb_printd(a, 50); flint_printf("\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_printd(b, 50); flint_printf("\n\n"); flint_abort(); } arb_atan(a, a, prec); if (!arb_equal(a, b)) { flint_printf("FAIL: aliasing\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); fmpq_clear(q); mpfr_clear(t); } /* Higher precision + large arguments. */ for (iter = 0; iter < 2000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d; slong prec1, prec2; prec1 = 2 + n_randint(state, 5000); prec2 = prec1 + 30; arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_precise(a, state, 1 + n_randint(state, 5000), 100); arb_atan(b, a, prec1); arb_atan(c, a, prec2); if (!arb_overlaps(b, c)) { flint_printf("FAIL: overlap\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_abort(); } /* check tan(atan(x)) = x */ arb_sin_cos(c, d, b, prec1); arb_div(c, c, d, prec1); if (!arb_contains(c, a)) { flint_printf("FAIL: functional equation\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("d = "); arb_print(d); flint_printf("\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } /* Check wide arguments. */ for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d; arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_precise(a, state, 1 + n_randint(state, 1000), 100); arb_randtest_precise(b, state, 1 + n_randint(state, 1000), 100); if (n_randint(state, 2)) arb_add(a, a, b, 2 + n_randint(state, 1000)); arb_union(d, a, b, 2 + n_randint(state, 1000)); arb_atan(a, a, 2 + n_randint(state, 2000)); arb_atan(b, b, 2 + n_randint(state, 2000)); arb_atan(c, d, 2 + n_randint(state, 2000)); if (!arb_overlaps(c, a) || !arb_overlaps(c, b)) { flint_printf("FAIL: overlap\n\n"); flint_printf("d = "); arb_print(d); flint_printf("\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }