diff --git a/doc/source/fq_default.rst b/doc/source/fq_default.rst index f74f136e9b..467132b10c 100644 --- a/doc/source/fq_default.rst +++ b/doc/source/fq_default.rst @@ -104,10 +104,13 @@ 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, int type) - 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 where the prime and degree is + set according to ``type``. + Assumes that ``fq_default_ctx_init`` has not been called on ``ctx`` already. + To see what prime and degrees may be output, see + ``type`` in :func:`_nmod_poly_conway_rand`. .. 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..9d2e82d67e 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, int type); /* Memory management *********************************************************/ diff --git a/src/fq_default/ctx.c b/src/fq_default/ctx.c index 778bb3df15..0422367589 100644 --- a/src/fq_default/ctx.c +++ b/src/fq_default/ctx.c @@ -199,3 +199,22 @@ 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); } } + +/* FIXME: fq_ctx_init_randtest only tests nmod sized primes, so that's + all we'll do here too? */ +void fq_default_ctx_init_randtest(fq_default_ctx_t ctx, flint_rand_t state, int type) +{ + ulong prime; + slong degree; + fmpz_t p; + + /* select a prime and degree to create the finite field */ + prime = _nmod_poly_conway_rand(°ree, state, type); + + /* fq_default initialisation wants an fmpz_t for the prime */ + fmpz_init(p); + fmpz_set_ui(p, prime); + + /* Initialise the context using the prime and degree selected above */ + fq_default_ctx_init(ctx, p, degree, "x"); +} diff --git a/src/fq_default/test/t-init.c b/src/fq_default/test/t-init.c index ae29d4d802..af8ec96aa1 100644 --- a/src/fq_default/test/t-init.c +++ b/src/fq_default/test/t-init.c @@ -81,5 +81,21 @@ 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; + + /* Sets the type used internally for prime selection. + See _nmod_poly_conway_rand for more info. */ + int type = n_randint(state, 4); + + fq_default_ctx_init_randtest(ctx, state, type); + fq_default_init(fq, ctx); + fq_default_clear(fq, ctx); + fq_default_randtest(fq, state, ctx); + fq_default_ctx_clear(ctx); + } + TEST_FUNCTION_END(state); }