void check_renf(renf_t nf) { arb_ptr a; arb_t b; a = nf->emb; if (fmpq_poly_length(nf->nf->pol) != fmpz_poly_length(nf->der) + 1) { printf("FAIL:\n"); printf("uninitalized derivative"); fflush(stdout); abort(); } arb_init(b); fmpq_poly_evaluate_arb(b, nf->nf->pol, a, nf->prec); if (!arb_contains_zero(b)) { printf("FAIL:\n"); printf("evaluation does not contain zero\n"); printf("pol = "); fmpq_poly_print_pretty(nf->nf->pol, "x"); printf("\n"); printf("a = "); arb_printd(a, 10); printf("\n"); printf("b = "); arb_printd(b, 10); printf("\n"); fflush(stdout); abort(); } arb_clear(b); }
void fmprb_poly_set_fmpz_poly(fmprb_poly_t poly, const fmpz_poly_t src, long prec) { long i, len = fmpz_poly_length(src); fmprb_poly_fit_length(poly, len); _fmprb_poly_set_length(poly, len); for (i = 0; i < len; i++) fmprb_set_round_fmpz(poly->coeffs + i, src->coeffs + i, prec); }
Tuple* from_fmpz_poly(fmpz_poly_t x){ uint n=fmpz_poly_length(x); Tuple* r=list(n); fmpz_t temp; fmpz_init(temp); for(uint i=0;i<n;i++){ fmpz_poly_get_coeff_fmpz(temp,x,i); r->tuple[i+1]=new Integer; fmpz_get_mpz(r->tuple[i+1].cast<Integer>().mpz,temp); } return r; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("get_coeff_ptr...."); fflush(stdout); for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_poly_t A; fmpz_t a; slong n = n_randint(state, 100); fmpz_poly_init(A); fmpz_poly_randtest(A, state, n_randint(state, 100), 100); fmpz_init(a); fmpz_poly_get_coeff_fmpz(a, A, n); result = n < fmpz_poly_length(A) ? fmpz_equal(a, fmpz_poly_get_coeff_ptr(A, n)) : fmpz_poly_get_coeff_ptr(A, n) == NULL; if (!result) { flint_printf("FAIL:\n"); flint_printf("A = "), fmpz_poly_print(A), flint_printf("\n\n"); flint_printf("a = "), fmpz_print(a), flint_printf("\n\n"); flint_printf("n = %wd\n\n", n); abort(); } fmpz_poly_clear(A); fmpz_clear(a); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
void fmpz_poly_bit_pack(fmpz_t f, const fmpz_poly_t poly, mp_bitcnt_t bit_size) { long len; __mpz_struct * mpz; long i, d; int negate; len = fmpz_poly_length(poly); if (len == 0 || bit_size == 0) { fmpz_zero(f); return; } mpz = _fmpz_promote(f); mpz_realloc2(mpz, len * bit_size); d = mpz->_mp_alloc; mpn_zero(mpz->_mp_d, d); if (fmpz_sgn(fmpz_poly_lead(poly)) < 0) negate = -1; else negate = 0; _fmpz_poly_bit_pack(mpz->_mp_d, poly->coeffs, len, bit_size, negate); for (i = d - 1; i >= 0; i--) { if (mpz->_mp_d[i] != 0) break; } d = i + 1; mpz->_mp_size = d; _fmpz_demote_val(f); if (negate) fmpz_neg(f, f); }
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_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; }