/* Compute a pure ratio P2(k)/Q2(k) for the term A(k)/B(k) * [P(1)P(2)...P(k)] / [Q(1)Q(2)...Q(k)] */ void hypgeom_standardize(fmpz_poly_t P2, fmpz_poly_t Q2, const fmpz_poly_t A, const fmpz_poly_t B, const fmpz_poly_t P, const fmpz_poly_t Q) { fmpz_t s; fmpz_poly_t T; fmpz_init(s); fmpz_poly_init(T); fmpz_set_si(s, -WORD(1)); /* P = A * B(k-1) * P */ fmpz_poly_taylor_shift(T, B, s); fmpz_poly_mul(P2, A, T); fmpz_poly_mul(P2, P2, P); /* Q = B * A(k-1) * Q */ fmpz_poly_taylor_shift(T, A, s); fmpz_poly_mul(Q2, B, T); fmpz_poly_mul(Q2, Q2, Q); fmpz_clear(s); fmpz_poly_clear(T); }
int main() { setbuf(stdout, NULL); for (long l = 256; l <= 16384; l *= 2) { // for (long n = 256; n <= 16384; n *= 2) { for (long idx = 0; idx < 13; idx ++) { long n = 256*(1L << idx/2); if (idx & 1) n += n/2; SetSeed((ZZ(l) << 64) + ZZ(n)); ZZX a, b, c; a.SetLength(n); for (long i = 0; i < n; i++) RandomBits(a[i], l); a.normalize(); b.SetLength(n); for (long i = 0; i < n; i++) RandomBits(b[i], l); b.normalize(); double t; mul(c, a, b); long iter = 1; do { t = GetTime(); for (long i = 0; i < iter; i++) mul(c, a, b); t = GetTime() - t; iter *= 2; } while (t < 3); iter /= 2; t = GetTime(); for (long i = 0; i < iter; i++) mul(c, a, b); t = GetTime()-t; double NTLTime = t; FlintZZX f_a(a), f_b(b), f_c(c); fmpz_poly_mul(f_c.value, f_a.value, f_b.value); t = GetTime(); for (long i = 0; i < iter; i++) fmpz_poly_mul(f_c.value, f_a.value, f_b.value); t = GetTime()-t; double FlintTime = t; printf("%8.2f", FlintTime/NTLTime); } printf("\n"); } }
void fmpz_poly_pow(fmpz_poly_t res, const fmpz_poly_t poly, ulong e) { const long len = poly->length; long rlen; if ((len < 2) | (e < 3UL)) { if (e == 0UL) fmpz_poly_set_ui(res, 1); else if (len == 0) fmpz_poly_zero(res); else if (len == 1) { fmpz_poly_fit_length(res, 1); fmpz_pow_ui(res->coeffs, poly->coeffs, e); _fmpz_poly_set_length(res, 1); } else if (e == 1UL) fmpz_poly_set(res, poly); else /* e == 2UL */ fmpz_poly_mul(res, poly, poly); return; } rlen = (long) e * (len - 1) + 1; if (res != poly) { fmpz_poly_fit_length(res, rlen); _fmpz_poly_set_length(res, rlen); _fmpz_poly_pow(res->coeffs, poly->coeffs, len, e); } else { fmpz_poly_t t; fmpz_poly_init2(t, rlen); _fmpz_poly_set_length(t, rlen); _fmpz_poly_pow(t->coeffs, poly->coeffs, len, e); fmpz_poly_swap(res, t); fmpz_poly_clear(t); } }
void fmpz_poly_q_mul(fmpz_poly_q_t rop, const fmpz_poly_q_t op1, const fmpz_poly_q_t op2) { if (fmpz_poly_q_is_zero(op1) || fmpz_poly_q_is_zero(op2)) { fmpz_poly_q_zero(rop); return; } if (op1 == op2) { fmpz_poly_pow(rop->num, op1->num, 2); fmpz_poly_pow(rop->den, op1->den, 2); return; } if (rop == op1 || rop == op2) { fmpz_poly_q_t t; fmpz_poly_q_init(t); fmpz_poly_q_mul(t, op1, op2); fmpz_poly_q_swap(rop, t); fmpz_poly_q_clear(t); return; } /* From here on, we may assume that rop, op1 and op2 refer to distinct objects in memory, and that op1 and op2 are non-zero */ /* Polynomials? */ if (fmpz_poly_length(op1->den) == 1 && fmpz_poly_length(op2->den) == 1) { const slong len1 = fmpz_poly_length(op1->num); const slong len2 = fmpz_poly_length(op2->num); fmpz_poly_fit_length(rop->num, len1 + len2 - 1); if (len1 >= len2) { _fmpq_poly_mul(rop->num->coeffs, rop->den->coeffs, op1->num->coeffs, op1->den->coeffs, len1, op2->num->coeffs, op2->den->coeffs, len2); } else { _fmpq_poly_mul(rop->num->coeffs, rop->den->coeffs, op2->num->coeffs, op2->den->coeffs, len2, op1->num->coeffs, op1->den->coeffs, len1); } _fmpz_poly_set_length(rop->num, len1 + len2 - 1); _fmpz_poly_set_length(rop->den, 1); return; } fmpz_poly_gcd(rop->num, op1->num, op2->den); if (fmpz_poly_is_one(rop->num)) { fmpz_poly_gcd(rop->den, op2->num, op1->den); if (fmpz_poly_is_one(rop->den)) { fmpz_poly_mul(rop->num, op1->num, op2->num); fmpz_poly_mul(rop->den, op1->den, op2->den); } else { fmpz_poly_div(rop->num, op2->num, rop->den); fmpz_poly_mul(rop->num, op1->num, rop->num); fmpz_poly_div(rop->den, op1->den, rop->den); fmpz_poly_mul(rop->den, rop->den, op2->den); } } else { fmpz_poly_gcd(rop->den, op2->num, op1->den); if (fmpz_poly_is_one(rop->den)) { fmpz_poly_div(rop->den, op2->den, rop->num); fmpz_poly_mul(rop->den, op1->den, rop->den); fmpz_poly_div(rop->num, op1->num, rop->num); fmpz_poly_mul(rop->num, rop->num, op2->num); } else { fmpz_poly_t t, u; fmpz_poly_init(t); fmpz_poly_init(u); fmpz_poly_div(t, op1->num, rop->num); fmpz_poly_div(u, op2->den, rop->num); fmpz_poly_div(rop->num, op2->num, rop->den); fmpz_poly_mul(rop->num, t, rop->num); fmpz_poly_div(rop->den, op1->den, rop->den); fmpz_poly_mul(rop->den, rop->den, u); fmpz_poly_clear(t); fmpz_poly_clear(u); } } }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("pseudo_div...."); fflush(stdout); /* Check r = a - q * b has small degree, no aliasing */ for (i = 0; i < 200 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, q, r, prod; fmpz_t p; ulong d; fmpz_init(p); fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_init(prod); fmpz_poly_randtest(a, state, n_randint(state, 100), 50); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 50); fmpz_poly_pseudo_div(q, &d, a, b); fmpz_poly_mul(prod, q, b); fmpz_pow_ui(p, b->coeffs + b->length - 1, d); fmpz_poly_scalar_mul_fmpz(a, a, p); fmpz_poly_sub(r, a, prod); result = (fmpz_poly_length(r) < fmpz_poly_length(b)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(prod), flint_printf("\n\n"); fmpz_poly_print(q), flint_printf("\n\n"); fmpz_poly_print(r), flint_printf("\n\n"); abort(); } fmpz_clear(p); fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); fmpz_poly_clear(r); fmpz_poly_clear(prod); } /* Check q and a alias */ for (i = 0; i < 50 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, q; ulong d; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_randtest(a, state, n_randint(state, 100), 50); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 50); fmpz_poly_pseudo_div(q, &d, a, b); fmpz_poly_pseudo_div(a, &d, a, b); result = (fmpz_poly_equal(a, q)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(q), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); } /* Check q and b alias */ for (i = 0; i < 50 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, q; ulong d; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_randtest(a, state, n_randint(state, 100), 50); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 50); fmpz_poly_pseudo_div(q, &d, a, b); fmpz_poly_pseudo_div(b, &d, a, b); result = (fmpz_poly_equal(b, q)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(q), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("mul...."); fflush(stdout); flint_randinit(state); /* Check aliasing of a and b */ for (i = 0; i < 2000; i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(b, state, n_randint(state, 50), 500); fmpz_poly_randtest(c, state, n_randint(state, 50), 500); fmpz_poly_mul(a, b, c); fmpz_poly_mul(b, b, c); result = (fmpz_poly_equal(a, b)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(b), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Check aliasing of a and c */ for (i = 0; i < 2000; i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(b, state, n_randint(state, 50), 500); fmpz_poly_randtest(c, state, n_randint(state, 50), 500); fmpz_poly_mul(a, b, c); fmpz_poly_mul(c, b, c); result = (fmpz_poly_equal(a, c)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(c), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Check (b*c)+(b*d) = b*(c+d) */ for (i = 0; i < 2000; i++) { fmpz_poly_t a1, a2, b, c, d; fmpz_poly_init(a1); fmpz_poly_init(a2); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_init(d); fmpz_poly_randtest(b, state, n_randint(state, 100), 500); fmpz_poly_randtest(c, state, n_randint(state, 100), 500); fmpz_poly_randtest(d, state, n_randint(state, 100), 500); fmpz_poly_mul(a1, b, c); fmpz_poly_mul(a2, b, d); fmpz_poly_add(a1, a1, a2); fmpz_poly_add(c, c, d); fmpz_poly_mul(a2, b, c); result = (fmpz_poly_equal(a1, a2)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a1), printf("\n\n"); fmpz_poly_print(a2), printf("\n\n"); abort(); } fmpz_poly_clear(a1); fmpz_poly_clear(a2); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(d); } /* Check _fmpz_poly_mul directly */ for (i = 0; i < 2000; i++) { long len1, len2; fmpz_poly_t a, b, out1, out2; len1 = n_randint(state, 100) + 1; len2 = n_randint(state, 100) + 1; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(out1); fmpz_poly_init(out2); fmpz_poly_randtest(a, state, len1, 200); fmpz_poly_randtest(b, state, len2, 200); fmpz_poly_mul(out1, a, b); fmpz_poly_fit_length(a, a->alloc + n_randint(state, 10)); fmpz_poly_fit_length(b, b->alloc + n_randint(state, 10)); a->length = a->alloc; b->length = b->alloc; fmpz_poly_fit_length(out2, a->length + b->length - 1); if (a->length >= b->length) _fmpz_poly_mul(out2->coeffs, a->coeffs, a->length, b->coeffs, b->length); else _fmpz_poly_mul(out2->coeffs, b->coeffs, b->length, a->coeffs, a->length); _fmpz_poly_set_length(out2, a->length + b->length - 1); _fmpz_poly_normalise(out2); result = (fmpz_poly_equal(out1, out2)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(out1), printf("\n\n"); fmpz_poly_print(out2), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(out1); fmpz_poly_clear(out2); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("resultant_modular_div...."); fflush(stdout); /* Just one specific test */ { fmpz_poly_t f, g; fmpz_t a, b, div; slong nbits; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_init(a); fmpz_init(b); fmpz_init(div); fmpz_poly_set_str(f, "11 -15 -2 -2 17 0 0 6 0 -5 1 -1"); fmpz_poly_set_str(g, "9 2 1 1 1 1 1 0 -1 -2"); fmpz_set_str(div, "11", 10); nbits = 42; fmpz_poly_resultant_modular_div(a, f, g, div, nbits); /* The result is -44081924855067 = -4007447714097 * 11 * We supply 11 and the missing divisor is less then 2^35 */ fmpz_set_str(b, "-4007447714097", 10); result = (fmpz_equal(a, b)); if (!result) { flint_printf("FAIL:\n"); flint_printf("f(x) = "), fmpz_poly_print_pretty(f, "x"), flint_printf("\n\n"); flint_printf("g(x) = "), fmpz_poly_print_pretty(g, "x"), flint_printf("\n\n"); flint_printf("res(f, h)/div = "), fmpz_print(b), flint_printf("\n\n"); flint_printf("res_mod_div(f, h) = "), fmpz_print(a), flint_printf("\n\n"); flint_printf("divr = "), fmpz_print(div), flint_printf("\n\n"); flint_printf("bitsbound = %wd", nbits), flint_printf("\n\n"); abort(); } fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_clear(a); fmpz_clear(b); fmpz_clear(div); } /* Check that R(fg, h) = R(f, h) R(g, h) */ for (i = 0; i < 100; i++) { fmpz_t a, b, c, d; fmpz_poly_t f, g, h, p; slong nbits; fmpz_init(a); fmpz_init(b); fmpz_init(c); fmpz_init(d); fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_init(h); fmpz_poly_init(p); fmpz_poly_randtest(f, state, n_randint(state, 50), 100); fmpz_poly_randtest(g, state, n_randint(state, 50), 100); fmpz_poly_randtest(h, state, n_randint(state, 50), 100); fmpz_poly_resultant_modular(a, f, h); fmpz_poly_resultant_modular(b, g, h); if (fmpz_is_zero(b) || fmpz_is_zero(a)) { fmpz_clear(b); fmpz_clear(a); fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_poly_clear(h); continue; } fmpz_mul(c, a, b); fmpz_poly_mul(p, f, g); nbits = (slong)fmpz_bits(a) + 1; /* for sign */ fmpz_poly_resultant_modular_div(d, p, h, b, nbits); result = (fmpz_equal(a, d)); if (!result) { flint_printf("FAIL:\n"); flint_printf("p(x) = "), fmpz_poly_print_pretty(p, "x"), flint_printf("\n\n"); flint_printf("h(x) = "), fmpz_poly_print_pretty(h, "x"), flint_printf("\n\n"); flint_printf("res(p, h) = "), fmpz_print(c), flint_printf("\n\n"); flint_printf("res(p, h) = "), fmpz_print(a), flint_printf(" * "), fmpz_print(b), flint_printf("\n\n"); flint_printf("supplied divisor = "), fmpz_print(b), flint_printf("\n\n"); flint_printf("result should be = "), fmpz_print(a), flint_printf("\n\n"); flint_printf("res(p, h)/div = "), fmpz_print(d), flint_printf("\n\n"); flint_printf("bitsbound for result = %wd", nbits), flint_printf("\n\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); fmpz_clear(d); fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_poly_clear(h); fmpz_poly_clear(p); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(int argc, char *argv[]) { fmpz_poly_t f, g; fmpz_poly_factor_t fac; fmpz_t t; slong compd, printd, i, j; if (argc < 2) { flint_printf("poly_roots [-refine d] [-print d] <poly>\n\n"); flint_printf("Isolates all the complex roots of a polynomial with integer coefficients.\n\n"); flint_printf("If -refine d is passed, the roots are refined to an absolute tolerance\n"); flint_printf("better than 10^(-d). By default, the roots are only computed to sufficient\n"); flint_printf("accuracy to isolate them. The refinement is not currently done efficiently.\n\n"); flint_printf("If -print d is passed, the computed roots are printed to d decimals.\n"); flint_printf("By default, the roots are not printed.\n\n"); flint_printf("The polynomial can be specified by passing the following as <poly>:\n\n"); flint_printf("a <n> Easy polynomial 1 + 2x + ... + (n+1)x^n\n"); flint_printf("t <n> Chebyshev polynomial T_n\n"); flint_printf("u <n> Chebyshev polynomial U_n\n"); flint_printf("p <n> Legendre polynomial P_n\n"); flint_printf("c <n> Cyclotomic polynomial Phi_n\n"); flint_printf("s <n> Swinnerton-Dyer polynomial S_n\n"); flint_printf("b <n> Bernoulli polynomial B_n\n"); flint_printf("w <n> Wilkinson polynomial W_n\n"); flint_printf("e <n> Taylor series of exp(x) truncated to degree n\n"); flint_printf("m <n> <m> The Mignotte-like polynomial x^n + (100x+1)^m, n > m\n"); flint_printf("coeffs <c0 c1 ... cn> c0 + c1 x + ... + cn x^n\n\n"); flint_printf("Concatenate to multiply polynomials, e.g.: p 5 t 6 coeffs 1 2 3\n"); flint_printf("for P_5(x)*T_6(x)*(1+2x+3x^2)\n\n"); return 1; } compd = 0; printd = 0; fmpz_poly_init(f); fmpz_poly_init(g); fmpz_init(t); fmpz_poly_one(f); for (i = 1; i < argc; i++) { if (!strcmp(argv[i], "-refine")) { compd = atol(argv[i+1]); i++; } else if (!strcmp(argv[i], "-print")) { printd = atol(argv[i+1]); i++; } else if (!strcmp(argv[i], "a")) { slong n = atol(argv[i+1]); fmpz_poly_zero(g); for (j = 0; j <= n; j++) fmpz_poly_set_coeff_ui(g, j, j+1); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "t")) { arith_chebyshev_t_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "u")) { arith_chebyshev_u_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "p")) { fmpq_poly_t h; fmpq_poly_init(h); arith_legendre_polynomial(h, atol(argv[i+1])); fmpq_poly_get_numerator(g, h); fmpz_poly_mul(f, f, g); fmpq_poly_clear(h); i++; } else if (!strcmp(argv[i], "c")) { arith_cyclotomic_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "s")) { arith_swinnerton_dyer_polynomial(g, atol(argv[i+1])); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "b")) { fmpq_poly_t h; fmpq_poly_init(h); arith_bernoulli_polynomial(h, atol(argv[i+1])); fmpq_poly_get_numerator(g, h); fmpz_poly_mul(f, f, g); fmpq_poly_clear(h); i++; } else if (!strcmp(argv[i], "w")) { slong n = atol(argv[i+1]); fmpz_poly_zero(g); fmpz_poly_fit_length(g, n+2); arith_stirling_number_1_vec(g->coeffs, n+1, n+2); _fmpz_poly_set_length(g, n+2); fmpz_poly_shift_right(g, g, 1); fmpz_poly_mul(f, f, g); i++; } else if (!strcmp(argv[i], "e")) { fmpq_poly_t h; fmpq_poly_init(h); fmpq_poly_set_coeff_si(h, 0, 0); fmpq_poly_set_coeff_si(h, 1, 1); fmpq_poly_exp_series(h, h, atol(argv[i+1]) + 1); fmpq_poly_get_numerator(g, h); fmpz_poly_mul(f, f, g); fmpq_poly_clear(h); i++; } else if (!strcmp(argv[i], "m")) { fmpz_poly_zero(g); fmpz_poly_set_coeff_ui(g, 0, 1); fmpz_poly_set_coeff_ui(g, 1, 100); fmpz_poly_pow(g, g, atol(argv[i+2])); fmpz_poly_set_coeff_ui(g, atol(argv[i+1]), 1); fmpz_poly_mul(f, f, g); i += 2; } else if (!strcmp(argv[i], "coeffs")) { fmpz_poly_zero(g); i++; j = 0; while (i < argc) { if (fmpz_set_str(t, argv[i], 10) != 0) { i--; break; } fmpz_poly_set_coeff_fmpz(g, j, t); i++; j++; } fmpz_poly_mul(f, f, g); } } fmpz_poly_factor_init(fac); flint_printf("computing squarefree factorization...\n"); TIMEIT_ONCE_START fmpz_poly_factor_squarefree(fac, f); TIMEIT_ONCE_STOP TIMEIT_ONCE_START for (i = 0; i < fac->num; i++) { flint_printf("roots with multiplicity %wd\n", fac->exp[i]); fmpz_poly_complex_roots_squarefree(fac->p + i, 32, compd * 3.32193 + 2, printd); } TIMEIT_ONCE_STOP fmpz_poly_factor_clear(fac); fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_clear(t); flint_cleanup(); return EXIT_SUCCESS; }
int main(void) { int i, result; flint_rand_t state; printf("hensel_start_continue_lift...."); fflush(stdout); flint_randinit(state); /* We check that lifting local factors of F yields factors */ for (i = 0; i < 1000; i++) { fmpz_poly_t F, G, H, R; nmod_poly_factor_t f_fac; fmpz_poly_factor_t F_fac; long bits, nbits, n, exp, j, part_exp; long r; fmpz_poly_t *v, *w; long *link; long prev_exp; bits = n_randint(state, 200) + 1; nbits = n_randint(state, FLINT_BITS - 6) + 6; fmpz_poly_init(F); fmpz_poly_init(G); fmpz_poly_init(H); fmpz_poly_init(R); nmod_poly_factor_init(f_fac); fmpz_poly_factor_init(F_fac); n = n_randprime(state, nbits, 0); exp = bits / (FLINT_BIT_COUNT(n) - 1) + 1; part_exp = n_randint(state, exp); /* Produce F as the product of random G and H */ { nmod_poly_t f; nmod_poly_init(f, n); do { do { fmpz_poly_randtest(G, state, n_randint(state, 200) + 2, bits); } while (G->length < 2); fmpz_randtest_not_zero(G->coeffs, state, bits); fmpz_one(fmpz_poly_lead(G)); do { fmpz_poly_randtest(H, state, n_randint(state, 200) + 2, bits); } while (H->length < 2); fmpz_randtest_not_zero(H->coeffs, state, bits); fmpz_one(fmpz_poly_lead(H)); fmpz_poly_mul(F, G, H); fmpz_poly_get_nmod_poly(f, F); } while (!nmod_poly_is_squarefree(f)); fmpz_poly_get_nmod_poly(f, G); nmod_poly_factor_insert(f_fac, f, 1); fmpz_poly_get_nmod_poly(f, H); nmod_poly_factor_insert(f_fac, f, 1); nmod_poly_clear(f); } r = f_fac->num; v = flint_malloc((2*r - 2)*sizeof(fmpz_poly_t)); w = flint_malloc((2*r - 2)*sizeof(fmpz_poly_t)); link = flint_malloc((2*r - 2)*sizeof(long)); for (j = 0; j < 2*r - 2; j++) { fmpz_poly_init(v[j]); fmpz_poly_init(w[j]); } if (part_exp < 1) { _fmpz_poly_hensel_start_lift(F_fac, link, v, w, F, f_fac, exp); } else { fmpz_t nn; fmpz_init_set_ui(nn, n); prev_exp = _fmpz_poly_hensel_start_lift(F_fac, link, v, w, F, f_fac, part_exp); _fmpz_poly_hensel_continue_lift(F_fac, link, v, w, F, prev_exp, part_exp, exp, nn); fmpz_clear(nn); } result = 1; for (j = 0; j < F_fac->num; j++) { fmpz_poly_rem(R, F, F_fac->p + j); result &= (R->length == 0); } for (j = 0; j < 2*r - 2; j++) { fmpz_poly_clear(v[j]); fmpz_poly_clear(w[j]); } flint_free(link); flint_free(v); flint_free(w); if (!result) { printf("FAIL:\n"); printf("bits = %ld, n = %ld, exp = %ld\n", bits, n, exp); fmpz_poly_print(F); printf("\n\n"); fmpz_poly_print(G); printf("\n\n"); fmpz_poly_print(H); printf("\n\n"); fmpz_poly_factor_print(F_fac); printf("\n\n"); abort(); } nmod_poly_factor_clear(f_fac); fmpz_poly_factor_clear(F_fac); fmpz_poly_clear(F); fmpz_poly_clear(H); fmpz_poly_clear(G); fmpz_poly_clear(R); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("divrem_basecase...."); fflush(stdout); flint_randinit(state); /* Check q*b + r = a, no aliasing */ for (i = 0; i < 2000; i++) { fmpz_poly_t a, b, q, r, prod; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_init(prod); fmpz_poly_randtest(a, state, n_randint(state, 100), 100); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 100); fmpz_poly_divrem_basecase(q, r, a, b); fmpz_poly_mul(prod, q, b); fmpz_poly_add(prod, prod, r); result = (fmpz_poly_equal(a, prod)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(prod), printf("\n\n"); fmpz_poly_print(q), printf("\n\n"); fmpz_poly_print(r), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); fmpz_poly_clear(r); fmpz_poly_clear(prod); } /* Check r and a alias */ for (i = 0; i < 1000; i++) { fmpz_poly_t a, b, q, r; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_randtest(a, state, n_randint(state, 100), 100); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 100); fmpz_poly_divrem_basecase(q, r, a, b); fmpz_poly_divrem_basecase(q, a, a, b); result = (fmpz_poly_equal(a, r)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(q), printf("\n\n"); fmpz_poly_print(r), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); fmpz_poly_clear(r); } /* Check r and b alias */ for (i = 0; i < 1000; i++) { fmpz_poly_t a, b, q, r; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_randtest(a, state, n_randint(state, 100), 100); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 100); fmpz_poly_divrem_basecase(q, r, a, b); fmpz_poly_divrem_basecase(q, b, a, b); result = (fmpz_poly_equal(b, r)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(q), printf("\n\n"); fmpz_poly_print(r), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); fmpz_poly_clear(r); } /* Check q and a alias */ for (i = 0; i < 1000; i++) { fmpz_poly_t a, b, q, r; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_randtest(a, state, n_randint(state, 100), 100); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 100); fmpz_poly_divrem_basecase(q, r, a, b); fmpz_poly_divrem_basecase(a, r, a, b); result = (fmpz_poly_equal(a, q)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(q), printf("\n\n"); fmpz_poly_print(r), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); fmpz_poly_clear(r); } /* Check q and b alias */ for (i = 0; i < 1000; i++) { fmpz_poly_t a, b, q, r; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_randtest(a, state, n_randint(state, 100), 100); fmpz_poly_randtest_not_zero(b, state, n_randint(state, 100) + 1, 100); fmpz_poly_divrem_basecase(q, r, a, b); fmpz_poly_divrem_basecase(b, r, a, b); result = (fmpz_poly_equal(b, q)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(q), printf("\n\n"); fmpz_poly_print(r), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(q); fmpz_poly_clear(r); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
var MulPolyZ(const Tuple& x,const Tuple& y){ fmpz_poly_t res; fmpz_poly_init(res); fmpz_poly_mul(res,to_fmpz_poly(x),to_fmpz_poly(y)); return from_fmpz_poly(res); }
int main(void) { int i, result; flint_rand_t state; printf("gcd...."); fflush(stdout); flint_randinit(state); /* Check aliasing of a and b */ for (i = 0; i < 500; i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(b, state, n_randint(state, 40), 80); fmpz_poly_randtest(c, state, n_randint(state, 40), 80); fmpz_poly_gcd(a, b, c); fmpz_poly_gcd(b, b, c); result = (fmpz_poly_equal(a, b)); if (!result) { printf("FAIL (aliasing a and b):\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(b), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Check aliasing of a and c */ for (i = 0; i < 500; i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(b, state, n_randint(state, 40), 80); fmpz_poly_randtest(c, state, n_randint(state, 40), 80); fmpz_poly_gcd(a, b, c); fmpz_poly_gcd(c, b, c); result = (fmpz_poly_equal(a, c)); if (!result) { printf("FAIL (aliasing a and c):\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(c), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Check that a divides GCD(af, ag) */ for (i = 0; i < 500; i++) { fmpz_poly_t a, d, f, g, q, r; fmpz_poly_init(a); fmpz_poly_init(d); fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_init(q); fmpz_poly_init(r); fmpz_poly_randtest_not_zero(a, state, n_randint(state, 24) + 1, 24); fmpz_poly_randtest(f, state, n_randint(state, 40), 80); fmpz_poly_randtest(g, state, n_randint(state, 40), 80); fmpz_poly_mul(f, a, f); fmpz_poly_mul(g, a, g); fmpz_poly_gcd(d, f, g); fmpz_poly_divrem_divconquer(q, r, d, a); result = (r->length == 0L); if (!result) { printf("FAIL (check a | gcd(af, ag)):\n"); fmpz_poly_print(f), printf("\n"); fmpz_poly_print(g), printf("\n"); fmpz_poly_print(d), printf("\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(d); fmpz_poly_clear(f); fmpz_poly_clear(g); fmpz_poly_clear(q); fmpz_poly_clear(r); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }