diff --git a/doc/source/fq_default.rst b/doc/source/fq_default.rst index f74f136e9b..190da0c868 100644 --- a/doc/source/fq_default.rst +++ b/doc/source/fq_default.rst @@ -104,10 +104,10 @@ Context Management Prints the context information to ``stdout``. -.. function:: void fq_default_ctx_randtest(fq_default_ctx_t ctx) +.. function:: void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state) - Initializes ``ctx`` to a random finite field. Assumes that - ``fq_default_ctx_init`` has not been called on ``ctx`` already. + Initializes ``ctx`` to a random finite field using one of the five internal representations. + Assumes that ``fq_default_ctx_init`` has not been called on ``ctx`` already. .. function:: void fq_default_get_coeff_fmpz(fmpz_t c, fq_default_t op, slong n, const fq_default_ctx_t ctx) diff --git a/src/fq_default.h b/src/fq_default.h index 6406358f99..3acad8f666 100644 --- a/src/fq_default.h +++ b/src/fq_default.h @@ -229,6 +229,7 @@ int fq_default_ctx_fprint(FILE * file, const fq_default_ctx_t ctx); #endif void fq_default_ctx_print(const fq_default_ctx_t ctx); +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state); /* Memory management *********************************************************/ diff --git a/src/fq_default/ctx.c b/src/fq_default/ctx.c index 778bb3df15..f6dc693018 100644 --- a/src/fq_default/ctx.c +++ b/src/fq_default/ctx.c @@ -199,3 +199,43 @@ void fq_default_ctx_modulus(fmpz_mod_poly_t p, const fq_default_ctx_t ctx) fmpz_mod_poly_set(p, FQ_DEFAULT_CTX_FQ(ctx)->modulus, mod); } } + +/* Create a random fq_default_ctx picked from the five internal representations */ +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state) +{ + fmpz_t prime; + slong deg; + + /* Select a context type [1,...,5] */ + int ctx_type = 1 + n_randint(state, 5); + switch (ctx_type) + { + /* Create GF(p^d) for FQ_ZECH context */ + case FQ_DEFAULT_FQ_ZECH: + fmpz_randprime(prime, state, 2 + n_randint(state, 3), 1); + deg = 1 + n_randint(state, 3); + break; + /* Create GF(p^d) for FQ_NMOD context */ + case FQ_DEFAULT_FQ_NMOD: + fmpz_randprime(prime, state, 2 + n_randint(state, 29), 1); + deg = 1 + n_randint(state, 15); + break; + /* Create GF(p^d) for FQ context */ + case FQ_DEFAULT_FQ: + fmpz_randprime(prime, state, 2 + n_randint(state, 62), 1); + deg = 1 + n_randint(state, 7); + break; + /* Create GF(p) for NMOD context */ + case FQ_DEFAULT_NMOD: + fmpz_randprime(prime, state, 2 + n_randint(state, 29), 1); + deg = 1; + break; + /* Create GF(p) for FMPZ_MOD context */ + case FQ_DEFAULT_FMPZ_MOD: + fmpz_randprime(prime, state, 2 + n_randint(state, 62), 1); + deg = 1; + break; + default: FLINT_UNREACHABLE; + } + fq_default_ctx_init_type(ctx, prime, deg, "a", ctx_type); +} diff --git a/src/fq_default/test/t-init.c b/src/fq_default/test/t-init.c index ae29d4d802..a470dbf810 100644 --- a/src/fq_default/test/t-init.c +++ b/src/fq_default/test/t-init.c @@ -81,5 +81,17 @@ TEST_FUNCTION_START(fq_default_init, state) fmpz_clear(p); } + for (i = 0; i < 100 * flint_test_multiplier(); i++) + { + fq_default_ctx_t ctx; + fq_default_t fq; + + fq_default_ctx_init_randtest(ctx, state); + fq_default_init(fq, ctx); + fq_default_randtest(fq, state, ctx); + fq_default_clear(fq, ctx); + fq_default_ctx_clear(ctx); + } + TEST_FUNCTION_END(state); }