// generate a random mpz_mat_t with the given number of rows and columns and number of bits per entry void mpz_randmat(mpz_mat_t mat, ulong r, ulong c, ulong maxbits) { ulong bits; mpz_t temp; mpz_init(temp); long i; for (i = 0; i < r; i++) { long j; for (j = 0; j < c; j++) { #if VARY_BITS bits = z_randint(maxbits+1); #else bits = maxbits; #endif if (bits == 0) mpz_set_ui(temp, 0); else { #if SPARSE if (z_randint(10) == 1) mpz_rrandomb(temp, randstate, bits); else mpz_set_ui(temp, 0); #else mpz_rrandomb(temp, randstate, bits); #endif #if SIGNS if (z_randint(2)) mpz_neg(temp, temp); #endif } mpz_set(mat->entries[i*c+j], temp); } } mpz_clear(temp); }
void mpz_mat_randintrel(mpz_mat_t mat, ulong r, ulong c, ulong maxbits) { ulong bits; mpz_t temp; mpz_init(temp); long i; for (i = 0; i < r; i++) { #if VARY_BITS bits = z_randint(maxbits+1); #else bits = maxbits; #endif if (bits == 0) mpz_set_ui(temp, 0); else { mpz_rrandomb(temp, randstate, bits); #if SIGNS if (z_randint(2)) mpz_neg(temp, temp); #endif } mpz_set(mat->entries[i*c + c - 1], temp); long j; for (j = 0; j < i; j++) mpz_set_ui(mat->entries[i*c + j], 0); mpz_set_ui(mat->entries[i*c + i], 1); for (j = i + 1; j < c - 1; j++) mpz_set_ui(mat->entries[i*c + j], 0); } mpz_clear(temp); }
int test_heuristic_scalar_product() { int result = 1; ulong count1, count2, i; double ** mat; F_mpz_mat_t B; int expo[50]; for (count1 = 0; count1 < 1000; count1++) { ulong rows = z_randint(50) + 1; ulong cols = z_randint(50) + 2; F_mpz_mat_init(B, rows, cols); F_mpz_randmat(B, rows, cols, 900); mat = d_mat_init(rows, cols); for (i = 0; i < rows; i++) expo[i] = _F_mpz_vec_to_d_vec_2exp(mat[i], B->rows[i], cols); for (count2 = 0; (count2 < 100); count2++) { ulong r1 = z_randint(rows); ulong r2 = z_randint(rows); double d1 = heuristic_scalar_product(mat[r1], mat[r2], cols, B, r1, r2, expo[r1] + expo[r2]); double d2 = heuristic_scalar_product(mat[r1], mat[r1], cols, B, r1, r1, expo[r1] + expo[r1]); double d3 = heuristic_scalar_product(mat[r2], mat[r2], cols, B, r2, r2, expo[r2] + expo[r2]); _d_vec_add(mat[r2], mat[r1], mat[r2], cols); _F_mpz_vec_add(B->rows[r2], B->rows[r1], B->rows[r2], cols); double d4 = heuristic_scalar_product(mat[r2], mat[r2], cols, B, r2, r2, expo[r2] + expo[r2]); result = (fabs(d4 - d3 - d2 - 2*d1) < 1.0E-12); if (!result) { printf("count2 = %ld Failed expo[r1] = %d expo[r2] = %d, d1 = %lf, d2 = %lf, d3 = %lf, d4 = %f\n", count2, expo[r1], expo[r2], d1, d2, d3, d4); } expo[r2] = _F_mpz_vec_to_d_vec_2exp(mat[r2], B->rows[r2], cols); } F_mpz_mat_clear(B); d_mat_clear(mat); } return result; }
int main(void) { int i, result; flint_rand_t state; printf("get_str... "); fflush(stdout); flint_randinit(state); for (i = 0; i < 10000; i++) { fmpz_t p; long N; padic_ctx_t ctx; padic_t x; fmpq_t y; char *s, *t; fmpz_init(p); fmpz_set_ui(p, n_randprime(state, 5, 1)); N = z_randint(state, 50) + 1; padic_ctx_init(ctx, p, N, PADIC_TERSE); padic_init(x, ctx); fmpq_init(y); padic_randtest(x, state, ctx); _padic_get_fmpq(y, x, ctx); s = _padic_get_str(NULL, x, ctx); t = fmpq_get_str(NULL, 10, y); result = strcmp(s, t) == 0; if (!result) { printf("FAIL:\n\n"); printf("x = "), _padic_print(x, ctx), printf("\n"); printf("y = "), fmpq_clear(y), printf("\n"); abort(); } free(s); free(t); _padic_clear(x); fmpq_clear(y); padic_ctx_clear(ctx); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return EXIT_SUCCESS; }
void F_mpz_test_random(F_mpz_t f, ulong bits) { if (bits == 0) { F_mpz_zero(f); return; } mpz_t temp; mpz_init(temp); mpz_rrandomb(temp, randstate, bits); #if SIGNS if (z_randint(2)) mpz_neg(temp, temp); #endif F_mpz_set_mpz(f, temp); mpz_clear(temp); }
void mpz_mat_randajtai(mpz_mat_t mat, ulong r, ulong c, double alpha) { ulong i, j, d, bits; mpz_t tmp; r = mat->r; c = mat->c; d = r; if (c != r) { printf("Exception: mpz_mat_ajtai called on an ill-formed matrix\n"); abort(); } mpz_init(tmp); for (i = 0; i < d; i++) { bits = (ulong) pow((double) (2*d - i), alpha); mpz_rrandomb(mat->entries[i*c + i], randstate, bits); mpz_add_ui(mat->entries[i*c + i], mat->entries[i*c + i], 2); mpz_fdiv_q_2exp(mat->entries[i*c + i], mat->entries[i*c + i], 1); for (j = i + 1; j < d; j++) { mpz_rrandomb(mat->entries[j*c + i], randstate, bits); if (z_randint(2)) mpz_neg(mat->entries[j*c + i], mat->entries[j*c + i]); mpz_set_ui(mat->entries[i*c + j], 0L); } } mpz_clear(tmp); }
void compute_A(QS_t * qs_inf, poly_t * poly_inf) { unsigned long min = poly_inf->min; unsigned long span = poly_inf->span; unsigned long s = poly_inf->s; unsigned long * A_ind = poly_inf->A_ind; unsigned long * A = poly_inf->A; unsigned long * target_A = poly_inf->target_A; unsigned long * current_A = (unsigned long *) flint_stack_alloc(qs_inf->prec+1); unsigned long * diff = (unsigned long *) flint_stack_alloc(qs_inf->prec+1); unsigned long * best_diff = (unsigned long *) flint_stack_alloc(qs_inf->prec+1); prime_t * factor_base = qs_inf->factor_base; unsigned long factor, p; unsigned long best1, best2, best3; unsigned long odds = s - 3; mp_limb_t msl; int taken; long i, j, k; A[0] = 1; A[1] = 1; for (i = 0; i < odds; i++) // Randomly choose the first s-3 prime factors of A with odd indices { do { taken = 0; A_ind[i] = ((z_randint(span) + min) | 1); if (A_ind[i] == min + span) A_ind[i] -= 2; for (j = 0; j < i; j++) { if (A_ind[i] == A_ind[j]) taken = 1; } } while (taken); msl = mpn_mul_1(A+1, A+1, A[0], factor_base[A_ind[i]].p); if (msl) // Compute the product of these s-3 primes { A[A[0]+1] = msl; A[0]++; } } for (k = 0; k < 30; k++) // Now try 8 different sets of even index primes as the remaining factors { F_mpn_copy(current_A, A, A[0] + 1); for (i = 0; i < 3; i++) // Randomly choose the last 3 prime factors of A with even indices { do { taken = 0; A_ind[s-3+i] = ((z_randint(span) + min) & -2L); if (A_ind[s-3+i] < min) A_ind[s-3+i] += 2; for (j = 0; j < i; j++) { if (A_ind[s-3+i] == A_ind[s-3+j]) taken = 1; } } while (taken); msl = mpn_mul_1(current_A+1, current_A+1, current_A[0], factor_base[A_ind[s-3+i]].p); if (msl) // Compute the product of these s-3 primes and the odd indexed primes { current_A[current_A[0]+1] = msl; current_A[0]++; } } if (k == 0) // Just store the first difference as the best one { if (target_A[0] >= current_A[0]) // Compute the difference with the target A { msl = mpn_sub(best_diff+1, target_A+1, target_A[0], current_A+1, current_A[0]); best_diff[0] = target_A[0]; } else { msl = mpn_sub(best_diff+1, current_A+1, current_A[0], target_A+1, target_A[0]); best_diff[0] = current_A[0]; } if (msl) F_mpn_negate(best_diff+1, best_diff+1, best_diff[0]); while ((!best_diff[best_diff[0]]) && (best_diff[0])) best_diff[0]--; // Normalise best_diff best1 = A_ind[s-3]; best2 = A_ind[s-2]; best3 = A_ind[s-1]; continue; } if (target_A[0] >= current_A[0]) // Compute the difference with the target A { msl = mpn_sub(diff+1, target_A+1, target_A[0], current_A+1, current_A[0]); diff[0] = target_A[0]; } else { msl = mpn_sub(diff+1, current_A+1, current_A[0], target_A+1, target_A[0]); diff[0] = current_A[0]; } if (msl) F_mpn_negate(diff+1, diff+1, diff[0]); while ((!diff[diff[0]]) && (diff[0])) diff[0]--; // Normalise diff if ((diff[0] < best_diff[0]) || ((diff[0] == best_diff[0]) && (mpn_cmp(diff+1, best_diff+1, diff[0]) < 0))) // The new diff is better { F_mpn_copy(best_diff, diff, diff[0]+1); best1 = A_ind[s-3]; best2 = A_ind[s-2]; best3 = A_ind[s-1]; } } A_ind[s-3] = best1; // Multiply A by the product of these 3 primes and store their indices A_ind[s-2] = best2; A_ind[s-1] = best3; for (i = 0; i < 3; i++) { msl = mpn_mul_1(A+1, A+1, A[0], factor_base[A_ind[s+i-3]].p); if (msl) { A[A[0]+1] = msl; A[0]++; } } #if POLY_A mpz_t A_disp, targ_A; mpz_init(A_disp); mpz_init(targ_A); fmpz_to_mpz(A_disp, A); fmpz_to_mpz(targ_A, target_A); gmp_printf("A = %Zd, target A = %Zd\n", A_disp, targ_A); mpz_clear(A_disp); mpz_clear(targ_A); #endif /*for (i = 0; i < s; i++) { p = factor_base[A_ind[i]].p; poly_inf->inv_p2[i] = z_precompute_inverse(p*p); } */ fmpz_to_mpz(poly_inf->A_mpz, A); flint_stack_release(); // release current_A flint_stack_release(); // release diff flint_stack_release(); // release best_diff }
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_TEST_INIT(state); flint_printf("mul... "); fflush(stdout); /* Check aliasing: a = a * b */ for (i = 0; i < 2000; i++) { fmpz_t p; slong d, N; qadic_ctx_t ctx; qadic_t a, b, c; fmpz_init_set_ui(p, n_randprime(state, 2 + n_randint(state, 3), 1)); d = n_randint(state, 10) + 1; N = z_randint(state, 50) + 1; qadic_ctx_init_conway(ctx, p, d, FLINT_MAX(0,N-10), FLINT_MAX(0,N+10), "a", PADIC_SERIES); qadic_init2(a, N); qadic_init2(b, N); qadic_init2(c, N); qadic_randtest(a, state, ctx); qadic_randtest(b, state, ctx); qadic_mul(c, a, b, ctx); qadic_mul(a, a, b, ctx); result = (qadic_equal(a, c)); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), qadic_print_pretty(a, ctx), flint_printf("\n"); flint_printf("b = "), qadic_print_pretty(b, ctx), flint_printf("\n"); flint_printf("c = "), qadic_print_pretty(c, ctx), flint_printf("\n"); abort(); } qadic_clear(a); qadic_clear(b); qadic_clear(c); fmpz_clear(p); qadic_ctx_clear(ctx); } /* Check aliasing: b = a * b */ for (i = 0; i < 2000; i++) { fmpz_t p; slong d, N; qadic_ctx_t ctx; qadic_t a, b, c; fmpz_init_set_ui(p, n_randprime(state, 2 + n_randint(state, 3), 1)); d = n_randint(state, 10) + 1; N = z_randint(state, 50) + 1; qadic_ctx_init_conway(ctx, p, d, FLINT_MAX(0, N-10), FLINT_MAX(0, N+10), "a", PADIC_SERIES); qadic_init2(a, N); qadic_init2(b, N); qadic_init2(c, N); qadic_randtest(a, state, ctx); qadic_randtest(b, state, ctx); qadic_mul(c, a, b, ctx); qadic_mul(b, a, b, ctx); result = (qadic_equal(b, c)); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), qadic_print_pretty(a, ctx), flint_printf("\n"); flint_printf("b = "), qadic_print_pretty(b, ctx), flint_printf("\n"); flint_printf("c = "), qadic_print_pretty(c, ctx), flint_printf("\n"); abort(); } qadic_clear(a); qadic_clear(b); qadic_clear(c); fmpz_clear(p); qadic_ctx_clear(ctx); } /* Check aliasing: a = a + a */ for (i = 0; i < 2000; i++) { fmpz_t p; slong d, N; qadic_ctx_t ctx; qadic_t a, c; fmpz_init_set_ui(p, n_randprime(state, 2 + n_randint(state, 3), 1)); d = n_randint(state, 10) + 1; N = z_randint(state, 50) + 1; qadic_ctx_init_conway(ctx, p, d, FLINT_MAX(0,N-10), FLINT_MAX(0,N+10), "a", PADIC_SERIES); qadic_init2(a, N); qadic_init2(c, N); qadic_randtest(a, state, ctx); qadic_add(c, a, a, ctx); qadic_add(a, a, a, ctx); result = (qadic_equal(a, c)); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), qadic_print_pretty(a, ctx), flint_printf("\n"); flint_printf("c = "), qadic_print_pretty(c, ctx), flint_printf("\n"); abort(); } qadic_clear(a); qadic_clear(c); fmpz_clear(p); qadic_ctx_clear(ctx); } /* Check that a * b == b * a */ for (i = 0; i < 2000; i++) { fmpz_t p; slong d, N; qadic_ctx_t ctx; qadic_t a, b, c1, c2; fmpz_init_set_ui(p, n_randprime(state, 2 + n_randint(state, 3), 1)); d = n_randint(state, 10) + 1; N = z_randint(state, 50) + 1; qadic_ctx_init_conway(ctx, p, d, FLINT_MAX(0,N-10), FLINT_MAX(0,N+10), "a", PADIC_SERIES); qadic_init2(a, N); qadic_init2(b, N); qadic_init2(c1, N); qadic_init2(c2, N); qadic_randtest(a, state, ctx); qadic_randtest(b, state, ctx); qadic_mul(c1, a, b, ctx); qadic_mul(c2, b, a, ctx); result = (qadic_equal(c1, c2)); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), qadic_print_pretty(a, ctx), flint_printf("\n"); flint_printf("b = "), qadic_print_pretty(b, ctx), flint_printf("\n"); flint_printf("c1 = "), qadic_print_pretty(c1, ctx), flint_printf("\n"); flint_printf("c2 = "), qadic_print_pretty(c2, ctx), flint_printf("\n"); abort(); } qadic_clear(a); qadic_clear(b); qadic_clear(c1); qadic_clear(c2); fmpz_clear(p); qadic_ctx_clear(ctx); } /* Check that (a * b) * c == a * (b * c) for integral values */ for (i = 0; i < 2000; i++) { fmpz_t p; slong d, N; qadic_ctx_t ctx; qadic_t a, b, c, lhs, rhs; fmpz_init_set_ui(p, n_randprime(state, 2 + n_randint(state, 3), 1)); d = n_randint(state, 10) + 1; N = n_randint(state, 50) + 1; qadic_ctx_init_conway(ctx, p, d, FLINT_MAX(0,N-10), FLINT_MAX(0,N+10), "a", PADIC_SERIES); qadic_init2(a, N); qadic_init2(b, N); qadic_init2(c, N); qadic_init2(lhs, N); qadic_init2(rhs, N); qadic_randtest_int(a, state, ctx); qadic_randtest_int(b, state, ctx); qadic_randtest_int(c, state, ctx); qadic_mul(lhs, a, b, ctx); qadic_mul(lhs, lhs, c, ctx); qadic_mul(rhs, b, c, ctx); qadic_mul(rhs, a, rhs, ctx); result = (qadic_equal(lhs, rhs)); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), qadic_print_pretty(a, ctx), flint_printf("\n"); flint_printf("b = "), qadic_print_pretty(b, ctx), flint_printf("\n"); flint_printf("c = "), qadic_print_pretty(c, ctx), flint_printf("\n"); flint_printf("lhs = "), qadic_print_pretty(lhs, ctx), flint_printf("\n"); flint_printf("rhs = "), qadic_print_pretty(rhs, ctx), flint_printf("\n"); abort(); } qadic_clear(a); qadic_clear(b); qadic_clear(c); qadic_clear(lhs); qadic_clear(rhs); fmpz_clear(p); qadic_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("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; flint_rand_t state; printf("init_set_readonly...."); fflush(stdout); flint_randinit(state); /* Create some small fmpz integers, clear the mpz_t */ for (i = 0; i < 100000; i++) { fmpz_t f; mpz_t z; *f = z_randint(state, COEFF_MAX + 1); mpz_init(z); fmpz_get_mpz(z, f); { fmpz_t g; fmpz_init_set_readonly(g, z); fmpz_clear_readonly(g); } mpz_clear(z); } /* Create some small fmpz integers, do *not* clear the mpz_t */ for (i = 0; i < 100000; i++) { fmpz_t f; mpz_t z; *f = z_randint(state, COEFF_MAX + 1); mpz_init(z); fmpz_get_mpz(z, f); { fmpz_t g; fmpz_init_set_readonly(g, z); } mpz_clear(z); } /* Create some more fmpz integers */ for (i = 0; i < 100000; i++) { fmpz_t f; mpz_t z; fmpz_init(f); fmpz_randtest(f, state, 2 * FLINT_BITS); mpz_init(z); fmpz_get_mpz(z, f); { fmpz_t g, h; fmpz_init_set_readonly(g, z); fmpz_init(h); fmpz_set_mpz(h, z); if (!fmpz_equal(g, h)) { printf("FAIL:\n\n"); printf("g = "), fmpz_print(g), printf("\n"); printf("h = "), fmpz_print(h), printf("\n"); gmp_printf("z = %Zd\n", z); } fmpz_clear_readonly(g); fmpz_clear(h); } fmpz_clear(f); mpz_clear(z); } flint_randclear(state); _fmpz_cleanup(); printf("PASS\n"); return 0; }
int test_F_mpz_LLL_randsimdioph() { mpz_mat_t m_mat; F_mpz_mat_t F_mat; int result = 1; ulong bits1, bits2; F_mpz_t fzero; F_mpz_init(fzero); ulong count1; for (count1 = 0; (count1 < 10*ITER) && (result == 1) ; count1++) { #if TRACE printf("count1 == %ld\n", count1); #endif ulong r = z_randint(200) + 1; ulong c = r; F_mpz_mat_init(F_mat, r, c); mpz_mat_init(m_mat, r, c); bits1 = z_randint(200) + 1; bits2 = z_randint(5) + 1; mpz_mat_randsimdioph(m_mat, r, c, bits1, bits2); mpz_mat_to_F_mpz_mat(F_mat, m_mat); F_mpz_set_d_2exp(fzero, 2.0, bits2); // good stuff here U_LLL_with_removal(F_mat, 350, fzero); mp_prec_t prec; prec = 20; __mpfr_struct ** Q, ** R; Q = mpfr_mat_init2(r, c, prec); R = mpfr_mat_init2(r, r, prec); F_mpz_mat_RQ_factor(F_mat, R, Q, r, c, prec); // should be that RQ = FM_copy /* long j; if (count1 == 29){ mpfr_printf("%.12Rf was R[i][i] for i = %ld\n", R[0] + 0, 0); for (j = 1; j < r; j++) { mpfr_printf("%.12Rf was R[i][i+1] for i = %ld\n", R[j] + j - 1, j); mpfr_printf("%.12Rf was R[i+1][i+1] for i = %ld\n", R[j] + j, j); } } */ result = mpfr_mat_R_reduced(R, r, (double) DELTA, (double) ETA, prec); mpfr_mat_clear(Q, r, c); mpfr_mat_clear(R, r, r); //result here result = mpz_mat_equal(res1, res2); if (!result) { F_mpz_mat_print_pretty(F_mat); printf("Error: bits1 = %ld, count1 = %ld\n", bits1, count1); } F_mpz_mat_clear(F_mat); mpz_mat_clear(m_mat); } F_mpz_clear(fzero); return result; }
int test_F_mpz_LLL_randajtai() { mpz_mat_t m_mat; F_mpz_mat_t F_mat; int result = 1; ulong bits; F_mpz_t fzero; F_mpz_init(fzero); ulong count1; for (count1 = 0; (count1 < 100*ITER) && (result == 1) ; count1++) { #if TRACE printf("count1 == %ld\n", count1); #endif ulong r = z_randint(10)+1; ulong c = r; F_mpz_mat_init(F_mat, r, c); mpz_mat_init(m_mat, r, c); bits = z_randint(200) + 1; mpz_mat_randajtai(m_mat, r, c, .5); mpz_mat_to_F_mpz_mat(F_mat, m_mat); F_mpz_set_d_2exp(fzero, (double) 2*r, 1); // good stuff here //F_mpz_mat_print_pretty(F_mat); long newd = U_LLL_with_removal(F_mat, 350, fzero); mp_prec_t prec; prec = 50; __mpfr_struct ** Q, ** R; Q = mpfr_mat_init2(r, c, prec); R = mpfr_mat_init2(r, r, prec); F_mpz_mat_RQ_factor(F_mat, R, Q, r, c, prec); // should be that RQ = FM_copy long j; #if TRACE if (count1 == 30){ mpfr_printf("%.12Rf was R[i][i] for i = %ld\n", R[0] + 0, 0); for (j = 1; j < r; j++) { mpfr_printf("%.12Rf was R[i][i+1] for i = %ld\n", R[j] + j - 1, j); mpfr_printf("%.12Rf was R[i+1][i+1] for i = %ld\n", R[j] + j, j); } } #endif result = mpfr_mat_R_reduced(R, r, (double) DELTA-.01, (double) ETA+.01, prec); mpfr_mat_clear(Q, r, c); mpfr_mat_clear(R, r, r); //result here result = mpz_mat_equal(res1, res2); if (!result) { printf("Error: bits = %ld, count1 = %ld, newd = %ld\n", bits, count1, newd); } F_mpz_mat_clear(F_mat); mpz_mat_clear(m_mat); } F_mpz_clear(fzero); return result; }
int main(void) { int i, result; FLINT_TEST_INIT(state); flint_printf("norm... "); fflush(stdout); /* Compare with product of Galois conjugates */ for (i = 0; i < 2000; i++) { fmpz_t p; slong d, N; qadic_ctx_t ctx; qadic_t a, b, c; padic_t x, y; slong j; int ans; fmpz_init_set_ui(p, n_randprime(state, 2 + n_randint(state, 3), 1)); d = n_randint(state, 10) + 1; N = z_randint(state, 50) + 1; qadic_ctx_init_conway(ctx, p, d, FLINT_MAX(0,N-10), FLINT_MAX(0,N+10), "a", PADIC_SERIES); qadic_init2(a, N); qadic_init2(b, N); qadic_init2(c, N); padic_init2(x, N); padic_init2(y, N); qadic_randtest_val(a, state, 0, ctx); qadic_reduce(a, ctx); qadic_norm(x, a, ctx); qadic_one(b); for (j = 0; j < d; j++) { qadic_frobenius(c, a, j, ctx); qadic_mul(b, b, c, ctx); } ans = qadic_get_padic(y, b, ctx); result = (ans && padic_equal(x, y)); if (!result) { flint_printf("FAIL:\n\n"); flint_printf("a = "), qadic_print_pretty(a, ctx), flint_printf("\n"); flint_printf("b = "), qadic_print_pretty(b, ctx), flint_printf("\n"); flint_printf("x = "), padic_print(x, &ctx->pctx), flint_printf("\n"); flint_printf("y = "), padic_print(y, &ctx->pctx), flint_printf("\n"); for (j = 0; j < d; j++) { qadic_frobenius(c, a, j, ctx); flint_printf("sigma^%wd = ", j), qadic_print_pretty(c, ctx), flint_printf("\n"); } flint_printf("ans = %d\n", ans); abort(); } qadic_clear(a); qadic_clear(b); qadic_clear(c); padic_clear(x); padic_clear(y); fmpz_clear(p); qadic_ctx_clear(ctx); } FLINT_TEST_CLEANUP(state); flint_printf("PASS\n"); return EXIT_SUCCESS; }
void tiny_compute_A(QS_t * qs_inf, poly_t * poly_inf) { unsigned long min = poly_inf->min; unsigned long span = poly_inf->span; unsigned long s = poly_inf->s; unsigned long * A_ind = poly_inf->A_ind; prime_t * factor_base = qs_inf->factor_base; unsigned long factor, i, p; unsigned long diff, best_diff, best1, best2; unsigned long A; if (s <= 4) { A_ind[0] = z_randint(span) + min; do { A_ind[1] = z_randint(span) + min; } while (A_ind[0] == A_ind[1]); } if (s == 2) A = factor_base[A_ind[0]].p * factor_base[A_ind[1]].p; if ((s == 3) || (s == 4)) { do { A_ind[2] = z_randint(span) + min; } while ((A_ind[0] == A_ind[2]) || (A_ind[1] == A_ind[2])); A = factor_base[A_ind[0]].p * factor_base[A_ind[1]].p * factor_base[A_ind[2]].p; } if (s == 4) { factor = (poly_inf->target_A - 1) / A + 1; for (i = min; i < min+span; i++) { if ((factor_base[i].p > factor) && (i != A_ind[0]) && (i != A_ind[1]) && (i != A_ind[2])) break; } if (i == min + span) { i--; while ((i == A_ind[0]) || (i == A_ind[1]) || (i == A_ind[2])) i--; } A_ind[3] = i; A *= factor_base[A_ind[3]].p; } if (s == 5) { A_ind[0] = ((z_randint(span) + min) | 1); if (A_ind[0] == min + span) A_ind[0] -= 2; do { A_ind[1] = ((z_randint(span) + min) | 1); if (A_ind[1] == min + span) A_ind[1] -= 2; } while (A_ind[0] == A_ind[1]); do { A_ind[2] = ((z_randint(span) + min) | 1); if (A_ind[2] == min + span) A_ind[2] -= 2; } while ((A_ind[0] == A_ind[2]) || (A_ind[1] == A_ind[2])); A = factor_base[A_ind[0]].p * factor_base[A_ind[1]].p * factor_base[A_ind[2]].p; factor = poly_inf->target_A / A; for (i = 0; i < 8; i++) { A_ind[3] = ((z_randint(span) + min) & -2L); if (A_ind[3] < min) A_ind[3]+=2; do { A_ind[4] = ((z_randint(span) + min) & -2L); if (A_ind[4] < min) A_ind[4]+=2; } while (A_ind[3] == A_ind[4]); if (i == 0) { best_diff = FLINT_ABS(factor_base[A_ind[3]].p * factor_base[A_ind[4]].p - factor); best1 = A_ind[3]; best2 = A_ind[4]; continue; } diff = FLINT_ABS(factor_base[A_ind[3]].p * factor_base[A_ind[4]].p - factor); if (diff < best_diff) { best_diff = diff; best1 = A_ind[3]; best2 = A_ind[4]; } } A_ind[3] = best1; A_ind[4] = best2; A = A * factor_base[A_ind[3]].p * factor_base[A_ind[4]].p; } poly_inf->A = A; #if POLY_A if ((s == 4) || (s == 5)) printf("A = %ld, target A = %ld\n", A, poly_inf->target_A); #endif for (i = 0; i < s; i++) { p = factor_base[A_ind[i]].p; poly_inf->inv_p2[i] = z_precompute_inverse(p*p); } }