/* TODO: move it to ANTIC? */ int _nf_equal_fmpq(const nf_elem_t a, const fmpq_t b, const nf_t nf) { if (nf->flag & NF_LINEAR) { return (fmpz_equal(LNF_ELEM_NUMREF(a), fmpq_numref(b)) && fmpz_equal(LNF_ELEM_DENREF(a), fmpq_denref(b))); } else if (nf->flag & NF_QUADRATIC) { const fmpz * const num = QNF_ELEM_NUMREF(a); const fmpz * const den = QNF_ELEM_DENREF(a); return (fmpz_is_zero(num + 1) && fmpz_equal(num, fmpq_numref(b)) && fmpz_equal(den, fmpq_denref(b))); } else { if (NF_ELEM(a)->length > 1) return 0; else if (NF_ELEM(a)->length == 0) return fmpq_is_zero(b); return (fmpz_equal(NF_ELEM_NUMREF(a), fmpq_numref(b)) && fmpz_equal(NF_ELEM_DENREF(a), fmpq_denref(b))); } return -1; }
void fmpr_get_fmpq(fmpq_t y, const fmpr_t x) { if (fmpr_is_zero(x)) { fmpq_zero(y); } else if (fmpr_is_special(x) || COEFF_IS_MPZ(*fmpr_expref(x))) { printf("exception: fmpr_get_fmpq: cannot convert to rational\n"); abort(); } else { long exp = *fmpr_expref(x); fmpz_set_ui(fmpq_denref(y), 1UL); if (exp >= 0) { fmpz_mul_2exp(fmpq_numref(y), fmpr_manref(x), exp); } else { fmpz_set(fmpq_numref(y), fmpr_manref(x)); fmpz_mul_2exp(fmpq_denref(y), fmpq_denref(y), -exp); } } }
void arb_rising_fmpq_ui(arb_t y, const fmpq_t x, ulong n, long prec) { if (n == 0) { arb_one(y); } else if (n == 1) { arb_set_fmpq(y, x, prec); } else { long wp; wp = ARF_PREC_ADD(prec, FLINT_BIT_COUNT(n)); bsplit(y, fmpq_numref(x), fmpq_denref(x), 0, n, wp); if (fmpz_is_one(fmpq_denref(x))) { arb_set_round(y, y, prec); } else { arb_t t; arb_init(t); arb_set_fmpz(t, fmpq_denref(x)); arb_pow_ui(t, t, n, wp); arb_div(y, y, t, prec); arb_clear(t); } } }
void nf_elem_scalar_div_fmpq(nf_elem_t a, const nf_elem_t b, const fmpq_t c, const nf_t nf) { if (nf->flag & NF_LINEAR) { fmpz * den = LNF_ELEM_DENREF(a); fmpz * num = LNF_ELEM_NUMREF(a); const fmpz * const den2 = LNF_ELEM_DENREF(b); const fmpz * const num2 = LNF_ELEM_NUMREF(b); _fmpq_mul(num, den, num2, den2, fmpq_denref(c), fmpq_numref(c)); } else if (nf->flag & NF_QUADRATIC) { fmpz * den = QNF_ELEM_DENREF(a); fmpz * num = QNF_ELEM_NUMREF(a); const fmpz * const den2 = QNF_ELEM_DENREF(b); const fmpz * const num2 = QNF_ELEM_NUMREF(b); _fmpq_poly_scalar_div_fmpq(num, den, num2, den2, 2, fmpq_numref(c), fmpq_denref(c)); } else { fmpq_poly_scalar_div_fmpq(NF_ELEM(a), NF_ELEM(b), c); } }
void fmpq_poly_resultant(fmpq_t r, const fmpq_poly_t f, const fmpq_poly_t g) { const long len1 = f->length; const long len2 = g->length; if (len1 == 0 || len2 == 0) { fmpq_zero(r); } else { if (len1 >= len2) { _fmpq_poly_resultant(fmpq_numref(r), fmpq_denref(r), f->coeffs, f->den, len1, g->coeffs, g->den, len2); } else { _fmpq_poly_resultant(fmpq_numref(r), fmpq_denref(r), g->coeffs, g->den, len2, f->coeffs, f->den, len1); if (((len1 | len2) & 1L) == 0L) fmpq_neg(r, r); } } }
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 fmpq_div(fmpq_t res, const fmpq_t op1, const fmpq_t op2) { if (fmpq_is_zero(op2)) { printf("Exception: fmpq_div: division by zero"); abort(); } _fmpq_div(fmpq_numref(res), fmpq_denref(res), fmpq_numref(op1), fmpq_denref(op1), fmpq_numref(op2), fmpq_denref(op2)); }
void _fmpq_bsplit_sum_abpq(fmpz_t P, fmpz_t Q, fmpz_t B, fmpz_t T, const fmpq * ab, const fmpq * pq, long n1, long n2) { if (n2 - n1 <= 0) { fmpz_zero(P); fmpz_one(Q); } else if (n2 - n1 == 1) { fmpz_set(P, fmpq_numref(pq + n1)); fmpz_set(Q, fmpq_denref(pq + n1)); fmpz_set(B, fmpq_denref(ab + n1)); fmpz_mul(T, P, fmpq_numref(ab + n1)); } else { long m = (n1 + n2) / 2; fmpz_t P2, Q2, B2, T2; fmpz_init(P2); fmpz_init(Q2); fmpz_init(B2); fmpz_init(T2); _fmpq_bsplit_sum_abpq(P, Q, B, T, ab, pq, n1, m); _fmpq_bsplit_sum_abpq(P2, Q2, B2, T2, ab, pq, m, n2); if (!fmpz_is_one(B2)) fmpz_mul(T, T, B2); fmpz_mul(T, T, Q2); if (!fmpz_is_one(B)) fmpz_mul(T2, T2, B); fmpz_mul(T2, T2, P); fmpz_add(T, T, T2); fmpz_mul(P, P, P2); fmpz_mul(Q, Q, Q2); fmpz_mul(B, B, B2); fmpz_clear(P2); fmpz_clear(Q2); fmpz_clear(B2); fmpz_clear(T2); } }
int main() { printf("seq_set_fmpq_pow...."); fflush(stdout); { long i; fmpq_t t, u; fmpz_holonomic_t op; fmpq_t c, initial; fmpz_holonomic_init(op); fmpq_init(t); fmpq_init(u); fmpq_init(c); fmpq_init(initial); fmpq_set_si(c, -7, 3); fmpz_holonomic_seq_set_fmpq_pow(op, c); fmpq_set_si(initial, -2, 1); for (i = 0; i < 20; i++) { fmpz_holonomic_get_nth_fmpq(t, op, initial, 0, i); fmpz_pow_ui(fmpq_numref(u), fmpq_numref(c), i); fmpz_pow_ui(fmpq_denref(u), fmpq_denref(c), i); fmpz_mul_si(fmpq_numref(u), fmpq_numref(u), -2); if (!fmpq_equal(t, u)) { printf("FAIL\n"); printf("i = %ld, t = ", i); fmpq_print(t); printf(" u = "); fmpq_print(u); printf("\n"); abort(); } } fmpq_clear(c); fmpq_clear(t); fmpq_clear(u); fmpq_clear(initial); fmpz_holonomic_clear(op); } flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void bernoulli_cache_compute(long n) { if (bernoulli_cache_num < n) { long i, new_num; bernoulli_rev_t iter; if (bernoulli_cache_num == 0) { flint_register_cleanup_function(bernoulli_cleanup); } new_num = FLINT_MAX(bernoulli_cache_num + 128, n); bernoulli_cache = flint_realloc(bernoulli_cache, new_num * sizeof(fmpq)); for (i = bernoulli_cache_num; i < new_num; i++) fmpq_init(bernoulli_cache + i); i = new_num - 1; i -= (i % 2); bernoulli_rev_init(iter, i); for ( ; i >= bernoulli_cache_num; i -= 2) { bernoulli_rev_next(fmpq_numref(bernoulli_cache + i), fmpq_denref(bernoulli_cache + i), iter); } bernoulli_rev_clear(iter); if (new_num > 1) fmpq_set_si(bernoulli_cache + 1, -1, 2); bernoulli_cache_num = new_num; } }
bool renf_elem_class::is_integer() const noexcept { if (nf == nullptr) return fmpz_is_one(fmpq_denref(b)); else return renf_elem_is_integer(a, nf->renf_t()); }
int fmpq_get_mpfr(mpfr_t r, const fmpq_t x, mpfr_rnd_t rnd) { __mpq_struct mpq; fmpz p, q; mp_limb_t pp, qq; p = *fmpq_numref(x); q = *fmpq_denref(x); if (p == 0) return mpfr_set_ui(r, 0, rnd); if (COEFF_IS_MPZ(p)) mpq._mp_num = *COEFF_TO_PTR(p); else { pp = FLINT_ABS(p); mpq._mp_num._mp_alloc = 1; mpq._mp_num._mp_size = (p < 0) ? -1 : 1; mpq._mp_num._mp_d = &pp; } if (COEFF_IS_MPZ(q)) mpq._mp_den = *COEFF_TO_PTR(q); else { qq = q; mpq._mp_den._mp_alloc = 1; mpq._mp_den._mp_size = 1; mpq._mp_den._mp_d = &qq; } return mpfr_set_q(r, &mpq, rnd); }
void fmpq_div_fmpz(fmpq_t res, const fmpq_t op, const fmpz_t x) { fmpz_t y; fmpz_init(y); fmpz_one(y); _fmpq_mul(fmpq_numref(res), fmpq_denref(res), fmpq_numref(op), fmpq_denref(op), y, x); fmpz_clear(y); if (fmpz_sgn(fmpq_denref(res)) < 0) { fmpz_neg(fmpq_numref(res), fmpq_numref(res)); fmpz_neg(fmpq_denref(res), fmpq_denref(res)); } }
void renf_elem_class::assign(ulong value) noexcept { if (nf == nullptr) { fmpz_one(fmpq_denref(b)); fmpz_set_ui(fmpq_numref(b), value); } else renf_elem_set_ui(a, value, nf->renf_t()); }
mpz_class renf_elem_class::ceil() const noexcept { fmpz_t tmp; fmpz_init(tmp); if (nf == nullptr) { fmpz_add(tmp, fmpq_numref(b), fmpq_denref(b)); fmpz_sub_ui(tmp, tmp, 1); fmpz_fdiv_q(tmp, tmp, fmpq_denref(b)); } else renf_elem_ceil(tmp, a, nf->renf_t()); mpz_class z; fmpz_get_mpz(z.get_mpz_t(), tmp); fmpz_clear(tmp); return z; }
void renf_elem_class::assign(const mpz_class & value) noexcept { if (nf == nullptr) { fmpz_one(fmpq_denref(b)); fmpz_set_mpz(fmpq_numref(b), value.__get_mp()); } else renf_elem_set_mpz(a, value.get_mpz_t(), nf->renf_t()); }
void arf_get_fmpq(fmpq_t y, const arf_t x) { if (arf_is_zero(x)) { fmpq_zero(y); } else if (arf_is_special(x) || !ARF_IS_LAGOM(x)) { flint_printf("exception: arf_get_fmpq: cannot convert to rational\n"); abort(); } else { fmpz_t man, exp; slong e; fmpz_init(man); fmpz_init(exp); arf_get_fmpz_2exp(man, exp, x); e = *exp; fmpz_set_ui(fmpq_denref(y), UWORD(1)); if (e >= 0) { fmpz_mul_2exp(fmpq_numref(y), man, e); } else { fmpz_set(fmpq_numref(y), man); fmpz_mul_2exp(fmpq_denref(y), fmpq_denref(y), -e); } fmpz_clear(man); fmpz_clear(exp); } }
int main(void) { int i; FLINT_TEST_INIT(state); flint_printf("cfrac_bound...."); fflush(stdout); for (i = 0; i < 10000; i++) { fmpq_t x, r; fmpz * c; slong n, bound; fmpq_init(x); fmpq_init(r); /* Test worst case (quotient of Fibonacci numbers) */ if (n_randint(state, 50) == 1) { slong v = 1 + n_randint(state, 1000); fmpz_fib_ui(fmpq_numref(x), v + 1); fmpz_fib_ui(fmpq_denref(x), v); } else { fmpq_randtest(x, state, 1 + n_randint(state, 1000)); } bound = fmpq_cfrac_bound(x); c = _fmpz_vec_init(bound + 10); n = fmpq_get_cfrac(c, r, x, bound); if (n > bound) { flint_printf("FAIL: length=%wd > bound=%wd\n", n, bound); abort(); } _fmpz_vec_clear(c, bound + 10); fmpq_clear(x); fmpq_clear(r); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void fmpq_mul_2exp(fmpq_t res, const fmpq_t x, mp_bitcnt_t exp) { if (fmpq_is_zero(x) || exp == 0) { fmpq_set(res, x); } else { mp_bitcnt_t v = fmpz_val2(fmpq_denref(x)); if (exp <= v) { fmpz_set(fmpq_numref(res), fmpq_numref(x)); fmpz_fdiv_q_2exp(fmpq_denref(res), fmpq_denref(x), exp); } else { fmpz_mul_2exp(fmpq_numref(res), fmpq_numref(x), exp - v); fmpz_fdiv_q_2exp(fmpq_denref(res), fmpq_denref(x), v); } } }
int main() { slong i, bound; double a, b; fmpq_t q; fmpr_t t; flint_printf("bound_2exp_si...."); fflush(stdout); fmpq_init(q); fmpr_init(t); for (i = 0; i < 1000; i++) { arith_bernoulli_number(q, i); bound = bernoulli_bound_2exp_si(i); fmpr_set_round_fmpz(t, fmpq_numref(q), 32, FMPR_RND_UP); fmpr_div_fmpz(t, t, fmpq_denref(q), 32, FMPR_RND_UP); if (fmpr_cmpabs_2exp_si(t, bound) > 0) { flint_printf("FAIL: %wd\n", i); fmpr_print(t); flint_printf("\n\n"); flint_printf("%wd\n", bound); flint_printf("\n\n"); abort(); } } fmpq_clear(q); fmpr_clear(t); for (i = 100; i < 4000000; i += 1) { i += (i & 1); a = bernoulli_bound_2exp_si(i); b = log2bern_approx(i); if (a < b || a > 1.01 * b) { flint_printf("FAIL: %wd\n", i); flint_printf("%wd: %f %f %f\n", i, a, b, (float) a / b); abort(); } } flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("sin_pi_fmpq...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t s1, s2; fmpq_t x; slong prec; prec = 2 + n_randint(state, 5000); arb_init(s1); arb_init(s2); fmpq_init(x); fmpq_randtest(x, state, 1 + n_randint(state, 200)); arb_sin_pi_fmpq(s1, x, prec); arb_const_pi(s2, prec); arb_mul_fmpz(s2, s2, fmpq_numref(x), prec); arb_div_fmpz(s2, s2, fmpq_denref(x), prec); arb_sin(s2, s2, prec); if (!arb_overlaps(s1, s2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("x = "); fmpq_print(x); 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(); } arb_clear(s1); arb_clear(s2); fmpq_clear(x); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { long iter; flint_rand_t state; printf("cos_pi_fmpq...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000; iter++) { fmprb_t c1, c2; fmpq_t x; long prec; prec = 2 + n_randint(state, 5000); fmprb_init(c1); fmprb_init(c2); fmpq_init(x); fmpq_randtest(x, state, 1 + n_randint(state, 200)); fmprb_cos_pi_fmpq(c1, x, prec); fmprb_const_pi(c2, prec); fmprb_mul_fmpz(c2, c2, fmpq_numref(x), prec); fmprb_div_fmpz(c2, c2, fmpq_denref(x), prec); fmprb_cos(c2, c2, prec); if (!fmprb_overlaps(c1, c2)) { printf("FAIL: overlap\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("c1 = "); fmprb_printd(c1, 15); printf("\n\n"); printf("c2 = "); fmprb_printd(c2, 15); printf("\n\n"); abort(); } fmprb_clear(c1); fmprb_clear(c2); fmpq_clear(x); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void arb_pow_fmpq(arb_t y, const arb_t x, const fmpq_t a, long prec) { if (fmpz_is_one(fmpq_denref(a))) { arb_pow_fmpz(y, x, fmpq_numref(a), prec); } else if (fmpz_cmp_ui(fmpq_denref(a), 36) <= 0) { arb_root(y, x, *fmpq_denref(a), prec); arb_pow_fmpz(y, y, fmpq_numref(a), prec); } else { long wp; wp = prec + 10; arb_log(y, x, wp); arb_mul_fmpz(y, y, fmpq_numref(a), wp); arb_div_fmpz(y, y, fmpq_denref(a), wp); arb_exp(y, y, prec); } }
mpz_class renf_elem_class::floor() const noexcept { fmpz_t tmp; fmpz_init(tmp); if (nf == nullptr) fmpz_fdiv_q(tmp, fmpq_numref(b), fmpq_denref(b)); else renf_elem_floor(tmp, a, nf->renf_t()); mpz_class z; fmpz_get_mpz(z.get_mpz_t(), tmp); fmpz_clear(tmp); return z; }
mpz_class renf_elem_class::den() const noexcept { mpz_class res; if (nf == nullptr) fmpz_get_mpz(res.__get_mp(), fmpq_denref(b)); else { fmpz_t z; fmpz_init(z); nf_elem_get_den(z, a->elem, nf->renf_t()->nf); fmpz_get_mpz(res.__get_mp(), z); fmpz_clear(z); } return res; }
void test_field1(flint_rand_t state) { /* tests in QQ[sqrt(5)] */ int iter; fmpq_t k; fmpq_poly_t p; arb_t emb; renf_t nf; renf_elem_t a; fmpq_poly_init(p); fmpq_poly_set_coeff_si(p, 2, 1); fmpq_poly_set_coeff_si(p, 1, -1); fmpq_poly_set_coeff_si(p, 0, -1); arb_init(emb); arb_set_d(emb, 1.61803398874989); arb_add_error_2exp_si(emb, -20); renf_init(nf, p, emb, 20 + n_randint(state, 20)); arb_clear(emb); renf_elem_init(a, nf); fmpq_init(k); /* (1+sqrt(5))/2 vs Fibonacci */ fmpq_poly_zero(p); fmpq_poly_set_coeff_si(p, 1, -1); for (iter = 1; iter < 50; iter++) { fprintf(stderr, "start iter = %d\n", iter); fflush(stderr); fmpz_fib_ui(fmpq_numref(k), iter+1); fmpz_fib_ui(fmpq_denref(k), iter); fmpq_poly_set_coeff_fmpq(p, 0, k); renf_elem_set_fmpq_poly(a, p, nf); check_ceil(a, nf, 1 - iter % 2, "sqrt(5)"); fprintf(stderr, "end\n"); fflush(stderr); } renf_elem_clear(a, nf); renf_clear(nf); fmpq_clear(k); fmpq_poly_clear(p); }
static unsigned int reduce_octant(fmpz_t v, fmpz_t w, const fmpq_t x) { const fmpz * p = fmpq_numref(x); const fmpz * q = fmpq_denref(x); unsigned int octant; mp_bitcnt_t vval, wval; if (*p > COEFF_MIN / 8 && *p < COEFF_MAX / 8 && *q > 0 && *q < COEFF_MAX / 4) { slong pp, qq, ww, vv, tt; pp = *p; qq = *q; tt = pp * 4; ww = tt / qq; vv = tt - qq * ww; /* compute correct (floor) quotient and remainder */ if (vv < 0) { ww--; vv += qq; } octant = ((ulong) ww) % 8; ww = qq * 4; if (octant % 2 != 0) vv = qq - vv; if (vv != 0) { count_trailing_zeros(vval, vv); count_trailing_zeros(wval, ww); vval = FLINT_MIN(vval, wval); vv >>= vval; ww >>= vval; }
void _fmpq_bsplit_sum_pq(fmpz_t P, fmpz_t Q, fmpz_t T, const fmpq * pq, long n1, long n2) { if (n2 - n1 <= 0) { fmpz_zero(P); fmpz_one(Q); } else if (n2 - n1 == 1) { fmpz_set(P, fmpq_numref(pq + n1)); fmpz_set(Q, fmpq_denref(pq + n1)); fmpz_set(T, P); } else { long m = (n1 + n2) / 2; fmpz_t P2, Q2, T2; fmpz_init(P2); fmpz_init(Q2); fmpz_init(T2); _fmpq_bsplit_sum_pq(P, Q, T, pq, n1, m); _fmpq_bsplit_sum_pq(P2, Q2, T2, pq, m, n2); fmpz_mul(T, T, Q2); fmpz_mul(T2, T2, P); fmpz_add(T, T, T2); fmpz_mul(P, P, P2); fmpz_mul(Q, Q, Q2); fmpz_clear(P2); fmpz_clear(Q2); fmpz_clear(T2); } }
void fmprb_get_rand_fmpq(fmpq_t q, flint_rand_t state, const fmprb_t x, long bits) { /* there is only one rational */ if (fmprb_is_exact(x)) { fmpr_get_fmpq(q, fmprb_midref(x)); return; } /* pick a denominator */ fmpz_randbits(fmpq_denref(q), state, n_randint(state, bits + 1)); fmpz_abs(fmpq_denref(q), fmpq_denref(q)); if (fmpz_is_zero(fmpq_denref(q))) fmpz_one(fmpq_denref(q)); _fmprb_get_rand_fmpq(fmpq_numref(q), fmpq_denref(q), state, fmpq_denref(q), x); fmpq_canonicalise(q); }
void fmpq_poly_scalar_div_fmpq(fmpq_poly_t rop, const fmpq_poly_t op, const fmpq_t c) { if (fmpq_is_zero(c)) { flint_printf("Exception (fmpq_poly_scalar_div_fmpq). Division by zero.\n"); abort(); } if (fmpq_poly_is_zero(op)) { fmpq_poly_zero(rop); } else { fmpq_poly_fit_length(rop, op->length); _fmpq_poly_set_length(rop, op->length); _fmpq_poly_scalar_div_fmpq(rop->coeffs, rop->den, op->coeffs, op->den, op->length, fmpq_numref(c), fmpq_denref(c)); } }