int main(void) { flint_rand_t state; int iter; printf("one/is_one...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 1000; iter++) { nmod_poly_mat_t A; long m, n; mp_limb_t mod; mod = n_randtest_prime(state, 0); m = n_randint(state, 10); n = n_randint(state, 10); nmod_poly_mat_init(A, m, n, mod); nmod_poly_mat_randtest(A, state, n_randint(state, 5)); nmod_poly_mat_one(A); if (!nmod_poly_mat_is_one(A)) { printf("FAIL: expected matrix to be one\n"); abort(); } if (m > 0 && n > 0) { m = n_randint(state, m); n = n_randint(state, n); if (m != n) nmod_poly_randtest_not_zero(nmod_poly_mat_entry(A, m, n), state, 5); else do { nmod_poly_randtest_not_zero(nmod_poly_mat_entry(A, m, n), state, 5); } while (nmod_poly_is_one(nmod_poly_mat_entry(A, m, n))); if (nmod_poly_mat_is_one(A)) { printf("FAIL: expected matrix not to be one\n"); abort(); } } nmod_poly_mat_clear(A); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main (void) { double t; nmod_poly_t f, g, h; for (int i= 15001;i < 16000; i++) { nmod_poly_init2 (f, 17, i/2+1); nmod_poly_init2 (g, 17, i+1); nmod_poly_set_coeff_ui (f, i/2, 1); nmod_poly_set_coeff_ui (f, 1, 1); nmod_poly_set_coeff_ui (f, 0, ((i%17)*(i%17)+3) % 17); nmod_poly_set_coeff_ui (g, i, 1); nmod_poly_set_coeff_ui (g, i/2+1, 1); nmod_poly_set_coeff_ui (g, 1, ((i % 17)+1)%17); nmod_poly_set_coeff_ui (g, 0, 15); nmod_poly_init (h, 17); nmod_poly_gcd (h, f, g); if (!nmod_poly_is_one (h)) { flint_printf ("i= %d\n", i); nmod_poly_factor_t factors; nmod_poly_factor_init (factors); t= clock(); nmod_poly_factor (factors, h); t = (clock() - t) / CLOCKS_PER_SEC; flint_printf("factorization %.2lf\n", t); nmod_poly_factor_clear (factors); } nmod_poly_clear (f); nmod_poly_clear (g); nmod_poly_clear (h); } return EXIT_SUCCESS; }
void nmod_poly_factor_squarefree(nmod_poly_factor_t res, const nmod_poly_t f) { nmod_poly_t f_d, g, g_1; mp_limb_t p; slong deg, i; if (f->length <= 1) { res->num = 0; return; } if (f->length == 2) { nmod_poly_factor_insert(res, f, 1); return; } p = nmod_poly_modulus(f); deg = nmod_poly_degree(f); /* Step 1, look at f', if it is zero then we are done since f = h(x)^p for some particular h(x), clearly f(x) = sum a_k x^kp, k <= deg(f) */ nmod_poly_init(g_1, p); nmod_poly_init(f_d, p); nmod_poly_init(g, p); nmod_poly_derivative(f_d, f); /* Case 1 */ if (nmod_poly_is_zero(f_d)) { nmod_poly_factor_t new_res; nmod_poly_t h; nmod_poly_init(h, p); for (i = 0; i <= deg / p; i++) /* this will be an integer since f'=0 */ { nmod_poly_set_coeff_ui(h, i, nmod_poly_get_coeff_ui(f, i * p)); } /* Now run square-free on h, and return it to the pth power */ nmod_poly_factor_init(new_res); nmod_poly_factor_squarefree(new_res, h); nmod_poly_factor_pow(new_res, p); nmod_poly_factor_concat(res, new_res); nmod_poly_clear(h); nmod_poly_factor_clear(new_res); } else { nmod_poly_t h, z; nmod_poly_gcd(g, f, f_d); nmod_poly_div(g_1, f, g); i = 1; nmod_poly_init(h, p); nmod_poly_init(z, p); /* Case 2 */ while (!nmod_poly_is_one(g_1)) { nmod_poly_gcd(h, g_1, g); nmod_poly_div(z, g_1, h); /* out <- out.z */ if (z->length > 1) { nmod_poly_factor_insert(res, z, 1); nmod_poly_make_monic(res->p + (res->num - 1), res->p + (res->num - 1)); if (res->num) res->exp[res->num - 1] *= i; } i++; nmod_poly_set(g_1, h); nmod_poly_div(g, g, h); } nmod_poly_clear(h); nmod_poly_clear(z); nmod_poly_make_monic(g, g); if (!nmod_poly_is_one(g)) { /* so now we multiply res with square-free(g^1/p) ^ p */ nmod_poly_t g_p; /* g^(1/p) */ nmod_poly_factor_t new_res_2; nmod_poly_init(g_p, p); for (i = 0; i <= nmod_poly_degree(g) / p; i++) nmod_poly_set_coeff_ui(g_p, i, nmod_poly_get_coeff_ui(g, i*p)); nmod_poly_factor_init(new_res_2); /* square-free(g^(1/p)) */ nmod_poly_factor_squarefree(new_res_2, g_p); nmod_poly_factor_pow(new_res_2, p); nmod_poly_factor_concat(res, new_res_2); nmod_poly_clear(g_p); nmod_poly_factor_clear(new_res_2); } } nmod_poly_clear(g_1); nmod_poly_clear(f_d); nmod_poly_clear(g); }