void fmpz_poly_disc_gauss_rounding(fmpz_poly_t rop, const fmpq_poly_t x, const mpfr_t r_f, gmp_randstate_t randstate) { mpfr_t xi; mpfr_init2(xi, mpfr_get_prec(r_f)); mpf_t xi_f; mpf_init2(xi_f, mpfr_get_prec(r_f)); mpq_t xi_q; mpq_init(xi_q); mpz_t s_z; mpz_init(s_z); const long n = fmpq_poly_length(x); const size_t tau = (ceil(2*sqrt(log2((double)n))) > 3) ? ceil(2*sqrt(log2((double)n))) : 3; fmpz_poly_zero(rop); for(int i=0; i<n; i++) { fmpq_poly_get_coeff_mpq(xi_q, x, i); mpf_set_q(xi_f, xi_q); mpfr_set_f(xi, xi_f, MPFR_RNDN); dgs_disc_gauss_mp_t *D = dgs_disc_gauss_mp_init(r_f, xi, tau, DGS_DISC_GAUSS_UNIFORM_ONLINE); D->call(s_z, D, randstate); dgs_disc_gauss_mp_clear(D); fmpz_poly_set_coeff_mpz(rop, i, s_z); } mpz_clear(s_z); mpq_clear(xi_q); mpf_clear(xi_f); mpfr_clear(xi); }
Tuple* from_fmpq_poly(fmpq_poly_t x){ uint n=fmpq_poly_length(x); Tuple* r=list(n); mpq_t temp; mpq_init(temp); for(uint i=0;i<n;i++){ fmpq_poly_get_coeff_mpq(temp,x,i); if(mpz_cmp_ui(mpq_denref(temp),1)==0){ r->tuple[i+1]=new Integer; mpz_set(r->tuple[i+1].cast<Integer>().mpz,mpq_numref(temp)); } else{ r->tuple[i+1]=new Rational; mpq_set(r->tuple[i+1].cast<Rational>().mpq,temp); } } return r; }
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; ulong cflags = UWORD(0); FLINT_TEST_INIT(state); flint_printf("compose...."); fflush(stdout); /* Check aliasing of the first argument */ for (i = 0; i < 10 * flint_test_multiplier(); i++) { fmpq_poly_t f, g, h; fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_init(h); fmpq_poly_randtest(g, state, n_randint(state, 50), 100); fmpq_poly_randtest(h, state, n_randint(state, 20), 50); fmpq_poly_compose(f, g, h); fmpq_poly_compose(g, g, h); cflags |= fmpq_poly_is_canonical(f) ? 0 : 1; cflags |= fmpq_poly_is_canonical(g) ? 0 : 2; result = (fmpq_poly_equal(f, g) && !cflags); if (!result) { flint_printf("FAIL (aliasing 1):\n"); fmpq_poly_debug(f), flint_printf("\n\n"); fmpq_poly_debug(g), flint_printf("\n\n"); flint_printf("cflags = %wu\n\n", cflags); abort(); } fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); } /* Check aliasing of the second argument */ for (i = 0; i < 10 * flint_test_multiplier(); i++) { fmpq_poly_t f, g, h; fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_init(h); fmpq_poly_randtest(g, state, n_randint(state, 50), 100); fmpq_poly_randtest(h, state, n_randint(state, 20), 50); fmpq_poly_compose(f, g, h); fmpq_poly_compose(h, g, h); cflags |= fmpq_poly_is_canonical(f) ? 0 : 1; cflags |= fmpq_poly_is_canonical(h) ? 0 : 2; result = (fmpq_poly_equal(f, h) && !cflags); if (!result) { flint_printf("FAIL (aliasing 2):\n"); fmpq_poly_debug(f), flint_printf("\n\n"); fmpq_poly_debug(h), flint_printf("\n\n"); flint_printf("cflags = %wu\n\n", cflags); abort(); } fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); } /* Compare with the naive method for g(h(t)) */ for (i = 0; i < 10 * flint_test_multiplier(); i++) { fmpq_poly_t f, g, h, s, t, u; mpq_t c; slong k; fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_init(h); fmpq_poly_init(s); fmpq_poly_init(t); fmpq_poly_init(u); mpq_init(c); fmpq_poly_randtest(g, state, n_randint(state, 20), 65); fmpq_poly_randtest(h, state, n_randint(state, 20), 65); fmpq_poly_zero(s); fmpq_poly_set_ui(t, UWORD(1)); for (k = WORD(0); k < g->length; k++) { fmpq_poly_get_coeff_mpq(c, g, k); fmpq_poly_scalar_mul_mpq(u, t, c); fmpq_poly_add(s, s, u); fmpq_poly_mul(t, t, h); } fmpq_poly_compose(f, g, h); result = (fmpq_poly_equal(f, s)); if (!result) { flint_printf("FAIL (compare with naive):\n"); flint_printf("g = "), fmpq_poly_debug(g), flint_printf("\n\n"); flint_printf("h = "), fmpq_poly_debug(h), flint_printf("\n\n"); flint_printf("f = "), fmpq_poly_debug(f), flint_printf("\n\n"); flint_printf("s = "), fmpq_poly_debug(s), flint_printf("\n\n"); abort(); } fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); fmpq_poly_clear(s); fmpq_poly_clear(t); fmpq_poly_clear(u); mpq_clear(c); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
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; }