void arf_fprintd(FILE * file, const arf_t x, slong d) { if (arf_is_finite(x) && (ARF_EXP(x) <= MPFR_EMIN_MIN + 1 || ARF_EXP(x) >= MPFR_EMAX_MAX - 1)) { arf_fprint(file, x); } else { mpfr_t t; mpfr_init2(t, d * 3.33 + 10); mpfr_set_emin(MPFR_EMIN_MIN); mpfr_set_emax(MPFR_EMAX_MAX); arf_get_mpfr(t, x, MPFR_RNDN); mpfr_fprintf(file, "%.*Rg", FLINT_MAX(d, 1), t); mpfr_clear(t); } }
int arf_submul_mpz(arf_ptr z, arf_srcptr x, const mpz_t y, slong prec, arf_rnd_t rnd) { mp_size_t xn, yn, zn, tn, alloc; mp_srcptr xptr, yptr, zptr; mp_ptr tptr, tptr2; fmpz_t texp, yexp; slong shift; int tsgnbit, ysgnbit, inexact; ARF_MUL_TMP_DECL yn = FLINT_ABS(y->_mp_size); if (arf_is_special(x) || yn == 0 || arf_is_special(z)) { if (arf_is_zero(z)) { /* TODO: make more efficient */ arf_mul_mpz(z, x, y, ARF_PREC_EXACT, rnd); return arf_neg_round(z, z, prec, rnd); } else if (arf_is_finite(x)) { return arf_set_round(z, z, prec, rnd); } else { /* todo: speed up */ arf_t t; arf_init(t); arf_mul_mpz(t, x, y, ARF_PREC_EXACT, ARF_RND_DOWN); inexact = arf_sub(z, z, t, prec, rnd); arf_clear(t); return inexact; } } ARF_GET_MPN_READONLY(xptr, xn, x); yptr = y->_mp_d; ysgnbit = (y->_mp_size > 0); *yexp = yn * FLINT_BITS; ARF_GET_MPN_READONLY(zptr, zn, z); fmpz_init(texp); tsgnbit = ARF_SGNBIT(x) ^ ysgnbit; alloc = tn = xn + yn; ARF_MUL_TMP_ALLOC(tptr2, alloc) tptr = tptr2; ARF_MPN_MUL(tptr, xptr, xn, yptr, yn); shift = (tptr[tn - 1] == 0) * FLINT_BITS; tn -= (tptr[tn - 1] == 0); _fmpz_add2_fast(texp, ARF_EXPREF(x), yexp, -shift); shift = _fmpz_sub_small(ARF_EXPREF(z), texp); if (shift >= 0) inexact = _arf_add_mpn(z, zptr, zn, ARF_SGNBIT(z), ARF_EXPREF(z), tptr, tn, tsgnbit, shift, prec, rnd); else inexact = _arf_add_mpn(z, tptr, tn, tsgnbit, texp, zptr, zn, ARF_SGNBIT(z), -shift, prec, rnd); ARF_MUL_TMP_FREE(tptr2, alloc) fmpz_clear(texp); return inexact; }
int arf_submul(arf_ptr z, arf_srcptr x, arf_srcptr y, slong prec, arf_rnd_t rnd) { mp_size_t xn, yn, zn, tn, alloc; mp_srcptr xptr, yptr, zptr; mp_ptr tptr, tptr2; fmpz_t texp; slong shift; int tsgnbit, inexact; ARF_MUL_TMP_DECL if (arf_is_special(x) || arf_is_special(y) || arf_is_special(z)) { if (arf_is_zero(z)) { return arf_neg_mul(z, x, y, prec, rnd); } else if (arf_is_finite(x) && arf_is_finite(y)) { return arf_set_round(z, z, prec, rnd); } else { /* todo: speed up */ arf_t t; arf_init(t); arf_mul(t, x, y, ARF_PREC_EXACT, ARF_RND_DOWN); inexact = arf_sub(z, z, t, prec, rnd); arf_clear(t); return inexact; } } tsgnbit = ARF_SGNBIT(x) ^ ARF_SGNBIT(y) ^ 1; ARF_GET_MPN_READONLY(xptr, xn, x); ARF_GET_MPN_READONLY(yptr, yn, y); ARF_GET_MPN_READONLY(zptr, zn, z); fmpz_init(texp); _fmpz_add2_fast(texp, ARF_EXPREF(x), ARF_EXPREF(y), 0); shift = _fmpz_sub_small(ARF_EXPREF(z), texp); alloc = tn = xn + yn; ARF_MUL_TMP_ALLOC(tptr2, alloc) tptr = tptr2; ARF_MPN_MUL(tptr, xptr, xn, yptr, yn); tn -= (tptr[0] == 0); tptr += (tptr[0] == 0); if (shift >= 0) inexact = _arf_add_mpn(z, zptr, zn, ARF_SGNBIT(z), ARF_EXPREF(z), tptr, tn, tsgnbit, shift, prec, rnd); else inexact = _arf_add_mpn(z, tptr, tn, tsgnbit, texp, zptr, zn, ARF_SGNBIT(z), -shift, prec, rnd); ARF_MUL_TMP_FREE(tptr2, alloc) fmpz_clear(texp); return inexact; }
void _arb_sin_cos_generic(arb_t s, arb_t c, const arf_t x, const mag_t xrad, slong prec) { int want_sin, want_cos; slong maglim; want_sin = (s != NULL); want_cos = (c != NULL); if (arf_is_zero(x) && mag_is_zero(xrad)) { if (want_sin) arb_zero(s); if (want_cos) arb_one(c); return; } if (!arf_is_finite(x) || !mag_is_finite(xrad)) { if (arf_is_nan(x)) { if (want_sin) arb_indeterminate(s); if (want_cos) arb_indeterminate(c); } else { if (want_sin) arb_zero_pm_one(s); if (want_cos) arb_zero_pm_one(c); } return; } maglim = FLINT_MAX(65536, 4 * prec); if (mag_cmp_2exp_si(xrad, -16) > 0 || arf_cmpabs_2exp_si(x, maglim) > 0) { _arb_sin_cos_wide(s, c, x, xrad, prec); return; } if (arf_cmpabs_2exp_si(x, -(prec/2) - 2) <= 0) { mag_t t, u, v; mag_init(t); mag_init(u); mag_init(v); arf_get_mag(t, x); mag_add(t, t, xrad); mag_mul(u, t, t); /* |sin(z)-z| <= z^3/6 */ if (want_sin) { arf_set(arb_midref(s), x); mag_set(arb_radref(s), xrad); arb_set_round(s, s, prec); mag_mul(v, u, t); mag_div_ui(v, v, 6); arb_add_error_mag(s, v); } /* |cos(z)-1| <= z^2/2 */ if (want_cos) { arf_one(arb_midref(c)); mag_mul_2exp_si(arb_radref(c), u, -1); } mag_clear(t); mag_clear(u); mag_clear(v); return; } if (mag_is_zero(xrad)) { arb_sin_cos_arf_generic(s, c, x, prec); } else { mag_t t; slong exp, radexp; mag_init_set(t, xrad); exp = arf_abs_bound_lt_2exp_si(x); radexp = MAG_EXP(xrad); if (radexp < MAG_MIN_LAGOM_EXP || radexp > MAG_MAX_LAGOM_EXP) radexp = MAG_MIN_LAGOM_EXP; if (want_cos && exp < -2) prec = FLINT_MIN(prec, 20 - FLINT_MAX(exp, radexp) - radexp); else prec = FLINT_MIN(prec, 20 - radexp); arb_sin_cos_arf_generic(s, c, x, prec); /* todo: could use quadratic bound */ if (want_sin) mag_add(arb_radref(s), arb_radref(s), t); if (want_cos) mag_add(arb_radref(c), arb_radref(c), t); mag_clear(t); } }
int main() { slong iter; flint_rand_t state; flint_printf("is_int_2exp_si...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arf_t x, y; fmpz_t t; slong e; int res1, res2; arf_init(x); arf_init(y); fmpz_init(t); arf_randtest_special(x, state, 2000, 100); e = n_randtest(state); arf_mul_2exp_si(y, x, e); res1 = arf_is_int(x); res2 = arf_is_int_2exp_si(y, e); if (res1 != res2) { flint_printf("FAIL! (1)\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y = "); arf_print(y); flint_printf("\n\n"); flint_printf("res1 = %d, res2 = %d\n\n", res1, res2); abort(); } if (res1) { if (n_randint(state, 2)) arf_floor(y, x); else arf_ceil(y, x); if (!arf_equal(x, y) || !arf_is_finite(x)) { flint_printf("FAIL! (2)\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y = "); arf_print(y); flint_printf("\n\n"); flint_printf("res1 = %d\n\n", res1); abort(); } } if (arf_is_finite(x) && !arf_is_zero(x)) { arf_bot(t, x); fmpz_neg(t, t); arf_mul_2exp_fmpz(x, x, t); res1 = arf_is_int(x); arf_mul_2exp_si(y, x, -1); res2 = arf_is_int(y); if (!arf_is_int(x) || arf_is_int(y)) { flint_printf("FAIL! (3)\n"); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y = "); arf_print(y); flint_printf("\n\n"); flint_printf("res1 = %d, res2 = %d\n\n", res1, res2); abort(); } } arf_clear(x); arf_clear(y); fmpz_clear(t); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
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; }
int acb_calc_integrate_taylor(acb_t res, acb_calc_func_t func, void * param, const acb_t a, const acb_t b, const arf_t inner_radius, const arf_t outer_radius, long accuracy_goal, long prec) { long num_steps, step, N, bp; int result; acb_t delta, m, x, y1, y2, sum; acb_ptr taylor_poly; arf_t err; acb_init(delta); acb_init(m); acb_init(x); acb_init(y1); acb_init(y2); acb_init(sum); arf_init(err); acb_sub(delta, b, a, prec); /* precision used for bounds calculations */ bp = MAG_BITS; /* compute the number of steps */ { arf_t t; arf_init(t); acb_get_abs_ubound_arf(t, delta, bp); arf_div(t, t, inner_radius, bp, ARF_RND_UP); arf_mul_2exp_si(t, t, -1); num_steps = (long) (arf_get_d(t, ARF_RND_UP) + 1.0); /* make sure it's not something absurd */ num_steps = FLINT_MIN(num_steps, 10 * prec); num_steps = FLINT_MAX(num_steps, 1); arf_clear(t); } result = ARB_CALC_SUCCESS; acb_zero(sum); for (step = 0; step < num_steps; step++) { /* midpoint of subinterval */ acb_mul_ui(m, delta, 2 * step + 1, prec); acb_div_ui(m, m, 2 * num_steps, prec); acb_add(m, m, a, prec); if (arb_calc_verbose) { printf("integration point %ld/%ld: ", 2 * step + 1, 2 * num_steps); acb_printd(m, 15); printf("\n"); } /* evaluate at +/- x */ /* TODO: exactify m, and include error in x? */ acb_div_ui(x, delta, 2 * num_steps, prec); /* compute bounds and number of terms to use */ { arb_t cbound, xbound, rbound; arf_t C, D, R, X, T; double DD, TT, NN; arb_init(cbound); arb_init(xbound); arb_init(rbound); arf_init(C); arf_init(D); arf_init(R); arf_init(X); arf_init(T); /* R is the outer radius */ arf_set(R, outer_radius); /* X = upper bound for |x| */ acb_get_abs_ubound_arf(X, x, bp); arb_set_arf(xbound, X); /* Compute C(m,R). Important subtlety: due to rounding when computing m, we will in general be farther than R away from the integration path. But since acb_calc_cauchy_bound actually integrates over the area traced by a complex interval, it will catch any extra singularities (giving an infinite bound). */ arb_set_arf(rbound, outer_radius); acb_calc_cauchy_bound(cbound, func, param, m, rbound, 8, bp); arf_set_mag(C, arb_radref(cbound)); arf_add(C, arb_midref(cbound), C, bp, ARF_RND_UP); /* Sanity check: we need C < inf and R > X */ if (arf_is_finite(C) && arf_cmp(R, X) > 0) { /* Compute upper bound for D = C * R * X / (R - X) */ arf_mul(D, C, R, bp, ARF_RND_UP); arf_mul(D, D, X, bp, ARF_RND_UP); arf_sub(T, R, X, bp, ARF_RND_DOWN); arf_div(D, D, T, bp, ARF_RND_UP); /* Compute upper bound for T = (X / R) */ arf_div(T, X, R, bp, ARF_RND_UP); /* Choose N */ /* TODO: use arf arithmetic to avoid overflow */ /* TODO: use relative accuracy (look at |f(m)|?) */ DD = arf_get_d(D, ARF_RND_UP); TT = arf_get_d(T, ARF_RND_UP); NN = -(accuracy_goal * 0.69314718055994530942 + log(DD)) / log(TT); N = NN + 0.5; N = FLINT_MIN(N, 100 * prec); N = FLINT_MAX(N, 1); /* Tail bound: D / (N + 1) * T^N */ { mag_t TT; mag_init(TT); arf_get_mag(TT, T); mag_pow_ui(TT, TT, N); arf_set_mag(T, TT); mag_clear(TT); } arf_mul(D, D, T, bp, ARF_RND_UP); arf_div_ui(err, D, N + 1, bp, ARF_RND_UP); } else { N = 1; arf_pos_inf(err); result = ARB_CALC_NO_CONVERGENCE; } if (arb_calc_verbose) { printf("N = %ld; bound: ", N); arf_printd(err, 15); printf("\n"); printf("R: "); arf_printd(R, 15); printf("\n"); printf("C: "); arf_printd(C, 15); printf("\n"); printf("X: "); arf_printd(X, 15); printf("\n"); } arb_clear(cbound); arb_clear(xbound); arb_clear(rbound); arf_clear(C); arf_clear(D); arf_clear(R); arf_clear(X); arf_clear(T); } /* evaluate Taylor polynomial */ taylor_poly = _acb_vec_init(N + 1); func(taylor_poly, m, param, N, prec); _acb_poly_integral(taylor_poly, taylor_poly, N + 1, prec); _acb_poly_evaluate(y2, taylor_poly, N + 1, x, prec); acb_neg(x, x); _acb_poly_evaluate(y1, taylor_poly, N + 1, x, prec); acb_neg(x, x); /* add truncation error */ arb_add_error_arf(acb_realref(y1), err); arb_add_error_arf(acb_imagref(y1), err); arb_add_error_arf(acb_realref(y2), err); arb_add_error_arf(acb_imagref(y2), err); acb_add(sum, sum, y2, prec); acb_sub(sum, sum, y1, prec); if (arb_calc_verbose) { printf("values: "); acb_printd(y1, 15); printf(" "); acb_printd(y2, 15); printf("\n"); } _acb_vec_clear(taylor_poly, N + 1); if (result == ARB_CALC_NO_CONVERGENCE) break; } acb_set(res, sum); acb_clear(delta); acb_clear(m); acb_clear(x); acb_clear(y1); acb_clear(y2); acb_clear(sum); arf_clear(err); return result; }