slong fmpr_rsqrt(fmpr_t y, const fmpr_t x, slong prec, fmpr_rnd_t rnd) { slong r; if (fmpr_is_special(x)) { if (fmpr_is_zero(x)) fmpr_pos_inf(y); else if (fmpr_is_pos_inf(x)) fmpr_zero(y); else fmpr_nan(y); return FMPR_RESULT_EXACT; } if (fmpr_sgn(x) < 0) { fmpr_nan(y); return FMPR_RESULT_EXACT; } /* special case: 4^n */ if (fmpz_is_one(fmpr_manref(x)) && fmpz_is_even(fmpr_expref(x))) { r = fmpr_set_round(y, x, prec, rnd); fmpz_tdiv_q_2exp(fmpr_expref(y), fmpr_expref(y), 1); fmpz_neg(fmpr_expref(y), fmpr_expref(y)); return r; } { fmpr_t t; fmpz_t e; fmpr_init(t); fmpz_init(e); fmpz_neg(e, fmpr_expref(x)); if (fmpz_is_odd(e)) fmpz_add_ui(e, e, 1); fmpr_mul_2exp_fmpz(t, x, e); CALL_MPFR_FUNC(r, mpfr_rec_sqrt, y, t, prec, rnd); fmpz_tdiv_q_2exp(e, e, 1); fmpr_mul_2exp_fmpz(y, y, e); fmpr_clear(t); fmpz_clear(e); return r; } }
int main() { slong iter; flint_rand_t state; flint_printf("mul_2exp_fmpz...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { fmpr_t x, y, z; mag_t xb, yb; fmpz_t e; fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpz_init(e); mag_init(xb); mag_init(yb); mag_randtest_special(xb, state, 100); fmpz_randtest(e, state, 100); mag_get_fmpr(x, xb); mag_mul_2exp_fmpz(yb, xb, e); fmpr_mul_2exp_fmpz(y, x, e); mag_get_fmpr(z, yb); MAG_CHECK_BITS(yb) if (!fmpr_equal(z, y)) { flint_printf("FAIL\n\n"); flint_printf("x = "); fmpr_printd(x, 15); flint_printf("\n\n"); flint_printf("y = "); fmpr_printd(y, 15); flint_printf("\n\n"); flint_printf("z = "); fmpr_printd(z, 15); flint_printf("\n\n"); abort(); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); mag_clear(xb); mag_clear(yb); fmpz_clear(e); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void _fmprb_poly_mullow_ztrunc(fmprb_ptr C, fmprb_srcptr A, long lenA, fmprb_srcptr B, long lenB, long n, long prec) { fmpz * Acoeffs, * Bcoeffs, * Ccoeffs; fmpz_t Aexp, Bexp, Cexp; fmpr_t Aerr, Berr, Anorm, Bnorm, err; long i; int squaring; lenA = FLINT_MIN(lenA, n); lenB = FLINT_MIN(lenB, n); squaring = (A == B) && (lenA == lenB); /* TODO: make the code below work correctly with out this workaround */ if (_fmprb_vec_rad_has_inf_nan(A, lenA) || (!squaring && _fmprb_vec_rad_has_inf_nan(B, lenB))) { _fmprb_vec_indeterminate(C, n); return; } fmpz_init(Aexp); fmpz_init(Bexp); fmpz_init(Cexp); Acoeffs = _fmpz_vec_init(lenA); Bcoeffs = _fmpz_vec_init(lenB); Ccoeffs = _fmpz_vec_init(n); fmpr_init(Aerr); fmpr_init(Berr); fmpr_init(Anorm); fmpr_init(Bnorm); fmpr_init(err); _fmprb_poly_get_fmpz_poly_2exp(Aerr, Aexp, Acoeffs, A, lenA, prec); if (squaring) { _fmpz_poly_sqrlow(Ccoeffs, Acoeffs, lenA, n); fmpz_add(Cexp, Aexp, Aexp); /* cross-multiply error bounds: (A+r)(B+s) = A^2 + 2Ar + r^2 */ _fmpr_fmpz_vec_max_norm(Anorm, Acoeffs, lenA, FMPRB_RAD_PREC); fmpr_mul_2exp_fmpz(Anorm, Anorm, Aexp); fmpr_mul(err, Anorm, Aerr, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_mul_2exp_si(err, err, 1); fmpr_addmul(err, Aerr, Aerr, FMPRB_RAD_PREC, FMPR_RND_UP); } else { _fmprb_poly_get_fmpz_poly_2exp(Berr, Bexp, Bcoeffs, B, lenB, prec); /* main multiplication */ if (lenA >= lenB) _fmpz_poly_mullow(Ccoeffs, Acoeffs, lenA, Bcoeffs, lenB, n); else _fmpz_poly_mullow(Ccoeffs, Bcoeffs, lenB, Acoeffs, lenA, n); fmpz_add(Cexp, Aexp, Bexp); /* cross-multiply error bounds: (A+r)(B+s) = AB + As + Br + rs */ _fmpr_fmpz_vec_max_norm(Anorm, Acoeffs, lenA, FMPRB_RAD_PREC); fmpr_mul_2exp_fmpz(Anorm, Anorm, Aexp); _fmpr_fmpz_vec_max_norm(Bnorm, Bcoeffs, lenB, FMPRB_RAD_PREC); fmpr_mul_2exp_fmpz(Bnorm, Bnorm, Bexp); fmpr_mul(err, Aerr, Berr, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_addmul(err, Anorm, Berr, FMPRB_RAD_PREC, FMPR_RND_UP); fmpr_addmul(err, Bnorm, Aerr, FMPRB_RAD_PREC, FMPR_RND_UP); } for (i = 0; i < n; i++) { fmprb_set_round_fmpz_2exp(C + i, Ccoeffs + i, Cexp, prec); /* there are at most (i+1) error terms for coefficient i */ /* TODO: make this tight */ fmpr_addmul_ui(fmprb_radref(C + i), err, i + 1, FMPRB_RAD_PREC, FMPR_RND_UP); } fmpr_clear(Aerr); fmpr_clear(Berr); fmpr_clear(Anorm); fmpr_clear(Bnorm); fmpr_clear(err); _fmpz_vec_clear(Acoeffs, lenA); _fmpz_vec_clear(Bcoeffs, lenB); _fmpz_vec_clear(Ccoeffs, n); fmpz_clear(Aexp); fmpz_clear(Bexp); fmpz_clear(Cexp); }
int main() { slong iter; flint_rand_t state; flint_printf("set_d_2exp_fmpz...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { fmpr_t a, b, c; fmpz_t e; mag_t m; double x; fmpr_init(a); fmpr_init(b); fmpr_init(c); fmpz_init(e); mag_init(m); x = d_randtest2(state); x = ldexp(x, 100 - n_randint(state, 200)); if (n_randint(state, 100) == 0) x = 0.0; fmpz_randtest(e, state, 10); fmpr_set_d(a, x); fmpr_mul_2exp_fmpz(a, a, e); mag_set_d_2exp_fmpz(m, x, e); mag_get_fmpr(b, m); fmpr_set(c, a); fmpr_mul_ui(c, c, 1025, MAG_BITS, FMPR_RND_UP); fmpr_mul_2exp_si(c, c, -10); MAG_CHECK_BITS(m) if (!(fmpr_cmpabs(a, b) <= 0 && fmpr_cmpabs(b, c) <= 0)) { flint_printf("FAIL\n\n"); flint_printf("a = "); fmpr_print(a); flint_printf("\n\n"); flint_printf("b = "); fmpr_print(b); flint_printf("\n\n"); flint_printf("c = "); fmpr_print(c); flint_printf("\n\n"); abort(); } fmpr_clear(a); fmpr_clear(b); fmpr_clear(c); fmpz_clear(e); mag_clear(m); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }