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() { slong iter, iter2; flint_rand_t state; flint_printf("mul...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); 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_mul(c, a, b, 2 + n_randint(state, 200)); fmpq_mul(z, x, y); if (!arb_contains_fmpq(c, z)) { flint_printf("FAIL: containment\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"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("z = "); fmpq_print(z); flint_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 * arb_test_multiplier(); 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_mul(a, a, b, 2 + n_randint(state, 200)); fmpq_mul(z, x, y); if (!arb_contains_fmpq(a, z)) { flint_printf("FAIL: aliasing (c, a)\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"); flint_printf("z = "); fmpq_print(z); flint_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 * arb_test_multiplier(); 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_mul(b, a, b, 2 + n_randint(state, 200)); fmpq_mul(z, x, y); if (!arb_contains_fmpq(b, z)) { flint_printf("FAIL: aliasing (c, b)\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"); flint_printf("z = "); fmpq_print(z); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } /* main test */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { arb_t x, y, z, v; slong prec; arb_init(x); arb_init(y); arb_init(z); arb_init(v); for (iter2 = 0; iter2 < 100; iter2++) { arb_randtest_special(x, state, n_randint(state,2) ? 2000 : 200, 200); arb_randtest_special(y, state, n_randint(state,2) ? 2000 : 200, 200); prec = 2 + n_randint(state, 2000); switch (n_randint(state, 5)) { case 0: arb_mul(z, x, y, prec); arb_mul_naive(v, x, y, prec); if (!arf_equal(arb_midref(z), arb_midref(v)) || !mag_close(arb_radref(z), arb_radref(v))) { flint_printf("FAIL!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("z = "); arb_print(z); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; case 1: arb_set(y, x); arb_mul(z, x, y, prec); arb_mul(v, x, x, prec); if (!arf_equal(arb_midref(z), arb_midref(v)) || !mag_close(arb_radref(z), arb_radref(v))) { flint_printf("FAIL (aliasing 1)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("z = "); arb_print(z); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; case 2: arb_mul(v, x, x, prec); arb_mul(x, x, x, prec); if (!arb_equal(v, x)) { flint_printf("FAIL (aliasing 2)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("z = "); arb_print(z); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; case 3: arb_mul(v, x, y, prec); arb_mul(x, x, y, prec); if (!arb_equal(v, x)) { flint_printf("FAIL (aliasing 3)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; default: arb_mul(v, x, y, prec); arb_mul(x, y, x, prec); if (!arf_equal(arb_midref(x), arb_midref(v)) || !mag_close(arb_radref(x), arb_radref(v))) { flint_printf("FAIL (aliasing 4)!\n"); flint_printf("x = "); arb_print(x); flint_printf("\n\n"); flint_printf("y = "); arb_print(y); flint_printf("\n\n"); flint_printf("v = "); arb_print(v); flint_printf("\n\n"); abort(); } break; } } arb_clear(x); arb_clear(y); arb_clear(z); arb_clear(v); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("evaluate_horner...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000; iter++) { slong qbits1, qbits2, rbits1, rbits2, rbits3; fmpq_poly_t F; fmpq_t X, Y; arb_poly_t f; arb_t x, y; qbits1 = 2 + n_randint(state, 200); qbits2 = 2 + n_randint(state, 200); rbits1 = 2 + n_randint(state, 200); rbits2 = 2 + n_randint(state, 200); rbits3 = 2 + n_randint(state, 200); fmpq_poly_init(F); fmpq_init(X); fmpq_init(Y); arb_poly_init(f); arb_init(x); arb_init(y); fmpq_poly_randtest(F, state, 1 + n_randint(state, 20), qbits1); fmpq_randtest(X, state, qbits2); fmpq_poly_evaluate_fmpq(Y, F, X); arb_poly_set_fmpq_poly(f, F, rbits1); arb_set_fmpq(x, X, rbits2); arb_poly_evaluate_horner(y, f, x, rbits3); if (!arb_contains_fmpq(y, Y)) { flint_printf("FAIL\n\n"); flint_printf("F = "); fmpq_poly_print(F); flint_printf("\n\n"); flint_printf("X = "); fmpq_print(X); flint_printf("\n\n"); flint_printf("Y = "); fmpq_print(Y); flint_printf("\n\n"); flint_printf("f = "); arb_poly_printd(f, 15); flint_printf("\n\n"); flint_printf("x = "); arb_printd(x, 15); flint_printf("\n\n"); flint_printf("y = "); arb_printd(y, 15); flint_printf("\n\n"); abort(); } /* aliasing */ arb_poly_evaluate_horner(x, f, x, rbits3); if (!arb_contains_fmpq(x, Y)) { flint_printf("FAIL (aliasing)\n\n"); abort(); } fmpq_poly_clear(F); fmpq_clear(X); fmpq_clear(Y); arb_poly_clear(f); arb_clear(x); arb_clear(y); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("evaluate_vec_fast...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { slong i, n, qbits1, qbits2, rbits1, rbits2, rbits3; fmpq_poly_t F; fmpq * X, * Y; arb_poly_t f; arb_ptr x, y; qbits1 = 2 + n_randint(state, 100); qbits2 = 2 + n_randint(state, 100); rbits1 = 2 + n_randint(state, 200); rbits2 = 2 + n_randint(state, 200); rbits3 = 2 + n_randint(state, 200); n = n_randint(state, 10); fmpq_poly_init(F); X = _fmpq_vec_init(n); Y = _fmpq_vec_init(n); arb_poly_init(f); x = _arb_vec_init(n); y = _arb_vec_init(n); fmpq_poly_randtest(F, state, 1 + n_randint(state, 20), qbits1); for (i = 0; i < n; i++) fmpq_randtest(X + i, state, qbits2); for (i = 0; i < n; i++) fmpq_poly_evaluate_fmpq(Y + i, F, X + i); arb_poly_set_fmpq_poly(f, F, rbits1); for (i = 0; i < n; i++) arb_set_fmpq(x + i, X + i, rbits2); arb_poly_evaluate_vec_fast(y, f, x, n, rbits3); for (i = 0; i < n; i++) { if (!arb_contains_fmpq(y + i, Y + i)) { flint_printf("FAIL (%wd of %wd)\n\n", i, n); flint_printf("F = "); fmpq_poly_print(F); flint_printf("\n\n"); flint_printf("X = "); fmpq_print(X + i); flint_printf("\n\n"); flint_printf("Y = "); fmpq_print(Y + i); flint_printf("\n\n"); flint_printf("f = "); arb_poly_printd(f, 15); flint_printf("\n\n"); flint_printf("x = "); arb_printd(x + i, 15); flint_printf("\n\n"); flint_printf("y = "); arb_printd(y + i, 15); flint_printf("\n\n"); abort(); } } fmpq_poly_clear(F); _fmpq_vec_clear(X, n); _fmpq_vec_clear(Y, n); arb_poly_clear(f); _arb_vec_clear(x, n); _arb_vec_clear(y, n); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; FLINT_TEST_INIT(state); { ulong k; arb_t a,b; arf_t c,d; fmpq_poly_t p; arb_init(a); arb_init(b); arf_init(c); arf_init(d); /* x+1 */ fmpq_poly_init(p); fmpq_poly_set_coeff_si(p, 0, 1); fmpq_poly_set_coeff_si(p, 1, 1); for (iter = 0; iter < 5000; iter++) { k = n_randint(state, 10000); arb_set_si(a, k); fmpq_poly_evaluate_arb(b, p, a, 30 + n_randint(state, 100)); if (!arb_equal_si(b, k + 1)) { printf("FAIL (fmpq_poly_evaluate_arb):\n"); printf("a = "); arb_print(a); printf("\n"); printf("b = "); arb_print(b); printf("\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); abort(); } arf_set_si(c, k); fmpq_poly_evaluate_arf(d, p, c, 30 + n_randint(state, 100)); if (!arf_equal_si(d, k + 1)) { printf("FAIL (fmpq_poly_evaluate_arf):\n"); printf("c = "); arf_print(c); printf("\n"); printf("d = "); arf_print(d); printf("\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); abort(); } } /* x^2 */ fmpq_poly_zero(p); fmpq_poly_set_coeff_si(p, 2, 1); for (iter = 0; iter < 1000; iter++) { k = n_randint(state, 10000); arb_set_si(a, k); fmpq_poly_evaluate_arb(b, p, a, 30 + n_randint(state, 100)); if (!arb_equal_si(b, k * k)) { printf("Error (test_fmpq_poly_evaluate_arb):\n"); printf("a = "); arb_print(a); printf("\n"); printf("b = "); arb_print(b); printf("\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); abort(); } arf_set_si(c, k); fmpq_poly_evaluate_arf(d, p, c, 30 + n_randint(state, 100)); if (!arf_equal_si(d, k * k)) { printf("Error (test_fmpq_poly_evaluate_arf):\n"); printf("c = "); arf_print(c); printf("\n"); printf("d = "); arf_print(d); printf("\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); abort(); } } fmpq_poly_clear(p); arb_clear(a); arb_clear(b); arf_clear(c); arf_clear(d); } /* check evaluate_arb agains exact evaluate_fmpq */ for (iter = 0; iter < 1000; iter++) { fmpq_poly_t p; fmpq_t x,y; arb_t a,b; fmpq_poly_init(p); fmpq_init(x); fmpq_init(y); arb_init(a); arb_init(b); fmpq_poly_randtest(p, state, 1 + n_randint(state,100), 10); fmpq_randtest(x, state, 10); arb_set_fmpq(a, x, 64); fmpq_poly_evaluate_fmpq(y, p, x); fmpq_poly_evaluate_arb(b, p, a, 60); if (!arb_contains_fmpq(b, y)) { printf("FAIL (y not in b):\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); printf("x = "); fmpq_print(x); printf("\n"); printf("y = "); fmpq_print(y); printf("\n"); printf("a = "); arb_print(a); printf("\n"); printf("b = "); arb_print(b); printf("\n"); abort(); } fmpq_poly_evaluate_arb(a, p, a, 60); if (!arb_equal(a,b)) { printf("FAIL (a not equal b):\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); printf("x = "); fmpq_print(x); printf("\n"); printf("y = "); fmpq_print(y); printf("\n"); printf("a = "); arb_print(a); printf("\n"); printf("b = "); arb_print(b); printf("\n"); abort(); } fmpq_poly_clear(p); fmpq_clear(x); fmpq_clear(y); arb_clear(a); arb_clear(b); } /* test aliasing */ for (iter = 0; iter < 1000; iter++) { fmpq_poly_t p; arb_t a,b; arf_t c,d; fmpq_poly_init(p); arb_init(a); arb_init(b); arf_init(c); arf_init(d); fmpq_poly_randtest(p, state, 1 + n_randint(state,100), 10); arb_randtest(a, state, 60, 10); arb_randtest(b, state, 60, 10); arf_randtest(c, state, 60, 10); arf_randtest(d, state, 60, 10); fmpq_poly_evaluate_arb(b, p, a, 60); fmpq_poly_evaluate_arb(a, p, a, 60); if (!arb_equal(a, b)) { printf("FAIL (a not equal b):\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); printf("a = "); arb_print(a); printf("\n"); printf("b = "); arb_print(b); printf("\n"); abort(); } fmpq_poly_evaluate_arf(d, p, c, 60); fmpq_poly_evaluate_arf(c, p, c, 60); if (!arf_equal(c, d)) { printf("FAIL (c not equal d):\n"); printf("p = "); fmpq_poly_print(p); printf("\n"); printf("c = "); arf_print(c); printf("\n"); printf("d = "); arf_print(d); printf("\n"); } fmpq_poly_clear(p); arb_clear(a); arb_clear(b); arf_clear(c); arf_clear(d); } FLINT_TEST_CLEANUP(state); return 0; }
int main() { slong iter; flint_rand_t state; flint_printf("fundamental_domain_approx...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { fmpq_t x, y; psl2z_t g; arf_t one_minus_eps, tol; acb_t z, w, w2; arb_t t; slong prec; fmpq_init(x); fmpq_init(y); psl2z_init(g); acb_init(z); acb_init(w); acb_init(w2); arf_init(one_minus_eps); arf_init(tol); arb_init(t); /* pick an exact point in the upper half plane */ fmpq_randtest(x, state, 1 + n_randint(state, 500)); do { fmpq_randtest(y, state, 1 + n_randint(state, 500)); } while (fmpz_sgn(fmpq_numref(y)) <= 0); /* pick a tolerance */ arf_set_ui_2exp_si(tol, 1, -(slong) n_randint(state, 500)); /* now increase the precision until convergence */ for (prec = 32; ; prec *= 2) { if (prec > 16384) { flint_printf("FAIL (no convergence)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); flint_printf("z = "); acb_printd(z, 50); flint_printf("\n\n"); flint_printf("w = "); acb_printd(w, 50); flint_printf("\n\n"); flint_printf("w2 = "); acb_printd(w2, 50); flint_printf("\n\n"); flint_printf("g = "); psl2z_print(g); flint_printf("\n\n"); abort(); } arb_set_fmpq(acb_realref(z), x, prec); arb_set_fmpq(acb_imagref(z), y, prec); arf_set_ui_2exp_si(one_minus_eps, 1, -prec / 4); arf_sub_ui(one_minus_eps, one_minus_eps, 1, prec, ARF_RND_DOWN); arf_neg(one_minus_eps, one_minus_eps); acb_modular_fundamental_domain_approx(w, g, z, one_minus_eps, prec); acb_modular_transform(w2, g, z, prec); if (!psl2z_is_correct(g) || !acb_overlaps(w, w2)) { flint_printf("FAIL (incorrect transformation)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); flint_printf("z = "); acb_printd(z, 50); flint_printf("\n\n"); flint_printf("w = "); acb_printd(w, 50); flint_printf("\n\n"); flint_printf("w2 = "); acb_printd(w2, 50); flint_printf("\n\n"); flint_printf("g = "); psl2z_print(g); flint_printf("\n\n"); abort(); } /* success */ if (acb_modular_is_in_fundamental_domain(w, tol, prec)) break; } /* check that g^(-1) * w contains x+yi */ psl2z_inv(g, g); acb_modular_transform(w2, g, w, 2 + n_randint(state, 1000)); if (!arb_contains_fmpq(acb_realref(w2), x) || !arb_contains_fmpq(acb_imagref(w2), y)) { flint_printf("FAIL (inverse containment)\n"); flint_printf("x = "); fmpq_print(x); flint_printf("\n\n"); flint_printf("y = "); fmpq_print(y); flint_printf("\n\n"); flint_printf("z = "); acb_printd(z, 50); flint_printf("\n\n"); flint_printf("w = "); acb_printd(w, 50); flint_printf("\n\n"); flint_printf("w2 = "); acb_printd(w2, 50); flint_printf("\n\n"); flint_printf("g = "); psl2z_print(g); flint_printf("\n\n"); abort(); } fmpq_clear(x); fmpq_clear(y); psl2z_clear(g); acb_clear(z); acb_clear(w); acb_clear(w2); arf_clear(one_minus_eps); arf_clear(tol); arb_clear(t); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("mul_more...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000 * arb_test_multiplier(); 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_mul(c, a, b, 2 + n_randint(state, 200)); fmpq_mul(z, x, y); if (!arb_contains_fmpq(c, z)) { flint_printf("FAIL: containment\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"); flint_printf("c = "); arb_print(c); flint_printf("\n\n"); flint_printf("z = "); fmpq_print(z); flint_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 * arb_test_multiplier(); 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_mul(a, a, b, 2 + n_randint(state, 200)); fmpq_mul(z, x, y); if (!arb_contains_fmpq(a, z)) { flint_printf("FAIL: aliasing (c, a)\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"); flint_printf("z = "); fmpq_print(z); flint_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 * arb_test_multiplier(); 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_mul(b, a, b, 2 + n_randint(state, 200)); fmpq_mul(z, x, y); if (!arb_contains_fmpq(b, z)) { flint_printf("FAIL: aliasing (c, b)\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"); flint_printf("z = "); fmpq_print(z); flint_printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); fmpq_clear(x); fmpq_clear(y); fmpq_clear(z); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("det...."); fflush(stdout); flint_randinit(state); for (iter = 0; iter < 100000; iter++) { fmpq_mat_t Q; fmpq_t Qdet; arb_mat_t A; arb_t Adet; slong n, qbits, prec; n = n_randint(state, 8); qbits = 1 + n_randint(state, 100); prec = 2 + n_randint(state, 200); fmpq_mat_init(Q, n, n); fmpq_init(Qdet); arb_mat_init(A, n, n); arb_init(Adet); fmpq_mat_randtest(Q, state, qbits); fmpq_mat_det(Qdet, Q); arb_mat_set_fmpq_mat(A, Q, prec); arb_mat_det(Adet, A, prec); if (!arb_contains_fmpq(Adet, Qdet)) { flint_printf("FAIL (containment, iter = %wd)\n", iter); flint_printf("n = %wd, prec = %wd\n", n, prec); flint_printf("\n"); flint_printf("Q = \n"); fmpq_mat_print(Q); flint_printf("\n\n"); flint_printf("Qdet = \n"); fmpq_print(Qdet); flint_printf("\n\n"); flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n"); flint_printf("Adet = \n"); arb_printd(Adet, 15); flint_printf("\n\n"); flint_printf("Adet = \n"); arb_print(Adet); flint_printf("\n\n"); abort(); } fmpq_mat_clear(Q); fmpq_clear(Qdet); arb_mat_clear(A); arb_clear(Adet); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { slong iter; flint_rand_t state; flint_printf("frobenius_norm...."); fflush(stdout); flint_randinit(state); /* compare to the exact rational norm */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { fmpq_mat_t Q; fmpq_t q; arb_mat_t A; slong n, qbits, prec; n = n_randint(state, 8); qbits = 1 + n_randint(state, 100); prec = 2 + n_randint(state, 200); fmpq_mat_init(Q, n, n); fmpq_init(q); arb_mat_init(A, n, n); fmpq_mat_randtest(Q, state, qbits); _fmpq_mat_sum_of_squares(q, Q); arb_mat_set_fmpq_mat(A, Q, prec); /* check that the arb interval contains the exact value */ { arb_t a; arb_init(a); arb_mat_frobenius_norm(a, A, prec); arb_mul(a, a, a, prec); if (!arb_contains_fmpq(a, q)) { flint_printf("FAIL (containment, iter = %wd)\n", iter); flint_printf("n = %wd, prec = %wd\n", n, prec); flint_printf("\n"); flint_printf("Q = \n"); fmpq_mat_print(Q); flint_printf("\n\n"); flint_printf("frobenius_norm(Q)^2 = \n"); fmpq_print(q); flint_printf("\n\n"); flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n"); flint_printf("frobenius_norm(A)^2 = \n"); arb_printd(a, 15); flint_printf("\n\n"); flint_printf("frobenius_norm(A)^2 = \n"); arb_print(a); flint_printf("\n\n"); abort(); } arb_clear(a); } /* check that the upper bound is not less than the exact value */ { mag_t b; fmpq_t y; mag_init(b); fmpq_init(y); arb_mat_bound_frobenius_norm(b, A); mag_mul(b, b, b); mag_get_fmpq(y, b); if (fmpq_cmp(q, y) > 0) { flint_printf("FAIL (bound, iter = %wd)\n", iter); flint_printf("n = %wd, prec = %wd\n", n, prec); flint_printf("\n"); flint_printf("Q = \n"); fmpq_mat_print(Q); flint_printf("\n\n"); flint_printf("frobenius_norm(Q)^2 = \n"); fmpq_print(q); flint_printf("\n\n"); flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n"); flint_printf("bound_frobenius_norm(A)^2 = \n"); mag_printd(b, 15); flint_printf("\n\n"); flint_printf("bound_frobenius_norm(A)^2 = \n"); mag_print(b); flint_printf("\n\n"); abort(); } mag_clear(b); fmpq_clear(y); } fmpq_mat_clear(Q); fmpq_clear(q); arb_mat_clear(A); } /* check trace(A^T A) = frobenius_norm(A)^2 */ for (iter = 0; iter < 10000 * arb_test_multiplier(); iter++) { slong m, n, prec; arb_mat_t A, AT, ATA; arb_t t; prec = 2 + n_randint(state, 200); m = n_randint(state, 10); n = n_randint(state, 10); arb_mat_init(A, m, n); arb_mat_init(AT, n, m); arb_mat_init(ATA, n, n); arb_init(t); arb_mat_randtest(A, state, 2 + n_randint(state, 100), 10); arb_mat_transpose(AT, A); arb_mat_mul(ATA, AT, A, prec); arb_mat_trace(t, ATA, prec); arb_sqrt(t, t, prec); /* check the norm bound */ { mag_t low, frobenius; mag_init(low); arb_get_mag_lower(low, t); mag_init(frobenius); arb_mat_bound_frobenius_norm(frobenius, A); if (mag_cmp(low, frobenius) > 0) { flint_printf("FAIL (bound)\n", iter); flint_printf("m = %wd, n = %wd, prec = %wd\n", m, n, prec); flint_printf("\n"); flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n"); flint_printf("lower(sqrt(trace(A^T A))) = \n"); mag_printd(low, 15); flint_printf("\n\n"); flint_printf("bound_frobenius_norm(A) = \n"); mag_printd(frobenius, 15); flint_printf("\n\n"); abort(); } mag_clear(low); mag_clear(frobenius); } /* check the norm interval */ { arb_t frobenius; arb_init(frobenius); arb_mat_frobenius_norm(frobenius, A, prec); if (!arb_overlaps(t, frobenius)) { flint_printf("FAIL (overlap)\n", iter); flint_printf("m = %wd, n = %wd, prec = %wd\n", m, n, prec); flint_printf("\n"); flint_printf("A = \n"); arb_mat_printd(A, 15); flint_printf("\n\n"); flint_printf("sqrt(trace(A^T A)) = \n"); arb_printd(t, 15); flint_printf("\n\n"); flint_printf("frobenius_norm(A) = \n"); arb_printd(frobenius, 15); flint_printf("\n\n"); abort(); } arb_clear(frobenius); } arb_mat_clear(A); arb_mat_clear(AT); arb_mat_clear(ATA); arb_clear(t); } flint_randclear(state); flint_cleanup(); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main() { long iter; flint_rand_t state; printf("div...."); 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); do { 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)); } while (fmpq_is_zero(y)); arb_div(c, a, b, 2 + n_randint(state, 200)); fmpq_div(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); do { 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)); } while (fmpq_is_zero(y)); arb_div(a, a, b, 2 + n_randint(state, 200)); fmpq_div(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); do { 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)); } while (fmpq_is_zero(y)); arb_div(b, a, b, 2 + n_randint(state, 200)); fmpq_div(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); } /* test special values */ for (iter = 0; iter < 100000; iter++) { arb_t a, b, c, d; arb_init(a); arb_init(b); arb_init(c); arb_init(d); arb_randtest_special(a, state, 1 + n_randint(state, 200), 1 + n_randint(state, 100)); arb_randtest_special(b, state, 1 + n_randint(state, 200), 1 + n_randint(state, 100)); arb_randtest_special(c, state, 1 + n_randint(state, 200), 1 + n_randint(state, 100)); arb_div(c, a, b, 2 + n_randint(state, 200)); arb_mul(d, c, b, 2 + n_randint(state, 200)); if (!arb_contains(d, a)) { printf("FAIL: containment\n\n"); printf("a = "); arb_printd(a, 15); printf("\n\n"); printf("b = "); arb_printd(b, 15); printf("\n\n"); printf("c = "); arb_printd(c, 15); printf("\n\n"); printf("d = "); arb_printd(d, 15); printf("\n\n"); abort(); } arb_clear(a); arb_clear(b); arb_clear(c); arb_clear(d); } flint_randclear(state); flint_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int fmpq_poly_check_unique_real_root(const fmpq_poly_t pol, const arb_t a, slong prec) { if (pol->length < 2) return 0; else if (pol->length == 2) { /* linear polynomial */ fmpq_t root; int ans; fmpq_init(root); fmpq_set_fmpz_frac(root, fmpq_poly_numref(pol), fmpq_poly_numref(pol) + 1); fmpq_neg(root, root); ans = arb_contains_fmpq(a, root); fmpq_clear(root); return ans; } else { arb_t b, c; arf_t l, r; fmpz * der; int lsign, rsign; fmpz_poly_t pol2; slong n; /* 1 - cheap test: */ /* - sign(left) * sign(right) = -1 */ /* - no zero of the derivative */ arb_init(b); arb_init(c); arf_init(l); arf_init(r); arb_get_interval_arf(l, r, a, prec); arb_set_arf(b, l); _fmpz_poly_evaluate_arb(c, pol->coeffs, pol->length, b, 2*prec); lsign = arb_sgn2(c); arb_set_arf(b, r); _fmpz_poly_evaluate_arb(c, pol->coeffs, pol->length, b, 2*prec); rsign = arb_sgn2(c); arb_clear(c); if (lsign * rsign == -1) { der = _fmpz_vec_init(pol->length - 1); _fmpz_poly_derivative(der, pol->coeffs, pol->length); _fmpz_poly_evaluate_arb(b, der, pol->length - 1, a, prec); _fmpz_vec_clear(der, pol->length - 1); if (!arb_contains_zero(b)) { arf_clear(l); arf_clear(r); arb_clear(b); return 1; } } else return 0; arb_clear(b); /* 2 - expensive testing */ fmpq_t ql, qr; fmpq_init(ql); fmpq_init(qr); arf_get_fmpq(ql, l); arf_get_fmpq(qr, r); fmpz_poly_init(pol2); fmpz_poly_fit_length(pol2, pol->length); _fmpz_vec_set(pol2->coeffs, pol->coeffs, pol->length); pol2->length = pol->length; _fmpz_poly_scale_0_1_fmpq(pol2->coeffs, pol2->length, ql, qr); n = fmpz_poly_num_real_roots_0_1(pol2); fmpz_poly_clear(pol2); fmpq_clear(ql); fmpq_clear(qr); return (n == 1); } }