void __fmpz_multi_CRT_sign(fmpz_t output, fmpz_t input, fmpz_comb_t comb) { unsigned long n = comb->n; if (n == 0L) { if (input[0] == 0L) { fmpz_set_ui(output, 0L); return; } unsigned long p = comb->primes[0]; if ((p - input[1]) < input[1]) fmpz_set_si(output, (long) (input[1] - p)); else fmpz_set_ui(output, input[1]); return; } fmpz_t temp = fmpz_init(fmpz_size(comb->comb[n-1][0]) + 1); fmpz_sub(temp, input, comb->comb[comb->n - 1][0]); if (fmpz_cmpabs(temp, input) <= 0L) fmpz_set(output, temp); else fmpz_set(output, input); fmpz_clear(temp); return; }
void fmpz_primorial(fmpz_t res, long n) { mp_size_t len, pi; ulong bits; __mpz_struct * mpz_ptr; if (n <= LARGEST_ULONG_PRIMORIAL) { if (n <= 2) fmpz_set_ui(res, 1 + (n==2)); else fmpz_set_ui(res, ULONG_PRIMORIALS[(n-1)/2-1]); return; } pi = n_prime_pi(n); n_compute_primes(pi); bits = FLINT_BIT_COUNT(flint_primes[pi - 1]); mpz_ptr = _fmpz_promote(res); mpz_realloc2(mpz_ptr, pi*bits); len = mpn_prod_limbs(mpz_ptr->_mp_d, flint_primes, pi, bits); mpz_ptr->_mp_size = len; }
int main(void) { long i; fmpz_t x; printf("fits_si...."); fflush(stdout); fmpz_init(x); fmpz_set_si(x, COEFF_MIN); check(x, 1); fmpz_set_si(x, COEFF_MAX); check(x, 1); fmpz_set_si(x, LONG_MAX); check(x, 1); fmpz_set_si(x, LONG_MIN); check(x, 1); fmpz_set_ui(x, ULONG_MAX); check(x, 0); fmpz_set_ui(x, ULONG_MAX); fmpz_neg(x, x); check(x, 0); fmpz_set_si(x, LONG_MAX); fmpz_add_ui(x, x, 1); check(x, 0); fmpz_set_si(x, LONG_MIN); fmpz_sub_ui(x, x, 1); check(x, 0); for (i = 0; i < 1000; i++) { fmpz_set_ui(x, 1); fmpz_mul_2exp(x, x, i); check(x, i < FLINT_BITS - 1); fmpz_neg(x, x); check(x, i < FLINT_BITS); /* LONG_MIN fits */ } fmpz_clear(x); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void _fmpz_ramanujan_tau(fmpz_t res, fmpz_factor_t factors) { fmpz_poly_t poly; fmpz_t tau_p, p_11, next, this, prev; long k, r; ulong max_prime; max_prime = 1UL; for (k = 0; k < factors->length; k++) { /* TODO: handle overflow properly */ max_prime = FLINT_MAX(max_prime, fmpz_get_ui(factors->p + k)); } fmpz_poly_init(poly); fmpz_poly_ramanujan_tau(poly, max_prime + 1); fmpz_set_ui(res, 1); fmpz_init(tau_p); fmpz_init(p_11); fmpz_init(next); fmpz_init(this); fmpz_init(prev); for (k = 0; k < factors->length; k++) { ulong p = fmpz_get_ui(factors->p + k); fmpz_set(tau_p, poly->coeffs + p); fmpz_set_ui(p_11, p); fmpz_pow_ui(p_11, p_11, 11); fmpz_set_ui(prev, 1); fmpz_set(this, tau_p); for (r = 1; r < fmpz_get_ui(factors->exp + k); r++) { fmpz_mul(next, tau_p, this); fmpz_submul(next, p_11, prev); fmpz_set(prev, this); fmpz_set(this, next); } fmpz_mul(res, res, this); } fmpz_clear(tau_p); fmpz_clear(p_11); fmpz_clear(next); fmpz_clear(this); fmpz_clear(prev); fmpz_poly_clear(poly); }
void _harmonic_number(fmpz_t num, fmpz_t den, long n) { n = FLINT_MAX(n, 0); if (n <= FLINT_HARMONIC_MAX_TINY) { fmpz_set_ui(num, FLINT_HARMONIC_TINY_P[n]); fmpz_set_ui(den, FLINT_HARMONIC_TINY_Q[n]); } else { _mpq_harmonic_odd_balanced(num, den, n); } }
static void precompute_dinv_p(fmpz *list, long M, long d, long p, long N) { fmpz_one(list + 0); if (M >= p) { fmpz_t P, PN; long r; fmpz_init_set_ui(P, p); fmpz_init(PN); fmpz_pow_ui(PN, P, N); fmpz_set_ui(list + 1, d); _padic_inv(list + 1, list + 1, P, N); for (r = 2; r <= M / p; r++) { fmpz_mul(list + r, list + (r - 1), list + 1); fmpz_mod(list + r, list + r, PN); } fmpz_clear(P); fmpz_clear(PN); } }
void _fmpq_poly_scalar_mul_ui(fmpz * rpoly, fmpz_t rden, const fmpz * poly, const fmpz_t den, slong len, ulong c) { fmpz_t gcd; /* GCD( den, c ) */ if (c == 0) { _fmpz_vec_zero(rpoly, len); fmpz_one(rden); return; } fmpz_init(gcd); fmpz_set_ui(gcd, c); fmpz_gcd(gcd, gcd, den); if (*gcd == WORD(1)) { _fmpz_vec_scalar_mul_ui(rpoly, poly, len, c); fmpz_set(rden, den); } else { ulong gcd2 = fmpz_get_ui(gcd); ulong c2 = c / gcd2; _fmpz_vec_scalar_mul_ui(rpoly, poly, len, c2); fmpz_fdiv_q_ui(rden, den, gcd2); } fmpz_clear(gcd); }
void fmpz_fac_ui(fmpz_t f, ulong n) { if (n < FLINT_NUM_TINY_FACTORIALS) fmpz_set_ui(f, flint_tiny_factorials[n]); else mpz_fac_ui(_fmpz_promote(f), n); }
void fmpz_tdiv_q_ui(fmpz_t f, const fmpz_t g, ulong h) { fmpz c1 = *g; ulong c2 = h; if (h == 0) { printf("Exception: division by zero in fmpz_tdiv_q_ui\n"); abort(); } if (!COEFF_IS_MPZ(c1)) /* g is small */ { if (c1 > 0) { fmpz_set_ui(f, c1 / c2); } else { ulong q = ((ulong) -c1) / c2; fmpz_set_si(f, - (long) q); } } else /* g is large */ { __mpz_struct *mpz_ptr = _fmpz_promote(f); mpz_tdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), c2); _fmpz_demote_val(f); /* division by h may result in small value */ } }
void _fmpq_poly_scalar_div_ui(fmpz * rpoly, fmpz_t rden, const fmpz * poly, const fmpz_t den, long len, ulong c) { if (c == 1UL) { if (rpoly != poly) _fmpz_vec_set(rpoly, poly, len); fmpz_set(rden, den); } else { fmpz_t d, fc; ulong ud; fmpz_init(d); fmpz_init(fc); _fmpz_vec_content(d, poly, len); fmpz_set_ui(fc, c); fmpz_gcd(d, d, fc); ud = fmpz_get_ui(d); /* gcd of d and c fits into a ulong */ _fmpz_vec_scalar_divexact_ui(rpoly, poly, len, ud); fmpz_mul_ui(rden, den, c / ud); fmpz_clear(d); fmpz_clear(fc); } }
void fmpz_mat_solve_cramer(fmpz * x, fmpz_t den, const fmpz_mat_t A, const fmpz * b) { long dim = A->r; switch (dim) { case 0: fmpz_set_ui(den, 1UL); break; case 1: fmpz_set(den, A->rows[0]); fmpz_set(x, b); break; case 2: _fmpz_mat_solve_cramer_2x2(x, den, A->rows, b); break; case 3: _fmpz_mat_solve_cramer_3x3(x, den, A->rows, b); break; default: printf("Exception: fmpz_mat_solve_cramer: dim > 3 not implemented"); abort(); } }
void _fmpq_poly_integral(fmpz * rpoly, fmpz_t rden, const fmpz * poly, const fmpz_t den, slong len) { slong k; fmpz_t t; fmpz_init(t); fmpz_one(t); for (k = len - 1; k > 0; k--) { fmpz_mul(rpoly + k, poly + k - 1, t); fmpz_mul_ui(t, t, k); } fmpz_mul(rden, den, t); fmpz_set_ui(t, UWORD(2)); for (k = 3; k < len; k++) { fmpz_mul(rpoly + k, rpoly + k, t); fmpz_mul_ui(t, t, k); } fmpz_zero(rpoly); _fmpq_poly_canonicalise(rpoly, rden, len); fmpz_clear(t); }
int fmpq_poly_set_str(fmpq_poly_t poly, const char * str) { int ans; long len; len = atol(str); if (len < 0) return -1; if (len == 0) { fmpq_poly_zero(poly); return 0; } fmpq_poly_fit_length(poly, len); ans = _fmpq_poly_set_str(poly->coeffs, poly->den, str); if (ans == 0) { _fmpq_poly_set_length(poly, len); _fmpq_poly_normalise(poly); } else { _fmpz_vec_zero(poly->coeffs, len); fmpz_set_ui(poly->den, 1); _fmpq_poly_set_length(poly, 0); } return ans; }
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 fmpz_fib_ui(fmpz_t f, ulong n) { if (n < NUM_SMALL_FIB) fmpz_set_ui(f, small_fib[n]); else mpz_fib_ui(_fmpz_promote(f), n); }
/* The floor+vec method *requires* n <= 1498 for floor(p(n)/2^64) to be equal to floor(T/2^64). It is faster up to n ~= 1200. With doubles, it is faster up to n ~= 500. */ void _partitions_fmpz_ui(fmpz_t res, ulong n, int use_doubles) { if (n < NUMBER_OF_SMALL_PARTITIONS) { fmpz_set_ui(res, partitions_lookup[n]); } else if (FLINT_BITS == 64 && (n < 500 || (!use_doubles && n < 1200))) { mp_ptr tmp = flint_malloc((n + 1) * sizeof(mp_limb_t)); if (n < 417) /* p(n) < 2^64 */ { partitions_vec(tmp, n + 1); fmpz_set_ui(res, tmp[n]); } else { arb_t x; arb_init(x); fmpz_set_ui(res, n); partitions_leading_fmpz(x, res, 4 * sqrt(n) - 50); arb_mul_2exp_si(x, x, -64); arb_floor(x, x, 4 * sqrt(n) - 50); if (arb_get_unique_fmpz(res, x)) { fmpz_mul_2exp(res, res, 64); partitions_vec(tmp, n + 1); fmpz_add_ui(res, res, tmp[n]); } else { flint_printf("warning: failed at %wu\n", n); fmpz_set_ui(res, n); partitions_fmpz_fmpz_hrr(res, res, use_doubles); } arb_clear(x); } flint_free(tmp); } else { fmpz_set_ui(res, n); partitions_fmpz_fmpz_hrr(res, res, use_doubles); } }
void fmpz_sub_ui(fmpz_t f, const fmpz_t g, ulong x) { fmpz c = *g; if (!COEFF_IS_MPZ(c)) /* coeff is small */ { mp_limb_t sum[2]; if (c < 0L) /* g negative, x positive, so difference is negative */ { add_ssaaaa(sum[1], sum[0], 0, -c, 0, x); if (sum[1] == 0) { fmpz_set_ui(f, sum[0]); /* result fits in 1 limb */ fmpz_neg(f, f); } else /* result takes two limbs */ { __mpz_struct * mpz_ptr; mpz_t temp; temp->_mp_d = sum; temp->_mp_size = -2; /* result is negative number minus negative number, hence negative */ mpz_ptr = _fmpz_promote(f); /* g has already been read */ mpz_set(mpz_ptr, temp); } } else /* coeff is non-negative, x non-negative */ { if (x < c) fmpz_set_ui(f, c - x); /* won't be negative and is smaller than c */ else { fmpz_set_ui(f, x - c); /* positive or zero */ fmpz_neg(f, f); } } } else { __mpz_struct *mpz_ptr, *mpz_ptr2; mpz_ptr = COEFF_TO_PTR(c); mpz_ptr2 = _fmpz_promote(f); /* g is already large */ mpz_sub_ui(mpz_ptr2, mpz_ptr, x); _fmpz_demote_val(f); /* cancellation may have occurred */ } }
ARingZZpFlint::ARingZZpFlint(size_t p0) : mCharac(p0) { nmod_init(&mModulus, p0); flint_randinit(mRandomState); fmpz_init(mFmpzCharac); fmpz_set_ui(mFmpzCharac, mCharac); mGenerator = n_primitive_root_prime(mCharac); }
int main(void) { long i; fmpz_t x; printf("abs_fits_ui...."); fflush(stdout); fmpz_init(x); fmpz_set_si(x, COEFF_MIN); check(x, 1); fmpz_set_si(x, COEFF_MAX); check(x, 1); fmpz_set_ui(x, ULONG_MAX); check(x, 1); fmpz_set_ui(x, ULONG_MAX); fmpz_neg(x, x); check(x, 1); fmpz_set_ui(x, ULONG_MAX); fmpz_add_ui(x, x, 1UL); check(x, 0); fmpz_neg(x, x); check(x, 0); for (i = 0; i < 1000; i++) { fmpz_set_ui(x, 1UL); fmpz_mul_2exp(x, x, i); check(x, i < FLINT_BITS); fmpz_neg(x, x); check(x, i < FLINT_BITS); } fmpz_clear(x); _fmpz_cleanup(); printf("PASS\n"); return 0; }
void fmpq_poly_set_ui(fmpq_poly_t poly, ulong x) { fmpq_poly_fit_length(poly, 1); fmpz_set_ui(poly->coeffs, x); fmpz_one(poly->den); _fmpq_poly_set_length(poly, 1); _fmpq_poly_normalise(poly); }
int main(void) { int i, result; flint_rand_t state; printf("is_zero...."); fflush(stdout); flint_randinit(state); /* Check zero vector */ for (i = 0; i < 10000; i++) { fmpz *a; long len = n_randint(state, 100); a = _fmpz_vec_init(len); _fmpz_vec_randtest(a, state, len, 200); _fmpz_vec_zero(a, len); result = (_fmpz_vec_is_zero(a, len)); if (!result) { printf("FAIL1:\n"); _fmpz_vec_print(a, len), printf("\n\n"); abort(); } _fmpz_vec_clear(a, len); } /* Check non-zero vector */ for (i = 0; i < 10000; i++) { fmpz *a; long len = n_randint(state, 100) + 1; a = _fmpz_vec_init(len); _fmpz_vec_randtest(a, state, len, 200); fmpz_set_ui(a + (len - 1), 1UL); result = (!_fmpz_vec_is_zero(a, len)); if (!result) { printf("FAIL2:\n"); _fmpz_vec_print(a, len), printf("\n\n"); abort(); } _fmpz_vec_clear(a, len); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("get_str... "); fflush(stdout); flint_randinit(state); for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t x; fmpq_t y; char *s, *t; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_TERSE); padic_init(x, ctx); fmpq_init(y); padic_randtest(x, state, ctx); _padic_get_fmpq(y, x, ctx); s = _padic_get_str(NULL, x, ctx); t = fmpq_get_str(NULL, 10, y); result = strcmp(s, t) == 0; if (!result) { printf("FAIL:\n\n"); printf("x = "), _padic_print(x, ctx), printf("\n"); printf("y = "), fmpq_clear(y), printf("\n"); abort(); } free(s); free(t); _padic_clear(x); fmpq_clear(y); padic_ctx_clear(ctx); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void fmpz_mat_randntrulike(fmpz_mat_t mat, flint_rand_t state, mp_bitcnt_t bits, ulong q) { long r, c, d, i, j, k; fmpz *h; r = mat->r; c = mat->c; d = r / 2; if ((c != r) || (c != 2 * d)) { printf ("Exception: fmpz_mat_randntrulike called on an ill-formed matrix\n"); abort(); } h = _fmpz_vec_init(d); for (i = 0; i < d; i++) fmpz_randbits(h + i, state, bits); for (i = 0; i < d; i++) { for (j = 0; j < i; j++) fmpz_zero(mat->rows[i] + j); fmpz_one(mat->rows[i] + i); for (j = i + 1; j < d; j++) fmpz_zero(mat->rows[i] + j); } for (i = d; i < r; i++) for (j = 0; j < d; j++) fmpz_zero(mat->rows[i] + j); for (i = d; i < r; i++) { for (j = d; j < i; j++) fmpz_zero(mat->rows[i] + j); fmpz_set_ui(mat->rows[i] + i, q); for (j = i + 1; j < c; j++) fmpz_zero(mat->rows[i] + j); } for (i = 0; i < d; i++) { for (j = d; j < c; j++) { k = j + i; while (k >= d) k -= d; fmpz_set(mat->rows[i] + j, h + k); } } _fmpz_vec_clear(h, d); }
void fmpz_tdiv_qr(fmpz_t f, fmpz_t s, const fmpz_t g, const fmpz_t h) { fmpz c1 = *g; fmpz c2 = *h; if (fmpz_is_zero(h)) { flint_printf("Exception: division by zero in fmpz_tdiv_qr\n"); abort(); } if (!COEFF_IS_MPZ(c1)) /* g is small */ { if (!COEFF_IS_MPZ(c2)) /* h is also small */ { fmpz q = c1 / c2; /* compute C quotient */ fmpz r = c1 - c2 * q; /* compute remainder */ fmpz_set_si(f, q); fmpz_set_si(s, r); } else /* h is large and g is small */ { fmpz_set_ui(f, WORD(0)); /* g is zero */ fmpz_set_si(s, c1); } } else /* g is large */ { __mpz_struct *mpz_ptr, *mpz_ptr2; _fmpz_promote(f); /* must not hang on to ptr whilst promoting s */ mpz_ptr2 = _fmpz_promote(s); mpz_ptr = COEFF_TO_PTR(*f); if (!COEFF_IS_MPZ(c2)) /* h is small */ { if (c2 > 0) /* h > 0 */ { flint_mpz_tdiv_qr_ui(mpz_ptr, mpz_ptr2, COEFF_TO_PTR(c1), c2); } else { flint_mpz_tdiv_qr_ui(mpz_ptr, mpz_ptr2, COEFF_TO_PTR(c1), -c2); mpz_neg(mpz_ptr, mpz_ptr); } } else /* both are large */ { mpz_tdiv_qr(mpz_ptr, mpz_ptr2, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2)); } _fmpz_demote_val(f); /* division by h may result in small value */ _fmpz_demote_val(s); /* division by h may result in small value */ } }
void fmpz_mat_unit(fmpz_mat_t mat) { long i; fmpz_mat_zero(mat); for (i = 0; i < FLINT_MIN(mat->r, mat->c); i++) fmpz_set_ui(mat->rows[i] + i, 1UL); }
void fmprb_fac_ui(fmprb_t x, ulong n, long prec) { fmpz_t t; fmpz_init(t); fmpz_set_ui(t, n); fmpz_add_ui(t, t, 1); fmprb_gamma_fmpz(x, t, prec); fmpz_clear(t); }
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()); }
void fmpz_cdiv_q(fmpz_t f, const fmpz_t g, const fmpz_t h) { fmpz c1 = *g; fmpz c2 = *h; if (fmpz_is_zero(h)) { printf("Exception: division by zero in fmpz_cdiv_q\n"); abort(); } if (!COEFF_IS_MPZ(c1)) /* g is small */ { if (!COEFF_IS_MPZ(c2)) /* h is also small */ { fmpz q = c1 / c2; /* compute C quotient */ fmpz r = c1 - c2 * q; /* compute remainder */ if (r && ((c2 ^ r) > 0L)) /* r != 0, c2 and r same sign */ ++q; fmpz_set_si(f, q); } else /* h is large and g is small */ { if ((c1 < 0L && fmpz_sgn(h) < 0) || (c1 > 0L && fmpz_sgn(h) > 0)) /* signs are the same */ fmpz_set_ui(f, 1); /* quotient is positive, round up to one */ else fmpz_zero(f); /* quotient is zero, or negative, round up to zero */ } } else /* g is large */ { __mpz_struct * mpz_ptr = _fmpz_promote(f); if (!COEFF_IS_MPZ(c2)) /* h is small */ { if (c2 > 0) /* h > 0 */ { mpz_cdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), c2); } else { mpz_fdiv_q_ui(mpz_ptr, COEFF_TO_PTR(c1), -c2); mpz_neg(mpz_ptr, mpz_ptr); } } else /* both are large */ { mpz_cdiv_q(mpz_ptr, COEFF_TO_PTR(c1), COEFF_TO_PTR(c2)); } _fmpz_demote_val(f); /* division by h may result in small value */ } }
void qsieve_ll_compute_C(qs_t qs_inf) { mp_limb_t A = qs_inf->A; mp_limb_t B = qs_inf->B; if ((mp_limb_signed_t) B < 0L) B = -B; fmpz_set_ui(qs_inf->C, B); fmpz_mul_ui(qs_inf->C, qs_inf->C, B); fmpz_sub(qs_inf->C, qs_inf->C, qs_inf->kn); fmpz_divexact_ui(qs_inf->C, qs_inf->C, A); }
int main(void) { int i, result; flint_rand_t state; printf("evaluate_mod...."); fflush(stdout); flint_randinit(state); /* Compare with evaluation over the integers */ for (i = 0; i < 10000; i++) { fmpz_t b, s; fmpz_poly_t f; mp_limb_t a, n, r; fmpz_poly_init(f); fmpz_poly_randtest(f, state, n_randint(state, 10), 20); n = n_randtest_not_zero(state); a = n_randint(state, n); fmpz_init(b); fmpz_init(s); fmpz_set_ui(b, a); r = fmpz_poly_evaluate_mod(f, a, n); fmpz_poly_evaluate_fmpz(s, f, b); result = (r == fmpz_mod_ui(s, s, n)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(f), printf("\n\n"); gmp_printf("a = %Mu\n\n", a); gmp_printf("n = %Mu\n\n", n); gmp_printf("r = %Mu\n\n", r); printf("s = "), fmpz_print(s), printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_clear(b); fmpz_clear(s); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }