void fmpr_divappr_abs_ubound(fmpr_t z, const fmpr_t x, const fmpr_t y, slong prec) { if (fmpr_is_special(x) || fmpr_is_special(y) || fmpz_is_pm1(fmpr_manref(y))) { fmpr_div(z, x, y, prec, FMPR_RND_UP); fmpr_abs(z, z); } else { fmpz_t t, u; slong xbits, ybits, tbits, ubits, shift; xbits = fmpz_bits(fmpr_manref(x)); ybits = fmpz_bits(fmpr_manref(y)); fmpz_init(t); fmpz_init(u); ubits = FLINT_MIN(ybits, prec); tbits = prec + ubits + 1; /* upper bound for |x|, shifted */ if (xbits <= tbits) { fmpz_mul_2exp(t, fmpr_manref(x), tbits - xbits); fmpz_abs(t, t); } else if (fmpz_sgn(fmpr_manref(x)) > 0) { fmpz_cdiv_q_2exp(t, fmpr_manref(x), xbits - tbits); } else { fmpz_fdiv_q_2exp(t, fmpr_manref(x), xbits - tbits); fmpz_neg(t, t); } /* lower bound for |y|, shifted */ if (ybits <= ubits) fmpz_mul_2exp(u, fmpr_manref(y), ubits - ybits); else fmpz_tdiv_q_2exp(u, fmpr_manref(y), ybits - ubits); fmpz_abs(u, u); fmpz_cdiv_q(fmpr_manref(z), t, u); shift = (ubits - ybits) - (tbits - xbits); fmpz_sub(fmpr_expref(z), fmpr_expref(x), fmpr_expref(y)); if (shift >= 0) fmpz_add_ui(fmpr_expref(z), fmpr_expref(z), shift); else fmpz_sub_ui(fmpr_expref(z), fmpr_expref(z), -shift); _fmpr_normalise(fmpr_manref(z), fmpr_expref(z), prec, FMPR_RND_UP); fmpz_clear(t); fmpz_clear(u); } }
void fmpz_multi_mod_ui(unsigned long * out, fmpz_t in, fmpz_comb_t comb, fmpz_t ** temp) { ulong i, j, k; ulong n = comb->n; long log_comb; ulong size; mp_limb_t * ptr; ulong num; unsigned long num_primes = comb->num_primes; if (num_primes == 1) // we are reducing modulo a single prime which is assumed to be big enough { if ((long)in[0] > 0L) out[0] = in[1]; else if ((long)in[0] < 0L) out[0] = comb->primes[0] - in[1]; else out[0] = 0L; return; } log_comb = n - 1; // find level in comb with entries bigger than the input integer log_comb = 0; if ((long) in[0] < 0L) while ((fmpz_bits(in) >= fmpz_bits(comb->comb[log_comb][0]) - 1) && (log_comb < comb->n - 1)) log_comb++; else while (fmpz_cmpabs(in, comb->comb[log_comb][0]) >= 0) log_comb++; num = (1L<<(n - log_comb - 1)); // set each entry of this level of temp to the input integer for (i = 0; i < num; i++) { fmpz_set(temp[log_comb][i], in); } log_comb--; num *= 2; // fill in other entries of temp by taking entries of temp at higher level mod pairs from comb while (log_comb > FLINT_LOG_MULTI_MOD_CUTOFF) // keep going until we reach the basecase { for (i = 0, j = 0; i < num; i += 2, j++) { fmpz_mod(temp[log_comb][i], temp[log_comb + 1][j], comb->comb[log_comb][i]); fmpz_mod(temp[log_comb][i+1], temp[log_comb + 1][j], comb->comb[log_comb][i+1]); } num *= 2; log_comb--; } // do basecase num /= 2; log_comb++; ulong stride = (1L << (log_comb + 1)); for (i = 0, j = 0; j < num_primes; i++, j += stride) { fmpz_multi_mod_ui_basecase(out + j, temp[log_comb][i], comb->primes + j, FLINT_MIN(stride, num_primes - j)); } }
slong fmpr_add_naive(fmpr_t z, const fmpr_t x, const fmpr_t y, slong prec, fmpr_rnd_t rnd) { slong shift, xsize, ysize; if (fmpr_is_special(x) || fmpr_is_special(y)) { return _fmpr_add_special(z, x, y, prec, rnd); } shift = _fmpz_sub_small(fmpr_expref(x), fmpr_expref(y)); if (shift == 0) { fmpz_add(fmpr_manref(z), fmpr_manref(x), fmpr_manref(y)); fmpz_set(fmpr_expref(z), fmpr_expref(x)); } else if (shift > 0) { ysize = _fmpz_size(fmpr_manref(y)) * FLINT_BITS; /* x and y do not overlap */ if (shift > ysize && prec != FMPR_PREC_EXACT) { /* y does not overlap with result */ if (ysize + prec - (slong) fmpz_bits(fmpr_manref(x)) < shift) { return _fmpr_add_eps(z, x, fmpz_sgn(fmpr_manref(y)), prec, rnd); } } fmpz_add_mul2exp(fmpr_manref(z), fmpr_manref(y), fmpr_manref(x), shift); fmpz_set(fmpr_expref(z), fmpr_expref(y)); } else { shift = -shift; xsize = _fmpz_size(fmpr_manref(x)) * FLINT_BITS; /* x and y do not overlap */ if (shift > xsize && prec != FMPR_PREC_EXACT) { /* y does not overlap with result */ if (xsize + prec - (slong) fmpz_bits(fmpr_manref(y)) < shift) { return _fmpr_add_eps(z, y, fmpz_sgn(fmpr_manref(x)), prec, rnd); } } fmpz_add_mul2exp(fmpr_manref(z), fmpr_manref(x), fmpr_manref(y), shift); fmpz_set(fmpr_expref(z), fmpr_expref(x)); } return _fmpr_normalise(fmpr_manref(z), fmpr_expref(z), prec, rnd); }
mp_bitcnt_t _fmpz_poly_2norm_normalised_bits(const fmpz * poly, long len) { fmpz_t norm; mp_bitcnt_t bits; fmpz_init(norm); _fmpz_poly_2norm(norm, poly, len); bits = fmpz_bits(norm); fmpz_clear(norm); return bits - fmpz_bits(poly + len - 1) + 1; }
void fmpz_poly_bit_unpack_unsigned(fmpz_poly_t poly, const fmpz_t f, mp_bitcnt_t bit_size) { slong len; mpz_t tmp; if (fmpz_sgn(f) < 0) { flint_printf("Exception (fmpz_poly_bit_unpack_unsigned). Expected an unsigned value.\n"); abort(); } if (bit_size == 0 || fmpz_is_zero(f)) { fmpz_poly_zero(poly); return; } len = (fmpz_bits(f) + bit_size - 1) / bit_size; mpz_init2(tmp, bit_size*len); flint_mpn_zero(tmp->_mp_d, tmp->_mp_alloc); fmpz_get_mpz(tmp, f); fmpz_poly_fit_length(poly, len); _fmpz_poly_bit_unpack_unsigned(poly->coeffs, len, tmp->_mp_d, bit_size); _fmpz_poly_set_length(poly, len); _fmpz_poly_normalise(poly); mpz_clear(tmp); }
void arb_log_arf_huge(arb_t z, const arf_t x, slong prec) { arf_t t; arb_t c; fmpz_t exp; slong wp; arf_init(t); arb_init(c); fmpz_init(exp); fmpz_neg(exp, ARF_EXPREF(x)); arf_mul_2exp_fmpz(t, x, exp); wp = prec + 4 - fmpz_bits(exp); wp = FLINT_MAX(wp, 4); arb_log_arf(z, t, wp); arb_const_log2(c, prec + 4); arb_submul_fmpz(z, c, exp, prec); arf_clear(t); arb_clear(c); fmpz_clear(exp); }
slong fmpq_cfrac_bound(const fmpq_t x) { if (fmpz_is_one(fmpq_denref(x))) return 1; return fmpz_bits(fmpq_denref(x)) * ONE_OVER_LOG2_PHI + 2; }
void fmpr_randtest_not_zero(fmpr_t x, flint_rand_t state, slong bits, slong mag_bits) { fmpz_randtest_not_zero(fmpr_manref(x), state, bits); fmpz_randtest(fmpr_expref(x), state, mag_bits); fmpz_sub_ui(fmpr_expref(x), fmpr_expref(x), fmpz_bits(fmpr_manref(x))); _fmpr_normalise(fmpr_manref(x), fmpr_expref(x), bits, FMPR_RND_DOWN); }
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 renf_randtest(renf_t nf, flint_rand_t state, slong len, slong prec, mp_bitcnt_t bits) { fmpz_poly_t p; fmpq_poly_t p2; fmpz * c_array; slong * k_array; slong n_interval, n_exact; ulong i; arb_t emb; /* compute a random irreducible polynomial */ if (len <= 1) { fprintf(stderr, "ERROR (renf_randtest): got length < 2\n"); abort(); } fmpz_poly_init(p); do{ fmpz_poly_randtest_irreducible(p, state, len, bits); }while(!fmpz_poly_has_real_root(p)); /* pick a random real root */ c_array = _fmpz_vec_init(p->length); k_array = malloc((p->length) * sizeof(slong)); n_interval = 0; fmpz_poly_isolate_real_roots(NULL, &n_exact, c_array, k_array, &n_interval, p); if (n_interval == 0) { fprintf(stderr, "Runtime error\n"); abort(); } i = n_randint(state, n_interval); /* construct the associated number field */ arb_init(emb); arb_from_interval(emb, c_array+i, k_array[i], fmpz_bits(c_array + i) + FLINT_MAX(k_array[i], 0) + 2); fmpq_poly_init(p2); fmpq_poly_set_fmpz_poly(p2, p); /* NOTE: renf init might not be happy with the ball emb */ renf_init(nf, p2, emb, prec); _fmpz_vec_clear(c_array, p->length); free(k_array); fmpz_poly_clear(p); fmpq_poly_clear(p2); arb_clear(emb); }
/* The coefficients in 2^d * \prod_{i=1}^d (x - cos(a_i)) are easily bounded using the binomial theorem. */ static slong magnitude_bound(slong d) { slong res; fmpz_t t; fmpz_init(t); fmpz_bin_uiui(t, d, d / 2); res = fmpz_bits(t); fmpz_clear(t); return FLINT_ABS(res) + d; }
int main(void) { int i, result; double output; slong exp; FLINT_TEST_INIT(state); flint_printf("get_d_2exp...."); fflush(stdout); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_t a; fmpz_init(a); fmpz_randtest(a, state, 200); output = fmpz_get_d_2exp(&exp, a); result = (fmpz_bits(a) == exp); if (!result) { flint_printf("FAIL:\n"); flint_printf("a = "), fmpz_print(a), flint_printf("\n"); flint_printf("output = %f\n", output); flint_printf("exp = %wd, bits = %wu\n", exp, fmpz_bits(a)); abort(); } fmpz_clear(a); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void acb_quadratic_roots_fmpz(acb_t r1, acb_t r2, const fmpz_t a, const fmpz_t b, const fmpz_t c, slong prec) { fmpz_t d; fmpz_init(d); /* d = b^2 - 4ac */ fmpz_mul(d, a, c); fmpz_mul_2exp(d, d, 2); fmpz_submul(d, b, b); fmpz_neg(d, d); /* +/- sqrt(d) */ acb_zero(r1); if (fmpz_sgn(d) >= 0) { arb_sqrt_fmpz(acb_realref(r1), d, prec + fmpz_bits(d) + 4); } else { fmpz_neg(d, d); arb_sqrt_fmpz(acb_imagref(r1), d, prec + fmpz_bits(d) + 4); } acb_neg(r2, r1); /* -b */ acb_sub_fmpz(r1, r1, b, prec + 4); acb_sub_fmpz(r2, r2, b, prec + 4); /* divide by 2a */ fmpz_mul_2exp(d, a, 1); acb_div_fmpz(r1, r1, d, prec); acb_div_fmpz(r2, r2, d, prec); fmpz_clear(d); return; }
int main(void) { int i, result; flint_rand_t state; printf("height...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 10000; i++) { fmpz *a; fmpz_t h; long len, bits, bits2; fmpz_init(h); len = n_randint(state, 100); a = _fmpz_vec_init(len); bits = n_randint(state, 200); _fmpz_vec_randtest(a, state, len, bits); bits2 = _fmpz_vec_max_bits(a, len); _fmpz_vec_height(h, a, len); result = (fmpz_bits(h) == FLINT_ABS(bits2)) && (fmpz_sgn(h) >= 0); if (!result) { printf("FAIL:\n"); printf("bits = %ld, bits2 = %ld\n", bits, bits2); printf("Computed height:\n"); fmpz_print(h); printf("\n"); abort(); } fmpz_clear(h); _fmpz_vec_clear(a, len); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
void acb_pow_arb(acb_t z, const acb_t x, const arb_t y, long prec) { const arf_struct * ymid = arb_midref(y); const mag_struct * yrad = arb_radref(y); if (arb_is_zero(y)) { acb_one(z); return; } if (mag_is_zero(yrad)) { /* 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); acb_pow_fmpz_binexp(z, x, e, prec); } else { /* hack: give something finite here (should fix sqrt/rsqrt etc) */ if (arb_contains_zero(acb_imagref(x)) && arb_contains_nonpositive(acb_realref(x))) { _acb_pow_arb_exp(z, x, y, prec); fmpz_clear(e); return; } arf_get_fmpz_fixed_si(e, ymid, -1); acb_sqrt(z, x, prec + fmpz_bits(e)); acb_pow_fmpz_binexp(z, z, e, prec); } fmpz_clear(e); return; } } _acb_pow_arb_exp(z, x, y, prec); }
void _qadic_exp(fmpz *rop, const fmpz *op, slong v, slong len, const fmpz *a, const slong *j, slong lena, const fmpz_t p, slong N, const fmpz_t pN) { if (N < (WORD(1) << 13) / (slong) fmpz_bits(p)) { _qadic_exp_rectangular(rop, op, v, len, a, j, lena, p, N, pN); } else { const slong d = j[lena - 1]; _qadic_exp_balanced(rop, op, v, len, a, j, lena, p, N, pN); _fmpz_vec_zero(rop + d, d - 1); } }
long _fmpz_vec_max_bits(const fmpz * vec, long len) { long i, bits, max_bits = 0, sign = 1; for (i = 0; i < len; i++) { bits = fmpz_bits(vec + i); if (bits > max_bits) max_bits = bits; if (fmpz_sgn(vec + i) < 0) sign = -1L; } return max_bits * sign; }
void arb_from_interval(arb_t x, const fmpz_t c, const slong k, const slong prec) { /* we build the ball that gives exactly (c 2^k, (c+1) 2^k) */ /* center: (2c+1) 2^(k-1) */ /* radius: 2^(k-1) */ if (prec <= 0 || prec < fmpz_bits(c) + 2) { fprintf(stderr, "not enough precision"); abort(); } arb_set_fmpz(x, c); arb_mul_2exp_si(x, x, 1); arb_add_si(x, x, 1, prec); arb_mul_2exp_si(x, x, k-1); arb_add_error_2exp_si(x, k-1); }
void fmpr_pow_sloppy_fmpz(fmpr_t y, const fmpr_t b, const fmpz_t e, long prec, fmpr_rnd_t rnd) { long i, wp, bits; if (fmpz_is_zero(e)) { fmpr_set_ui(y, 1UL); return; } if (fmpz_sgn(e) < 0) { fmpz_t f; fmpz_init(f); fmpz_neg(f, e); fmpr_pow_sloppy_fmpz(y, b, f, prec + 2, (rnd == FMPR_RND_FLOOR || rnd == FMPR_RND_DOWN) ? FMPR_RND_UP : FMPR_RND_DOWN); fmpr_ui_div(y, 1UL, y, prec, rnd); fmpz_clear(f); } if (y == b) { fmpr_t t; fmpr_init(t); fmpr_set(t, b); fmpr_pow_sloppy_fmpz(y, t, e, prec, rnd); fmpr_clear(t); return; } fmpr_set(y, b); bits = fmpz_bits(e); wp = FMPR_PREC_ADD(prec, bits); for (i = bits - 2; i >= 0; i--) { fmpr_mul(y, y, y, wp, rnd); if (fmpz_tstbit(e, i)) fmpr_mul(y, y, b, wp, rnd); } }
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 fmpz_randtest_mod(fmpz_t f, flint_rand_t state, const fmpz_t m) { fmpz_t t; fmpz_init(t); fmpz_randtest_unsigned(t, state, fmpz_bits(m) + 2); fmpz_mod(t, t, m); if (n_randlimb(state) & UWORD(1)) { fmpz_sub(t, m, t); fmpz_sub_ui(t, t, UWORD(1)); } fmpz_set(f, t); fmpz_clear(t); }
void fmpz_poly_bit_unpack(fmpz_poly_t poly, const fmpz_t f, mp_bitcnt_t bit_size) { slong len; mpz_t tmp; int negate, borrow; if (bit_size == 0 || fmpz_is_zero(f)) { fmpz_poly_zero(poly); return; } /* Round up */ len = (fmpz_bits(f) + bit_size - 1) / bit_size; negate = (fmpz_sgn(f) < 0) ? -1 : 0; mpz_init2(tmp, bit_size*len); /* TODO: avoid all this wastefulness */ flint_mpn_zero(tmp->_mp_d, tmp->_mp_alloc); fmpz_get_mpz(tmp, f); fmpz_poly_fit_length(poly, len + 1); borrow = _fmpz_poly_bit_unpack(poly->coeffs, len, tmp->_mp_d, bit_size, negate); if (borrow) { fmpz_set_si(poly->coeffs + len, negate ? WORD(-1) : WORD(1)); _fmpz_poly_set_length(poly, len + 1); } else { _fmpz_poly_set_length(poly, len); _fmpz_poly_normalise(poly); } mpz_clear(tmp); }
int main(void) { int i, j, result; ulong count = UWORD(0); gmp_randstate_t st; FLINT_TEST_INIT(state); gmp_randinit_default(st); flint_printf("factor_pp1...."); fflush(stdout); for (i = 0; i < 50 * flint_test_multiplier(); i++) /* Test random numbers */ { mp_bitcnt_t bits; mpz_t m, n; fmpz_t n1, n2, r; mpz_init(n); mpz_init(m); fmpz_init(n1); fmpz_init(n2); fmpz_init(r); do { mpz_urandomb(n, st, n_randint(state, 128) + 2); } while (flint_mpz_cmp_ui(n, 2) < 0); do { mpz_urandomb(m, st, n_randint(state, 50) + 2); } while (!mpz_probab_prime_p(m, 20)); mpz_mul(n, n, m); fmpz_set_mpz(n1, n); bits = FLINT_MIN(fmpz_bits(n1), FLINT_BITS); for (j = 0; j < 20; j++) { fmpz_factor_pp1(n2, n1, 10000, 10000, n_randbits(state, bits - 2) + 3); if (fmpz_cmp_ui(n2, 1) > 0) break; } if (fmpz_cmp_ui(n2, 1) > 0) { count++; fmpz_mod(r, n1, n2); result = (fmpz_is_zero(r)); if (!result) { flint_printf("FAIL:\n"); flint_printf("n1 = "); fmpz_print(n1); flint_printf(", n2 = "); fmpz_print(n2); flint_printf("\n"); fmpz_print(r); flint_printf("\n"); abort(); } } fmpz_clear(n1); fmpz_clear(n2); fmpz_clear(r); mpz_clear(m); mpz_clear(n); } if (count < 49 * flint_test_multiplier()) { flint_printf("FAIL:\n"); flint_printf("Only %wu numbers factored\n", count); abort(); } gmp_randclear(st); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i; FLINT_TEST_INIT(state); flint_printf("multi_CRT_ui...."); fflush(stdout); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { slong bits, prime_bits, rows, cols, num_primes, j; fmpz_t mod; fmpz_mat_t A, B, C; nmod_mat_t Amod[1000]; mp_limb_t primes[1000]; bits = n_randint(state, 500) + 1; rows = n_randint(state, 10); cols = n_randint(state, 10); prime_bits = 1 + n_randint(state, FLINT_BITS - 1); fmpz_mat_init(A, rows, cols); fmpz_mat_init(B, rows, cols); fmpz_mat_init(C, rows, cols); fmpz_mat_randtest(A, state, bits); fmpz_init(mod); num_primes = 0; primes[0] = n_nextprime(UWORD(1) << prime_bits, 0); fmpz_set_ui(mod, primes[0]); /* + 1 for sign */ while (fmpz_bits(mod) <= bits + 1) { primes[num_primes + 1] = n_nextprime(primes[num_primes], 0); fmpz_mul_ui(mod, mod, primes[num_primes + 1]); num_primes++; } num_primes++; for (j = 0; j < num_primes; j++) nmod_mat_init(Amod[j], rows, cols, primes[j]); fmpz_mat_multi_mod_ui(Amod, num_primes, A); fmpz_mat_multi_CRT_ui(B, Amod, num_primes, 1); if (!fmpz_mat_equal(B, A)) { flint_printf("FAIL!\n"); flint_printf("primes: "); for (j = 0; j < num_primes; j++) flint_printf("%wu ", primes[j]); flint_printf("\nA: \n"); fmpz_mat_print_pretty(A); flint_printf("\nB: \n"); fmpz_mat_print_pretty(B); flint_printf("\n"); abort(); } for (j = 0; j < num_primes; j++) nmod_mat_clear(Amod[j]); fmpz_mat_clear(A); fmpz_mat_clear(B); fmpz_mat_clear(C); fmpz_clear(mod); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main() { slong iter, iter2; flint_rand_t state; flint_printf("addmul_si...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 1000 * arb_test_multiplier(); iter++) { arf_t x, z, v; slong y; slong prec, r1, r2; arf_rnd_t rnd; arf_init(x); arf_init(z); arf_init(v); for (iter2 = 0; iter2 < 100; iter2++) { arf_randtest_special(x, state, 2000, 100); y = z_randtest(state); arf_randtest_special(z, state, 2000, 100); arf_set(v, z); prec = 2 + n_randint(state, 2000); if (n_randint(state, 10) == 0 && fmpz_bits(ARF_EXPREF(x)) < 10 && fmpz_bits(ARF_EXPREF(z)) < 10) { prec = ARF_PREC_EXACT; } switch (n_randint(state, 5)) { case 0: rnd = ARF_RND_DOWN; break; case 1: rnd = ARF_RND_UP; break; case 2: rnd = ARF_RND_FLOOR; break; case 3: rnd = ARF_RND_CEIL; break; default: rnd = ARF_RND_NEAR; break; } switch (n_randint(state, 2)) { case 0: r1 = arf_addmul_si(z, x, y, prec, rnd); r2 = arf_addmul_si_naive(v, x, y, prec, rnd); if (!arf_equal(z, v) || r1 != r2) { flint_printf("FAIL!\n"); flint_printf("prec = %wd, rnd = %d\n\n", prec, rnd); flint_printf("x = "); arf_print(x); flint_printf("\n\n"); flint_printf("y = %wd", y); flint_printf("\n\n"); flint_printf("z = "); arf_debug(z); flint_printf("\n\n"); flint_printf("v = "); arf_debug(v); flint_printf("\n\n"); flint_printf("r1 = %wd, r2 = %wd\n", r1, r2); abort(); } break; default: r2 = arf_addmul_si_naive(v, v, y, prec, rnd); r1 = arf_addmul_si(z, z, y, prec, rnd); if (!arf_equal(v, z) || r1 != r2) { flint_printf("FAIL (aliasing)!\n"); flint_printf("prec = %wd, rnd = %d\n\n", prec, rnd); flint_printf("y = %wd", y); flint_printf("\n\n"); flint_printf("v = "); arf_print(v); flint_printf("\n\n"); flint_printf("z = "); arf_print(z); flint_printf("\n\n"); flint_printf("r1 = %wd, r2 = %wd\n", r1, r2); abort(); } break; } } arf_clear(x); arf_clear(z); arf_clear(v); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("add...."); fflush(stdout); flint_randinit(state); /* test exact addition: (x + y) + z == x + (y + z) */ for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { slong bits, res1, res2, res3, res4; fmpr_t x, y, z, t, u; bits = 2 + n_randint(state, 200); fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(t); fmpr_init(u); fmpr_randtest_special(x, state, bits, 10); fmpr_randtest_special(y, state, bits, 10); fmpr_randtest_special(z, state, bits, 10); fmpr_randtest_special(t, state, bits, 10); fmpr_randtest_special(u, state, bits, 10); res1 = fmpr_add(t, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); res2 = fmpr_add(t, t, z, FMPR_PREC_EXACT, FMPR_RND_DOWN); res3 = fmpr_add(u, y, z, FMPR_PREC_EXACT, FMPR_RND_DOWN); res4 = fmpr_add(u, x, u, FMPR_PREC_EXACT, FMPR_RND_DOWN); if (!fmpr_equal(t, u) || res1 != FMPR_RESULT_EXACT || res2 != FMPR_RESULT_EXACT || res3 != FMPR_RESULT_EXACT || res4 != FMPR_RESULT_EXACT) { flint_printf("FAIL\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("t = "); fmpr_print(t); flint_printf("\n\n"); flint_printf("u = "); fmpr_print(u); flint_printf("\n\n"); abort(); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(t); fmpr_clear(u); } /* compare with add_naive */ for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++) { slong prec, ret1, ret2; fmpr_t x, y, z, w; fmpr_rnd_t rnd; fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(w); fmpr_randtest_special(x, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200)); fmpr_randtest_special(y, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200)); fmpr_randtest_special(z, state, 1 + n_randint(state, 1000), 1 + n_randint(state, 200)); prec = 2 + n_randint(state, 1000); if (n_randint(state, 10) == 0 && fmpz_bits(fmpr_expref(x)) < 10 && fmpz_bits(fmpr_expref(y)) < 10) prec = FMPR_PREC_EXACT; switch (n_randint(state, 4)) { case 0: rnd = FMPR_RND_DOWN; break; case 1: rnd = FMPR_RND_UP; break; case 2: rnd = FMPR_RND_FLOOR; break; default: rnd = FMPR_RND_CEIL; break; } ret1 = fmpr_add(z, x, y, prec, rnd); ret2 = fmpr_add_naive(w, x, y, prec, rnd); if (!fmpr_equal(z, w) || ret1 != ret2 || !fmpr_check_ulp(z, ret1, prec) || !fmpr_check_ulp(w, ret2, prec)) { flint_printf("FAIL\n\n"); flint_printf("iter %wd\n", iter); flint_printf("prec = %wd\n", prec); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("w = "); fmpr_print(w); flint_printf("\n\n"); flint_printf("ret1 = %wd, ret2 = %wd\n", ret1, ret2); abort(); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(w); } /* compare rounding with mpfr */ for (iter = 0; iter < 1000000 * arb_test_multiplier(); iter++) { slong bits, res; int mpfr_res; fmpr_t x, y, z, w; mpfr_t X, Y, Z; bits = 2 + n_randint(state, 500); fmpr_init(x); fmpr_init(y); fmpr_init(z); fmpr_init(w); mpfr_init2(X, bits + 500); mpfr_init2(Y, bits + 500); mpfr_init2(Z, bits); fmpr_randtest_special(x, state, bits + n_randint(state, 500), 10); fmpr_randtest_special(y, state, bits + n_randint(state, 500), 10); fmpr_randtest_special(z, state, bits, 10); fmpr_get_mpfr(X, x, MPFR_RNDN); fmpr_get_mpfr(Y, y, MPFR_RNDN); switch (n_randint(state, 4)) { case 0: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDZ); res = fmpr_add(z, x, y, bits, FMPR_RND_DOWN); break; case 1: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDA); res = fmpr_add(z, x, y, bits, FMPR_RND_UP); break; case 2: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDD); res = fmpr_add(z, x, y, bits, FMPR_RND_FLOOR); break; default: mpfr_res = mpfr_add(Z, X, Y, MPFR_RNDU); res = fmpr_add(z, x, y, bits, FMPR_RND_CEIL); break; } fmpr_set_mpfr(w, Z); if (!fmpr_equal(z, w) || (res == FMPR_RESULT_EXACT) != (mpfr_res == 0) || !fmpr_check_ulp(z, res, bits)) { flint_printf("FAIL\n\n"); flint_printf("iter %wd\n", iter); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("w = "); fmpr_print(w); flint_printf("\n\n"); flint_printf("returned: %wd, %d\n", res, mpfr_res); abort(); } /* check error bound */ if (!fmpr_is_nan(z) && !fmpr_is_inf(z)) { fmpr_t z_exact, error_bound, true_error; fmpr_init(z_exact); fmpr_init(error_bound); fmpr_init(true_error); fmpr_set_error_result(error_bound, z, res); fmpr_add(z_exact, x, y, FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_sub(true_error, z, z_exact, FMPR_PREC_EXACT, FMPR_RND_DOWN); fmpr_abs(true_error, true_error); if (fmpr_is_zero(error_bound) != fmpr_is_zero(true_error) || fmpr_cmp(true_error, error_bound) > 0) { flint_printf("FAIL: error bound\n\n"); flint_printf("bits = %wd\n", bits); flint_printf("x = "); fmpr_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpr_print(y); flint_printf("\n\n"); flint_printf("z = "); fmpr_print(z); flint_printf("\n\n"); flint_printf("z_exact = "); fmpr_print(z_exact); flint_printf("\n\n"); flint_printf("true_error = "); fmpr_print(true_error); flint_printf("\n\n"); flint_printf("error_bound = "); fmpr_print(error_bound); flint_printf("\n\n"); abort(); } fmpr_clear(z_exact); fmpr_clear(error_bound); fmpr_clear(true_error); } fmpr_clear(x); fmpr_clear(y); fmpr_clear(z); fmpr_clear(w); mpfr_clear(X); mpfr_clear(Y); mpfr_clear(Z); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
static void _fmprb_gamma(fmprb_t y, const fmprb_t x, long prec, int inverse) { int reflect; long r, n, wp; fmprb_t t, u, v; if (fmprb_is_exact(x)) { const fmpr_struct * mid = fmprb_midref(x); if (fmpr_is_special(mid)) { if (!inverse && fmpr_is_pos_inf(mid)) { fmprb_set(y, x); } else if (fmpr_is_nan(mid) || fmpr_is_neg_inf(mid) || !inverse) { fmpr_nan(fmprb_midref(y)); fmpr_pos_inf(fmprb_radref(y)); } else { fmprb_zero(y); } return; } else { const fmpz exp = *fmpr_expref(mid); const fmpz man = *fmpr_manref(mid); /* fast gamma(n), gamma(n/2) or gamma(n/4) */ if (!COEFF_IS_MPZ(exp) && (exp >= -2) && ((double) fmpz_bits(&man) + exp < prec)) { fmpq_t a; fmpq_init(a); fmpr_get_fmpq(a, mid); fmprb_gamma_fmpq(y, a, prec + 2 * inverse); if (inverse) fmprb_ui_div(y, 1, y, prec); fmpq_clear(a); return; } } } wp = prec + FLINT_BIT_COUNT(prec); gamma_stirling_choose_param_fmprb(&reflect, &r, &n, x, 1, 0, wp); fmprb_init(t); fmprb_init(u); fmprb_init(v); if (reflect) { /* gamma(x) = (rf(1-x, r) * pi) / (gamma(1-x+r) sin(pi x)) */ fmprb_sub_ui(t, x, 1, wp); fmprb_neg(t, t); gamma_rising_fmprb_ui_bsplit(u, t, r, wp); fmprb_const_pi(v, wp); fmprb_mul(u, u, v, wp); fmprb_add_ui(t, t, r, wp); gamma_stirling_eval_fmprb(v, t, n, 0, wp); fmprb_exp(v, v, wp); fmprb_sin_pi(t, x, wp); fmprb_mul(v, v, t, wp); } else { /* gamma(x) = gamma(x+r) / rf(x,r) */ fmprb_add_ui(t, x, r, wp); gamma_stirling_eval_fmprb(u, t, n, 0, wp); fmprb_exp(u, u, prec); gamma_rising_fmprb_ui_bsplit(v, x, r, wp); } if (inverse) fmprb_div(y, v, u, prec); else fmprb_div(y, u, v, prec); fmprb_clear(t); fmprb_clear(u); fmprb_clear(v); }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("resultant_div...."); fflush(stdout); for (i = 0; i < 50; i++) { fmpq_poly_t f, g, h, p; fmpq_t x, y, z, zz; fmpz_t den; slong nbits; fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_init(p); fmpq_poly_init(h); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmpq_init(zz); fmpz_init(den); fmpq_poly_randtest(f, state, n_randint(state, 50), 100); fmpq_poly_randtest(g, state, n_randint(state, 50), 100); fmpq_poly_randtest(h, state, n_randint(state, 50), 100); fmpz_set(den, fmpq_poly_denref(f)); fmpq_poly_scalar_mul_fmpz(f, f, den); fmpz_set(den, fmpq_poly_denref(g)); fmpq_poly_scalar_mul_fmpz(g, g, den); fmpz_set(den, fmpq_poly_denref(h)); fmpq_poly_scalar_mul_fmpz(h, h, den); fmpq_poly_mul(p, f, g); fmpq_poly_resultant(x, f, h); if (!fmpz_is_one(fmpq_denref(x))) { flint_printf("FAIL resultant not integral\n"); flint_printf("f = "), fmpq_poly_print(f), flint_printf("\n\n"); flint_printf("g = "), fmpq_poly_print(g), flint_printf("\n\n"); flint_printf("y = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_poly_resultant(y, g, h); if (!fmpz_is_one(fmpq_denref(y))) { flint_printf("FAIL resultant not integral\n"); flint_printf("h = "), fmpq_poly_print(f), flint_printf("\n\n"); flint_printf("g = "), fmpq_poly_print(g), flint_printf("\n\n"); flint_printf("z = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_poly_resultant(z, p, h); if (!fmpz_is_one(fmpq_denref(z))) { flint_printf("FAIL resultant not integral\n"); flint_printf("p = "), fmpq_poly_print(f), flint_printf("\n\n"); flint_printf("g = "), fmpq_poly_print(g), flint_printf("\n\n"); flint_printf("y = "), fmpq_print(y), flint_printf("\n\n"); abort(); } if (fmpq_is_zero(z)) { fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); fmpq_poly_clear(p); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); fmpq_clear(zz); fmpz_clear(den); continue; } nbits = (slong)fmpz_bits(fmpq_numref(y)) + 1; fmpq_poly_resultant_div(z, p, h, fmpq_numref(x), nbits); fmpq_poly_resultant(zz, p, h); result = fmpq_equal(z, y); if (!result) { flint_printf("FAIL (res(p, g)/div == res(p, g)/div:\n"); flint_printf("p = "), fmpq_poly_print_pretty(p, "x"), flint_printf("\n\n"); flint_printf("h = "), fmpq_poly_print_pretty(h, "x"), flint_printf("\n\n"); flint_printf("res(p, h) = "), fmpq_print(zz), flint_printf("\n\n"); flint_printf("res(p, h) = "), fmpq_print(x), flint_printf(" * "), fmpq_print(y), flint_printf("\n\n"); flint_printf("supplied divisor = "), fmpq_print(x), flint_printf("\n\n"); flint_printf("nbits = %wu\n\n", nbits); flint_printf("divisor found = "), fmpq_print(z), flint_printf("\n\n"); flint_printf("correct result = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); fmpq_poly_clear(p); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); fmpq_clear(zz); fmpz_clear(den); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void acb_pow_fmpz_binexp(acb_t y, const acb_t b, const fmpz_t e, long prec) { long i, wp, bits; if (-2L <= *e && *e <= 4L) { if (*e == 0L) { acb_one(y); } else if (*e == 1L) { acb_set_round(y, b, prec); } else if (*e == -1L) { acb_inv(y, b, prec); } else if (*e == 2L) { acb_mul(y, b, b, prec); } else if (*e == 3L) { acb_cube(y, b, prec); } else if (*e == 4L) { acb_mul(y, b, b, prec); acb_mul(y, y, y, prec); } else { acb_inv(y, b, prec); acb_mul(y, y, y, prec); } return; } if (fmpz_sgn(e) < 0) { fmpz_t f; fmpz_init(f); fmpz_neg(f, e); acb_pow_fmpz_binexp(y, b, f, prec + 2); acb_inv(y, y, prec); fmpz_clear(f); return; } if (!COEFF_IS_MPZ(*e) && ((*e) % 3 == 0)) { fmpz e3 = (*e) / 3; acb_pow_fmpz_binexp(y, b, &e3, prec); acb_cube(y, y, prec); return; } if (y == b) { acb_t t; acb_init(t); acb_set(t, b); acb_pow_fmpz_binexp(y, t, e, prec); acb_clear(t); return; } acb_set(y, b); bits = fmpz_bits(e); wp = ARF_PREC_ADD(prec, bits); for (i = bits - 2; i >= 0; i--) { acb_mul(y, y, y, wp); if (fmpz_tstbit(e, i)) acb_mul(y, y, b, wp); } }
void _fq_nmod_pow(mp_limb_t *rop, const mp_limb_t *op, slong len, const fmpz_t e, const fq_nmod_ctx_t ctx) { const slong d = fq_nmod_ctx_degree(ctx); if (fmpz_is_zero(e)) { rop[0] = WORD(1); _nmod_vec_zero(rop + 1, 2 * d - 1 - 1); } else if (fmpz_is_one(e)) { _nmod_vec_set(rop, op, len); _nmod_vec_zero(rop + len, 2 * d - 1 - len); } else { ulong bit; mp_limb_t *v = _nmod_vec_init(2 * d - 1); mp_limb_t *R, *S, *T; _nmod_vec_zero(v, 2 * d - 1); _nmod_vec_zero(rop, 2 * d - 1); /* Set bits to the bitmask with a 1 one place lower than the msb of e */ bit = fmpz_bits(e) - 2; /* Trial run without any polynomial arithmetic to determine the parity of the number of swaps; then set R and S accordingly */ { unsigned int swaps = 0U; ulong bit2 = bit; if (fmpz_tstbit(e, bit2)) swaps = ~swaps; while (bit2--) if (!fmpz_tstbit(e, bit2)) swaps = ~swaps; if (swaps == 0U) { R = rop; S = v; } else { R = v; S = rop; } } /* We unroll the first step of the loop, referring to {op, len} */ _nmod_poly_mul(R, op, len, op, len, ctx->mod); _fq_nmod_reduce(R, 2 * len - 1, ctx); if (fmpz_tstbit(e, bit)) { _nmod_poly_mul(S, R, d, op, len, ctx->mod); _fq_nmod_reduce(S, d + len - 1, ctx); T = R; R = S; S = T; } while (bit--) { if (fmpz_tstbit(e, bit)) { _nmod_poly_mul(S, R, d, R, d, ctx->mod); _fq_nmod_reduce(S, 2 * d - 1, ctx); _nmod_poly_mul(R, S, d, op, len, ctx->mod); _fq_nmod_reduce(R, d + len - 1, ctx); } else { _nmod_poly_mul(S, R, d, R, d, ctx->mod); _fq_nmod_reduce(S, 2 * d - 1, ctx); T = R; R = S; S = T; } } _nmod_vec_clear(v); } }