void fmpz_poly_q_scalar_mul_si(fmpz_poly_q_t rop, const fmpz_poly_q_t op, long x) { fmpz_t cont, fx, gcd; if (fmpz_poly_q_is_zero(op) || (x == 0)) { fmpz_poly_q_zero(rop); return; } if (x == 1) { fmpz_poly_q_set(rop, op); return; } fmpz_init(cont); fmpz_poly_content(cont, op->den); if (fmpz_is_one(cont)) { fmpz_poly_scalar_mul_si(rop->num, op->num, x); fmpz_poly_set(rop->den, op->den); fmpz_clear(cont); return; } fmpz_init(fx); fmpz_init(gcd); fmpz_set_si(fx, x); fmpz_gcd(gcd, cont, fx); if (fmpz_is_one(gcd)) { fmpz_poly_scalar_mul_si(rop->num, op->num, x); fmpz_poly_set(rop->den, op->den); } else { fmpz_divexact(fx, fx, gcd); fmpz_poly_scalar_mul_fmpz(rop->num, op->num, fx); fmpz_poly_scalar_divexact_fmpz(rop->den, op->den, gcd); } fmpz_clear(cont); fmpz_clear(fx); fmpz_clear(gcd); }
void fmpz_poly_scalar_mul_ui(fmpz_poly_t poly1, const fmpz_poly_t poly2, ulong x) { long i; /* Either scalar or input poly is zero */ if ((x == 0) || (poly2->length == 0)) { fmpz_poly_zero(poly1); return; } /* Special case, multiply by 1 */ if (x == 1) { fmpz_poly_set(poly1, poly2); return; } fmpz_poly_fit_length(poly1, poly2->length); for (i = 0; i < poly2->length; i++) fmpz_mul_ui(poly1->coeffs + i, poly2->coeffs + i, x); _fmpz_poly_set_length(poly1, poly2->length); }
int dgsl_rot_mp_call_gpv_inlattice(fmpz_poly_t rop, const dgsl_rot_mp_t *self, gmp_randstate_t state) { assert(rop); assert(self); const long n = self->n; mpz_t tmp_z; mpz_init(tmp_z); fmpz_t tmp; fmpz_init(tmp); fmpz_poly_zero(rop); fmpz_poly_t tmp_poly; fmpz_poly_init(tmp_poly); fmpz_poly_set(tmp_poly, self->B); fmpz_poly_realloc(tmp_poly, n); tmp_poly->length = n; fmpz_poly_t tmp2; fmpz_poly_init(tmp2); for(long i=0; i<n; i++) { self->D[i]->call(tmp_z, self->D[i], state); fmpz_set_mpz(tmp, tmp_z); fmpz_poly_scalar_mul_fmpz(tmp2, tmp_poly, tmp); fmpz_poly_add(rop, rop, tmp2); _fmpz_vec_rot_left_neg(tmp_poly->coeffs, tmp_poly->coeffs, n); } fmpz_poly_clear(tmp_poly); fmpz_poly_add(rop, rop, self->c_z); fmpz_poly_clear(tmp2); mpz_clear(tmp_z); fmpz_clear(tmp); return 0; }
void poly_starmultiply(fmpz_poly_t c, const fmpz_poly_t a, const fmpz_poly_t b, const ntru_params *params, uint32_t modulus) { fmpz_poly_t a_tmp; fmpz_t c_coeff_k; fmpz_poly_init(a_tmp); fmpz_init(c_coeff_k); /* avoid side effects */ fmpz_poly_set(a_tmp, a); fmpz_poly_zero(c); for (int k = params->N - 1; k >= 0; k--) { int j; j = k + 1; fmpz_set_si(c_coeff_k, 0); for (int i = params->N - 1; i >= 0; i--) { fmpz *a_tmp_coeff_i, *b_coeff_j; if (j == (int)(params->N)) j = 0; a_tmp_coeff_i = fmpz_poly_get_coeff_ptr(a_tmp, i); b_coeff_j = fmpz_poly_get_coeff_ptr(b, j); if (fmpz_cmp_si_n(a_tmp_coeff_i, 0) && fmpz_cmp_si_n(b_coeff_j, 0)) { fmpz_t fmpz_tmp; fmpz_init(fmpz_tmp); fmpz_mul(fmpz_tmp, a_tmp_coeff_i, b_coeff_j); fmpz_add(fmpz_tmp, fmpz_tmp, c_coeff_k); fmpz_mod_ui(c_coeff_k, fmpz_tmp, modulus); fmpz_poly_set_coeff_fmpz(c, k, c_coeff_k); fmpz_clear(fmpz_tmp); } j++; } fmpz_clear(c_coeff_k); } fmpz_poly_clear(a_tmp); }
int main(void) { int iter; FLINT_TEST_INIT(state); flint_printf("sgn_eval_at_half...."); fflush(stdout); /* Check aliasing */ for (iter = 0; iter < 1000 * flint_test_multiplier(); iter++) { fmpz_t a; fmpz_poly_t f, g; long i, d; int s1, s2; fmpz_init(a); fmpz_poly_init(f); fmpz_poly_init(g); fmpz_poly_randtest(f, state, n_randint(state, 100), 200); s1 = fmpz_poly_sgn_eval_at_half(f); fmpz_poly_set(g, f); d = fmpz_poly_degree(g); for (i = 0; i <= d; i++) { fmpz_mul_2exp(fmpz_poly_get_coeff_ptr(g, i), fmpz_poly_get_coeff_ptr(g, i), d - i); } fmpz_one(a); fmpz_poly_evaluate_fmpz(a, g, a); s2 = fmpz_sgn(a); if (s1 != s2) { flint_printf("FAIL:\n"); fmpz_poly_print(f); printf("\n\n"); printf("s1 = %d, s2 = %d\n\n", s1, s2); abort(); } fmpz_clear(a); fmpz_poly_clear(f); fmpz_poly_clear(g); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int dgsl_rot_mp_call_plus_fmpz_poly(fmpz_poly_t rop, const dgsl_rot_mp_t *self, const fmpz_poly_t c, gmp_randstate_t state) { fmpz_poly_t t; fmpz_poly_init(t); fmpz_poly_set(t, c); fmpq_poly_t tq; fmpq_poly_init(tq); // == 0 fmpq_poly_set_fmpz_poly(tq, t); fmpq_poly_neg(tq, tq); dgsl_rot_mp_call_recenter_fmpq_poly(rop, self, tq, state); fmpz_poly_add(rop, rop, t); fmpq_poly_clear(tq); fmpz_poly_clear(t); return 0; }
void fmpz_poly_mat_trace(fmpz_poly_t trace, const fmpz_poly_mat_t mat) { long i, n = fmpz_poly_mat_nrows(mat); if (n == 0) fmpz_poly_zero(trace); else { fmpz_poly_set(trace, fmpz_poly_mat_entry(mat, 0, 0)); for (i = 1; i < n; i++) fmpz_poly_add(trace, trace, fmpz_poly_mat_entry(mat, i, i)); } }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("swap...."); fflush(stdout); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(a, state, n_randint(state, 100), 200); fmpz_poly_randtest(b, state, n_randint(state, 100), 200); fmpz_poly_set(c, b); fmpz_poly_swap(a, b); result = (fmpz_poly_equal(a, c)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(b), flint_printf("\n\n"); fmpz_poly_print(c), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void fmpz_poly_pow_multinomial(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_sqr(res, poly); return; } rlen = (long) e * (len - 1) + 1; if (res != poly) { fmpz_poly_fit_length(res, rlen); _fmpz_poly_pow_multinomial(res->coeffs, poly->coeffs, len, e); _fmpz_poly_set_length(res, rlen); } else { fmpz_poly_t t; fmpz_poly_init2(t, rlen); _fmpz_poly_pow_multinomial(t->coeffs, poly->coeffs, len, e); _fmpz_poly_set_length(t, rlen); fmpz_poly_swap(res, t); fmpz_poly_clear(t); } }
void fmpz_poly_pow_binomial(fmpz_poly_t res, const fmpz_poly_t poly, ulong e) { const long len = poly->length; long rlen; if (len != 2) { printf("Exception: poly->length not equal to 2 in fmpz_poly_pow_binomial\n"); abort(); } if (e < 3UL) { if (e == 0UL) fmpz_poly_set_ui(res, 1UL); else if (e == 1UL) fmpz_poly_set(res, poly); else /* e == 2UL */ fmpz_poly_sqr(res, poly); return; } rlen = (long) e + 1; if (res != poly) { fmpz_poly_fit_length(res, rlen); _fmpz_poly_set_length(res, rlen); _fmpz_poly_pow_binomial(res->coeffs, poly->coeffs, e); } else { fmpz_poly_t t; fmpz_poly_init2(t, rlen); _fmpz_poly_set_length(t, rlen); _fmpz_poly_pow_binomial(t->coeffs, poly->coeffs, e); fmpz_poly_swap(res, t); fmpz_poly_clear(t); } }
void fmpz_poly_deflate(fmpz_poly_t result, const fmpz_poly_t input, ulong deflation) { slong res_length, i; if (deflation == 0) { flint_printf("Exception (fmpz_poly_deflate). Division by zero.\n"); abort(); } if (input->length <= 1 || deflation == 1) { fmpz_poly_set(result, input); return; } res_length = (input->length - 1) / deflation + 1; fmpz_poly_fit_length(result, res_length); for (i = 0; i < res_length; i++) fmpz_set(result->coeffs + i, input->coeffs + i*deflation); result->length = res_length; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("set_si_equal...."); fflush(stdout); /* equal polynomials */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t a, b; slong n; fmpz_poly_init(a); fmpz_poly_init(b); n = z_randtest(state); fmpz_poly_set_si(a, n); fmpz_poly_set(b, a); result = (fmpz_poly_equal(a, b)); if (!result) { flint_printf("FAIL:\n"); flint_printf("n = %wd\n\n", n); flint_printf("a = "), fmpz_poly_print(a), flint_printf("\n\n"); flint_printf("b = "), fmpz_poly_print(b), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); } for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t a, b; slong m, n; fmpz_poly_init(a); fmpz_poly_init(b); m = z_randtest(state); n = z_randtest(state); while (m == n) n = z_randtest(state); fmpz_poly_set_si(a, m); fmpz_poly_set_si(b, n); result = (!fmpz_poly_equal(a, b)); if (!result) { flint_printf("FAIL:\n"); flint_printf("m = %wd\n\n", m); flint_printf("n = %wd\n\n", n); flint_printf("a = "), fmpz_poly_print(a), flint_printf("\n\n"); flint_printf("b = "), fmpz_poly_print(b), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
bool poly_inverse_poly_q(fmpz_poly_t Fq, const fmpz_poly_t a, const ntru_params *params) { bool retval = false; int k = 0, j = 0; fmpz *b_last; fmpz_poly_t a_tmp, b, c, f, g; /* general initialization of temp variables */ fmpz_poly_init(b); fmpz_poly_set_coeff_ui(b, 0, 1); fmpz_poly_init(c); fmpz_poly_init(f); fmpz_poly_set(f, a); /* set g(x) = x^N − 1 */ fmpz_poly_init(g); fmpz_poly_set_coeff_si(g, 0, -1); fmpz_poly_set_coeff_si(g, params->N, 1); /* avoid side effects */ fmpz_poly_init(a_tmp); fmpz_poly_set(a_tmp, a); fmpz_poly_zero(Fq); while (1) { while (fmpz_poly_get_coeff_ptr(f, 0) && fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { for (uint32_t i = 1; i <= params->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i); /* f(x) = f(x) / x */ fmpz_poly_set_coeff_fmpz_n(f, i - 1, f_coeff); /* c(x) = c(x) * x */ fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i, c_coeff); } fmpz_poly_set_coeff_si(f, params->N, 0); fmpz_poly_set_coeff_si(c, 0, 0); k++; if (fmpz_poly_degree(f) == -1) goto cleanup; } if (fmpz_poly_is_zero(g) == 1) goto cleanup; if (fmpz_poly_degree(f) == 0) break; if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) { fmpz_poly_swap(f, g); fmpz_poly_swap(b, c); } fmpz_poly_add(f, g, f); fmpz_poly_mod_unsigned(f, 2); fmpz_poly_add(b, c, b); fmpz_poly_mod_unsigned(b, 2); } k = k % params->N; b_last = fmpz_poly_get_coeff_ptr(b, params->N); if (fmpz_cmp_si_n(b_last, 0)) goto cleanup; /* Fq(x) = x^(N-k) * b(x) */ for (int i = params->N - 1; i >= 0; i--) { fmpz *b_i; j = i - k; if (j < 0) j = j + params->N; b_i = fmpz_poly_get_coeff_ptr(b, i); fmpz_poly_set_coeff_fmpz_n(Fq, j, b_i); } poly_mod2_to_modq(Fq, a_tmp, params); /* check if the f * Fq = 1 (mod p) condition holds true */ fmpz_poly_set(a_tmp, a); poly_starmultiply(a_tmp, a_tmp, Fq, params, params->q); if (fmpz_poly_is_one(a_tmp)) retval = true; else fmpz_poly_zero(Fq); cleanup: fmpz_poly_clear(a_tmp); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(f); fmpz_poly_clear(g); return retval; }
dgsl_rot_mp_t *dgsl_rot_mp_init(const long n, const fmpz_poly_t B, mpfr_t sigma, fmpq_poly_t c, const dgsl_alg_t algorithm, const oz_flag_t flags) { assert(mpfr_cmp_ui(sigma, 0) > 0); dgsl_rot_mp_t *self = (dgsl_rot_mp_t*)calloc(1, sizeof(dgsl_rot_mp_t)); if(!self) dgs_die("out of memory"); dgsl_alg_t alg = algorithm; self->n = n; self->prec = mpfr_get_prec(sigma); fmpz_poly_init(self->B); fmpz_poly_set(self->B, B); if(fmpz_poly_length(self->B) > n) dgs_die("polynomial is longer than length n"); else fmpz_poly_realloc(self->B, n); fmpz_poly_init(self->c_z); fmpq_poly_init(self->c); mpfr_init2(self->sigma, self->prec); mpfr_set(self->sigma, sigma, MPFR_RNDN); if (alg == DGSL_DETECT) { if (fmpz_poly_is_one(self->B) && (c && fmpq_poly_is_zero(c))) { alg = DGSL_IDENTITY; } else if (c && fmpq_poly_is_zero(c)) alg = DGSL_INLATTICE; else alg = DGSL_COSET; //TODO: we could test for lattice membership here } size_t tau = 3; if (2*ceil(sqrt(log2((double)n))) > tau) tau = 2*ceil(sqrt(log2((double)n))); switch(alg) { case DGSL_IDENTITY: { self->D = (dgs_disc_gauss_mp_t**)calloc(1, sizeof(dgs_disc_gauss_mp_t*)); mpfr_t c_; mpfr_init2(c_, self->prec); mpfr_set_d(c_, 0.0, MPFR_RNDN); self->D[0] = dgs_disc_gauss_mp_init(self->sigma, c_, tau, DGS_DISC_GAUSS_DEFAULT); self->call = dgsl_rot_mp_call_identity; mpfr_clear(c_); break; } case DGSL_GPV_INLATTICE: { self->D = (dgs_disc_gauss_mp_t**)calloc(n, sizeof(dgs_disc_gauss_mp_t*)); if (c && !fmpq_poly_is_zero(c)) { fmpq_t c_i; fmpq_init(c_i); for(int i=0; i<n; i++) { fmpq_poly_get_coeff_fmpq(c_i, c, i); fmpz_poly_set_coeff_fmpz(self->c_z, i, fmpq_numref(c_i)); } fmpq_clear(c_i); } mpfr_mat_t G; mpfr_mat_init(G, n, n, self->prec); mpfr_mat_set_fmpz_poly(G, B); mpfr_mat_gso(G, MPFR_RNDN); mpfr_t sigma_; mpfr_init2(sigma_, self->prec); mpfr_t norm; mpfr_init2(norm, self->prec); mpfr_t c_; mpfr_init2(c_, self->prec); mpfr_set_d(c_, 0.0, MPFR_RNDN); for(long i=0; i<n; i++) { _mpfr_vec_2norm(norm, G->rows[i], n, MPFR_RNDN); assert(mpfr_cmp_d(norm, 0.0) > 0); mpfr_div(sigma_, self->sigma, norm, MPFR_RNDN); assert(mpfr_cmp_d(sigma_, 0.0) > 0); self->D[i] = dgs_disc_gauss_mp_init(sigma_, c_, tau, DGS_DISC_GAUSS_DEFAULT); } mpfr_clear(sigma_); mpfr_clear(norm); mpfr_clear(c_); mpfr_mat_clear(G); self->call = dgsl_rot_mp_call_gpv_inlattice; break; } case DGSL_INLATTICE: { fmpq_poly_init(self->sigma_sqrt); long r= 2*ceil(sqrt(log(n))); fmpq_poly_t Bq; fmpq_poly_init(Bq); fmpq_poly_set_fmpz_poly(Bq, self->B); fmpq_poly_oz_invert_approx(self->B_inv, Bq, n, self->prec, flags); fmpq_poly_clear(Bq); _dgsl_rot_mp_sqrt_sigma_2(self->sigma_sqrt, self->B, sigma, r, n, self->prec, flags); mpfr_init2(self->r_f, self->prec); mpfr_set_ui(self->r_f, r, MPFR_RNDN); self->call = dgsl_rot_mp_call_inlattice; break; } case DGSL_COSET: dgs_die("not implemented"); default: dgs_die("not implemented"); } return self; }
void fmpz_poly_pseudo_divrem_cohen(fmpz_poly_t Q, fmpz_poly_t R, const fmpz_poly_t A, const fmpz_poly_t B) { long lenq, lenr; fmpz *q, *r; if (B->length == 0) { printf("Exception: division by zero in fmpz_poly_pseudo_divrem_cohen\n"); abort(); } if (Q == R) { printf("Exception: output arguments Q and R may not be aliased\n"); abort(); } if (A->length < B->length) { fmpz_poly_zero(Q); fmpz_poly_set(R, A); return; } lenq = A->length - B->length + 1; lenr = A->length; if ((Q == A) || (Q == B)) q = _fmpz_vec_init(lenq); else { fmpz_poly_fit_length(Q, lenq); q = Q->coeffs; } if (R == B) r = _fmpz_vec_init(lenr); else { fmpz_poly_fit_length(R, lenr); r = R->coeffs; } _fmpz_poly_pseudo_divrem_cohen(q, r, A->coeffs, A->length, B->coeffs, B->length); for (lenr = B->length - 1; (lenr >= 0) && r[lenr] == 0L; lenr--) ; lenr++; if ((Q == A) || (Q == B)) { _fmpz_vec_clear(Q->coeffs, Q->alloc); Q->coeffs = q; Q->alloc = lenq; Q->length = lenq; } else _fmpz_poly_set_length(Q, lenq); if (R == B) { _fmpz_vec_clear(R->coeffs, R->alloc); R->coeffs = r; R->alloc = A->length; R->length = lenr; } else _fmpz_poly_set_length(R, lenr); }
bool poly_inverse_poly_p(fmpz_poly_t Fp, const fmpz_poly_t a, const ntru_params *params) { bool retval = false; int k = 0, j = 0; fmpz *b_last; fmpz_poly_t a_tmp, b, c, f, g; /* general initialization of temp variables */ fmpz_poly_init(b); fmpz_poly_set_coeff_ui(b, 0, 1); fmpz_poly_init(c); fmpz_poly_init(f); fmpz_poly_set(f, a); /* set g(x) = x^N − 1 */ fmpz_poly_init(g); fmpz_poly_set_coeff_si(g, 0, -1); fmpz_poly_set_coeff_si(g, params->N, 1); /* avoid side effects */ fmpz_poly_init(a_tmp); fmpz_poly_set(a_tmp, a); fmpz_poly_zero(Fp); while (1) { while (fmpz_poly_get_coeff_ptr(f, 0) && fmpz_is_zero(fmpz_poly_get_coeff_ptr(f, 0))) { for (uint32_t i = 1; i <= params->N; i++) { fmpz *f_coeff = fmpz_poly_get_coeff_ptr(f, i); fmpz *c_coeff = fmpz_poly_get_coeff_ptr(c, params->N - i); /* f(x) = f(x) / x */ fmpz_poly_set_coeff_fmpz_n(f, i - 1, f_coeff); /* c(x) = c(x) * x */ fmpz_poly_set_coeff_fmpz_n(c, params->N + 1 - i, c_coeff); } fmpz_poly_set_coeff_si(f, params->N, 0); fmpz_poly_set_coeff_si(c, 0, 0); k++; if (fmpz_poly_degree(f) == -1) goto cleanup; } if (fmpz_poly_is_zero(g) == 1) goto cleanup; if (fmpz_poly_degree(f) == 0) break; if (fmpz_poly_degree(f) < fmpz_poly_degree(g)) { /* exchange f and g and exchange b and c */ fmpz_poly_swap(f, g); fmpz_poly_swap(b, c); } { fmpz_poly_t c_tmp, g_tmp; fmpz_t u, mp_tmp; fmpz_init(u); fmpz_zero(u); fmpz_init_set(mp_tmp, fmpz_poly_get_coeff_ptr(f, 0)); fmpz_poly_init(g_tmp); fmpz_poly_set(g_tmp, g); fmpz_poly_init(c_tmp); fmpz_poly_set(c_tmp, c); /* u = f[0] * g[0]^(-1) mod p */ /* = (f[0] mod p) * (g[0] inverse mod p) mod p */ fmpz_invmod_ui(u, fmpz_poly_get_coeff_ptr(g, 0), params->p); fmpz_mod_ui(mp_tmp, mp_tmp, params->p); fmpz_mul(u, mp_tmp, u); fmpz_mod_ui(u, u, params->p); /* f = f - u * g mod p */ fmpz_poly_scalar_mul_fmpz(g_tmp, g_tmp, u); fmpz_poly_sub(f, f, g_tmp); fmpz_poly_mod_unsigned(f, params->p); /* b = b - u * c mod p */ fmpz_poly_scalar_mul_fmpz(c_tmp, c_tmp, u); fmpz_poly_sub(b, b, c_tmp); fmpz_poly_mod_unsigned(b, params->p); fmpz_clear(u); fmpz_poly_clear(g_tmp); fmpz_poly_clear(c_tmp); } } k = k % params->N; b_last = fmpz_poly_get_coeff_ptr(b, params->N); if (fmpz_cmp_si_n(b_last, 0)) goto cleanup; /* Fp(x) = x^(N-k) * b(x) */ for (int i = params->N - 1; i >= 0; i--) { fmpz *b_i; /* b(X) = f[0]^(-1) * b(X) (mod p) */ { fmpz_t mp_tmp; fmpz_init(mp_tmp); fmpz_invmod_ui(mp_tmp, fmpz_poly_get_coeff_ptr(f, 0), params->p); if (fmpz_poly_get_coeff_ptr(b, i)) { fmpz_mul(fmpz_poly_get_coeff_ptr(b, i), fmpz_poly_get_coeff_ptr(b, i), mp_tmp); fmpz_mod_ui(fmpz_poly_get_coeff_ptr(b, i), fmpz_poly_get_coeff_ptr(b, i), params->p); } } j = i - k; if (j < 0) j = j + params->N; b_i = fmpz_poly_get_coeff_ptr(b, i); fmpz_poly_set_coeff_fmpz_n(Fp, j, b_i); } /* check if the f * Fp = 1 (mod p) condition holds true */ fmpz_poly_set(a_tmp, a); poly_starmultiply(a_tmp, a_tmp, Fp, params, params->p); if (fmpz_poly_is_one(a_tmp)) retval = true; else fmpz_poly_zero(Fp); cleanup: fmpz_poly_clear(a_tmp); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(f); fmpz_poly_clear(g); return retval; }
int main(void) { int i, result; flint_rand_t state; printf("mul_KS...."); 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), 200); fmpz_poly_randtest(c, state, n_randint(state, 50), 200); fmpz_poly_mul_KS(a, b, c); fmpz_poly_mul_KS(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), 200); fmpz_poly_randtest(c, state, n_randint(state, 50), 200); fmpz_poly_mul_KS(a, b, c); fmpz_poly_mul_KS(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 aliasing of b 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), 200); fmpz_poly_set(c, b); fmpz_poly_mul_KS(a, b, b); fmpz_poly_mul_KS(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); } /* Compare with mul_classical */ for (i = 0; i < 10000; i++) { fmpz_poly_t a, b, c, d; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_init(d); fmpz_poly_randtest(b, state, n_randint(state, 50), 200); fmpz_poly_randtest(c, state, n_randint(state, 50), 200); fmpz_poly_mul_KS(a, b, c); fmpz_poly_mul_classical(d, b, c); result = (fmpz_poly_equal(a, d)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(d), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(d); } /* Compare with mul_classical unsigned */ for (i = 0; i < 10000; i++) { fmpz_poly_t a, b, c, d; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_init(d); fmpz_poly_randtest_unsigned(b, state, n_randint(state, 50), 200); fmpz_poly_randtest_unsigned(c, state, n_randint(state, 50), 200); fmpz_poly_mul_KS(a, b, c); fmpz_poly_mul_classical(d, b, c); result = (fmpz_poly_equal(a, d)); if (!result) { printf("FAIL:\n"); fmpz_poly_print(a), printf("\n\n"); fmpz_poly_print(d), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); fmpz_poly_clear(d); } /* Check _fmpz_poly_mul_KS 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_KS(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); _fmpz_poly_mul_KS(out2->coeffs, a->coeffs, a->length, b->coeffs, b->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; }
long _fmpz_poly_hensel_start_lift(fmpz_poly_factor_t lifted_fac, long *link, fmpz_poly_t *v, fmpz_poly_t *w, const fmpz_poly_t f, const nmod_poly_factor_t local_fac, long N) { const long r = local_fac->num; long i, preve; fmpz_t p, P; fmpz_poly_t monic_f; fmpz_init(p); fmpz_init(P); fmpz_poly_init(monic_f); fmpz_set_ui(p, (local_fac->p + 0)->mod.n); fmpz_pow_ui(P, p, N); if (fmpz_is_one(fmpz_poly_lead(f))) { fmpz_poly_set(monic_f, f); } else if (fmpz_cmp_si(fmpz_poly_lead(f), -1) == 0) { fmpz_poly_neg(monic_f, f); } else { fmpz_t t; fmpz_init(t); fmpz_mod(t, fmpz_poly_lead(f), P); if (fmpz_invmod(t, t, P) == 0) { printf("Exception in fmpz_poly_start_hensel_lift.\n"); abort(); } fmpz_poly_scalar_mul_fmpz(monic_f, f, t); fmpz_poly_scalar_mod_fmpz(monic_f, monic_f, P); fmpz_clear(t); } fmpz_poly_hensel_build_tree(link, v, w, local_fac); { long *e, n = FLINT_CLOG2(N) + 1; e = flint_malloc(n * sizeof(long)); for (e[i = 0] = N; e[i] > 1; i++) e[i + 1] = (e[i] + 1) / 2; for (i--; i > 0; i--) { fmpz_poly_hensel_lift_tree(link, v, w, monic_f, r, p, e[i+1], e[i], 1); } if (N > 1) { fmpz_poly_hensel_lift_tree(link, v, w, monic_f, r, p, e[i+1], e[i], 0); } preve = e[i+1]; flint_free(e); } /* Now everything is lifted to p^N, we just need to insert the factors into their correct places in lifted_fac. */ fmpz_poly_factor_fit_length(lifted_fac, r); for (i = 0; i < 2*r - 2; i++) { if (link[i] < 0) { fmpz_poly_scalar_smod_fmpz(lifted_fac->p + (- link[i] - 1), v[i], P); lifted_fac->exp[- link[i] - 1] = 1L; } } lifted_fac->num = r; fmpz_clear(p); fmpz_clear(P); fmpz_poly_clear(monic_f); return preve; }
int main(int argc, char *argv[]) { cmdline_params_t cmdline_params; const char *name = "Jigsaw Puzzles"; parse_cmdline(cmdline_params, argc, argv, name, NULL); print_header(name, cmdline_params); aes_randstate_t randstate; aes_randinit_seed(randstate, cmdline_params->shaseed, NULL); uint64_t t = ggh_walltime(0); uint64_t t_total = ggh_walltime(0); uint64_t t_gen = 0; gghlite_sk_t self; gghlite_jigsaw_init(self, cmdline_params->lambda, cmdline_params->kappa, cmdline_params->flags, randstate); printf("\n"); gghlite_params_print(self->params); printf("\n---\n\n"); t_gen = ggh_walltime(t); printf("1. GGH InstGen wall time: %8.2f s\n", ggh_seconds(t_gen)); t = ggh_walltime(0); fmpz_t p; fmpz_init(p); fmpz_poly_oz_ideal_norm(p, self->g, self->params->n, 0); fmpz_t a[5]; for(long k=0; k<5; k++) fmpz_init(a[k]); fmpz_t acc; fmpz_init(acc); fmpz_set_ui(acc, 1); // reducing these elements to something small mod g is expensive, for benchmarketing purposes we // hence avoid this costly step most of the time by doing it only twice and by then computing the // remaining elements from those two elements using cheap operations. The reported times still // refer to the expensive step for fairness. fmpz_randm_aes(a[0], randstate, p); fmpz_mul(acc, acc, a[0]); fmpz_mod(acc, acc, p); fmpz_randm_aes(a[1], randstate, p); fmpz_mul(acc, acc, a[1]); fmpz_mod(acc, acc, p); fmpz_set(a[3], a[0]); fmpz_set(a[4], a[1]); for(long k=2; k<cmdline_params->kappa; k++) { fmpz_mul(a[2], a[0], a[1]); fmpz_add_ui(a[2], a[2], k); fmpz_mod(a[2], a[2], p); fmpz_mul(acc, acc, a[2]); fmpz_mod(acc, acc, p); fmpz_set(a[1], a[0]); fmpz_set(a[0], a[2]); } printf("2. Sampling from U(Z_p) wall time: %8.2f s\n", ggh_seconds(ggh_walltime(t))); gghlite_clr_t e[3]; gghlite_clr_init(e[0]); gghlite_clr_init(e[1]); gghlite_clr_init(e[2]); gghlite_enc_t u_k; gghlite_enc_init(u_k, self->params); gghlite_enc_t left; gghlite_enc_init(left, self->params); gghlite_enc_set_ui0(left, 1, self->params); fmpz_poly_set_coeff_fmpz(e[0], 0, a[3]); int GAMMA = 20; uint64_t t_enc = ggh_walltime(0); int group0[GAMMA]; memset(group0, 0, GAMMA * sizeof(int)); group0[0] = 1; gghlite_enc_set_gghlite_clr(u_k, self, e[0], 1, group0, 1, randstate); t_enc = ggh_walltime(t_enc); printf("3. Encoding wall time: %8.2f s (per elem)\n", ggh_seconds(t_enc)); fflush(0); uint64_t t_mul = 0; t = ggh_walltime(0); gghlite_enc_mul(left, self->params, left, u_k); t_mul += ggh_walltime(t); fmpz_poly_set_coeff_fmpz(e[1], 0, a[4]); int group1[GAMMA]; memset(group1, 0, GAMMA * sizeof(int)); group1[1] = 1; gghlite_enc_set_gghlite_clr(u_k, self, e[1], 1, group1, 1, randstate); t = ggh_walltime(0); gghlite_enc_mul(left, self->params, left, u_k); t_mul += ggh_walltime(t); const mp_bitcnt_t prec = (self->params->n/4 < 8192) ? 8192 : self->params->n/4; const oz_flag_t flags = (self->params->flags & GGHLITE_FLAGS_VERBOSE) ? OZ_VERBOSE : 0; _fmpz_poly_oz_rem_small_iter(e[0], e[0], self->g, self->params->n, self->g_inv, prec, flags); _fmpz_poly_oz_rem_small_iter(e[1], e[1], self->g, self->params->n, self->g_inv, prec, flags); for(long k=2; k<cmdline_params->kappa; k++) { fmpz_poly_oz_mul(e[2], e[0], e[1], self->params->n); assert(fmpz_poly_degree(e[2])>=0); fmpz_add_ui(e[2]->coeffs, e[2]->coeffs, k); _fmpz_poly_oz_rem_small_iter(e[2], e[2], self->g, self->params->n, self->g_inv, prec, flags); int groupk[GAMMA]; memset(groupk, 0, GAMMA * sizeof(int)); groupk[k] = 1; gghlite_enc_set_gghlite_clr(u_k, self, e[2], 1, groupk, 1, randstate); t = ggh_walltime(0); gghlite_enc_mul(left, self->params, left, u_k); t_mul += ggh_walltime(t); fmpz_poly_set(e[1], e[0]); fmpz_poly_set(e[0], e[2]); } printf("4. Multiplication wall time: %8.4f s\n", ggh_seconds(t_mul)); fflush(0); t = ggh_walltime(0); gghlite_enc_t rght; gghlite_enc_init(rght, self->params); gghlite_enc_set_ui0(rght, 1, self->params); fmpz_poly_t tmp; fmpz_poly_init(tmp); fmpz_poly_set_coeff_fmpz(tmp, 0, acc); gghlite_enc_set_gghlite_clr0(rght, self, tmp, randstate); for(long k=0; k<cmdline_params->kappa; k++) { gghlite_enc_mul(rght, self->params, rght, self->z_inv[k]); } printf("5. RHS generation wall time: %8.2f s\n", ggh_seconds(ggh_walltime(t))); t = ggh_walltime(0); gghlite_enc_sub(rght, self->params, rght, left); int status = 1 - gghlite_enc_is_zero(self->params, rght); gghlite_clr_t clr; gghlite_clr_init(clr); gghlite_enc_extract(clr, self->params, rght); double size = fmpz_poly_2norm_log2(clr); gghlite_clr_clear(clr); printf("6. Checking correctness wall time: %8.2f s\n", ggh_seconds(ggh_walltime(t))); printf(" Correct: %8s (%8.2f)\n\n", (status == 0) ? "TRUE" : "FALSE", size); for(long i=0; i<5; i++) { fmpz_clear(a[i]); } gghlite_clr_clear(e[0]); gghlite_clr_clear(e[1]); gghlite_clr_clear(e[2]); gghlite_enc_clear(u_k); fmpz_clear(acc); gghlite_enc_clear(left); gghlite_enc_clear(rght); gghlite_clr_clear(tmp); fmpz_clear(p); gghlite_sk_clear(self, 1); t_total = ggh_walltime(t_total); printf("λ: %3ld, κ: %2ld, n: %6ld, seed: 0x%08lx, success: %d, gen: %10.2fs, enc: %8.2fs, mul: %8.4fs, time: %10.2fs\n", cmdline_params->lambda, cmdline_params->kappa, self->params->n, cmdline_params->seed, status==0, ggh_seconds(t_gen), ggh_seconds(t_enc), ggh_seconds(t_mul), ggh_seconds(t_total)); aes_randclear(randstate); mpfr_free_cache(); flint_cleanup(); return status; }
int main(void) { int i, result; flint_rand_t state; printf("set_ui_equal...."); fflush(stdout); flint_randinit(state); /* equal polynomials */ for (i = 0; i < 10000; i++) { fmpz_poly_t a, b; ulong n; fmpz_poly_init(a); fmpz_poly_init(b); n = n_randtest(state); fmpz_poly_set_ui(a, n); fmpz_poly_set(b, a); result = (fmpz_poly_equal(a, b)); if (!result) { printf("FAIL:\n"); printf("n = %lu\n\n", n); printf("a = "), fmpz_poly_print(a), printf("\n\n"); printf("b = "), fmpz_poly_print(b), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); } for (i = 0; i < 10000; i++) { fmpz_poly_t a, b; ulong m, n; fmpz_poly_init(a); fmpz_poly_init(b); m = n_randtest(state); n = n_randtest(state); while (m == n) n = n_randtest(state); fmpz_poly_set_ui(a, m); fmpz_poly_set_ui(b, n); result = (!fmpz_poly_equal(a, b)); if (!result) { printf("FAIL:\n"); printf("m = %lu\n\n", m); printf("n = %lu\n\n", n); printf("a = "), fmpz_poly_print(a), printf("\n\n"); printf("b = "), fmpz_poly_print(b), printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int fmpz_poly_mat_inv(fmpz_poly_mat_t Ainv, fmpz_poly_t den, const fmpz_poly_mat_t A) { long n = fmpz_poly_mat_nrows(A); if (n == 0) { fmpz_poly_one(den); return 1; } else if (n == 1) { fmpz_poly_set(den, E(A, 0, 0)); fmpz_poly_one(E(Ainv, 0, 0)); return !fmpz_poly_is_zero(den); } else if (n == 2) { fmpz_poly_mat_det(den, A); if (fmpz_poly_is_zero(den)) { return 0; } else if (Ainv == A) { fmpz_poly_swap(E(A, 0, 0), E(A, 1, 1)); fmpz_poly_neg(E(A, 0, 1), E(A, 0, 1)); fmpz_poly_neg(E(A, 1, 0), E(A, 1, 0)); return 1; } else { fmpz_poly_set(E(Ainv, 0, 0), E(A, 1, 1)); fmpz_poly_set(E(Ainv, 1, 1), E(A, 0, 0)); fmpz_poly_neg(E(Ainv, 0, 1), E(A, 0, 1)); fmpz_poly_neg(E(Ainv, 1, 0), E(A, 1, 0)); return 1; } } else { fmpz_poly_mat_t LU, I; long * perm; int result; perm = _perm_init(n); fmpz_poly_mat_init_set(LU, A); result = (fmpz_poly_mat_fflu(LU, den, perm, LU, 1) == n); if (result) { fmpz_poly_mat_init(I, n, n); fmpz_poly_mat_one(I); fmpz_poly_mat_solve_fflu_precomp(Ainv, perm, LU, I); fmpz_poly_mat_clear(I); } else fmpz_poly_zero(den); if (_perm_parity(perm, n)) { fmpz_poly_mat_neg(Ainv, Ainv); fmpz_poly_neg(den, den); } _perm_clear(perm); fmpz_poly_mat_clear(LU); return result; } }
int main(int argc, char *argv[]) { int ans; char *str, *strout; fmpz_poly_t zpoly; fmpz_poly_q_t qpoly1; mpz_t mpzzero, mpzone, mpztwo; mpq_t mpqzero, mpqone, mpqtwo, mpqtwoinv; FLINT_TEST_INIT(state); flint_printf("all... "); fflush(stdout); /* Accessing numerator and denominator ***********************************/ fmpz_poly_q_init(qpoly1); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); str = "2 -1 1"; strout = fmpz_poly_get_str(fmpz_poly_q_numref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_numref: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); flint_printf(" qpoly1 = \""), fmpz_poly_q_print(qpoly1), flint_printf("\"\n"); abort(); } fmpz_poly_q_clear(qpoly1); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); str = "2 0 1"; strout = fmpz_poly_get_str(fmpz_poly_q_denref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_denref: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); fmpz_poly_set(zpoly, fmpz_poly_q_numref(qpoly1)); str = "2 -1 1"; strout = fmpz_poly_get_str(zpoly); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_get_num: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "2 -1 1/2 0 1"); fmpz_poly_set(zpoly, fmpz_poly_q_denref(qpoly1)); str = "2 0 1"; strout = fmpz_poly_get_str(zpoly); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_get_den: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "1 1/1 1"); fmpz_poly_set_str(zpoly, "2 0 1"); fmpz_poly_set(fmpz_poly_q_numref(qpoly1), zpoly); str = "2 0 1"; strout = fmpz_poly_get_str(fmpz_poly_q_numref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_set_num: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); fmpz_poly_q_init(qpoly1); fmpz_poly_init(zpoly); fmpz_poly_q_set_str(qpoly1, "1 1/1 1"); fmpz_poly_set_str(zpoly, "2 0 1"); fmpz_poly_set(fmpz_poly_q_denref(qpoly1), zpoly); str = "2 0 1"; strout = fmpz_poly_get_str(fmpz_poly_q_denref(qpoly1)); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_set_den: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_clear(zpoly); flint_free(strout); /* Canonicalise **********************************************************/ fmpz_poly_q_init(qpoly1); str = "2 -1 1/2 0 1"; fmpz_poly_q_set_str(qpoly1, str); strout = fmpz_poly_q_get_str(qpoly1); ans = !strcmp(str, strout); if (!ans) { flint_printf("test_canonicalize: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } fmpz_poly_q_clear(qpoly1); flint_free(strout); fmpz_poly_q_init(qpoly1); str = "2 -1 -1/2 0 1"; fmpz_poly_q_set_str(qpoly1, "2 1 1/2 0 -1"); strout = fmpz_poly_q_get_str(qpoly1); ans = !strcmp("2 -1 -1/2 0 1", strout); if (!ans) { flint_printf("test_canonicalize: failed\n"); flint_printf(" Expected \"%s\", got \"%s\"\n", str, strout); abort(); } flint_free(strout); fmpz_poly_q_clear(qpoly1); /* Initialization, memory management and basic operations ****************/ test_set("0", "0"); test_set("0/1 1", "0"); test_set("3 -1 0 1/2 0 1", "3 -1 0 1/2 0 1"); test_set("3 -1 0 1/2 1 1", "2 -1 1"); test_set_si(-1, "1 -1"); test_set_si(13, "1 13"); test_set_si(0, "0"); test_swap("3 -1 0 1/2 0 1", "1 2/1 3", "1 2/1 3", "3 -1 0 1/2 0 1"); test_zero("0", "0"); test_zero("0/1 1", "0"); test_zero("3 -1 0 1/2 0 1", "0"); test_neg("0", "0"); test_neg("1 1/1 2", "1 -1/1 2"); test_neg("3 -1 0 1/2 0 1", "3 1 0 -1/2 0 1"); test_inv("1 1/1 2", "1 2"); test_inv("3 -1 0 1/2 0 1", "2 0 1/3 -1 0 1"); test_inv("3 -1 0 -1/2 0 1", "2 0 -1/3 1 0 1"); test_inv_inplace("1 1/1 2", "1 2"); test_inv_inplace("3 -1 0 1/2 0 1", "2 0 1/3 -1 0 1"); test_inv_inplace("3 -1 0 -1/2 0 1", "2 0 -1/3 1 0 1"); test_is_zero("0", 1); test_is_zero("0/1 1", 1); test_is_zero("3 -1 0 1/2 0 1", 0); test_is_zero("3 -1 0 1/2 1 1", 0); test_is_one("0", 0); test_is_one("0/1 1", 0); test_is_one("1 1/1 1", 1); test_is_one("2 1 1/2 1 1", 1); test_is_one("3 -1 0 1/2 0 1", 0); test_equal("1 1/1 2", "1 1/1 2", 1); test_equal("1 1/1 2", "1 1/1 2", 1); test_equal("3 -1 0 1/2 1 1", "2 -1 1", 1); test_equal("3 -1 0 1/2 -1 1", "2 -1 1", 0); /* Addition and subtraction **********************************************/ test_add("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 1 0 1/4 0 1 0 1"); test_add("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 3 -2 1/2 -1 1"); test_add("0/2 1 1", "1 2/1 1", "1 2"); test_add("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_add("2 1 1/1 1", "2 -1 1/1 1", "2 0 2"); test_add("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 -1 2 2/3 0 -1 1"); test_add("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 7 12 7 1/3 2 3 1"); test_add("2 1 1/2 -1 1", "2 1 1", "3 0 1 1/2 -1 1"); test_add("1 1/2 1 1", "2 0 1/2 1 1", "1 1"); test_add("2 1 1/3 4 -4 1", "1 1/2 -2 1", "2 -1 2/3 4 -4 1"); test_add("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "0"); test_add("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2"); test_add("1 1/3 3 5 2", "1 1/3 6 7 2", "1 1/3 2 3 1"); test_add_in_place1("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 1 0 1/4 0 1 0 1"); test_add_in_place1("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 3 -2 1/2 -1 1"); test_add_in_place1("0/2 1 1", "1 2/1 1", "1 2"); test_add_in_place1("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_add_in_place1("2 1 1/1 1", "2 -1 1/1 1", "2 0 2"); test_add_in_place1("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 -1 2 2/3 0 -1 1"); test_add_in_place1("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 7 12 7 1/3 2 3 1"); test_add_in_place1("2 1 1/2 -1 1", "2 1 1", "3 0 1 1/2 -1 1"); test_add_in_place1("1 1/2 1 1", "2 0 1/2 1 1", "1 1"); test_add_in_place1("2 1 1/3 4 -4 1", "1 1/2 -2 1", "2 -1 2/3 4 -4 1"); test_add_in_place1("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "0"); test_add_in_place1("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2"); test_add_in_place2("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 1 0 1/4 0 1 0 1"); test_add_in_place2("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 3 -2 1/2 -1 1"); test_add_in_place2("0/2 1 1", "1 2/1 1", "1 2"); test_add_in_place2("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_add_in_place2("2 1 1/1 1", "2 -1 1/1 1", "2 0 2"); test_add_in_place2("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 -1 2 2/3 0 -1 1"); test_add_in_place2("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 7 12 7 1/3 2 3 1"); test_add_in_place2("2 1 1/2 -1 1", "2 1 1", "3 0 1 1/2 -1 1"); test_add_in_place2("1 1/2 1 1", "2 0 1/2 1 1", "1 1"); test_add_in_place2("2 1 1/3 4 -4 1", "1 1/2 -2 1", "2 -1 2/3 4 -4 1"); test_add_in_place2("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "0"); test_add_in_place2("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2"); test_add_in_place3("2 1 1", "2 2 2"); test_add_in_place3("2 1 1/1 2", "2 1 1"); test_sub("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub("0/2 1 1", "1 2/1 1", "1 -2"); test_sub("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub("2 -1 1/2 0 1", "1 1", "1 -1/2 0 1"); test_sub("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub("0/2 1 1", "1 2/1 1", "1 -2"); test_sub("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub("2 1 1/2 -1 1", "2 1 1", "3 2 1 -1/2 -1 1"); test_sub("1 1/2 1 1", "2 0 1/2 1 1", "2 1 -1/2 1 1"); test_sub("2 1 1/3 4 -4 1", "1 1/2 -2 1", "1 3/3 4 -4 1"); test_sub("3 0 1 1/3 1 2 1", "2 0 -1/2 1 1", "2 0 2/2 1 1"); test_sub("2 1 1/2 0 1", "2 -1 1/2 0 1", "1 2/2 0 1"); test_sub("1 1/3 3 5 2", "1 1/3 6 7 2", "1 1/4 6 13 9 2"); test_sub("2 1 1/2 0 2", "2 1 1/2 0 2", "0"); test_sub("2 -1 2/2 0 1", "2 -1 1/2 0 1", "1 1"); test_sub_in_place1("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub_in_place1("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub_in_place1("0/2 1 1", "1 2/1 1", "1 -2"); test_sub_in_place1("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub_in_place1("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub_in_place1("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub_in_place1("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub_in_place2("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "5 1 0 3 0 1/4 0 1 0 1"); test_sub_in_place2("3 -1 0 1/2 1 1", "1 2/2 -1 1", "3 -1 -2 1/2 -1 1"); test_sub_in_place2("0/2 1 1", "1 2/1 1", "1 -2"); test_sub_in_place2("1 -3/1 4", "0/3 1 0 1", "1 -3/1 4"); test_sub_in_place2("2 1 1/1 1", "2 -1 1/1 1", "1 2"); test_sub_in_place2("2 1 1/2 0 1", "2 2 1/2 -1 1", "2 -1 -2/3 0 -1 1"); test_sub_in_place2("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "4 -9 -12 -5 -1/3 2 3 1"); test_sub_in_place3("2 -1 1/2 2 1", "0"); test_addmul("1 1/2 0 2", "2 3 1/1 4", "3 1 0 1/4 -2 0 0 1", "5 -4 3 1 5 1/5 0 -8 0 0 4"); test_submul("1 1/2 0 2", "2 3 1/1 4", "3 1 0 1/4 -2 0 0 1", "5 -4 -3 -1 -1 -1/5 0 -8 0 0 4"); /* Scalar multiplication and devision ************************************/ flint_mpz_init_set_si(mpzzero, 0); flint_mpz_init_set_si(mpzone, 1); flint_mpz_init_set_si(mpztwo, 2); mpq_init(mpqzero); flint_mpq_set_si(mpqzero, 0, 1); mpq_init(mpqone); flint_mpq_set_si(mpqone, 1, 1); mpq_init(mpqtwo); flint_mpq_set_si(mpqtwo, 2, 1); mpq_init(mpqtwoinv); flint_mpq_set_si(mpqtwoinv, 1, 2); test_scalar_mul_si("0", 1, "0"); test_scalar_mul_si("0", 0, "0"); test_scalar_mul_si("1 2", 0, "0"); test_scalar_mul_si("1 1/1 2", -2, "1 -1"); test_scalar_mul_si("2 1 1/2 -2 3", 5, "2 5 5/2 -2 3"); test_scalar_mul_si("2 1 1/2 -2 2", 3, "2 3 3/2 -2 2"); test_scalar_mul_mpz("0", mpzone, "0"); test_scalar_mul_mpz("0", mpzzero, "0"); test_scalar_mul_mpz("1 2", mpzzero, "0"); test_scalar_mul_mpz("1 1/1 2", mpztwo, "1 1"); test_scalar_mul_mpq("0", mpqone, "0"); test_scalar_mul_mpq("0", mpqzero, "0"); test_scalar_mul_mpq("1 2", mpqzero, "0"); test_scalar_mul_mpq("1 1/1 2", mpqtwo, "1 1"); test_scalar_mul_mpq("1 -2/1 1", mpqtwoinv, "1 -1"); test_scalar_div_si("0", 1, "0"); test_scalar_div_si("1 2", 2, "1 1"); test_scalar_div_si("1 1/1 2", -2, "1 -1/1 4"); test_scalar_div_si("3 -5 0 3/2 1 1", 2, "3 -5 0 3/2 2 2"); test_scalar_div_si("3 2 8 4/2 0 1", 3, "3 2 8 4/2 0 3"); test_scalar_div_si("3 2 8 4/2 0 1", -3, "3 -2 -8 -4/2 0 3"); test_scalar_div_si("3 -27 0 9/2 0 1", -3, "3 9 0 -3/2 0 1"); test_scalar_div_mpz("0", mpzone, "0"); test_scalar_div_mpz("1 2", mpztwo, "1 1"); test_scalar_div_mpz("1 1/1 2", mpztwo, "1 1/1 4"); test_scalar_div_mpq("0", mpqone, "0"); test_scalar_div_mpq("1 2", mpqone, "1 2"); test_scalar_div_mpq("1 1/1 2", mpqtwo, "1 1/1 4"); test_scalar_div_mpq("1 -2/1 1", mpqtwoinv, "1 -4"); mpz_clear(mpzzero); mpz_clear(mpzone); mpz_clear(mpztwo); mpq_clear(mpqzero); mpq_clear(mpqone); mpq_clear(mpqtwo); mpq_clear(mpqtwoinv); /* Multiplication, division and powing *********************************/ test_mul("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "1 -1"); test_mul("3 -1 0 1/2 1 1", "1 2/2 -1 1", "1 2"); test_mul("0/2 1 1", "1 2/1 1", "0"); test_mul("1 -3/1 4", "0/3 1 0 1", "0"); test_mul("2 1 1/1 1", "2 -1 1/1 1", "3 -1 0 1"); test_mul("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 2 3 1/3 0 -1 1"); test_mul("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "3 -2 1 1/2 1 1"); test_mul_in_place1("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "1 -1"); test_mul_in_place1("3 -1 0 1/2 1 1", "1 2/2 -1 1", "1 2"); test_mul_in_place1("0/2 1 1", "1 2/1 1", "0"); test_mul_in_place1("1 -3/1 4", "0/3 1 0 1", "0"); test_mul_in_place1("2 1 1/1 1", "2 -1 1/1 1", "3 -1 0 1"); test_mul_in_place1("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 2 3 1/3 0 -1 1"); test_mul_in_place1("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "3 -2 1 1/2 1 1"); test_mul_in_place2("3 1 0 1/2 0 1", "2 0 -1/3 1 0 1", "1 -1"); test_mul_in_place2("3 -1 0 1/2 1 1", "1 2/2 -1 1", "1 2"); test_mul_in_place2("0/2 1 1", "1 2/1 1", "0"); test_mul_in_place2("1 -3/1 4", "0/3 1 0 1", "0"); test_mul_in_place2("2 1 1/1 1", "2 -1 1/1 1", "3 -1 0 1"); test_mul_in_place2("2 1 1/2 0 1", "2 2 1/2 -1 1", "3 2 3 1/3 0 -1 1"); test_mul_in_place2("2 -1 1/2 2 1", "3 4 4 1/2 1 1", "3 -2 1 1/2 1 1"); test_mul_in_place3("2 0 1/2 1 1", "3 0 0 1/3 1 2 1"); test_div("3 -1 0 1/1 2", "2 1 1/1 1", "2 -1 1/1 2"); test_div("0/2 1 1", "2 1 1/1 1", "0"); test_div("3 -1 0 1/1 4", "2 -1 -1/1 2", "2 1 -1/1 2"); test_div("2 1 1", "2 1 -1/2 1 -1", "2 1 1"); test_div("2 1 1/3 4 4 1", "2 -1 1/3 6 5 1", "3 3 4 1/3 -2 1 1"); test_div_in_place1("3 -1 0 1/1 2", "2 1 1/1 1", "2 -1 1/1 2"); test_div_in_place1("0/2 1 1", "2 1 1/1 1", "0"); test_div_in_place1("3 -1 0 1/1 4", "2 -1 -1/1 2", "2 1 -1/1 2"); test_div_in_place1("2 1 1", "2 1 -1/2 1 -1", "2 1 1"); test_div_in_place1("2 1 1/3 4 4 1", "2 -1 1/3 6 5 1", "3 3 4 1/3 -2 1 1"); test_div_in_place1("0", "1 2/2 3 5", "0"); test_div_in_place2("3 -1 0 1/1 2", "2 1 1/1 1", "2 -1 1/1 2"); test_div_in_place2("0/2 1 1", "2 1 1/1 1", "0"); test_div_in_place2("3 -1 0 1/1 4", "2 -1 -1/1 2", "2 1 -1/1 2"); test_div_in_place2("2 1 1", "2 1 -1/2 1 -1", "2 1 1"); test_div_in_place2("2 1 1/3 4 4 1", "2 -1 1/3 6 5 1", "3 3 4 1/3 -2 1 1"); test_div_in_place3("3 -1 0 1/1 2", "1 1"); test_pow("2 0 -1/1 2", 3, "4 0 0 0 -1/1 8"); test_pow("0", 0, "1 1"); test_pow("2 1 -1", 0, "1 1"); test_pow("2 1 1/2 0 1", 0, "1 1"); /* Derivative ************************************************************/ test_derivative("0", "0"); test_derivative("1 2", "0"); test_derivative("1 -1/1 2", "0"); test_derivative("2 0 1", "1 1"); test_derivative("3 1 0 1", "2 0 2"); test_derivative("1 1/2 0 1", "1 -1/3 0 0 1"); test_derivative("2 2 1/2 -1 1", "1 -3/3 1 -2 1"); test_derivative("2 0 1/3 1 2 1", "2 1 -1/4 1 3 3 1"); /* Bug which allowed constant factors */ test_derivative("3 5 1 -2/2 10 2", "3 0 -10 -1/3 25 10 1"); /* Evaluation ************************************************************/ test_evaluate("1 1/1 2", -2, 3, "1/2"); test_evaluate("3 1 0 1/2 0 1", -1, 2, "-5/2"); test_evaluate("2 3 1/2 -1 1", 1, 1, "P"); test_evaluate("2 3 1/2 -1 1", 2, 3, "-11"); test_evaluate("2 3 1/2 -1 2", 1, 2, "P"); test_evaluate("2 1 1/2 -1 1", 2, 1, "3"); /* String methods ********************************************************/ fmpz_poly_q_init(qpoly1); ans = fmpz_poly_q_set_str(qpoly1, "1 3/xyz"); if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1)) { flint_printf("test_set_str: failed\n"); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_q_init(qpoly1); ans = fmpz_poly_q_set_str(qpoly1, "abc/1 3"); if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1)) { flint_printf("test_set_str: failed\n"); abort(); } fmpz_poly_q_clear(qpoly1); fmpz_poly_q_init(qpoly1); ans = fmpz_poly_q_set_str(qpoly1, "abc/xyz"); if ((ans == 0) || !fmpz_poly_q_is_zero(qpoly1)) { flint_printf("test_set_str: failed\n"); abort(); } fmpz_poly_q_clear(qpoly1); test_get_str_pretty("1 -3", "-3"); test_get_str_pretty("3 1 2 1", "t^2+2*t+1"); test_get_str_pretty("1 -2/2 1 1", "-2/(t+1)"); test_get_str_pretty("2 1 1/2 -1 1", "(t+1)/(t-1)"); test_get_str_pretty("2 1 1/1 2", "(t+1)/2"); test_get_str_pretty("1 1/1 2", "1/2"); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("sqr_karatsuba...."); fflush(stdout); /* Check aliasing of a and b */ for (i = 0; i < 200 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(a, state, n_randint(state, 50), 200); fmpz_poly_set(b, a); fmpz_poly_sqr_karatsuba(c, b); fmpz_poly_sqr_karatsuba(b, b); result = (fmpz_poly_equal(b, c)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(b), flint_printf("\n\n"); fmpz_poly_print(c), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Compare with mul_karatsuba */ for (i = 0; i < 200 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, c; fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_poly_randtest(a, state, n_randint(state, 50), 200); fmpz_poly_sqr_karatsuba(b, a); fmpz_poly_mul_karatsuba(c, a, a); result = (fmpz_poly_equal(b, c)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(a), flint_printf("\n\n"); fmpz_poly_print(b), flint_printf("\n\n"); fmpz_poly_print(c), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Check _fmpz_poly_sqr_karatsuba directly */ for (i = 0; i < 200 * flint_test_multiplier(); i++) { slong len; fmpz_poly_t a, out1, out2; len = n_randint(state, 100) + 1; fmpz_poly_init(a); fmpz_poly_init(out1); fmpz_poly_init(out2); fmpz_poly_randtest(a, state, len, 200); fmpz_poly_sqr_karatsuba(out1, a); fmpz_poly_fit_length(a, a->alloc + n_randint(state, 10)); a->length = a->alloc; fmpz_poly_fit_length(out2, 2 * a->length - 1); _fmpz_poly_sqr_karatsuba(out2->coeffs, a->coeffs, a->length); _fmpz_poly_set_length(out2, 2 * a->length - 1); _fmpz_poly_normalise(out2); result = (fmpz_poly_equal(out1, out2)); if (!result) { flint_printf("FAIL:\n"); fmpz_poly_print(out1), flint_printf("\n\n"); fmpz_poly_print(out2), flint_printf("\n\n"); abort(); } fmpz_poly_clear(a); fmpz_poly_clear(out1); fmpz_poly_clear(out2); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int fmpz_poly_gcd_heuristic(fmpz_poly_t res, const fmpz_poly_t poly1, const fmpz_poly_t poly2) { const long len1 = poly1->length; const long len2 = poly2->length; long rlen; int done = 0; if (len1 == 0) { if (len2 == 0) fmpz_poly_zero(res); else { if (fmpz_sgn(poly2->coeffs + (len2 - 1)) > 0) fmpz_poly_set(res, poly2); else fmpz_poly_neg(res, poly2); } return 1; } else { if (len2 == 0) { if (fmpz_sgn(poly1->coeffs + (len1 - 1)) > 0) fmpz_poly_set(res, poly1); else fmpz_poly_neg(res, poly1); return 1; } } rlen = FLINT_MIN(len1, len2); if (res == poly1 || res == poly2) { fmpz_poly_t temp; fmpz_poly_init2(temp, rlen); if (len1 >= len2) done = _fmpz_poly_gcd_heuristic(temp->coeffs, poly1->coeffs, len1, poly2->coeffs, len2); else done = _fmpz_poly_gcd_heuristic(temp->coeffs, poly2->coeffs, len2, poly1->coeffs, len1); fmpz_poly_swap(temp, res); fmpz_poly_clear(temp); } else { fmpz_poly_fit_length(res, rlen); if (len1 >= len2) done = _fmpz_poly_gcd_heuristic(res->coeffs, poly1->coeffs, len1, poly2->coeffs, len2); else done = _fmpz_poly_gcd_heuristic(res->coeffs, poly2->coeffs, len2, poly1->coeffs, len1); } if (done) { _fmpz_poly_set_length(res, rlen); _fmpz_poly_normalise(res); } return done; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("scalar_addmul_fmpz...."); fflush(stdout); /* Check aliasing of a and b */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, c; fmpz_t x; fmpz_init(x); fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_randtest(x, state, n_randint(state, 100)); fmpz_poly_randtest(a, state, n_randint(state, 100), 200); fmpz_poly_set(b, a); fmpz_poly_set(c, a); fmpz_poly_scalar_addmul_fmpz(b, a, x); fmpz_poly_scalar_addmul_fmpz(a, a, x); result = (fmpz_poly_equal(a, b)); if (!result) { flint_printf("FAIL (1):\n"); flint_printf("a = "), fmpz_poly_print(a), flint_printf("\n\n"); flint_printf("b = "), fmpz_poly_print(b), flint_printf("\n\n"); flint_printf("c = "), fmpz_poly_print(c), flint_printf("\n\n"); flint_printf("x = "), fmpz_print(x), flint_printf("\n\n"); abort(); } fmpz_clear(x); fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } /* Check that b += x*a is the same as c = b + x*a */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t a, b, c; fmpz_t x; fmpz_init(x); fmpz_poly_init(a); fmpz_poly_init(b); fmpz_poly_init(c); fmpz_randtest(x, state, n_randint(state, 100)); fmpz_poly_randtest(a, state, n_randint(state, 100), 200); fmpz_poly_randtest(b, state, n_randint(state, 100), 200); fmpz_poly_scalar_mul_fmpz(c, a, x); fmpz_poly_add(c, b, c); fmpz_poly_scalar_addmul_fmpz(b, a, x); result = (fmpz_poly_equal(b, c)); if (!result) { flint_printf("FAIL (2):\n"); flint_printf("a = "), fmpz_poly_print(a), flint_printf("\n\n"); flint_printf("b = "), fmpz_poly_print(b), flint_printf("\n\n"); flint_printf("c = "), fmpz_poly_print(c), flint_printf("\n\n"); flint_printf("x = "), fmpz_print(x), flint_printf("\n\n"); abort(); } fmpz_clear(x); fmpz_poly_clear(a); fmpz_poly_clear(b); fmpz_poly_clear(c); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void frob(const mpoly_t P, const ctx_t ctxFracQt, const qadic_t t1, const qadic_ctx_t Qq, prec_t *prec, const prec_t *prec_in, int verbose) { const padic_ctx_struct *Qp = &Qq->pctx; const fmpz *p = Qp->p; const long a = qadic_ctx_degree(Qq); const long n = P->n - 1; const long d = mpoly_degree(P, -1, ctxFracQt); const long b = gmc_basis_size(n, d); long i, j, k; /* Diagonal fibre */ padic_mat_t F0; /* Gauss--Manin Connection */ mat_t M; mon_t *bR, *bC; fmpz_poly_t r; /* Local solution */ fmpz_poly_mat_t C, Cinv; long vC, vCinv; /* Frobenius */ fmpz_poly_mat_t F; long vF; fmpz_poly_mat_t F1; long vF1; fmpz_poly_t cp; clock_t c0, c1; double c; if (verbose) { printf("Input:\n"); printf(" P = "), mpoly_print(P, ctxFracQt), printf("\n"); printf(" p = "), fmpz_print(p), printf("\n"); printf(" t1 = "), qadic_print_pretty(t1, Qq), printf("\n"); printf("\n"); fflush(stdout); } /* Step 1 {M, r} *********************************************************/ c0 = clock(); mat_init(M, b, b, ctxFracQt); fmpz_poly_init(r); gmc_compute(M, &bR, &bC, P, ctxFracQt); { fmpz_poly_t t; fmpz_poly_init(t); fmpz_poly_set_ui(r, 1); for (i = 0; i < M->m; i++) for (j = 0; j < M->n; j++) { fmpz_poly_lcm(t, r, fmpz_poly_q_denref( (fmpz_poly_q_struct *) mat_entry(M, i, j, ctxFracQt))); fmpz_poly_swap(r, t); } fmpz_poly_clear(t); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Gauss-Manin connection:\n"); printf(" r(t) = "), fmpz_poly_print_pretty(r, "t"), printf("\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } { qadic_t t; qadic_init2(t, 1); fmpz_poly_evaluate_qadic(t, r, t1, Qq); if (qadic_is_zero(t)) { printf("Exception (deformation_frob).\n"); printf("The resultant r evaluates to zero (mod p) at t1.\n"); abort(); } qadic_clear(t); } /* Precisions ************************************************************/ if (prec_in != NULL) { *prec = *prec_in; } else { deformation_precisions(prec, p, a, n, d, fmpz_poly_degree(r)); } if (verbose) { printf("Precisions:\n"); printf(" N0 = %ld\n", prec->N0); printf(" N1 = %ld\n", prec->N1); printf(" N2 = %ld\n", prec->N2); printf(" N3 = %ld\n", prec->N3); printf(" N3i = %ld\n", prec->N3i); printf(" N3w = %ld\n", prec->N3w); printf(" N3iw = %ld\n", prec->N3iw); printf(" N4 = %ld\n", prec->N4); printf(" m = %ld\n", prec->m); printf(" K = %ld\n", prec->K); printf(" r = %ld\n", prec->r); printf(" s = %ld\n", prec->s); printf("\n"); fflush(stdout); } /* Initialisation ********************************************************/ padic_mat_init2(F0, b, b, prec->N4); fmpz_poly_mat_init(C, b, b); fmpz_poly_mat_init(Cinv, b, b); fmpz_poly_mat_init(F, b, b); vF = 0; fmpz_poly_mat_init(F1, b, b); vF1 = 0; fmpz_poly_init(cp); /* Step 2 {F0} ***********************************************************/ { padic_ctx_t pctx_F0; fmpz *t; padic_ctx_init(pctx_F0, p, FLINT_MIN(prec->N4 - 10, 0), prec->N4, PADIC_VAL_UNIT); t = _fmpz_vec_init(n + 1); c0 = clock(); mpoly_diagonal_fibre(t, P, ctxFracQt); diagfrob(F0, t, n, d, prec->N4, pctx_F0, 0); padic_mat_transpose(F0, F0); c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Diagonal fibre:\n"); printf(" P(0) = {"), _fmpz_vec_print(t, n + 1), printf("}\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } _fmpz_vec_clear(t, n + 1); padic_ctx_clear(pctx_F0); } /* Step 3 {C, Cinv} ******************************************************/ /* Compute C as a matrix over Z_p[[t]]. A is the same but as a series of matrices over Z_p. Mt is the matrix -M^t, and Cinv is C^{-1}^t, the local solution of the differential equation replacing M by Mt. */ c0 = clock(); { const long K = prec->K; padic_mat_struct *A; gmde_solve(&A, K, p, prec->N3, prec->N3w, M, ctxFracQt); gmde_convert_soln(C, &vC, A, K, p); for(i = 0; i < K; i++) padic_mat_clear(A + i); free(A); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Local solution:\n"); printf(" Time for C = %f\n", c); fflush(stdout); } c0 = clock(); { const long K = (prec->K + (*p) - 1) / (*p); mat_t Mt; padic_mat_struct *Ainv; mat_init(Mt, b, b, ctxFracQt); mat_transpose(Mt, M, ctxFracQt); mat_neg(Mt, Mt, ctxFracQt); gmde_solve(&Ainv, K, p, prec->N3i, prec->N3iw, Mt, ctxFracQt); gmde_convert_soln(Cinv, &vCinv, Ainv, K, p); fmpz_poly_mat_transpose(Cinv, Cinv); fmpz_poly_mat_compose_pow(Cinv, Cinv, *p); for(i = 0; i < K; i++) padic_mat_clear(Ainv + i); free(Ainv); mat_clear(Mt, ctxFracQt); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf(" Time for C^{-1} = %f\n", c); printf("\n"); fflush(stdout); } /* Step 4 {F(t) := C(t) F(0) C(t^p)^{-1}} ********************************/ /* Computes the product C(t) F(0) C(t^p)^{-1} modulo (p^{N_2}, t^K). This is done by first computing the unit part of the product exactly over the integers modulo t^K. */ c0 = clock(); { fmpz_t pN; fmpz_poly_mat_t T; fmpz_init(pN); fmpz_poly_mat_init(T, b, b); for (i = 0; i < b; i++) { /* Find the unique k s.t. F0(i,k) is non-zero */ for (k = 0; k < b; k++) if (!fmpz_is_zero(padic_mat_entry(F0, i, k))) break; if (k == b) { printf("Exception (frob). F0 is singular.\n\n"); abort(); } for (j = 0; j < b; j++) { fmpz_poly_scalar_mul_fmpz(fmpz_poly_mat_entry(T, i, j), fmpz_poly_mat_entry(Cinv, k, j), padic_mat_entry(F0, i, k)); } } fmpz_poly_mat_mul(F, C, T); fmpz_poly_mat_truncate(F, prec->K); vF = vC + padic_mat_val(F0) + vCinv; /* Canonicalise (F, vF) */ { long v = fmpz_poly_mat_ord_p(F, p); if (v == LONG_MAX) { printf("ERROR (deformation_frob). F(t) == 0.\n"); abort(); } else if (v > 0) { fmpz_pow_ui(pN, p, v); fmpz_poly_mat_scalar_divexact_fmpz(F, F, pN); vF = vF + v; } } /* Reduce (F, vF) modulo p^{N2} */ fmpz_pow_ui(pN, p, prec->N2 - vF); fmpz_poly_mat_scalar_mod_fmpz(F, F, pN); fmpz_clear(pN); fmpz_poly_mat_clear(T); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Matrix for F(t):\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } /* Step 5 {G = r(t)^m F(t)} **********************************************/ c0 = clock(); { fmpz_t pN; fmpz_poly_t t; fmpz_init(pN); fmpz_poly_init(t); fmpz_pow_ui(pN, p, prec->N2 - vF); /* Compute r(t)^m mod p^{N2-vF} */ if (prec->denR == NULL) { fmpz_mod_poly_t _t; fmpz_mod_poly_init(_t, pN); fmpz_mod_poly_set_fmpz_poly(_t, r); fmpz_mod_poly_pow(_t, _t, prec->m); fmpz_mod_poly_get_fmpz_poly(t, _t); fmpz_mod_poly_clear(_t); } else { /* TODO: We don't really need a copy */ fmpz_poly_set(t, prec->denR); } fmpz_poly_mat_scalar_mul_fmpz_poly(F, F, t); fmpz_poly_mat_scalar_mod_fmpz(F, F, pN); /* TODO: This should not be necessary? */ fmpz_poly_mat_truncate(F, prec->K); fmpz_clear(pN); fmpz_poly_clear(t); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Analytic continuation:\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } /* Steps 6 and 7 *********************************************************/ if (a == 1) { /* Step 6 {F(1) = r(t_1)^{-m} G(t_1)} ********************************/ c0 = clock(); { const long N = prec->N2 - vF; fmpz_t f, g, t, pN; fmpz_init(f); fmpz_init(g); fmpz_init(t); fmpz_init(pN); fmpz_pow_ui(pN, p, N); /* f := \hat{t_1}, g := r(\hat{t_1})^{-m} */ _padic_teichmuller(f, t1->coeffs + 0, p, N); if (prec->denR == NULL) { _fmpz_mod_poly_evaluate_fmpz(g, r->coeffs, r->length, f, pN); fmpz_powm_ui(t, g, prec->m, pN); } else { _fmpz_mod_poly_evaluate_fmpz(t, prec->denR->coeffs, prec->denR->length, f, pN); } _padic_inv(g, t, p, N); /* F1 := g G(\hat{t_1}) */ for (i = 0; i < b; i++) for (j = 0; j < b; j++) { const fmpz_poly_struct *poly = fmpz_poly_mat_entry(F, i, j); const long len = poly->length; if (len == 0) { fmpz_poly_zero(fmpz_poly_mat_entry(F1, i, j)); } else { fmpz_poly_fit_length(fmpz_poly_mat_entry(F1, i, j), 1); _fmpz_mod_poly_evaluate_fmpz(t, poly->coeffs, len, f, pN); fmpz_mul(fmpz_poly_mat_entry(F1, i, j)->coeffs + 0, g, t); fmpz_mod(fmpz_poly_mat_entry(F1, i, j)->coeffs + 0, fmpz_poly_mat_entry(F1, i, j)->coeffs + 0, pN); _fmpz_poly_set_length(fmpz_poly_mat_entry(F1, i, j), 1); _fmpz_poly_normalise(fmpz_poly_mat_entry(F1, i, j)); } } vF1 = vF; fmpz_poly_mat_canonicalise(F1, &vF1, p); fmpz_clear(f); fmpz_clear(g); fmpz_clear(t); fmpz_clear(pN); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Evaluation:\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } } else { /* Step 6 {F(1) = r(t_1)^{-m} G(t_1)} ********************************/ c0 = clock(); { const long N = prec->N2 - vF; fmpz_t pN; fmpz *f, *g, *t; fmpz_init(pN); f = _fmpz_vec_init(a); g = _fmpz_vec_init(2 * a - 1); t = _fmpz_vec_init(2 * a - 1); fmpz_pow_ui(pN, p, N); /* f := \hat{t_1}, g := r(\hat{t_1})^{-m} */ _qadic_teichmuller(f, t1->coeffs, t1->length, Qq->a, Qq->j, Qq->len, p, N); if (prec->denR == NULL) { fmpz_t e; fmpz_init_set_ui(e, prec->m); _fmpz_mod_poly_compose_smod(g, r->coeffs, r->length, f, a, Qq->a, Qq->j, Qq->len, pN); _qadic_pow(t, g, a, e, Qq->a, Qq->j, Qq->len, pN); fmpz_clear(e); } else { _fmpz_mod_poly_reduce(prec->denR->coeffs, prec->denR->length, Qq->a, Qq->j, Qq->len, pN); _fmpz_poly_normalise(prec->denR); _fmpz_mod_poly_compose_smod(t, prec->denR->coeffs, prec->denR->length, f, a, Qq->a, Qq->j, Qq->len, pN); } _qadic_inv(g, t, a, Qq->a, Qq->j, Qq->len, p, N); /* F1 := g G(\hat{t_1}) */ for (i = 0; i < b; i++) for (j = 0; j < b; j++) { const fmpz_poly_struct *poly = fmpz_poly_mat_entry(F, i, j); const long len = poly->length; fmpz_poly_struct *poly2 = fmpz_poly_mat_entry(F1, i, j); if (len == 0) { fmpz_poly_zero(poly2); } else { _fmpz_mod_poly_compose_smod(t, poly->coeffs, len, f, a, Qq->a, Qq->j, Qq->len, pN); fmpz_poly_fit_length(poly2, 2 * a - 1); _fmpz_poly_mul(poly2->coeffs, g, a, t, a); _fmpz_mod_poly_reduce(poly2->coeffs, 2 * a - 1, Qq->a, Qq->j, Qq->len, pN); _fmpz_poly_set_length(poly2, a); _fmpz_poly_normalise(poly2); } } /* Now the matrix for p^{-1} F_p at t=t_1 is (F1, vF1). */ vF1 = vF; fmpz_poly_mat_canonicalise(F1, &vF1, p); fmpz_clear(pN); _fmpz_vec_clear(f, a); _fmpz_vec_clear(g, 2 * a - 1); _fmpz_vec_clear(t, 2 * a - 1); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Evaluation:\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } /* Step 7 {Norm} *****************************************************/ /* Computes the matrix for $q^{-1} F_q$ at $t = t_1$ as the product $F \sigma(F) \dotsm \sigma^{a-1}(F)$ up appropriate transpositions because our convention of columns vs rows is the opposite of that used by Gerkmann. Note that, in any case, transpositions do not affect the characteristic polynomial. */ c0 = clock(); { const long N = prec->N1 - a * vF1; fmpz_t pN; fmpz_poly_mat_t T; fmpz_init(pN); fmpz_poly_mat_init(T, b, b); fmpz_pow_ui(pN, p, N); fmpz_poly_mat_frobenius(T, F1, 1, p, N, Qq); _qadic_mat_mul(F1, F1, T, pN, Qq); for (i = 2; i < a; i++) { fmpz_poly_mat_frobenius(T, T, 1, p, N, Qq); _qadic_mat_mul(F1, F1, T, pN, Qq); } vF1 = a * vF1; fmpz_poly_mat_canonicalise(F1, &vF1, p); fmpz_clear(pN); fmpz_poly_mat_clear(T); } c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Norm:\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } } /* Step 8 {Reverse characteristic polynomial} ****************************/ c0 = clock(); deformation_revcharpoly(cp, F1, vF1, n, d, prec->N0, prec->r, prec->s, Qq); c1 = clock(); c = (double) (c1 - c0) / CLOCKS_PER_SEC; if (verbose) { printf("Reverse characteristic polynomial:\n"); printf(" p(T) = "), fmpz_poly_print_pretty(cp, "T"), printf("\n"); printf(" Time = %f\n", c); printf("\n"); fflush(stdout); } /* Clean up **************************************************************/ padic_mat_clear(F0); mat_clear(M, ctxFracQt); free(bR); free(bC); fmpz_poly_clear(r); fmpz_poly_mat_clear(C); fmpz_poly_mat_clear(Cinv); fmpz_poly_mat_clear(F); fmpz_poly_mat_clear(F1); fmpz_poly_clear(cp); }
void fmpz_poly_divrem_divconquer(fmpz_poly_t Q, fmpz_poly_t R, const fmpz_poly_t A, const fmpz_poly_t B) { const long lenA = A->length; const long lenB = B->length; fmpz_poly_t tQ, tR; fmpz *q, *r; if (lenB == 0) { printf("Exception: division by zero in fmpz_poly_divrem_divconquer\n"); abort(); } if (lenA < lenB) { fmpz_poly_set(R, A); fmpz_poly_zero(Q); return; } if (Q == A || Q == B) { fmpz_poly_init2(tQ, lenA - lenB + 1); q = tQ->coeffs; } else { fmpz_poly_fit_length(Q, lenA - lenB + 1); q = Q->coeffs; } if (R == A || R == B) { fmpz_poly_init2(tR, lenA); r = tR->coeffs; } else { fmpz_poly_fit_length(R, lenA); r = R->coeffs; } _fmpz_poly_divrem_divconquer(q, r, A->coeffs, lenA, B->coeffs, lenB); if (Q == A || Q == B) { _fmpz_poly_set_length(tQ, lenA - lenB + 1); fmpz_poly_swap(tQ, Q); fmpz_poly_clear(tQ); } else _fmpz_poly_set_length(Q, lenA - lenB + 1); if (R == A || R == B) { _fmpz_poly_set_length(tR, lenA); fmpz_poly_swap(tR, R); fmpz_poly_clear(tR); } else _fmpz_poly_set_length(R, lenA); _fmpz_poly_normalise(Q); _fmpz_poly_normalise(R); }
void fmpz_poly_complex_roots_squarefree(const fmpz_poly_t poly, slong initial_prec, slong target_prec, slong print_digits) { slong i, j, prec, deg, deg_deflated, isolated, maxiter, deflation; acb_poly_t cpoly, cpoly_deflated; fmpz_poly_t poly_deflated; acb_ptr roots, roots_deflated; int removed_zero; if (fmpz_poly_degree(poly) < 1) return; fmpz_poly_init(poly_deflated); acb_poly_init(cpoly); acb_poly_init(cpoly_deflated); /* try to write poly as poly_deflated(x^deflation), possibly multiplied by x */ removed_zero = fmpz_is_zero(poly->coeffs); if (removed_zero) fmpz_poly_shift_right(poly_deflated, poly, 1); else fmpz_poly_set(poly_deflated, poly); deflation = fmpz_poly_deflation(poly_deflated); fmpz_poly_deflate(poly_deflated, poly_deflated, deflation); deg = fmpz_poly_degree(poly); deg_deflated = fmpz_poly_degree(poly_deflated); flint_printf("searching for %wd roots, %wd deflated\n", deg, deg_deflated); roots = _acb_vec_init(deg); roots_deflated = _acb_vec_init(deg_deflated); for (prec = initial_prec; ; prec *= 2) { acb_poly_set_fmpz_poly(cpoly_deflated, poly_deflated, prec); maxiter = FLINT_MIN(FLINT_MAX(deg_deflated, 32), prec); TIMEIT_ONCE_START flint_printf("prec=%wd: ", prec); isolated = acb_poly_find_roots(roots_deflated, cpoly_deflated, prec == initial_prec ? NULL : roots_deflated, maxiter, prec); flint_printf("%wd isolated roots | ", isolated); TIMEIT_ONCE_STOP if (isolated == deg_deflated) { if (!check_accuracy(roots_deflated, deg_deflated, target_prec)) continue; if (deflation == 1) { _acb_vec_set(roots, roots_deflated, deg_deflated); } else /* compute all nth roots */ { acb_t w, w2; acb_init(w); acb_init(w2); acb_unit_root(w, deflation, prec); acb_unit_root(w2, 2 * deflation, prec); for (i = 0; i < deg_deflated; i++) { if (arf_sgn(arb_midref(acb_realref(roots_deflated + i))) > 0) { acb_root_ui(roots + i * deflation, roots_deflated + i, deflation, prec); } else { acb_neg(roots + i * deflation, roots_deflated + i); acb_root_ui(roots + i * deflation, roots + i * deflation, deflation, prec); acb_mul(roots + i * deflation, roots + i * deflation, w2, prec); } for (j = 1; j < deflation; j++) { acb_mul(roots + i * deflation + j, roots + i * deflation + j - 1, w, prec); } } acb_clear(w); acb_clear(w2); } /* by assumption that poly is squarefree, must be just one */ if (removed_zero) acb_zero(roots + deg_deflated * deflation); if (!check_accuracy(roots, deg, target_prec)) continue; acb_poly_set_fmpz_poly(cpoly, poly, prec); if (!acb_poly_validate_real_roots(roots, cpoly, prec)) continue; for (i = 0; i < deg; i++) { if (arb_contains_zero(acb_imagref(roots + i))) arb_zero(acb_imagref(roots + i)); } flint_printf("done!\n"); break; } } if (print_digits != 0) { _acb_vec_sort_pretty(roots, deg); for (i = 0; i < deg; i++) { acb_printn(roots + i, print_digits, 0); flint_printf("\n"); } } fmpz_poly_clear(poly_deflated); acb_poly_clear(cpoly); acb_poly_clear(cpoly_deflated); _acb_vec_clear(roots, deg); _acb_vec_clear(roots_deflated, deg_deflated); }