int main(void) { int i, result; flint_rand_t state; printf("exp... "); fflush(stdout); flint_randinit(state); /** p == 2 *******************************************************************/ /* Check aliasing: a = exp(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b; int ans1, ans2; fmpz_init(p); fmpz_set_ui(p, 2); N = n_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_randtest(a, state, ctx); ans1 = padic_exp(b, a, ctx); ans2 = padic_exp(a, a, ctx); result = ((ans1 == ans2) && (!ans1 || padic_equal(a, b, ctx))); if (!result) { printf("FAIL (aliasing):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("ans1 = %d\n", ans1); printf("ans2 = %d\n", ans2); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Functional equation: exp(a + b) == exp(a) exp(b) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; int ans1, ans2, ans3; fmpz_init(p); fmpz_set_ui(p, 2); N = n_randint(state, 10) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(c, ctx); padic_init(d, ctx); padic_init(e, ctx); padic_init(f, ctx); padic_init(g, ctx); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_add(c, a, b, ctx); ans1 = padic_exp(d, a, ctx); ans2 = padic_exp(e, b, ctx); padic_mul(f, d, e, ctx); ans3 = padic_exp(g, c, ctx); result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g, ctx))); if (!result) { printf("FAIL (functional equation):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = a + b = "), padic_print(c, ctx), printf("\n"); printf("d = exp(a) = "), padic_print(d, ctx), printf("\n"); printf("e = exp(b) = "), padic_print(e, ctx), printf("\n"); printf("f = exp(a) exp(b) = "), padic_print(f, ctx), printf("\n"); printf("g = exp(a + b) = "), padic_print(g, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(c, ctx); padic_clear(d, ctx); padic_clear(e, ctx); padic_clear(f, ctx); padic_clear(g, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /** p > 2 ********************************************************************/ /* Check aliasing: a = exp(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b; int ans1, ans2; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = n_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_randtest(a, state, ctx); ans1 = padic_exp(b, a, ctx); ans2 = padic_exp(a, a, ctx); result = ((ans1 == ans2) && (!ans1 || padic_equal(a, b, ctx))); if (!result) { printf("FAIL (aliasing):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("ans1 = %d\n", ans1); printf("ans2 = %d\n", ans2); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Functional equation: exp(a + b) == exp(a) exp(b) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; int ans1, ans2, ans3; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = n_randint(state, 10) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(c, ctx); padic_init(d, ctx); padic_init(e, ctx); padic_init(f, ctx); padic_init(g, ctx); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_add(c, a, b, ctx); ans1 = padic_exp(d, a, ctx); ans2 = padic_exp(e, b, ctx); padic_mul(f, d, e, ctx); ans3 = padic_exp(g, c, ctx); result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g, ctx))); if (!result) { printf("FAIL (functional equation):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = a + b = "), padic_print(c, ctx), printf("\n"); printf("d = exp(a) = "), padic_print(d, ctx), printf("\n"); printf("e = exp(b) = "), padic_print(e, ctx), printf("\n"); printf("f = exp(a) exp(b) = "), padic_print(f, ctx), printf("\n"); printf("g = exp(a + b) = "), padic_print(g, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(c, ctx); padic_clear(d, ctx); padic_clear(e, ctx); padic_clear(f, ctx); padic_clear(g, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; flint_rand_t state; printf("log_rectangular... "); fflush(stdout); flint_randinit(state); /** p == 2 *******************************************************************/ /** p > 2 ********************************************************************/ /* Check aliasing: a = log(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = n_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); _padic_init(a); _padic_init(b); padic_randtest(a, state, ctx); if (!_padic_is_zero(a) && (padic_val(a) < 1 || (*p == 2L && padic_val(a) < 2))) { padic_val(a) = (*p == 2L) + 1; padic_reduce(a, ctx); } _padic_one(b); _padic_add(a, a, b, ctx); padic_log_rectangular(b, a, ctx); padic_log_rectangular(a, a, ctx); result = (padic_equal(a, b, ctx)); if (!result) { printf("FAIL (aliasing):\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("b = "), _padic_print(b, ctx), printf("\n"); abort(); } _padic_clear(a); _padic_clear(b); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check: log(a) + log(b) == log(a * b) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = n_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); _padic_init(a); _padic_init(b); _padic_init(c); _padic_init(d); _padic_init(e); _padic_init(f); _padic_init(g); padic_randtest(a, state, ctx); if (!_padic_is_zero(a) && (padic_val(a) < 1 || (*p == 2L && padic_val(a) < 2))) { padic_val(a) = (*p == 2L) + 1; padic_reduce(a, ctx); } _padic_one(c); _padic_add(a, a, c, ctx); padic_randtest(b, state, ctx); if (!_padic_is_zero(b) && (padic_val(b) < 1 || (*p == 2L && padic_val(b) < 2))) { padic_val(b) = (*p == 2L) + 1; padic_reduce(b, ctx); } _padic_one(c); _padic_add(b, b, c, ctx); padic_mul(c, a, b, ctx); padic_log_rectangular(d, a, ctx); padic_log_rectangular(e, b, ctx); padic_add(f, d, e, ctx); padic_log_rectangular(g, c, ctx); result = (padic_equal(f, g, ctx)); if (!result) { printf("FAIL (functional equation):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = a * b = "), padic_print(c, ctx), printf("\n"); printf("d = log(a) = "), padic_print(d, ctx), printf("\n"); printf("e = log(b) = "), padic_print(e, ctx), printf("\n"); printf("f = log(a) + log(b) = "), padic_print(f, ctx), printf("\n"); printf("g = log(a * b) = "), padic_print(g, ctx), printf("\n"); abort(); } _padic_clear(a); _padic_clear(b); _padic_clear(c); _padic_clear(d); _padic_clear(e); _padic_clear(f); _padic_clear(g); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check: log(exp(x)) == x */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, c; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = n_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_SERIES); _padic_init(a); _padic_init(b); _padic_init(c); padic_randtest(a, state, ctx); if (!_padic_is_zero(a) && (padic_val(a) < 1 || (*p == 2L && padic_val(a) < 2))) { padic_val(a) = (*p == 2L) + 1; padic_reduce(a, ctx); } padic_exp(b, a, ctx); padic_log_rectangular(c, b, ctx); result = (padic_equal(a, c, ctx)); if (!result) { printf("FAIL (log(exp(x)) == x):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = "), padic_print(c, ctx), printf("\n"); abort(); } _padic_clear(a); _padic_clear(b); _padic_clear(c); fmpz_clear(p); padic_ctx_clear(ctx); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("log... "); fflush(stdout); /** p == 2 *******************************************************************/ /* Check aliasing: a = log(a) */ for (i = 0; i < 1000; i++) { fmpz_t p = {WORD(2)}; slong N; padic_ctx_t ctx; padic_t a, b; int ans1, ans2; N = __rand_prec(state, i); padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_randtest(a, state, ctx); padic_one(b); padic_add(a, a, b, ctx); ans1 = padic_log(b, a, ctx); ans2 = padic_log(a, a, ctx); result = (ans1 == ans2) && (!ans1 || padic_equal(a, b)); if (!result) { flint_printf("FAIL (aliasing):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); abort(); } padic_clear(a); padic_clear(b); padic_ctx_clear(ctx); } /* Check: log(a) + log(b) == log(a * b) */ for (i = 0; i < 10000; i++) { fmpz_t p = {WORD(2)}; slong N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; int ans1, ans2, ans3; N = __rand_prec(state, i); padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_init2(c, N); padic_init2(d, N); padic_init2(e, N); padic_init2(f, N); padic_init2(g, N); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_one(c); padic_add(a, a, c, ctx); padic_add(b, b, c, ctx); padic_mul(c, a, b, ctx); ans1 = padic_log(d, a, ctx); ans2 = padic_log(e, b, ctx); padic_add(f, d, e, ctx); ans3 = padic_log(g, c, ctx); result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g))); if (!result) { flint_printf("FAIL (functional equation):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("c = a * b = "), padic_print(c, ctx), flint_printf("\n"); flint_printf("d = log(a) = "), padic_print(d, ctx), flint_printf("\n"); flint_printf("e = log(b) = "), padic_print(e, ctx), flint_printf("\n"); flint_printf("f = log(a) + log(b) = "), padic_print(f, ctx), flint_printf("\n"); flint_printf("g = log(a * b) = "), padic_print(g, ctx), flint_printf("\n"); abort(); } padic_clear(a); padic_clear(b); padic_clear(c); padic_clear(d); padic_clear(e); padic_clear(f); padic_clear(g); padic_ctx_clear(ctx); } /* Check: log(exp(x)) == x */ for (i = 0; i < 10000; i++) { fmpz_t p = {WORD(2)}; slong N; padic_ctx_t ctx; padic_t a, b, c; int ans1, ans2; N = __rand_prec(state, i); padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_init2(c, N); padic_randtest(a, state, ctx); ans1 = padic_exp(b, a, ctx); if (ans1) ans2 = padic_log(c, b, ctx); result = !ans1 || (ans1 == ans2 && padic_equal(a, c)); if (!result) { flint_printf("FAIL (log(exp(x)) == x):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("c = "), padic_print(c, ctx), flint_printf("\n"); flint_printf("ans1 = %d\n", ans1); flint_printf("ans2 = %d\n", ans2); abort(); } padic_clear(a); padic_clear(b); padic_clear(c); padic_ctx_clear(ctx); } /** p > 2 ********************************************************************/ /* Check aliasing: a = log(a) */ for (i = 0; i < 1000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b; int ans1, ans2; fmpz_init_set_ui(p, n_randtest_prime(state, 0)); N = __rand_prec(state, i); padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_randtest(a, state, ctx); padic_one(b); padic_add(a, a, b, ctx); ans1 = padic_log(b, a, ctx); ans2 = padic_log(a, a, ctx); result = (ans1 == ans2) && (!ans1 || padic_equal(a, b)); if (!result) { flint_printf("FAIL (aliasing):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); abort(); } padic_clear(a); padic_clear(b); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check: log(a) + log(b) == log(a * b) */ for (i = 0; i < 10000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; int ans1, ans2, ans3; /* fmpz_init_set_ui(p, n_randtest_prime(state, 0)); */ fmpz_init_set_ui(p, n_randprime(state, 5, 1)); N = __rand_prec(state, i); padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_init2(c, N); padic_init2(d, N); padic_init2(e, N); padic_init2(f, N); padic_init2(g, N); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_one(c); padic_add(a, a, c, ctx); padic_one(c); padic_add(b, b, c, ctx); padic_mul(c, a, b, ctx); ans1 = padic_log(d, a, ctx); ans2 = padic_log(e, b, ctx); padic_add(f, d, e, ctx); ans3 = padic_log(g, c, ctx); result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g))); if (!result) { flint_printf("FAIL (functional equation):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("c = a * b = "), padic_print(c, ctx), flint_printf("\n"); flint_printf("d = log(a) = "), padic_print(d, ctx), flint_printf("\n"); flint_printf("e = log(b) = "), padic_print(e, ctx), flint_printf("\n"); flint_printf("f = log(a) + log(b) = "), padic_print(f, ctx), flint_printf("\n"); flint_printf("g = log(a * b) = "), padic_print(g, ctx), flint_printf("\n"); flint_printf("ans1 = %d\n", ans1); flint_printf("ans2 = %d\n", ans2); flint_printf("ans3 = %d\n", ans3); abort(); } padic_clear(a); padic_clear(b); padic_clear(c); padic_clear(d); padic_clear(e); padic_clear(f); padic_clear(g); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check: log(exp(x)) == x */ for (i = 0; i < 10000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b, c; int ans1, ans2; fmpz_init_set_ui(p, n_randtest_prime(state, 0)); N = __rand_prec(state, i); padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_init2(c, N); padic_randtest(a, state, ctx); ans1 = padic_exp(b, a, ctx); if (ans1) ans2 = padic_log(c, b, ctx); result = !ans1 || (ans1 == ans2 && padic_equal(a, c)); if (!result) { flint_printf("FAIL (log(exp(x)) == x):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("c = "), padic_print(c, ctx), flint_printf("\n"); flint_printf("ans1 = %d\n", ans1); flint_printf("ans2 = %d\n", ans2); abort(); } padic_clear(a); padic_clear(b); padic_clear(c); fmpz_clear(p); padic_ctx_clear(ctx); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; flint_rand_t state; printf("sqrt... "); fflush(stdout); flint_randinit(state); /* PRIME p = 2 ***************************************************************/ /* Check aliasing: a = sqrt(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; int ans1, ans2; padic_t a, d; fmpz_init(p); fmpz_set_ui(p, 2); N = z_randint(state, 100); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); ans1 = padic_sqrt(d, a, ctx); ans2 = padic_sqrt(a, a, ctx); result = ((ans1 == ans2) && (!ans1 || padic_equal(a, d, ctx))); if (!result) { printf("FAIL (aliasing):\n\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Test random elements */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; int ans; padic_t a, b, d; fmpz_init(p); fmpz_set_ui(p, 2); N = z_randint(state, 100); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); ans = padic_sqrt(b, a, ctx); padic_mul(d, b, b, ctx); if (ans && padic_val(a) < 0) { padic_ctx_t ctx2; padic_t a2, d2; padic_ctx_init(ctx2, p, N + padic_val(a), PADIC_SERIES); padic_init(a2, ctx2); padic_init(d2, ctx2); padic_set(a2, a, ctx2); padic_set(d2, d, ctx2); padic_reduce(a2, ctx2); padic_reduce(d2, ctx2); result = (padic_equal(a2, d2, ctx2)); if (!result) { printf("FAIL (random elements):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("a2 = "), padic_print(a2, ctx2), printf("\n"); printf("d2 = "), padic_print(d2, ctx2), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } padic_clear(a2, ctx2); padic_clear(d2, ctx2); padic_ctx_clear(ctx2); } else { result = (!ans || padic_equal(a, d, ctx)); if (!result) { printf("FAIL (random elements):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Test random squares */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; int ans; padic_t a, b, c, d; fmpz_init(p); fmpz_set_ui(p, 2); N = z_randint(state, 100); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(c, ctx); padic_init(d, ctx); padic_randtest(b, state, ctx); padic_mul(a, b, b, ctx); ans = padic_sqrt(c, a, ctx); padic_mul(d, c, c, ctx); if (ans && padic_val(a) < 0) { padic_ctx_t ctx2; padic_t a2, d2; padic_ctx_init(ctx2, p, N + padic_val(a), PADIC_SERIES); padic_init(a2, ctx2); padic_init(d2, ctx2); padic_set(a2, a, ctx2); padic_set(d2, d, ctx2); padic_reduce(a2, ctx2); padic_reduce(d2, ctx2); result = (padic_equal(a2, d2, ctx2)); if (!result) { printf("FAIL (random elements):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = "), padic_print(c, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("a2 = "), padic_print(a2, ctx2), printf("\n"); printf("d2 = "), padic_print(d2, ctx2), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } padic_clear(a2, ctx2); padic_clear(d2, ctx2); padic_ctx_clear(ctx2); } else { result = (ans && padic_equal(a, d, ctx)); if (!result) { printf("FAIL (random squares):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = "), padic_print(c, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(c, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* PRIME p > 2 ***************************************************************/ /* Check aliasing: a = sqrt(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; int ans1, ans2; padic_t a, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 100); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); ans1 = padic_sqrt(d, a, ctx); ans2 = padic_sqrt(a, a, ctx); result = ((ans1 == ans2) && (!ans1 || padic_equal(a, d, ctx))); if (!result) { printf("FAIL (aliasing):\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Test random elements */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; int ans; padic_t a, b, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 100); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); ans = padic_sqrt(b, a, ctx); padic_mul(d, b, b, ctx); if (ans && padic_val(a) < 0) { padic_ctx_t ctx2; padic_t a2, d2; padic_ctx_init(ctx2, p, N + padic_val(a), PADIC_SERIES); padic_init(a2, ctx2); padic_init(d2, ctx2); padic_set(a2, a, ctx2); padic_set(d2, d, ctx2); padic_reduce(a2, ctx2); padic_reduce(d2, ctx2); result = (padic_equal(a2, d2, ctx2)); if (!result) { printf("FAIL (random elements):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("a2 = "), padic_print(a2, ctx2), printf("\n"); printf("d2 = "), padic_print(d2, ctx2), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } padic_clear(a2, ctx2); padic_clear(d2, ctx2); padic_ctx_clear(ctx2); } else { result = (!ans || padic_equal(a, d, ctx)); if (!result) { printf("FAIL (random elements):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Test random squares */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; int ans; padic_t a, b, c, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 100); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(c, ctx); padic_init(d, ctx); padic_randtest(b, state, ctx); padic_mul(a, b, b, ctx); ans = padic_sqrt(c, a, ctx); padic_mul(d, c, c, ctx); if (ans && padic_val(a) < 0) { padic_ctx_t ctx2; padic_t a2, d2; padic_ctx_init(ctx2, p, N + padic_val(a), PADIC_SERIES); padic_init(a2, ctx2); padic_init(d2, ctx2); padic_set(a2, a, ctx2); padic_set(d2, d, ctx2); padic_reduce(a2, ctx2); padic_reduce(d2, ctx2); result = (padic_equal(a2, d2, ctx2)); if (!result) { printf("FAIL (random elements):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = "), padic_print(c, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("a2 = "), padic_print(a2, ctx2), printf("\n"); printf("d2 = "), padic_print(d2, ctx2), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } padic_clear(a2, ctx2); padic_clear(d2, ctx2); padic_ctx_clear(ctx2); } else { result = (ans && padic_equal(a, d, ctx)); if (!result) { printf("FAIL (random squares):\n\n"); printf("a = "), padic_print(a, ctx), printf("\n"); printf("b = "), padic_print(b, ctx), printf("\n"); printf("c = "), padic_print(c, ctx), printf("\n"); printf("d = "), padic_print(d, ctx), printf("\n"); printf("p = "), fmpz_print(p), printf("\n"); printf("ans = %d\n", ans); abort(); } } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(c, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; flint_rand_t state; printf("mul... "); fflush(stdout); flint_randinit(state); /* Check aliasing: a = a * b (mod p^N) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_mul(d, a, b, ctx); padic_mul(a, a, b, ctx); result = (padic_equal(a, d, ctx)); if (!result) { printf("FAIL:\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("b = "), _padic_print(b, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check aliasing: b = a * b (mod p^N) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_mul(d, a, b, ctx); padic_mul(b, a, b, ctx); result = (padic_equal(b, d, ctx)); if (!result) { printf("FAIL:\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("b = "), _padic_print(b, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check aliasing: a = a * a (mod p^N) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); padic_mul(d, a, a, ctx); padic_mul(a, a, a, ctx); result = (padic_equal(a, d, ctx)); if (!result) { printf("FAIL:\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check that a * b == b * a (mod p^N) */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, c, d; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(c, ctx); padic_init(d, ctx); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_mul(c, a, b, ctx); padic_mul(d, b, a, ctx); result = (padic_equal(c, d, ctx)); if (!result) { printf("FAIL:\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("b = "), _padic_print(b, ctx), printf("\n"); printf("c = "), _padic_print(c, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(c, ctx); padic_clear(d, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check that (a * b) * c == a * (b * c), correct only mod p^{N-v} */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b, c, d, e; long v; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_init(c, ctx); padic_init(d, ctx); padic_init(e, ctx); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_randtest(c, state, ctx); v = FLINT_MIN(padic_val(a), padic_val(b)); v = FLINT_MIN(v, padic_val(c)); v = FLINT_MIN(v, 0); if ((v >= 0) || (-v < N)) /* Otherwise, no precision left */ { padic_ctx_t ctx2; padic_ctx_init(ctx2, p, (v >= 0) ? N : N + v, PADIC_SERIES); padic_mul(d, a, b, ctx); padic_mul(d, d, c, ctx); padic_mul(e, b, c, ctx); padic_mul(e, a, e, ctx); padic_reduce(d, ctx2); padic_reduce(e, ctx2); result = (padic_equal(d, e, ctx2)); if (!result) { printf("FAIL:\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("b = "), _padic_print(b, ctx), printf("\n"); printf("c = "), _padic_print(c, ctx), printf("\n"); printf("d = "), _padic_print(d, ctx2), printf("\n"); printf("e = "), _padic_print(e, ctx2), printf("\n"); abort(); } padic_ctx_clear(ctx2); } padic_clear(a, ctx); padic_clear(b, ctx); padic_clear(c, ctx); padic_clear(d, ctx); padic_clear(e, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } /* Check that a * 1 == a */ for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t a, b; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50); padic_ctx_init(ctx, p, N, PADIC_SERIES); padic_init(a, ctx); padic_init(b, ctx); padic_randtest(a, state, ctx); _padic_one(b); padic_mul(b, a, b, ctx); result = (padic_equal(a, b, ctx)); if (!result) { printf("FAIL:\n\n"); printf("a = "), _padic_print(a, ctx), printf("\n"); printf("b = "), _padic_print(b, ctx), printf("\n"); abort(); } padic_clear(a, ctx); padic_clear(b, ctx); fmpz_clear(p); padic_ctx_clear(ctx); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("exp_balanced... "); fflush(stdout); /** p == 2 *******************************************************************/ /* Check aliasing: a = exp(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b; int ans1, ans2; fmpz_init_set_ui(p, 2); N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) + PADIC_TEST_PREC_MIN; padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_randtest(a, state, ctx); ans1 = padic_exp_balanced(b, a, ctx); ans2 = padic_exp_balanced(a, a, ctx); result = ((ans1 == ans2) && (!ans1 || padic_equal(a, b))); if (!result) { flint_printf("FAIL (aliasing):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("ans1 = %d\n", ans1); flint_printf("ans2 = %d\n", ans2); abort(); } padic_clear(a); padic_clear(b); fmpz_clear(p); padic_ctx_clear(ctx); } /* Functional equation: exp(a + b) == exp(a) exp(b) */ for (i = 0; i < 10000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; int ans1, ans2, ans3; fmpz_init_set_ui(p, 2); N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) + PADIC_TEST_PREC_MIN; padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_init2(c, N); padic_init2(d, N); padic_init2(e, N); padic_init2(f, N); padic_init2(g, N); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_add(c, a, b, ctx); ans1 = padic_exp_balanced(d, a, ctx); ans2 = padic_exp_balanced(e, b, ctx); padic_mul(f, d, e, ctx); ans3 = padic_exp_balanced(g, c, ctx); result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g))); if (!result) { flint_printf("FAIL (functional equation):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("c = a + b = "), padic_print(c, ctx), flint_printf("\n"); flint_printf("d = exp(a) = "), padic_print(d, ctx), flint_printf("\n"); flint_printf("e = exp(b) = "), padic_print(e, ctx), flint_printf("\n"); flint_printf("f = exp(a) exp(b) = "), padic_print(f, ctx), flint_printf("\n"); flint_printf("g = exp(a + b) = "), padic_print(g, ctx), flint_printf("\n"); abort(); } padic_clear(a); padic_clear(b); padic_clear(c); padic_clear(d); padic_clear(e); padic_clear(f); padic_clear(g); fmpz_clear(p); padic_ctx_clear(ctx); } /** p > 2 ********************************************************************/ /* Check aliasing: a = exp(a) */ for (i = 0; i < 10000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b; int ans1, ans2; fmpz_init_set_ui(p, n_randtest_prime(state, 0)); N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) + PADIC_TEST_PREC_MIN; padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_randtest(a, state, ctx); ans1 = padic_exp_balanced(b, a, ctx); ans2 = padic_exp_balanced(a, a, ctx); result = ((ans1 == ans2) && (!ans1 || padic_equal(a, b))); if (!result) { flint_printf("FAIL (aliasing):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("ans1 = %d\n", ans1); flint_printf("ans2 = %d\n", ans2); abort(); } padic_clear(a); padic_clear(b); fmpz_clear(p); padic_ctx_clear(ctx); } /* Functional equation: exp(a + b) == exp(a) exp(b) */ for (i = 0; i < 10000; i++) { fmpz_t p; slong N; padic_ctx_t ctx; padic_t a, b, c, d, e, f, g; int ans1, ans2, ans3; fmpz_init_set_ui(p, n_randtest_prime(state, 0)); N = n_randint(state, PADIC_TEST_PREC_MAX - PADIC_TEST_PREC_MIN) + PADIC_TEST_PREC_MIN; padic_ctx_init(ctx, p, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), PADIC_SERIES); padic_init2(a, N); padic_init2(b, N); padic_init2(c, N); padic_init2(d, N); padic_init2(e, N); padic_init2(f, N); padic_init2(g, N); padic_randtest(a, state, ctx); padic_randtest(b, state, ctx); padic_add(c, a, b, ctx); ans1 = padic_exp_balanced(d, a, ctx); ans2 = padic_exp_balanced(e, b, ctx); padic_mul(f, d, e, ctx); ans3 = padic_exp_balanced(g, c, ctx); result = (!ans1 || !ans2 || (ans3 && padic_equal(f, g))); if (!result) { flint_printf("FAIL (functional equation):\n\n"); flint_printf("a = "), padic_print(a, ctx), flint_printf("\n"); flint_printf("b = "), padic_print(b, ctx), flint_printf("\n"); flint_printf("c = a + b = "), padic_print(c, ctx), flint_printf("\n"); flint_printf("d = exp(a) = "), padic_print(d, ctx), flint_printf("\n"); flint_printf("e = exp(b) = "), padic_print(e, ctx), flint_printf("\n"); flint_printf("f = exp(a) exp(b) = "), padic_print(f, ctx), flint_printf("\n"); flint_printf("g = exp(a + b) = "), padic_print(g, ctx), flint_printf("\n"); abort(); } padic_clear(a); padic_clear(b); padic_clear(c); padic_clear(d); padic_clear(e); padic_clear(f); padic_clear(g); fmpz_clear(p); padic_ctx_clear(ctx); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return EXIT_SUCCESS; }