ulong arb_fmpz_poly_deflation(const fmpz_poly_t input) { slong i, coeff, deflation; if (input->length <= 1) return input->length; coeff = 1; while (fmpz_is_zero(input->coeffs + coeff)) coeff++; deflation = n_gcd(input->length - 1, coeff); while ((deflation > 1) && (coeff + deflation < input->length)) { for (i = 0; i < deflation - 1; i++) { coeff++; if (!fmpz_is_zero(input->coeffs + coeff)) deflation = n_gcd(coeff, deflation); } if (i == deflation - 1) coeff++; } return deflation; }
ulong z_gcd(long a, long b) { ulong ua = FLINT_ABS(a); ulong ub = FLINT_ABS(b); return (ua >= ub) ? n_gcd(ua, ub) : n_gcd(ub, ua); }
static ulong do_gcd(ulong q1, ulong q2) { ulong n, q, k; for (n = 0, q = q1; q <= q2; q++) for (k = 1; k < q; k++) n += (n_gcd(k, q) == 1); return n; }
void dlog_vec_trivial(ulong *v, ulong nv, ulong a, ulong va, const nmod_t mod, ulong na, const nmod_t order) { ulong k; dlog_precomp_t pre; dlog_precomp_n_init(pre, a, mod.n, na, 50); for (k = 1; k < nv; k++) if (n_gcd(k, mod.n) > 1) v[k] = DLOG_NOT_FOUND; else v[k] = dlog_precomp(pre, k % mod.n); dlog_precomp_clear(pre); }
void sec_init(sec_t * c, slong m, acb_srcptr x, slong d) { slong k; c->m = m; c->d = d; c->delta = n_gcd(m,d); c->g = ((m-1)*(d-1) - c->delta + 1)/ 2; c->roots = _acb_vec_init(d); for (k = 0; k < d; k++) acb_set(c->roots + k, x + k); }
void acb_dirichlet_jacobi_sum_naive(acb_t res, const dirichlet_group_t G, const dirichlet_char_t chi1, const dirichlet_char_t chi2, slong prec) { ulong k1, k2, m1, m2, g, e, m; ulong * v1, * v2; slong *v; nmod_t expo; acb_t z; v1 = flint_malloc(G->q * sizeof(ulong)); v2 = flint_malloc(G->q * sizeof(ulong)); dirichlet_vec_set_null(v1, G, G->q); dirichlet_chi_vec_loop(v1, G, chi1, G->q); dirichlet_vec_set_null(v2, G, G->q); dirichlet_chi_vec_loop(v2, G, chi2, G->q); nmod_init(&expo, G->expo); m1 = dirichlet_order_char(G, chi1); m2 = dirichlet_order_char(G, chi2); g = m1 * m2 / n_gcd(m1, m2); m = G->expo / g; v = flint_malloc(g * sizeof(slong)); for (e = 0; e < g; e++) v[e] = 0; for (k1 = 2, k2 = G->q - 1; k2 > 1; k1++, k2--) { if (v1[k1] == DIRICHLET_CHI_NULL || v2[k2] == DIRICHLET_CHI_NULL) continue; e = nmod_add(v1[k1], v2[k2], expo) / m; v[e]++; } acb_init(z); acb_unit_root(z, g, prec); acb_dirichlet_si_poly_evaluate(res, v, g, z, prec); acb_clear(z); flint_free(v); flint_free(v2); flint_free(v1); }
int main(void) { int i, result; flint_rand_t state; printf("xgcd...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 100000; i++) { mp_limb_t a, b, c, g, bits1, bits2, bits3, ph, pl, qh, ql; mp_limb_t s, t; bits1 = n_randint(state, FLINT_BITS-1) + 1; bits2 = n_randint(state, bits1) + 1; bits3 = n_randint(state, FLINT_BITS - bits1) + 1; do { a = n_randbits(state, bits1); b = n_randbits(state, bits2); } while ((n_gcd(a, b) != 1UL) || (b > a)); c = n_randbits(state, bits3); g = n_xgcd(&s, &t, a*c, b*c); umul_ppmm(ph, pl, a*c, s); umul_ppmm(qh, ql, b*c, t); sub_ddmmss(ph, pl, ph, pl, qh, ql); result = ((g == c) && (ph == 0UL) && (pl == c)); if (!result) { printf("FAIL:\n"); printf("a = %lu, b = %lu, c = %lu, g = %lu, s = %lu, t = %lu\n", a, b, c, g, s, t); abort(); } } flint_randclear(state); printf("PASS\n"); return 0; }
static void dirichlet_group_lift_generators(dirichlet_group_t G) { slong k; dirichlet_prime_group_struct * P = G->P; G->expo = G->phi_q = 1; if (G->neven) { G->phi_q = G->q_even / 2; G->expo = P[G->neven - 1].phi.n; } for (k = G->neven; k < G->num; k++) { G->phi_q *= P[k].phi.n; G->expo *= P[k].phi.n / n_gcd(G->expo, P[k].p - 1); } for (k = 0; k < G->num; k++) { nmod_t pe; ulong qpe, v; G->PHI[k] = G->expo / G->P[k].phi.n; /* lift generators mod q */ /* u * p^e + v * q/p^e = 1 -> g mod q = 1 + (g-1) * v*(q/p^e) */ pe = G->P[k].pe; qpe = G->q / pe.n; if (G->q < G->P[k].pe.n) { flint_printf("lift generator %wu from %wu to %wu e=%wu\n", G->P[k].g, G->P[k].pe.n, G->q, G->P[k].e); } v = nmod_inv(qpe % pe.n, pe); /* no overflow since v * qpe < q */ G->generators[k] = (1 + (G->P[k].g-1) * v * qpe) % G->q; } }
void _arith_cos_minpoly(fmpz * coeffs, slong d, ulong n) { slong i, j; fmpz * alpha; fmpz_t half; mpfr_t t, u; mp_bitcnt_t prec; slong exp; if (n <= MAX_32BIT) { for (i = 0; i <= d; i++) fmpz_set_si(coeffs + i, lookup_table[n - 1][i]); return; } /* Direct formula for odd primes > 3 */ if (n_is_prime(n)) { slong s = (n - 1) / 2; switch (s % 4) { case 0: fmpz_set_si(coeffs, WORD(1)); fmpz_set_si(coeffs + 1, -s); break; case 1: fmpz_set_si(coeffs, WORD(1)); fmpz_set_si(coeffs + 1, s + 1); break; case 2: fmpz_set_si(coeffs, WORD(-1)); fmpz_set_si(coeffs + 1, s); break; case 3: fmpz_set_si(coeffs, WORD(-1)); fmpz_set_si(coeffs + 1, -s - 1); break; } for (i = 2; i <= s; i++) { slong b = (s - i) % 2; fmpz_mul2_uiui(coeffs + i, coeffs + i - 2, s+i-b, s+2-b-i); fmpz_divexact2_uiui(coeffs + i, coeffs + i, i, i-1); fmpz_neg(coeffs + i, coeffs + i); } return; } prec = magnitude_bound(d) + 5 + FLINT_BIT_COUNT(d); alpha = _fmpz_vec_init(d); fmpz_init(half); mpfr_init2(t, prec); mpfr_init2(u, prec); fmpz_one(half); fmpz_mul_2exp(half, half, prec - 1); mpfr_const_pi(t, prec); mpfr_div_ui(t, t, n, MPFR_RNDN); for (i = j = 0; j < d; i++) { if (n_gcd(n, i) == 1) { mpfr_mul_ui(u, t, 2 * i, MPFR_RNDN); mpfr_cos(u, u, MPFR_RNDN); mpfr_neg(u, u, MPFR_RNDN); exp = mpfr_get_z_2exp(_fmpz_promote(alpha + j), u); _fmpz_demote_val(alpha + j); fmpz_mul_or_div_2exp(alpha + j, alpha + j, exp + prec); j++; } } balanced_product(coeffs, alpha, d, prec); /* Scale and round */ for (i = 0; i < d + 1; i++) { slong r = d; if ((n & (n - 1)) == 0) r--; fmpz_mul_2exp(coeffs + i, coeffs + i, r); fmpz_add(coeffs + i, coeffs + i, half); fmpz_fdiv_q_2exp(coeffs + i, coeffs + i, prec); } fmpz_clear(half); mpfr_clear(t); mpfr_clear(u); _fmpz_vec_clear(alpha, d); }
int main() { slong iter; flint_rand_t state; flint_printf("cos_pi_fmpq_algebraic...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t c1, c2; ulong p, q, g; slong prec; prec = 2 + n_randint(state, 5000); q = 1 + n_randint(state, 500); p = n_randint(state, q / 2 + 1); g = n_gcd(q, p); q /= g; p /= g; arb_init(c1); arb_init(c2); _arb_cos_pi_fmpq_algebraic(c1, p, q, prec); arb_const_pi(c2, prec); arb_mul_ui(c2, c2, p, prec); arb_div_ui(c2, c2, q, prec); arb_cos(c2, c2, prec); if (!arb_overlaps(c1, c2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("p/q = %wu/%wu", p, q); flint_printf("\n\n"); flint_printf("c1 = "); arb_printd(c1, 15); flint_printf("\n\n"); flint_printf("c2 = "); arb_printd(c2, 15); flint_printf("\n\n"); abort(); } if (arb_rel_accuracy_bits(c1) < prec - 2) { flint_printf("FAIL: accuracy\n\n"); flint_printf("p/q = %wu/%wu", p, q); flint_printf("\n\n"); flint_printf("prec=%wd eff=%wd\n", prec, arb_rel_accuracy_bits(c1)); flint_printf("c1 = "); arb_printd(c1, 15); flint_printf("\n\n"); flint_printf("c2 = "); arb_printd(c2, 15); flint_printf("\n\n"); abort(); } arb_clear(c1); arb_clear(c2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void acb_modular_hilbert_class_poly(fmpz_poly_t res, slong D) { slong i, a, b, c, ac, h, qbf_alloc, qbf_len, prec; slong * qbf; double lgh; if (D >= 0 || ((D & 3) > 1)) { fmpz_poly_zero(res); return; } qbf_alloc = qbf_len = 0; qbf = NULL; b = D & 1; h = 0; /* Cohen algorithm 5.3.5 */ do { ac = (b*b - D) / 4; a = FLINT_MAX(b, 1); do { if (ac % a == 0 && n_gcd_full(n_gcd(a, b), ac/a) == 1) { c = ac / a; if (qbf_len >= qbf_alloc) { qbf_alloc = FLINT_MAX(4, FLINT_MAX(qbf_len + 1, qbf_alloc * 2)); qbf = flint_realloc(qbf, qbf_alloc * 3 * sizeof(slong)); } if (a == b || a*a == ac || b == 0) { qbf[3 * qbf_len + 0] = a; qbf[3 * qbf_len + 1] = b; qbf[3 * qbf_len + 2] = c; h += 1; } else { /* -b indicates that we have both b and -b */ qbf[3 * qbf_len + 0] = a; qbf[3 * qbf_len + 1] = -b; qbf[3 * qbf_len + 2] = c; h += 2; } qbf_len++; } a++; } while (a*a <= ac); b += 2; } while (3*b*b <= -D); /* Estimate precision - see p.7 in http://hal.inria.fr/inria-00001040 */ lgh = 0.0; for (i = 0; i < qbf_len; i++) { if (qbf[3 * i + 1] < 0) lgh += 2.0 / qbf[3 * i]; else lgh += 1.0 / qbf[3 * i]; } lgh = 3.141593 * sqrt(-D) * lgh; #if 0 lgh += 3.012 * h; prec = lgh * 1.442696; prec = prec + 10; #else prec = lgh * 1.442696; /* heuristic, but more accurate */ prec = prec * 1.005 + 20; #endif while (!_acb_modular_hilbert_class_poly(res, D, qbf, qbf_len, prec)) { flint_printf("hilbert_class_poly failed at %wd bits of precision\n", prec); prec = prec * 1.2 + 10; } flint_free(qbf); }
int main() { slong iter; flint_rand_t state; flint_printf("chi...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { acb_t zn1, zn2, zn1n2, zn1zn2; dirichlet_group_t G; dirichlet_char_t chi; ulong q, m, n1, n2, iter2; int res; q = 1 + n_randint(state, 1000); dirichlet_group_init(G, q); dirichlet_char_init(chi, G); acb_init(zn1); acb_init(zn2); acb_init(zn1n2); acb_init(zn1zn2); /* check chi(n1) chi(n2) = chi(n1 n2) */ for (iter2 = 0; iter2 < 10; iter2++) { do { m = 1 + n_randint(state, q); } while (n_gcd(q, m) != 1); dirichlet_char_log(chi, G, m); n1 = n_randint(state, 1000); n2 = n_randint(state, 1000); acb_dirichlet_chi(zn1, G, chi, n1, 53); acb_dirichlet_pairing(zn2, G, m, n1, 53); if (!acb_overlaps(zn1, zn2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("m = %wu\n\n", m); flint_printf("n = %wu\n\n", n1); flint_printf("char = "); acb_printd(zn1, 15); flint_printf("\n\n"); flint_printf("pairing = "); acb_printd(zn2, 15); flint_printf("\n\n"); dirichlet_char_print(G, chi); dirichlet_char_log(chi, G, m); flint_printf("log(m) = "); dirichlet_char_print(G, chi); dirichlet_char_log(chi, G, n1); flint_printf("log(n1) = "); dirichlet_char_print(G, chi); flint_abort(); } acb_dirichlet_pairing(zn2, G, m, n2, 53); acb_dirichlet_pairing(zn1n2, G, m, n1 * n2, 53); acb_mul(zn1zn2, zn1, zn2, 53); if (!acb_overlaps(zn1n2, zn1zn2)) { flint_printf("FAIL: overlap\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("m = %wu\n\n", m); flint_printf("n1 = %wu\n\n", n1); flint_printf("n2 = %wu\n\n", n2); flint_printf("zn1 = "); acb_printd(zn1, 15); flint_printf("\n\n"); flint_printf("zn2 = "); acb_printd(zn2, 15); flint_printf("\n\n"); flint_printf("zn1n2 = "); acb_printd(zn1n2, 15); flint_printf("\n\n"); flint_printf("zn1zn2 = "); acb_printd(zn1zn2, 15); flint_printf("\n\n"); flint_abort(); } } if (iter % 10 == 0) { /* check orthogonality */ acb_zero(zn1); n1 = n_randint(state, 1000); for (m = 1; m <= q; m++) { if (n_gcd(q, m) == 1) { acb_dirichlet_pairing(zn2, G, m, n1, 53); acb_add(zn1, zn1, zn2, 53); } } if (n1 % q == 1 % q) res = arb_contains_si(acb_realref(zn1), n_euler_phi(q)) && arb_contains_zero(acb_imagref(zn1)); else res = acb_contains_zero(zn1); if (!res) { flint_printf("FAIL: orthogonality\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("phi = %wu\n\n", n_euler_phi(q)); flint_printf("n1 = %wu\n\n", n1); flint_printf("zn1 = "); acb_printd(zn1, 15); flint_printf("\n\n"); flint_abort(); } } dirichlet_group_clear(G); dirichlet_char_clear(chi); acb_clear(zn1); acb_clear(zn2); acb_clear(zn1n2); acb_clear(zn1zn2); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter, bits; flint_rand_t state; flint_printf("properties...."); fflush(stdout); flint_randinit(state); for (bits = 5; bits <= 30; bits += 5) { for (iter = 0; iter < 50; iter++) { dirichlet_group_t G; dirichlet_char_t chi, psi; ulong q, iter2; q = 2 + n_randint(state, 1 << bits); dirichlet_group_init(G, q); dirichlet_char_init(chi, G); dirichlet_char_init(psi, G); /* check number char properties */ for (iter2 = 0; iter2 < 100; iter2++) { ulong m, n; ulong p1, p2, pairing, cm, cn, q2, q3; dirichlet_group_t G2, G3; dirichlet_char_t chi2, chi3; if (iter2 == 50) dirichlet_group_dlog_precompute(G, 5); /* one random character */ do m = n_randint(state, q); while (n_gcd(q, m) > 1); dirichlet_char_log(chi, G, m); p1 = dirichlet_order_ui(G, m); p2 = dirichlet_order_char(G, chi); check_eq(p1, p2, q, m, "order m", "order chi"); p1 = dirichlet_conductor_ui(G, m); p2 = dirichlet_conductor_char(G, chi); check_eq(p1, p2, q, m, "conductor m", "conductor chi"); p1 = dirichlet_parity_ui(G, m); p2 = dirichlet_parity_char(G, chi); check_eq(p1, p2, q, m, "parity m", "parity chi"); p1 = dirichlet_char_is_real(G, chi); p2 = (dirichlet_order_char(G, chi) <= 2); check_eq(p1, p2, q, m, "is_real", "(order <= 2)"); /* check index */ p1 = dirichlet_index_char(G, chi); dirichlet_char_index(psi, G, p1); if (!dirichlet_char_eq_deep(G, chi, psi)) { flint_printf("FAIL: index\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("m = %wu\n\n", m); flint_printf("chi = "); dirichlet_char_print(G, chi); flint_printf("\n\nindex(chi) = %wu\n\n", p1); flint_printf("psi(index) = %wu\n\n", psi->n); flint_printf("psi = "); dirichlet_char_print(G, psi); flint_printf("\n\n"); abort(); } /* lift to higher modulus */ q2 = q * (1 + n_randint(state, 100)); dirichlet_group_init(G2, q2); dirichlet_char_init(chi2, G2); dirichlet_char_lift(chi2, G2, chi, G); p1 = dirichlet_conductor_char(G, chi); p2 = dirichlet_conductor_char(G2, chi2); check_eq(p1, p2, q, m, "conductor chi", "conductor lift"); p1 = dirichlet_order_char(G, chi); p2 = dirichlet_order_char(G2, chi2); check_eq(p1, p2, q, m, "order chi", "order lift"); /* and lower */ dirichlet_char_lower(psi, G, chi2, G2); if (!dirichlet_char_eq_deep(G, chi, psi)) { flint_printf("FAIL: lift and lower back\n\n"); flint_printf("q = %wu\n\nchi = ", q); dirichlet_char_print(G, chi); flint_printf("\n\nq2 = %wu\n\nchi2 = ", q2); dirichlet_char_print(G2, chi2); flint_printf("\n\nq = %wu\n\npsi = ", q); dirichlet_char_print(G, psi); flint_printf("\n\n"); abort(); } q3 = dirichlet_conductor_char(G, chi) * random_divisor(state, G); q3 = n_gcd(q, q3); dirichlet_group_init(G3, q3); dirichlet_char_init(chi3, G3); dirichlet_char_lower(chi3, G3, chi2, G2); p1 = dirichlet_conductor_char(G, chi); p2 = dirichlet_conductor_char(G3, chi3); check_eq(p1, p2, q, m, "conductor chi", "conductor lower"); p1 = dirichlet_order_char(G, chi); p2 = dirichlet_order_char(G3, chi3); check_eq(p1, p2, q, m, "order chi", "order lower"); dirichlet_char_clear(chi3); dirichlet_group_clear(G3); dirichlet_char_clear(chi2); dirichlet_group_clear(G2); /* another random character */ do n = n_randint(state, q); while (n_gcd(q, n) > 1); dirichlet_char_log(psi, G, n); pairing = dirichlet_pairing(G, m, n); cn = dirichlet_chi(G, chi, n); cm = dirichlet_chi(G, psi, m); if (pairing != cn || pairing != cm) { flint_printf("FAIL: pairing\n\n"); flint_printf("q = %wu\n\n", q); flint_printf("m = %wu\n\n", m); flint_printf("n = %wu\n\n", n); flint_printf("chi(m,n) = %wu\n\n", pairing); flint_printf("chi(m)(n) = %wu\n\n", cn); flint_printf("chi(n)(m) = %wu\n\n", cm); abort(); } } dirichlet_group_dlog_clear(G); dirichlet_char_clear(chi); dirichlet_char_clear(psi); dirichlet_group_clear(G); } } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; flint_rand_t state; printf("powmod_precomp...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 100000; i++) { mp_limb_t a, d, r1, r2, bits; mpz_t a_m, d_m, r2_m; mp_limb_signed_t exp; double dpre; mpz_init(a_m); mpz_init(d_m); mpz_init(r2_m); bits = n_randint(state, FLINT_D_BITS) + 1; d = n_randbits(state, bits); do { a = n_randint(state, d); } while (n_gcd(d, a) != 1UL); exp = n_randtest(state); dpre = n_precompute_inverse(d); r1 = n_powmod_precomp(a, exp, d, dpre); mpz_set_ui(a_m, a); mpz_set_ui(d_m, d); if (exp < 0L) { mpz_powm_ui(r2_m, a_m, -exp, d_m); mpz_invert(r2_m, r2_m, d_m); } else mpz_powm_ui(r2_m, a_m, exp, d_m); r2 = mpz_get_ui(r2_m); result = (r1 == r2); if (!result) { printf("FAIL:\n"); printf("a = %lu, exp = %ld, d = %lu\n", a, exp, d); printf("r1 = %lu, r2 = %lu\n", r1, r2); abort(); } mpz_clear(a_m); mpz_clear(d_m); mpz_clear(r2_m); } flint_randclear(state); printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; flint_randinit(state); printf("make_monic...."); fflush(stdout); /* Check new leading coeff = gcd old leading coeff and modulus */ for (i = 0; i < 10000; i++) { nmod_poly_t a, b; mp_limb_t n = n_randtest_not_zero(state); mp_limb_t l; nmod_poly_init(a, n); nmod_poly_init(b, n); if (n == 1) continue; do { nmod_poly_randtest(a, state, n_randint(state, 100) + 1); } while (a->length == 0); nmod_poly_make_monic(b, a); l = n_gcd(a->mod.n, a->coeffs[a->length - 1]); result = (l == b->coeffs[b->length - 1]); if (!result) { printf("FAIL:\n"); printf("l = %lu, a->lead = %ld, n = %lu\n", l, a->coeffs[a->length - 1], a->mod.n); nmod_poly_print(a), printf("\n\n"); nmod_poly_print(b), printf("\n\n"); abort(); } nmod_poly_clear(a); nmod_poly_clear(b); } /* test aliasing */ for (i = 0; i < 10000; i++) { nmod_poly_t a; mp_limb_t n = n_randtest_not_zero(state); mp_limb_t l; nmod_poly_init(a, n); if (n == 1) continue; do { nmod_poly_randtest(a, state, n_randint(state, 100) + 1); } while (a->length == 0); l = n_gcd(a->mod.n, a->coeffs[a->length - 1]); nmod_poly_make_monic(a, a); result = (l == a->coeffs[a->length - 1]); if (!result) { printf("FAIL:\n"); printf("l = %lu, a->lead = %ld, n = %lu\n", l, a->coeffs[a->length - 1], a->mod.n); nmod_poly_print(a), printf("\n\n"); abort(); } nmod_poly_clear(a); } flint_randclear(state); printf("PASS\n"); return 0; }