mpz_class renf_elem_class::num() const noexcept { mpz_class x; if (nf == nullptr) { fmpz_get_mpz(x.__get_mp(), fmpq_numref(b)); return x; } else if (nf->renf_t()->nf->flag & NF_LINEAR) fmpz_get_mpz(x.__get_mp(), LNF_ELEM_NUMREF(a->elem)); else if (nf->renf_t()->nf->flag & NF_QUADRATIC) { assert(fmpz_is_zero(QNF_ELEM_NUMREF(a->elem) + 1) && "renf_elem_class not a rational"); fmpz_get_mpz(x.__get_mp(), QNF_ELEM_NUMREF(a->elem)); } else { if (fmpq_poly_length(NF_ELEM(a->elem)) == 0) mpz_set_si(x.__get_mp(), 0); else { assert(fmpq_poly_length(NF_ELEM(a->elem)) == 1 && "renf_elem_class not a rational"); fmpz_get_mpz(x.__get_mp(), NF_ELEM_NUMREF(a->elem)); } } return x; }
std::vector<mpz_class> renf_elem_class::num_vector() const noexcept { mpz_class x; std::vector<mpz_class> res; if (nf == nullptr) { fmpz_get_mpz(x.__get_mp(), fmpq_numref(b)); res.push_back(x); } else { fmpq_poly_t f; fmpq_poly_init(f); nf_elem_get_fmpq_poly(f, a->elem, nf->renf_t()->nf); for (size_t i = 0; i < fmpq_poly_length(f); i++) { fmpz_get_mpz(x.__get_mp(), fmpq_poly_numref(f) + i); res.push_back(x); } size_t deg = fmpq_poly_degree(nf->renf_t()->nf->pol); for (size_t i = fmpq_poly_length(f); i < deg; i++) res.push_back(mpz_class(0)); fmpq_poly_clear(f); } return res; }
mpz_class renf_elem_class::den() const noexcept { mpz_class res; if (nf == nullptr) fmpz_get_mpz(res.__get_mp(), fmpq_denref(b)); else { fmpz_t z; fmpz_init(z); nf_elem_get_den(z, a->elem, nf->renf_t()->nf); fmpz_get_mpz(res.__get_mp(), z); fmpz_clear(z); } return res; }
void fmpz_poly_bit_unpack_unsigned(fmpz_poly_t poly, const fmpz_t f, mp_bitcnt_t bit_size) { slong len; mpz_t tmp; if (fmpz_sgn(f) < 0) { flint_printf("Exception (fmpz_poly_bit_unpack_unsigned). Expected an unsigned value.\n"); abort(); } if (bit_size == 0 || fmpz_is_zero(f)) { fmpz_poly_zero(poly); return; } len = (fmpz_bits(f) + bit_size - 1) / bit_size; mpz_init2(tmp, bit_size*len); flint_mpn_zero(tmp->_mp_d, tmp->_mp_alloc); fmpz_get_mpz(tmp, f); fmpz_poly_fit_length(poly, len); _fmpz_poly_bit_unpack_unsigned(poly->coeffs, len, tmp->_mp_d, bit_size); _fmpz_poly_set_length(poly, len); _fmpz_poly_normalise(poly); mpz_clear(tmp); }
static void flint_to_fplll(IntMatrix &b, fmpz_mat_t A) { for(int i=0; i<A->r; i++) { for(int j=0; j<A->c; j++) { fmpz_get_mpz(b[i][j].getData(), fmpz_mat_entry(A, i, j)); } } }
inline void determinant(const DMatZZGMP& A, M2::ARingZZGMP::ElementType& result_det) { FlintZZMat A1(A); fmpz_t det; fmpz_init(det); fmpz_mat_det(det, A1.value()); fmpz_get_mpz(&result_det, det); fmpz_clear(det); }
void fmpz_randm_crypto(fmpz_t f, cryptorand_t state, const fmpz_t m) { mpz_t x, rop; mpz_init(x); mpz_init(rop); fmpz_get_mpz(x, m); mpz_urandomm_crypto(rop, state, x); fmpz_set_mpz(f, rop); mpz_clear(x); mpz_clear(rop); }
void convert_fmp2j(JNIEnv* env, fmpz_t mvalue, jbyteArray* jvalue) { mpz_t tmp; mpz_init(tmp); fmpz_get_mpz(tmp, mvalue); convert_mp2j(env, tmp, jvalue); mpz_clear(tmp); }
int main(void) { int i, result; flint_rand_t state; printf("dlog...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 10000; i++) { fmpz_t x; mpz_t z; mpfr_t r; double y, w; fmpz_init(x); mpz_init(z); mpfr_init2(r, 53); fmpz_randtest_not_zero(x, state, 10000); fmpz_abs(x, x); y = fmpz_dlog(x); fmpz_get_mpz(z, x); mpfr_set_z(r, z, MPFR_RNDN); mpfr_log(r, r, MPFR_RNDN); w = mpfr_get_d(r, MPFR_RNDN); result = (FLINT_ABS(y - w) <= w * 1e-13); if (!result) { printf("FAIL:\n"); printf("x = "), fmpz_print(x), printf("\n"); printf("y = %.20g\n", y); printf("w = %.20g\n", w); abort(); } fmpz_clear(x); mpz_clear(z); mpfr_clear(r); } mpfr_free_cache(); flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
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 fmpz2bn(bn_t out, fmpz_t in) { mpz_t temp; mpz_init(temp); fmpz_get_mpz(temp , in); mpz2bn(out, temp); mpz_clear(temp); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("setbit...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 100000; i++) { ulong j; fmpz_t a, c; mpz_t b; fmpz_init(a); fmpz_init(c); mpz_init(b); fmpz_randtest(a, state, 2 * FLINT_BITS); fmpz_get_mpz(b, a); j = n_randint(state, 3 * FLINT_BITS); fmpz_setbit(a, j); mpz_setbit(b, j); fmpz_set_mpz(c, b); result = (fmpz_equal(a, c)); if (!result) { printf("FAIL:\n"); printf("a = "), fmpz_print(a), printf("\n"); gmp_printf("b = %Zd\n", b); printf("c = "), fmpz_print(c), printf("\n"); printf("j = %ld\n", j); abort(); } fmpz_clear(a); fmpz_clear(c); mpz_clear(b); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
mpz_class renf_elem_class::floor() const noexcept { fmpz_t tmp; fmpz_init(tmp); if (nf == nullptr) fmpz_fdiv_q(tmp, fmpq_numref(b), fmpq_denref(b)); else renf_elem_floor(tmp, a, nf->renf_t()); mpz_class z; fmpz_get_mpz(z.get_mpz_t(), tmp); fmpz_clear(tmp); return z; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("is_square...."); fflush(stdout); for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a; mpz_t b; int r1, r2; fmpz_init(a); mpz_init(b); fmpz_randtest(a, state, 200); if (n_randint(state, 2) == 0) fmpz_mul(a, a, a); fmpz_get_mpz(b, a); r1 = fmpz_is_square(a); r2 = mpz_perfect_square_p(b); result = (r1 == r2); if (!result) { flint_printf("FAIL:\n"); gmp_printf("b = %Zd\n", b); abort(); } fmpz_clear(a); mpz_clear(b); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int gghlite_enc_is_zero(const gghlite_params_t self, const fmpz_mod_poly_t op) { gghlite_clr_t t; mpfr_t norm, bound; int r; gghlite_clr_init(t); _gghlite_enc_extract_raw(t, self, op); mpfr_init2(norm, _gghlite_prec(self)); mpfr_init2(bound, _gghlite_prec(self)); fmpz_poly_2norm_mpfr(norm, t, MPFR_RNDN); { /* Set bound = q */ mpz_t q_z; mpz_init(q_z); fmpz_get_mpz(q_z, self->q); mpfr_set_z(bound, q_z, MPFR_RNDN); mpz_clear(q_z); } { /* compute q^{1-xi} */ mpfr_t ex; mpfr_init2(ex, _gghlite_prec(self)); mpfr_set_ui(ex, 1, MPFR_RNDN); mpfr_sub(ex, ex, self->xi, MPFR_RNDN); mpfr_pow(bound, bound, ex, MPFR_RNDN); mpfr_clear(ex); } r = mpfr_cmp(norm, bound); mpfr_clear(bound); mpfr_clear(norm); gghlite_clr_clear(t); if (r <= 0) return 1; else return 0; }
mpz_class renf_elem_class::ceil() const noexcept { fmpz_t tmp; fmpz_init(tmp); if (nf == nullptr) { fmpz_add(tmp, fmpq_numref(b), fmpq_denref(b)); fmpz_sub_ui(tmp, tmp, 1); fmpz_fdiv_q(tmp, tmp, fmpq_denref(b)); } else renf_elem_ceil(tmp, a, nf->renf_t()); mpz_class z; fmpz_get_mpz(z.get_mpz_t(), tmp); fmpz_clear(tmp); return z; }
void fmpz_poly_bit_unpack(fmpz_poly_t poly, const fmpz_t f, mp_bitcnt_t bit_size) { slong len; mpz_t tmp; int negate, borrow; if (bit_size == 0 || fmpz_is_zero(f)) { fmpz_poly_zero(poly); return; } /* Round up */ len = (fmpz_bits(f) + bit_size - 1) / bit_size; negate = (fmpz_sgn(f) < 0) ? -1 : 0; mpz_init2(tmp, bit_size*len); /* TODO: avoid all this wastefulness */ flint_mpn_zero(tmp->_mp_d, tmp->_mp_alloc); fmpz_get_mpz(tmp, f); fmpz_poly_fit_length(poly, len + 1); borrow = _fmpz_poly_bit_unpack(poly->coeffs, len, tmp->_mp_d, bit_size, negate); if (borrow) { fmpz_set_si(poly->coeffs + len, negate ? WORD(-1) : WORD(1)); _fmpz_poly_set_length(poly, len + 1); } else { _fmpz_poly_set_length(poly, len); _fmpz_poly_normalise(poly); } mpz_clear(tmp); }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("divexact_si...."); fflush(stdout); for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, c; mpz_t e, f, g; slong n; fmpz_init(a); fmpz_init(c); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); n = z_randtest_not_zero(state); fmpz_mul_si(c, a, n); fmpz_get_mpz(e, c); fmpz_divexact_si(a, c, n); flint_mpz_divexact_ui(f, e, FLINT_ABS(n)); if (n < 0) mpz_neg(f, f); fmpz_get_mpz(g, a); result = (mpz_cmp(f, g) == 0); if (!result) { flint_printf("FAIL:\n"); gmp_printf("n = %wd, e = %Zd, f = %Zd, g = %Zd\n", n, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(c); mpz_clear(e); mpz_clear(f); mpz_clear(g); } /* Test aliasing of a and c */ for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, c; mpz_t d, f, g; slong n; fmpz_init(a); fmpz_init(c); mpz_init(d); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); n = z_randtest_not_zero(state); fmpz_mul_si(c, a, n); fmpz_get_mpz(d, c); fmpz_divexact_si(c, c, n); flint_mpz_divexact_ui(f, d, FLINT_ABS(n)); if (n < 0) mpz_neg(f, f); fmpz_get_mpz(g, c); result = (mpz_cmp(f, g) == 0); if (!result) { flint_printf("FAIL;\n"); gmp_printf("d = %Zd, n = %wd, f = %Zd, g = %Zd\n", d, n, f, g); abort(); } fmpz_clear(a); fmpz_clear(c); mpz_clear(d); mpz_clear(f); mpz_clear(g); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("pow_ui...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 100000; i++) { fmpz_t a, b; mpz_t d, e, f; ulong x; fmpz_init(a); fmpz_init(b); mpz_init(d); mpz_init(e); mpz_init(f); fmpz_randtest(a, state, 200); fmpz_get_mpz(d, a); x = n_randint(state, 20); fmpz_pow_ui(b, a, x); mpz_pow_ui(e, d, x); fmpz_get_mpz(f, b); result = (mpz_cmp(e, f) == 0); if (!result) { printf("FAIL:\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, x = %lu\n", d, e, f, x); abort(); } fmpz_clear(a); fmpz_clear(b); mpz_clear(d); mpz_clear(e); mpz_clear(f); } /* Check aliasing of a and b */ for (i = 0; i < 100000; i++) { fmpz_t a; mpz_t d, e, f; ulong x; fmpz_init(a); mpz_init(d); mpz_init(e); mpz_init(f); fmpz_randtest(a, state, 200); fmpz_get_mpz(d, a); x = n_randint(state, 20); fmpz_pow_ui(a, a, x); mpz_pow_ui(e, d, x); fmpz_get_mpz(f, a); result = (mpz_cmp(e, f) == 0); if (!result) { printf("FAIL:\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, x = %lu\n", d, e, f, x); abort(); } fmpz_clear(a); mpz_clear(d); mpz_clear(e); mpz_clear(f); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i; flint_rand_t state; printf("init_set_readonly...."); fflush(stdout); flint_randinit(state); /* Create some small fmpz integers, clear the mpz_t */ for (i = 0; i < 100000; i++) { fmpz_t f; mpz_t z; *f = z_randint(state, COEFF_MAX + 1); mpz_init(z); fmpz_get_mpz(z, f); { fmpz_t g; fmpz_init_set_readonly(g, z); fmpz_clear_readonly(g); } mpz_clear(z); } /* Create some small fmpz integers, do *not* clear the mpz_t */ for (i = 0; i < 100000; i++) { fmpz_t f; mpz_t z; *f = z_randint(state, COEFF_MAX + 1); mpz_init(z); fmpz_get_mpz(z, f); { fmpz_t g; fmpz_init_set_readonly(g, z); } mpz_clear(z); } /* Create some more fmpz integers */ for (i = 0; i < 100000; i++) { fmpz_t f; mpz_t z; fmpz_init(f); fmpz_randtest(f, state, 2 * FLINT_BITS); mpz_init(z); fmpz_get_mpz(z, f); { fmpz_t g, h; fmpz_init_set_readonly(g, z); fmpz_init(h); fmpz_set_mpz(h, z); if (!fmpz_equal(g, h)) { printf("FAIL:\n\n"); printf("g = "), fmpz_print(g), printf("\n"); printf("h = "), fmpz_print(h), printf("\n"); gmp_printf("z = %Zd\n", z); } fmpz_clear_readonly(g); fmpz_clear(h); } fmpz_clear(f); mpz_clear(z); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("evaluate..."); fflush(stdout); flint_randinit(state); /* Check aliasing */ for (i = 0; i < 100; i++) { int ans1, ans2; mpq_t a, b; fmpz_t num, den; fmpz_poly_q_t f; mpq_init(a); mpq_init(b); fmpz_init(num); fmpz_init(den); fmpz_poly_q_init(f); fmpz_poly_q_randtest(f, state, n_randint(state, 10), 10, n_randint(state, 10), 10); fmpz_randtest(num, state, 50); fmpz_randtest_not_zero(den, state, 50); fmpz_get_mpz(mpq_numref(a), num); fmpz_get_mpz(mpq_denref(a), den); mpq_canonicalize(a); ans1 = fmpz_poly_q_evaluate(b, f, a); ans2 = fmpz_poly_q_evaluate(a, f, a); result = (ans1 == ans2) && mpq_equal(a, b); if (!result) { printf("FAIL:\n"); printf("f = "), fmpz_poly_q_print(f), printf("\n"); printf("num = "), fmpz_print(num), printf("\n"); printf("den = "), fmpz_print(den), printf("\n"); gmp_printf("a = %Qd\n", a); gmp_printf("b = %Qd\n", b); printf("ans1 = %d\n", ans1); printf("ans2 = %d\n", ans2); abort(); } mpq_clear(a); mpq_clear(b); fmpz_clear(num); fmpz_clear(den); fmpz_poly_q_clear(f); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, j, result; flint_rand_t state; ulong cflags = 0UL; mpq_t n1, n2; printf("get/set_coeff_mpz...."); fflush(stdout); mpq_init(n1); mpq_init(n2); flint_randinit(state); for (i = 0; i < 1000UL; i++) { fmpq_poly_t a; fmpz_t x1, x2; long coeff, len; fmpq_poly_init(a); fmpz_init(x1); fmpz_init(x2); len = (long) (n_randint(state, 100) + 1); for (j = 0; j < 1000; j++) { fmpz_randtest(x1, state, 200); fmpz_get_mpz(mpq_numref(n1), x1); mpz_set_si(mpq_denref(n1), 1); coeff = (long) n_randint(state, len); fmpq_poly_set_coeff_mpz(a, coeff, mpq_numref(n1)); fmpq_poly_get_coeff_mpq(n2, a, coeff); result = (mpq_equal(n1, n2)); if (!result) { printf("FAIL:\n\n"); printf("a = "), fmpq_poly_debug(a), printf("\n\n"); printf("coeff = %ld\n\n", coeff); printf("len = %ld\n\n", len); printf("cflags = %lu\n\n", cflags); gmp_printf("n1 = %Qd\n\n", n1); gmp_printf("n2 = %Qd\n\n", n2); abort(); } } fmpz_clear(x1); fmpz_clear(x2); fmpq_poly_clear(a); } mpq_clear(n1); mpq_clear(n2); flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("remove...."); fflush(stdout); /* Compare with MPIR, random input */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_t a, b, c; mpz_t d, e, f, g; slong x, y; fmpz_init(a); fmpz_init(b); fmpz_init(c); mpz_init(d); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest_not_zero(a, state, 200); do { fmpz_randtest_not_zero(b, state, 200); fmpz_abs(b, b); } while (fmpz_is_one(b)); fmpz_get_mpz(d, a); fmpz_get_mpz(e, b); x = fmpz_remove(c, a, b); y = mpz_remove(f, d, e); fmpz_get_mpz(g, c); result = ((x == y) && (mpz_cmp(f, g) == 0)); if (!result) { flint_printf("FAIL:\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, g = %Zd\n", d, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); mpz_clear(d); mpz_clear(e); mpz_clear(f); mpz_clear(g); } /* Compare with MPIR, random input but ensure that factors exist */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_t a, b, c, pow; mpz_t d, e, f, g; slong x, y; ulong n; fmpz_init(a); fmpz_init(b); fmpz_init(c); fmpz_init(pow); mpz_init(d); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest_not_zero(a, state, 200); do { fmpz_randtest_not_zero(b, state, 200); fmpz_abs(b, b); } while (fmpz_is_one(b)); n = n_randint(state, 10); fmpz_pow_ui(pow, b, n); fmpz_mul(a, a, pow); fmpz_get_mpz(d, a); fmpz_get_mpz(e, b); x = fmpz_remove(c, a, b); y = mpz_remove(f, d, e); fmpz_get_mpz(g, c); result = ((x == y) && (x >= n) && (mpz_cmp(f, g) == 0)); if (!result) { flint_printf("FAIL:\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, g = %Zd\n", d, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); fmpz_clear(pow); mpz_clear(d); mpz_clear(e); mpz_clear(f); mpz_clear(g); } /* Check aliasing of a and b */ for (i = 0; i < 100 * flint_test_multiplier(); i++) { fmpz_t a, c; slong x; fmpz_init(a); fmpz_init(c); do { fmpz_randtest_not_zero(a, state, 200); fmpz_abs(a, a); } while (fmpz_is_one(a)); x = fmpz_remove(c, a, a); result = ((x == 1) && (fmpz_cmp_ui(c, 1) == 0)); if (!result) { flint_printf("FAIL:\n"); fmpz_print(a), flint_printf("\n"); fmpz_print(c), flint_printf("\n"); abort(); } fmpz_clear(a); fmpz_clear(c); } /* Check aliasing of a and c */ for (i = 0; i < 100 * flint_test_multiplier(); i++) { fmpz_t a, b, c; slong x, y; fmpz_init(a); fmpz_init(b); fmpz_init(c); fmpz_randtest_not_zero(a, state, 200); do { fmpz_randtest_not_zero(b, state, 200); fmpz_abs(b, b); } while (fmpz_is_one(b)); x = fmpz_remove(c, a, b); y = fmpz_remove(a, a, b); result = ((x == y) && fmpz_equal(a, c)); if (!result) { flint_printf("FAIL:\n"); fmpz_print(a), flint_printf("\n"); fmpz_print(b), flint_printf("\n"); fmpz_print(c), flint_printf("\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); } /* Check aliasing of b and c */ for (i = 0; i < 100 * flint_test_multiplier(); i++) { fmpz_t a, b, c; slong x, y; fmpz_init(a); fmpz_init(b); fmpz_init(c); fmpz_randtest_not_zero(a, state, 200); do { fmpz_randtest_not_zero(b, state, 200); fmpz_abs(b, b); } while (fmpz_is_one(b)); x = fmpz_remove(c, a, b); y = fmpz_remove(b, a, b); result = ((x == y) && fmpz_equal(b, c)); if (!result) { flint_printf("FAIL:\n"); fmpz_print(a), flint_printf("\n"); fmpz_print(b), flint_printf("\n"); fmpz_print(c), flint_printf("\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; flint_randinit(state); printf("bit_pack/bit_unpack...."); fflush(stdout); /* Check aliasing of a and c */ for (i = 0; i < 10000; i++) { nmod_poly_t a, b; mp_limb_t n; ulong bits; mp_ptr mpn; do { n = n_randtest_not_zero(state); } while (n == 1); bits = 2 * FLINT_BIT_COUNT(n) + n_randint(state, FLINT_BITS); nmod_poly_init(a, n); nmod_poly_init(b, n); do { nmod_poly_randtest(a, state, n_randint(state, 100)); } while (a->length == 0); mpn = flint_malloc(sizeof(mp_limb_t) * ((bits * a->length - 1) / FLINT_BITS + 1)); _nmod_poly_bit_pack(mpn, a->coeffs, a->length, bits); nmod_poly_fit_length(b, a->length); _nmod_poly_bit_unpack(b->coeffs, a->length, mpn, bits, a->mod); b->length = a->length; result = (nmod_poly_equal(a, b)); if (!result) { printf("FAIL:\n"); nmod_poly_print(a), printf("\n\n"); nmod_poly_print(b), printf("\n\n"); abort(); } nmod_poly_clear(a); nmod_poly_clear(b); flint_free(mpn); } for (i = 0; i < 20000; i++) { fmpz_t f; nmod_poly_t A, B; long b; mp_limb_t n; do { n = n_randtest_not_zero(state); } while (n == 1); fmpz_init(f); nmod_poly_init(A, n); nmod_poly_init(B, n); nmod_poly_randtest(A, state, 1+n_randint(state,100)); b = FLINT_BIT_COUNT(n) + n_randint(state, FLINT_BITS); nmod_poly_bit_pack(f, A, b); nmod_poly_bit_unpack(B, f, b); if (!nmod_poly_equal(A, B)) { mpz_t zz; printf("FAIL:\n"); printf("INPUT: "); nmod_poly_print(A); printf("\n"); mpz_init(zz); fmpz_get_mpz(zz, f); printf("PACKED: "); mpz_out_str(stdout, 2, zz); printf("\n"); printf("OUTPUT: "); nmod_poly_print(B); printf("\n\n"); abort(); } fmpz_clear(f); nmod_poly_clear(A); nmod_poly_clear(B); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("equal...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 100000; i++) { fmpz_t a, b; fmpz_init(a); fmpz_init(b); fmpz_randtest(a, state, 200); fmpz_set(b, a); result = (fmpz_equal(a, b)); if (!result) { printf("FAIL:\n"); printf("a = "), fmpz_print(a), printf("\n"); printf("b = "), fmpz_print(b), printf("\n"); abort(); } fmpz_clear(a); fmpz_clear(b); } for (i = 0; i < 100000; i++) { fmpz_t a, b; mpz_t c, d; fmpz_init(a); fmpz_init(b); mpz_init(c); mpz_init(d); fmpz_randtest(a, state, 200); fmpz_randtest(b, state, 200); fmpz_get_mpz(c, a); fmpz_get_mpz(d, b); result = (fmpz_equal(a, b) == (mpz_cmp(c, d) == 0)); if (!result) { printf("FAIL:\n"); gmp_printf("c = %Zd, d = %Zd\n", c, d); abort(); } fmpz_clear(a); fmpz_clear(b); mpz_clear(c); mpz_clear(d); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("divexact_ui...."); fflush(stdout); flint_randinit(state); for (i = 0; i < 100000; i++) { fmpz_t a, c; mpz_t e, f, g; ulong n; fmpz_init(a); fmpz_init(c); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); n = n_randtest_not_zero(state); fmpz_mul_ui(c, a, n); fmpz_get_mpz(e, c); fmpz_divexact_ui(a, c, n); mpz_divexact_ui(f, e, n); fmpz_get_mpz(g, a); result = (mpz_cmp(f, g) == 0); if (!result) { printf("FAIL1\n"); gmp_printf("n = %lu, e = %Zd, f = %Zd, g = %Zd\n", n, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(c); mpz_clear(e); mpz_clear(f); mpz_clear(g); } /* Test aliasing of a and c */ for (i = 0; i < 100000; i++) { fmpz_t a, c; mpz_t d, f, g; ulong n; fmpz_init(a); fmpz_init(c); mpz_init(d); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); n = n_randtest_not_zero(state); fmpz_mul_ui(c, a, n); fmpz_get_mpz(d, c); fmpz_divexact_ui(c, c, n); mpz_divexact_ui(f, d, n); fmpz_get_mpz(g, c); result = (mpz_cmp(f, g) == 0); if (!result) { printf("FAIL:\n"); gmp_printf("d = %Zd, n = %lu, f = %Zd, g = %Zd\n", d, n, f, g); abort(); } fmpz_clear(a); fmpz_clear(c); mpz_clear(d); mpz_clear(f); mpz_clear(g); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("cdiv_q...."); fflush(stdout); for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, b, c; mpz_t d, e, f, g; fmpz_init(a); fmpz_init(b); fmpz_init(c); mpz_init(d); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); fmpz_randtest_not_zero(b, state, 200); fmpz_get_mpz(d, a); fmpz_get_mpz(e, b); fmpz_cdiv_q(c, a, b); mpz_cdiv_q(f, d, e); fmpz_get_mpz(g, c); result = (mpz_cmp(f, g) == 0); if (!result) { flint_printf("FAIL\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, g = %Zd\n", d, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(b); fmpz_clear(c); mpz_clear(d); mpz_clear(e); mpz_clear(f); mpz_clear(g); } /* Check aliasing of a and b */ for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, c; mpz_t d, f, g; fmpz_init(a); fmpz_init(c); mpz_init(d); mpz_init(f); mpz_init(g); fmpz_randtest_not_zero(a, state, 200); fmpz_get_mpz(d, a); fmpz_cdiv_q(c, a, a); mpz_cdiv_q(f, d, d); fmpz_get_mpz(g, c); result = (mpz_cmp(f, g) == 0); if (!result) { flint_printf("FAIL\n"); gmp_printf("d = %Zd, f = %Zd, g = %Zd\n", d, f, g); abort(); } fmpz_clear(a); fmpz_clear(c); mpz_clear(d); mpz_clear(f); mpz_clear(g); } /* Test aliasing of a and c */ for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, b; mpz_t d, e, f, g; fmpz_init(a); fmpz_init(b); mpz_init(d); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); fmpz_randtest_not_zero(b, state, 200); fmpz_get_mpz(d, a); fmpz_get_mpz(e, b); fmpz_cdiv_q(a, a, b); mpz_cdiv_q(f, d, e); fmpz_get_mpz(g, a); result = (mpz_cmp(f, g) == 0); if (!result) { flint_printf("FAIL\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, g = %Zd\n", d, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(b); mpz_clear(d); mpz_clear(e); mpz_clear(f); mpz_clear(g); } /* Test aliasing of b and c */ for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, b; mpz_t d, e, f, g; fmpz_init(a); fmpz_init(b); mpz_init(d); mpz_init(e); mpz_init(f); mpz_init(g); fmpz_randtest(a, state, 200); fmpz_randtest_not_zero(b, state, 200); fmpz_get_mpz(d, a); fmpz_get_mpz(e, b); fmpz_cdiv_q(b, a, b); mpz_cdiv_q(f, d, e); fmpz_get_mpz(g, b); result = (mpz_cmp(f, g) == 0); if (!result) { flint_printf("FAIL\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, g = %Zd\n", d, e, f, g); abort(); } fmpz_clear(a); fmpz_clear(b); mpz_clear(d); mpz_clear(e); mpz_clear(f); mpz_clear(g); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, j, result; ulong cflags = UWORD(0); mpq_t n1, n2; FLINT_TEST_INIT(state); flint_printf("get/set_coeff_fmpz...."); fflush(stdout); mpq_init(n1); mpq_init(n2); for (i = 0; i < 100 * flint_test_multiplier(); i++) { fmpq_poly_t a; fmpz_t x1, x2; slong coeff, len; fmpq_poly_init(a); fmpz_init(x1); fmpz_init(x2); len = (slong) (n_randint(state, 100) + 1); for (j = 0; j < 100; j++) { fmpz_randtest(x1, state, 200); fmpz_get_mpz(mpq_numref(n1), x1); flint_mpz_set_si(mpq_denref(n1), 1); coeff = (slong) n_randint(state, len); fmpq_poly_set_coeff_fmpz(a, coeff, x1); fmpq_poly_get_coeff_mpq(n2, a, coeff); cflags |= fmpq_poly_is_canonical(a) ? 0 : 1; result = (mpq_equal(n1, n2) && !cflags); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), fmpq_poly_debug(a), flint_printf("\n\n"); flint_printf("coeff = %wd\n\n", coeff); flint_printf("len = %wd\n\n", len); flint_printf("cflags = %wu\n\n", cflags); gmp_printf("n1 = %Qd\n\n", n1); gmp_printf("n2 = %Qd\n\n", n2); abort(); } } fmpz_clear(x1); fmpz_clear(x2); fmpq_poly_clear(a); } mpq_clear(n1); mpq_clear(n2); FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("scalar_mul_mpq... "); fflush(stdout); flint_randinit(state); /* Check aliasing of a and b */ for (i = 0; i < 100; i++) { fmpz_poly_q_t a, b; fmpz_t x1, x2; mpq_t y; fmpz_poly_q_init(a); fmpz_poly_q_init(b); fmpz_init(x1); fmpz_init(x2); mpq_init(y); fmpz_poly_q_randtest(b, state, n_randint(state, 50), 50, n_randint(state, 50), 50); fmpz_randtest(x1, state, 50); fmpz_randtest_not_zero(x2, state, 50); fmpz_get_mpz(mpq_numref(y), x1); fmpz_get_mpz(mpq_denref(y), x2); mpq_canonicalize(y); fmpz_poly_q_scalar_mul_mpq(a, b, y); fmpz_poly_q_scalar_mul_mpq(b, b, y); result = fmpz_poly_q_equal(a, b); if (!result) { printf("FAIL:\n"); fmpz_poly_q_print(a), printf("\n\n"); fmpz_poly_q_print(b), printf("\n\n"); abort(); } fmpz_poly_q_clear(a); fmpz_poly_q_clear(b); fmpz_clear(x1); fmpz_clear(x2); mpq_clear(y); } /* Check that x (a + b) == x * a + x * b */ for (i = 0; i < 100; i++) { fmpz_poly_q_t a, b, c, d; fmpz_t x1, x2; mpq_t y; fmpz_poly_q_init(a); fmpz_poly_q_init(b); fmpz_poly_q_init(c); fmpz_poly_q_init(d); fmpz_init(x1); fmpz_init(x2); mpq_init(y); fmpz_poly_q_randtest(a, state, n_randint(state, 50), 50, n_randint(state, 50), 50); fmpz_poly_q_randtest(b, state, n_randint(state, 50), 50, n_randint(state, 50), 50); fmpz_randtest(x1, state, 50); fmpz_randtest_not_zero(x2, state, 50); fmpz_get_mpz(mpq_numref(y), x1); fmpz_get_mpz(mpq_denref(y), x2); mpq_canonicalize(y); fmpz_poly_q_scalar_mul_mpq(c, a, y); fmpz_poly_q_scalar_mul_mpq(d, b, y); fmpz_poly_q_add(d, c, d); fmpz_poly_q_add(c, a, b); fmpz_poly_q_scalar_mul_mpq(c, c, y); result = fmpz_poly_q_equal(c, d) && fmpz_poly_q_is_canonical(c); if (!result) { printf("FAIL:\n"); printf("a = "), fmpz_poly_q_print(a), printf("\n\n"); printf("b = "), fmpz_poly_q_print(b), printf("\n\n"); printf("c = "), fmpz_poly_q_print(c), printf("\n\n"); printf("d = "), fmpz_poly_q_print(d), printf("\n\n"); gmp_printf("y = %Qd\n\n", y); abort(); } fmpz_poly_q_clear(a); fmpz_poly_q_clear(b); fmpz_poly_q_clear(c); fmpz_poly_q_clear(d); fmpz_clear(x1); fmpz_clear(x2); mpq_clear(y); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("sub_ui...."); fflush(stdout); for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a, b; mpz_t d, e, f; ulong x; fmpz_init(a); fmpz_init(b); mpz_init(d); mpz_init(e); mpz_init(f); fmpz_randtest(a, state, 200); fmpz_get_mpz(d, a); x = n_randtest(state); fmpz_sub_ui(b, a, x); flint_mpz_sub_ui(e, d, x); fmpz_get_mpz(f, b); result = (mpz_cmp(e, f) == 0); if (!result) { flint_printf("FAIL:\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, x = %wu\n", d, e, f, x); abort(); } fmpz_clear(a); fmpz_clear(b); mpz_clear(d); mpz_clear(e); mpz_clear(f); } /* Check aliasing of a and b */ for (i = 0; i < 10000 * flint_test_multiplier(); i++) { fmpz_t a; mpz_t d, e, f; ulong x; fmpz_init(a); mpz_init(d); mpz_init(e); mpz_init(f); fmpz_randtest(a, state, 200); fmpz_get_mpz(d, a); x = n_randtest(state); fmpz_sub_ui(a, a, x); flint_mpz_sub_ui(e, d, x); fmpz_get_mpz(f, a); result = (mpz_cmp(e, f) == 0); if (!result) { flint_printf("FAIL:\n"); gmp_printf("d = %Zd, e = %Zd, f = %Zd, x = %wu\n", d, e, f, x); abort(); } fmpz_clear(a); mpz_clear(d); mpz_clear(e); mpz_clear(f); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }