int main() { slong iter; flint_rand_t state; flint_printf("pow_fmpq...."); fflush(stdout); flint_randinit(state); /* check large arguments */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a, b, c, d; fmpq_t e1, e2, e3; slong prec1, prec2; prec1 = 2 + n_randint(state, 1000); prec2 = prec1 + 30; arb_init(a); arb_init(b); arb_init(c); arb_init(d); fmpq_init(e1); fmpq_init(e2); fmpq_init(e3); arb_randtest_precise(a, state, 1 + n_randint(state, 1000), 200); arb_randtest_precise(b, state, 1 + n_randint(state, 1000), 200); fmpq_randtest(e1, state, 200); fmpq_randtest(e2, state, 200); arb_pow_fmpq(b, a, e1, prec1); arb_pow_fmpq(c, a, e1, prec2); if (!arb_overlaps(b, c)) { flint_printf("FAIL: overlap\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("e1 = "); fmpq_print(e1); flint_printf("\n\n"); abort(); } /* check a^(e1+e2) = a^e1*a^e2 */ arb_pow_fmpq(c, a, e2, prec1); arb_mul(d, b, c, prec1); fmpq_add(e3, e1, e2); arb_pow_fmpq(c, a, e3, prec1); if (!arb_overlaps(c, d)) { flint_printf("FAIL: functional equation\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("d = "); arb_print(d); flint_printf("\n\n"); flint_printf("e1 = "); fmpq_print(e1); flint_printf("\n\n"); flint_printf("e2 = "); fmpq_print(e2); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); fmpq_clear(e1); fmpq_clear(e2); fmpq_clear(e3); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("rising_ui_rs...."); fflush(stdout); flint_randinit(state); /* compare with fmpq */ for (iter = 0; iter < 1000; iter++) { arb_t a, b; fmpq_t x, y, z; ulong n, step; slong i; arb_init(a); arb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 1000), 10); arb_randtest(b, state, 1 + n_randint(state, 1000), 10); n = n_randint(state, 80); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 10)); step = n_randint(state, 20); arb_rising_ui_rs(b, a, n, step, 2 + n_randint(state, 1000)); fmpq_one(y); for (i = 0; i < n; i++) { fmpq_set_si(z, i, 1); fmpq_add(z, x, z); fmpq_mul(y, y, z); } if (!arb_contains_fmpq(b, y)) { flint_printf("FAIL: containment\n\n"); flint_printf("n = %wu\n", n); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of y and x */ for (iter = 0; iter < 500; iter++) { arb_t x, y; ulong n, step; slong prec; arb_init(x); arb_init(y); arb_randtest(x, state, 1 + n_randint(state, 200), 10); arb_randtest(y, state, 1 + n_randint(state, 200), 10); n = n_randint(state, 100); prec = 2 + n_randint(state, 1000); step = n_randint(state, 20); arb_rising_ui_rs(y, x, n, step, prec); arb_rising_ui_rs(x, x, n, step, prec); if (!arb_equal(x, y)) { flint_printf("FAIL: aliasing\n\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("n = %wu\n", n); abort(); } arb_clear(x); arb_clear(y); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { long iter; flint_rand_t state; printf("interpolate_fast...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000; iter++) { long i, n, qbits1, qbits2, rbits1, rbits2, rbits3; fmpq_poly_t P; arb_poly_t R, S; fmpq_t t, u; arb_ptr xs, ys; fmpq_poly_init(P); arb_poly_init(R); arb_poly_init(S); fmpq_init(t); fmpq_init(u); qbits1 = 2 + n_randint(state, 200); qbits2 = 2 + n_randint(state, 5); rbits1 = 2 + n_randint(state, 200); rbits2 = 2 + n_randint(state, 200); rbits3 = 2 + n_randint(state, 200); fmpq_poly_randtest(P, state, 1 + n_randint(state, 20), qbits1); n = P->length; xs = _arb_vec_init(n); ys = _arb_vec_init(n); arb_poly_set_fmpq_poly(R, P, rbits1); if (n > 0) { fmpq_randtest(t, state, qbits2); arb_set_fmpq(xs, t, rbits2); for (i = 1; i < n; i++) { fmpq_randtest_not_zero(u, state, qbits2); fmpq_abs(u, u); fmpq_add(t, t, u); arb_set_fmpq(xs + i, t, rbits2); } } for (i = 0; i < n; i++) arb_poly_evaluate(ys + i, R, xs + i, rbits2); arb_poly_interpolate_fast(S, xs, ys, n, rbits3); if (!arb_poly_contains_fmpq_poly(S, P)) { printf("FAIL:\n"); printf("P = "); fmpq_poly_print(P); printf("\n\n"); printf("R = "); arb_poly_printd(R, 15); printf("\n\n"); printf("S = "); arb_poly_printd(S, 15); printf("\n\n"); abort(); } fmpq_poly_clear(P); arb_poly_clear(R); arb_poly_clear(S); fmpq_clear(t); fmpq_clear(u); _arb_vec_clear(xs, n); _arb_vec_clear(ys, n); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("contains_arf...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t a; arf_t b; fmpq_t am, ar, bm, t; int c1, c2; arb_init(a); arf_init(b); fmpq_init(am); fmpq_init(ar); fmpq_init(bm); fmpq_init(t); arb_randtest(a, state, 1 + n_randint(state, 500), 14); arf_randtest(b, state, 1 + n_randint(state, 500), 14); arf_get_fmpq(am, arb_midref(a)); mag_get_fmpq(ar, arb_radref(a)); arf_get_fmpq(bm, b); c1 = arb_contains_arf(a, b); fmpq_sub(t, am, ar); c2 = fmpq_cmp(t, bm) <= 0; fmpq_add(t, am, ar); c2 = c2 && (fmpq_cmp(t, bm) >= 0); if (c1 != c2) { flint_printf("FAIL:\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arf_print(b); flint_printf("\n\n"); flint_printf("am = "); fmpq_print(am); flint_printf("\n\n"); flint_printf("ar = "); fmpq_print(ar); flint_printf("\n\n"); flint_printf("bm = "); fmpq_print(bm); flint_printf("\n\n"); flint_printf("t = "); fmpq_print(t); flint_printf("\n\n"); flint_printf("c1 = %d, c2 = %d\n\n", c1, c2); abort(); } arb_clear(a); arf_clear(b); fmpq_clear(am); fmpq_clear(ar); fmpq_clear(bm); fmpq_clear(t); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("evaluate_fmpz...."); fflush(stdout); /* Check that (f+g)(a) = f(a) + g(a) */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_t a; fmpq_poly_t f, g, h; fmpq_t x, y; fmpq_init(x); fmpq_init(y); fmpz_init(a); fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_init(h); fmpq_poly_randtest(f, state, n_randint(state, 100), 200); fmpq_poly_randtest(g, state, n_randint(state, 100), 200); fmpz_randtest(a, state, n_randint(state, 100)); fmpq_poly_evaluate_fmpz(x, f, a); fmpq_poly_evaluate_fmpz(y, g, a); fmpq_add(x, x, y); fmpq_poly_add(h, f, g); fmpq_poly_evaluate_fmpz(y, h, a); result = (fmpq_equal(x, y)); if (!result) { flint_printf("FAIL:\n"); flint_printf("f = "), fmpq_poly_debug(f), flint_printf("\n"); flint_printf("g = "), fmpq_poly_debug(g), flint_printf("\n"); flint_printf("a = "), fmpz_print(a), flint_printf("\n"); flint_printf("f(a) + g(a) = "), fmpq_print(x), flint_printf("\n\n"); flint_printf("(f + g)(a) = "), fmpq_print(y), flint_printf("\n\n"); abort(); } fmpq_clear(x); fmpq_clear(y); fmpz_clear(a); fmpq_poly_clear(f); fmpq_poly_clear(g); fmpq_poly_clear(h); } /* Check that (f*g)(a) = f(a) * g(a) */ for (i = 0; i < 1000 * flint_test_multiplier(); i++) { fmpz_t a; fmpq_poly_t f, g; fmpq_t x, y; fmpq_init(x); fmpq_init(y); fmpz_init(a); fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_randtest(f, state, n_randint(state, 100), 200); fmpq_poly_randtest(g, state, n_randint(state, 100), 200); fmpz_randtest(a, state, n_randint(state, 100)); fmpq_poly_evaluate_fmpz(x, f, a); fmpq_poly_evaluate_fmpz(y, g, a); fmpq_mul(x, x, y); fmpq_poly_mul(f, f, g); fmpq_poly_evaluate_fmpz(y, f, a); result = (fmpq_equal(x, y)); if (!result) { flint_printf("FAIL:\n"); fmpz_print(a), flint_printf("\n\n"); abort(); } fmpq_clear(x); fmpq_clear(y); fmpz_clear(a); fmpq_poly_clear(f); fmpq_poly_clear(g); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }
int main(void) { int i, result; flint_rand_t state; printf("evaluate_fmpq...."); fflush(stdout); flint_randinit(state); /* Check aliasing */ for (i = 0; i < 1000; i++) { fmpz_t a, b; fmpq_t x, y; fmpq_poly_t f; fmpz_init(a); fmpz_init(b); fmpq_init(x); fmpq_init(y); fmpq_poly_init(f); fmpq_poly_randtest(f, state, n_randint(state, 80), 100); fmpz_randtest(a, state, 80); fmpz_randtest_not_zero(b, state, 80); fmpz_set(fmpq_numref(x), a); fmpz_set(fmpq_denref(x), a); fmpq_canonicalise(x); fmpq_poly_evaluate_fmpq(y, f, x); fmpq_poly_evaluate_fmpq(x, f, x); result = (fmpq_equal(x, y)); if (!result) { printf("FAIL:\n"); fmpz_print(a), printf("\n\n"); fmpz_print(b), printf("\n\n"); fmpq_poly_debug(f), printf("\n\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_poly_clear(f); } /* Check that (f+g)(a) = f(a) + g(a) */ for (i = 0; i < 1000; i++) { fmpz_t a, b; fmpq_t x, y, z; fmpq_poly_t f, g; fmpz_init(a); fmpz_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_randtest(f, state, n_randint(state, 80), 100); fmpq_poly_randtest(g, state, n_randint(state, 80), 100); fmpz_randtest(a, state, 80); fmpz_randtest_not_zero(b, state, 80); fmpz_set(fmpq_numref(x), a); fmpz_set(fmpq_denref(x), a); fmpq_canonicalise(x); fmpq_poly_evaluate_fmpq(y, f, x); fmpq_poly_evaluate_fmpq(z, g, x); fmpq_add(y, y, z); fmpq_poly_add(f, f, g); fmpq_poly_evaluate_fmpq(z, f, x); result = (fmpq_equal(y, z)); if (!result) { printf("FAIL:\n"); fmpz_print(a), printf("\n\n"); fmpz_print(b), printf("\n\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); fmpq_poly_clear(f); fmpq_poly_clear(g); } /* Check that (f*g)(a) = f(a) * g(a) */ for (i = 0; i < 1000; i++) { fmpz_t a, b; fmpq_t x, y, z; fmpq_poly_t f, g; fmpz_init(a); fmpz_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); fmpq_poly_init(f); fmpq_poly_init(g); fmpq_poly_randtest(f, state, n_randint(state, 50), 80); fmpq_poly_randtest(g, state, n_randint(state, 50), 80); fmpz_randtest(a, state, 80); fmpz_randtest_not_zero(b, state, 80); fmpz_set(fmpq_numref(x), a); fmpz_set(fmpq_denref(x), a); fmpq_canonicalise(x); fmpq_poly_evaluate_fmpq(y, f, x); fmpq_poly_evaluate_fmpq(z, g, x); fmpq_mul(y, y, z); fmpq_poly_mul(f, f, g); fmpq_poly_evaluate_fmpq(z, f, x); result = (fmpq_equal(y, z)); if (!result) { printf("FAIL:\n"); fmpz_print(a), printf("\n\n"); fmpz_print(b), printf("\n\n"); abort(); } fmpz_clear(a); fmpz_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); fmpq_poly_clear(f); fmpq_poly_clear(g); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main() { long iter; flint_rand_t state; printf("add...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { arb_t a, b, c; fmpq_t x, y, z; arb_init(a); arb_init(b); arb_init(c); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_randtest(c, state, 1 + n_randint(state, 200), 10); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); arb_add(c, a, b, 2 + n_randint(state, 200)); fmpq_add(z, x, y); if (!arb_contains_fmpq(c, z)) { printf("FAIL: containment\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("y = "); fmpq_print(y); printf("\n\n"); printf("c = "); arb_print(c); printf("\n\n"); printf("z = "); fmpq_print(z); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of c and a */ for (iter = 0; iter < 10000; iter++) { arb_t a, b; fmpq_t x, y, z; arb_init(a); arb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); arb_add(a, a, b, 2 + n_randint(state, 200)); fmpq_add(z, x, y); if (!arb_contains_fmpq(a, z)) { printf("FAIL: aliasing (c, a)\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("y = "); fmpq_print(y); printf("\n\n"); printf("z = "); fmpq_print(z); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* aliasing of c and b */ for (iter = 0; iter < 10000; iter++) { arb_t a, b; fmpq_t x, y, z; arb_init(a); arb_init(b); fmpq_init(x); fmpq_init(y); fmpq_init(z); arb_randtest(a, state, 1 + n_randint(state, 200), 10); arb_randtest(b, state, 1 + n_randint(state, 200), 10); arb_get_rand_fmpq(x, state, a, 1 + n_randint(state, 200)); arb_get_rand_fmpq(y, state, b, 1 + n_randint(state, 200)); arb_add(b, a, b, 2 + n_randint(state, 200)); fmpq_add(z, x, y); if (!arb_contains_fmpq(b, z)) { printf("FAIL: aliasing (c, b)\n\n"); printf("a = "); arb_print(a); printf("\n\n"); printf("x = "); fmpq_print(x); printf("\n\n"); printf("b = "); arb_print(b); printf("\n\n"); printf("y = "); fmpq_print(y); printf("\n\n"); printf("z = "); fmpq_print(z); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("overlaps...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); iter++) { arb_t a, b; fmpq_t am, ar, bm, br, t, u; int c1, c2; arb_init(a); arb_init(b); fmpq_init(am); fmpq_init(ar); fmpq_init(bm); fmpq_init(br); fmpq_init(t); fmpq_init(u); arb_randtest(a, state, 1 + n_randint(state, 500), 14); arb_randtest(b, state, 1 + n_randint(state, 500), 14); arf_get_fmpq(am, arb_midref(a)); mag_get_fmpq(ar, arb_radref(a)); arf_get_fmpq(bm, arb_midref(b)); mag_get_fmpq(br, arb_radref(b)); fmpq_sub(t, am, bm); fmpz_abs(fmpq_numref(t), fmpq_numref(t)); fmpq_add(u, ar, br); c1 = arb_overlaps(a, b); c2 = (fmpq_cmp(t, u) <= 0); if (c1 != c2) { flint_printf("FAIL:\n\n"); flint_printf("a = "); arb_print(a); flint_printf("\n\n"); flint_printf("b = "); arb_print(b); flint_printf("\n\n"); flint_printf("am = "); fmpq_print(am); flint_printf("\n\n"); flint_printf("ar = "); fmpq_print(ar); flint_printf("\n\n"); flint_printf("bm = "); fmpq_print(bm); flint_printf("\n\n"); flint_printf("br = "); fmpq_print(br); flint_printf("\n\n"); flint_printf("t = "); fmpq_print(t); flint_printf("\n\n"); flint_printf("u = "); fmpq_print(u); flint_printf("\n\n"); flint_printf("c1 = %d, c2 = %d\n\n", c1, c2); flint_abort(); } arb_clear(a); arb_clear(b); fmpq_clear(am); fmpq_clear(ar); fmpq_clear(bm); fmpq_clear(br); fmpq_clear(t); fmpq_clear(u); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int fmpq_poly_oz_sqrt_approx_pade(fmpq_poly_t f_sqrt, const fmpq_poly_t f, const long n, const int p, const mpfr_prec_t prec, const mpfr_prec_t bound, oz_flag_t flags, const fmpq_poly_t init) { fmpq_poly_t y; fmpq_poly_init(y); fmpq_poly_t y_next; fmpq_poly_init(y_next); fmpq_poly_t z; fmpq_poly_init(z); fmpq_poly_t z_next; fmpq_poly_init(z_next); mpfr_t norm; mpfr_init2(norm, prec); mpfr_t prev_norm; mpfr_init2(prev_norm, prec); mpfr_t log_f; mpfr_init2(log_f, prec); if (init) { // z = y/x fmpq_poly_set(y, init); _fmpq_poly_oz_invert_approx(z, f, n, prec); fmpq_poly_oz_mul(z, z, y, n); } else { fmpq_poly_set(y, f); fmpq_poly_set_coeff_si(z, 0, 1); } fmpq_t *xi = (fmpq_t*)calloc(p, sizeof(fmpq_t)); fmpq_t *a2 = (fmpq_t*)calloc(p, sizeof(fmpq_t)); fmpq_t *c = (fmpq_t*)calloc(p, sizeof(fmpq_t)); fmpq_poly_t *t_ = (fmpq_poly_t*)calloc(p, sizeof(fmpq_poly_t)); fmpq_poly_t *s_ = (fmpq_poly_t*)calloc(p, sizeof(fmpq_poly_t)); mpfr_t pi; mpfr_init2(pi, 4*prec); mpfr_const_pi(pi, MPFR_RNDN); #pragma omp parallel for for(int i=0; i<p; i++) { mpfr_t xi_r; mpfr_init2(xi_r, 4*prec); mpfr_t a2_r; mpfr_init2(a2_r, 4*prec); /* ζ_i = 1/2 * (1 + cos( (2·i -1)·π/(2·p) )) */ mpfr_set_si(xi_r, 2*i+1, MPFR_RNDN); mpfr_mul(xi_r, xi_r, pi, MPFR_RNDN); mpfr_div_si(xi_r, xi_r, 2*p, MPFR_RNDN); mpfr_cos(xi_r, xi_r, MPFR_RNDN); mpfr_add_si(xi_r, xi_r, 1, MPFR_RNDN); mpfr_div_si(xi_r, xi_r, 2, MPFR_RNDN); /* α_i^2 = 1/ζ_i -1 */ mpfr_set_si(a2_r, 1, MPFR_RNDN); mpfr_div(a2_r, a2_r, xi_r, MPFR_RNDN); mpfr_sub_si(a2_r, a2_r, 1, MPFR_RNDN); fmpq_init(xi[i]); fmpq_init(a2[i]); fmpq_set_mpfr(xi[i], xi_r, MPFR_RNDN); fmpq_set_mpfr(a2[i], a2_r, MPFR_RNDN); fmpq_init(c[i]); fmpq_poly_init(t_[i]); fmpq_poly_init(s_[i]); mpfr_clear(xi_r); mpfr_clear(a2_r); } mpfr_clear(pi); uint64_t t = oz_walltime(0); int r = 0; int cont = 1; for(long k=0; cont; k++) { if (k == 0 || mpfr_cmp_ui(prev_norm, 1) > 0) _fmpq_poly_oz_sqrt_approx_scale(y, z, n, prec); /* T = sum([1/xi[i] * ~(Z*Y + a2[i]) for i in range(p)]) */ #pragma omp parallel for for(int i=0; i<p; i++) { fmpq_poly_oz_mul(t_[i], z, y, n); fmpq_poly_get_coeff_fmpq(c[i], t_[i], 0); fmpq_add(c[i], c[i], a2[i]); fmpq_poly_set_coeff_fmpq(t_[i], 0, c[i]); fmpq_poly_scalar_mul_fmpq(t_[i], t_[i], xi[i]); _fmpq_poly_oz_invert_approx(s_[i], t_[i], n, prec); } for(int i=1; i<p; i++) fmpq_poly_add(s_[0], s_[0], s_[i]); #pragma omp parallel sections { #pragma omp section { fmpq_poly_oz_mul(y_next, y, s_[0], n); fmpq_poly_scalar_div_si(y_next, y_next, p); fmpq_poly_set(y, y_next); } #pragma omp section { fmpq_poly_oz_mul(z_next, z, s_[0], n); fmpq_poly_scalar_div_si(z_next, z_next, p); fmpq_poly_set(z, z_next); } } cont = !_fmpq_poly_oz_sqrt_approx_break(norm, y, f, n, bound, prec); if(flags & OZ_VERBOSE) { mpfr_log2(log_f, norm, MPFR_RNDN); mpfr_fprintf(stderr, "Computing sqrt(Σ):: k: %4d, Δ=|sqrt(Σ)^2-Σ|: %7.2Rf", k, log_f); fprintf(stderr, " <? %4ld, ", -bound); fprintf(stderr, "t: %8.2fs\n", oz_seconds(oz_walltime(t))); fflush(0); } if (cont) { if (k>0 && mpfr_cmp_ui_2exp(norm, 1, bound) >= 0) { /* something went really wrong */ r = -1; break; } if (k>0 && mpfr_cmp(norm, prev_norm) >= 0) { /* we don't converge any more */ r = 1; break; } mpfr_set(prev_norm, norm, MPFR_RNDN); } } for(int i=0; i<p; i++) { fmpq_clear(xi[i]); fmpq_clear(a2[i]); fmpq_clear(c[i]); fmpq_poly_clear(t_[i]); fmpq_poly_clear(s_[i]); } free(xi); free(a2); free(c); free(t_); free(s_); mpfr_clear(log_f); fmpq_poly_set(f_sqrt, y); mpfr_clear(norm); mpfr_clear(prev_norm); fmpq_poly_clear(y_next); fmpq_poly_clear(y); fmpq_poly_clear(z_next); fmpq_poly_clear(z); return r; }
void add(ElementType& result, const ElementType& a, const ElementType& b) const {fmpq_add(&result,&a,&b);}
int main(void) { int i; flint_rand_t state; flint_randinit(state); printf("bsplit_sum_abpq...."); fflush(stdout); for (i = 0; i < 10000; i++) { fmpq *ab, *pq; fmpq_t s1, s2, t, pqp; fmpq_bsplit_t sum; long k, n; n = n_randint(state, 40); ab = _fmpq_vec_init(n); pq = _fmpq_vec_init(n); fmpq_init(s1); fmpq_init(s2); fmpq_init(pqp); fmpq_init(t); for (k = 0; k < n; k++) fmpq_randtest(ab + k, state, 10); for (k = 0; k < n; k++) fmpq_randtest(pq + k, state, 10); fmpq_bsplit_init(sum); fmpq_bsplit_sum_abpq(sum, ab, pq, 0, n); fmpq_bsplit_get_fmpq(s1, sum); fmpq_zero(s2); fmpq_one(pqp); for (k = 0; k < n; k++) { fmpq_mul(pqp, pqp, pq + k); fmpq_mul(t, pqp, ab + k); fmpq_add(s2, s2, t); } if (!fmpq_is_canonical(s1) || !fmpq_equal(s1, s2)) { printf("FAIL\n"); printf("(a/b) = "); for (k = 0; k < n; k++) fmpq_print(ab+k), printf(" "); printf("\n"); printf("(p/q) = "); for (k = 0; k < n; k++) fmpq_print(pq+k), printf(" "); printf("\n"); printf("s1: "); fmpq_print(s1); printf("\n"); printf("s2: "); fmpq_print(s2); printf("\n"); abort(); } /* Check numerical evaluation */ { mpfr_t f1, f2; mpfr_prec_t prec; prec = 5 + n_randint(state, 1000); mpfr_init2(f1, prec); mpfr_init2(f2, prec); fmpq_bsplit_get_mpfr(f1, sum); fmpq_get_mpfr(f2, s1, MPFR_RNDN); mpfr_sub(f1, f1, f2, MPFR_RNDN); if (!mpfr_zero_p(f1) && !(mpfr_get_exp(f1) <= mpfr_get_exp(f2) - prec + 3)) { printf("FAIL: numerical evaluation\n"); printf("%ld, %ld, %ld\n", prec, mpfr_get_exp(f1), mpfr_get_exp(f2) - prec + 3); abort(); } mpfr_clear(f1); mpfr_clear(f2); } fmpq_bsplit_clear(sum); fmpq_clear(s1); fmpq_clear(s2); fmpq_clear(pqp); fmpq_clear(t); _fmpq_vec_clear(ab, n); _fmpq_vec_clear(pq, n); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int main(void) { int i; FLINT_TEST_INIT(state); flint_printf("add...."); fflush(stdout); /* x = y + z */ for (i = 0; i < 10000; i++) { fmpq_t x, y, z; mpq_t X, Y, Z; fmpq_init(x); fmpq_init(y); fmpq_init(z); mpq_init(X); mpq_init(Y); mpq_init(Z); fmpq_randtest(x, state, 200); fmpq_randtest(y, state, 200); fmpq_randtest(z, state, 200); fmpq_get_mpq(X, x); fmpq_get_mpq(Y, y); fmpq_get_mpq(Z, z); fmpq_add(x, y, z); if (!fmpq_is_canonical(x)) { flint_printf("FAIL: result not canonical!\n"); abort(); } mpq_add(X, Y, Z); fmpq_get_mpq(Y, x); if (!mpq_equal(X, Y)) { flint_printf("FAIL: fmpq_add(x,y,z) != mpq_add(X,Y,Z)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\ny = "); fmpq_print(y); flint_printf("\nz = "); fmpq_print(z); flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); mpq_clear(X); mpq_clear(Y); mpq_clear(Z); } /* x = x + y */ for (i = 0; i < 10000; i++) { fmpq_t x, y; mpq_t X, Y; fmpq_init(x); fmpq_init(y); mpq_init(X); mpq_init(Y); fmpq_randtest(x, state, 200); fmpq_randtest(y, state, 200); fmpq_get_mpq(X, x); fmpq_get_mpq(Y, y); fmpq_add(x, x, y); if (!fmpq_is_canonical(x)) { flint_printf("FAIL: result not canonical!\n"); abort(); } mpq_add(X, X, Y); fmpq_get_mpq(Y, x); if (!mpq_equal(X, Y)) { flint_printf("FAIL: fmpq_add(x,x,y) != mpq_add(X,X,Y)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\ny = "); fmpq_print(y); flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); mpq_clear(X); mpq_clear(Y); } /* x = y + x */ for (i = 0; i < 10000; i++) { fmpq_t x, y; mpq_t X, Y; fmpq_init(x); fmpq_init(y); mpq_init(X); mpq_init(Y); fmpq_randtest(x, state, 200); fmpq_randtest(y, state, 200); fmpq_get_mpq(X, x); fmpq_get_mpq(Y, y); fmpq_add(x, y, x); if (!fmpq_is_canonical(x)) { flint_printf("FAIL: result not canonical!\n"); abort(); } mpq_add(X, Y, X); fmpq_get_mpq(Y, x); if (!mpq_equal(X, Y)) { flint_printf("FAIL: fmpq_add(x,y,x) != mpq_add(X,Y,X)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\ny = "); fmpq_print(y); flint_printf("\n"); abort(); } fmpq_clear(x); fmpq_clear(y); mpq_clear(X); mpq_clear(Y); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return 0; }