Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
e6f8dcd
Add _nmod_vec_rand
dlesnoff Oct 30, 2025
4ea9a57
Add nmod_poly rand funcs
dlesnoff Oct 29, 2025
4f0ef9b
Add nmod_poly_mat rand funcs
dlesnoff Oct 29, 2025
cacfb53
Add fmpz_mod_poly_rand, rand_monic, irred funcs
dlesnoff Oct 29, 2025
bb5d986
Add fmpz_mod_vec_rand
dlesnoff Oct 29, 2025
400598a
Add a rand function to mpn_mod
dlesnoff Oct 29, 2025
9060505
Add mpn_mod_vec_rand func
dlesnoff Oct 29, 2025
59f5c7d
Revert "Add fmpz_mod_vec_rand"
dlesnoff Oct 29, 2025
eafe4cf
Replace randm_non_zero by randtest calls
dlesnoff Nov 3, 2025
4dff27e
Fix documentation of mpn_mod
dlesnoff Nov 3, 2025
692c2b8
Merge branch 'main' into mod_poly-random-generation
dlesnoff Nov 3, 2025
716a13e
Remove +1 to uint pointers
dlesnoff Nov 3, 2025
43cdbc6
Merge branch 'trunk' into mod_poly-random-generation
dlesnoff Aug 6, 2026
320496a
Randm is now really uniform
dlesnoff Aug 6, 2026
606465e
Remove ctx and move fmpz_mod_poly_rand funcs to correct doc file
dlesnoff Aug 6, 2026
440ebee
Remove duplicate prototype in nmod_vec.h
dlesnoff Aug 6, 2026
e8ecc0c
apply fix fmpz_randm_nonzero
dlesnoff Aug 6, 2026
2eec4e3
Don't get why fmpz_bits was used in this reduction? Fix Segfault but …
dlesnoff Aug 6, 2026
266bc61
randm_not_zero
dlesnoff Aug 16, 2026
534ace3
Apply documentation and comments suggestions
dlesnoff Aug 16, 2026
98bf66c
Remove m=2 from edge cases
dlesnoff Aug 16, 2026
7256cd5
Remove strange documentation change
dlesnoff Aug 16, 2026
dd08311
Change to fmpz_randtest_mod
dlesnoff Aug 16, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/source/fmpz.rst
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,11 @@ should call :func:`flint_rand_clear` to clean up.

Generates a random integer in the range `0` to `m - 1` inclusive.

.. function:: void fmpz_randm_not_zero(fmpz_t f, flint_rand_t state, const fmpz_t m)

Generates a random integer in the range `1` to `m - 1` inclusive. Requires
`m \geq 2`, otherwise an exception is thrown.

.. function:: void fmpz_randtest_mod(fmpz_t f, flint_rand_t state, const fmpz_t m)

Generates a random integer in the range `0` to `m - 1` inclusive, with an
Expand Down
12 changes: 12 additions & 0 deletions doc/source/fmpz_mod_poly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ Randomisation
set to a random monic irreducible polynomial.


.. function:: void fmpz_mod_poly_rand(fmpz_mod_poly_t f, flint_rand_t state, slong len, const fmpz_mod_ctx_t ctx)

Generates a random polynomial with length up to `len`, with uniformly chosen coefficients.

.. function:: void fmpz_mod_poly_rand_monic(fmpz_mod_poly_t f, flint_rand_t state, slong len, const fmpz_mod_ctx_t ctx)

Generates a random monic polynomial with length `len`, with uniformly chosen coefficients.

.. function:: void fmpz_mod_poly_rand_irreducible(fmpz_mod_poly_t f, flint_rand_t state, slong len, const fmpz_mod_ctx_t ctx)

Generates a random irreducible polynomial with up to the given length and where the coefficients
are generated uniformly.

Attributes
--------------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions doc/source/mpn_mod.rst
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ Context objects
Basic operations and arithmetic
-------------------------------------------------------------------------------


.. function:: int mpn_mod_rand(nn_ptr res, flint_rand_t state, gr_ctx_t ctx)

Generates a coefficient uniformly between `0` and `MPN_MOD_CTX_MODULUS(ctx)` exclusive.

.. function:: int mpn_mod_ctx_write(gr_stream_t out, gr_ctx_t ctx)
void mpn_mod_ctx_clear(gr_ctx_t ctx)
truth_t mpn_mod_ctx_is_field(gr_ctx_t ctx)
Expand Down
15 changes: 13 additions & 2 deletions doc/source/nmod_poly.rst
Original file line number Diff line number Diff line change
Expand Up @@ -223,14 +223,25 @@ Assignment and basic manipulation
Randomization
--------------------------------------------------------------------------------

.. function:: void nmod_poly_rand(nmod_poly_t poly, flint_rand_t state, slong len)

Generates a random polynomial with length up to ``len`` and coefficients generated uniformly.

.. function:: void nmod_poly_rand_monic(nmod_poly_t poly, flint_rand_t state, slong len)

Generates a random monic polynomial of length ``len`` and non-leading coefficients generated uniformly.

.. function:: void nmod_poly_rand_irreducible(nmod_poly_t poly, flint_rand_t state, slong len)

Generates a random irreducible polynomial with length up to ``len`` with coefficients generated uniformly.

.. function:: void nmod_poly_randtest(nmod_poly_t poly, flint_rand_t state, slong len)

Generates a random polynomial with length up to ``len``.
Generates a random polynomial with length up to ``len``, sparse with increased probability, and with coefficients generated by :func:`n_randtest`. This function is intended for use in test code.

.. function:: void nmod_poly_randtest_monic(nmod_poly_t poly, flint_rand_t state, slong len)

Generates a random monic polynomial with length ``len``.
Generates a random monic polynomial with length ``len``. For each coefficient the probability of some special values is increased (see :func:`n_randtest`). In particular, the polynomial is sparse with increased probability. This function is intended for use in test code.

.. function:: void nmod_poly_randtest_trinomial(nmod_poly_t poly, flint_rand_t state, slong len)

Expand Down
7 changes: 5 additions & 2 deletions doc/source/nmod_poly_mat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,13 @@ Random matrix generation
--------------------------------------------------------------------------------


.. function:: void nmod_poly_mat_rand(nmod_poly_mat_t mat, flint_rand_t state, slong len)

Generates a matrix of polynomials with uniformly generated coefficients.

.. function:: void nmod_poly_mat_randtest(nmod_poly_mat_t mat, flint_rand_t state, slong len)

This is equivalent to applying ``nmod_poly_randtest`` to all entries
in the matrix.
Generates a matrix of polynomials sparse with an increased probability. It is equivalent to apply ``nmod_poly_randtest`` to all entries in the matrix.

.. function:: void nmod_poly_mat_randtest_sparse(nmod_poly_mat_t A, flint_rand_t state, slong len, float density)

Expand Down
1 change: 1 addition & 0 deletions src/fmpz.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ int _fmpz_is_canonical(const fmpz_t x);
void fmpz_randbits_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
void fmpz_randbits(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
void fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m);
void fmpz_randm_not_zero(fmpz_t f, flint_rand_t state, const fmpz_t m);
void fmpz_randtest(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
void fmpz_randtest_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
void fmpz_randtest_not_zero(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits);
Expand Down
14 changes: 14 additions & 0 deletions src/fmpz/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,20 @@ fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m)
}
}

void
fmpz_randm_not_zero(fmpz_t f, flint_rand_t state, const fmpz_t m)
{
if (fmpz_cmp_ui(m, 2) < 0)
{
flint_throw(FLINT_ERROR, "Exception (fmpz_randm_not_zero). m < 2.\n");
}
fmpz_t mMinusOne;
fmpz_sub_ui(mMinusOne, m, 1);

fmpz_randm(f, state, mMinusOne); /* 0..m-2 */
fmpz_add_ui(f, f, 1); /* 1..m-1 */
}

void fmpz_randprime(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits, int proved)
{
if (bits <= FLINT_BITS)
Expand Down
9 changes: 9 additions & 0 deletions src/fmpz_mod_poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ int fmpz_mod_poly_is_canonical(const fmpz_mod_poly_t A, const fmpz_mod_ctx_t ctx


/* Randomisation ************************************************************/
void fmpz_mod_poly_rand(fmpz_mod_poly_t f, flint_rand_t state,
slong len, const fmpz_mod_ctx_t ctx);

void fmpz_mod_poly_rand_monic(fmpz_mod_poly_t f,
flint_rand_t state, slong len, const fmpz_mod_ctx_t ctx);

void fmpz_mod_poly_rand_irreducible(fmpz_mod_poly_t f,
flint_rand_t state, slong len, const fmpz_mod_ctx_t ctx);


void fmpz_mod_poly_randtest(fmpz_mod_poly_t f, flint_rand_t state,
slong len, const fmpz_mod_ctx_t ctx);
Expand Down
73 changes: 61 additions & 12 deletions src/fmpz_mod_poly/randtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#include "fmpz_mod_poly.h"
#include "fmpz_mod_poly_factor.h"

void fmpz_mod_poly_randtest(fmpz_mod_poly_t f, flint_rand_t state, slong len,
void fmpz_mod_poly_rand(fmpz_mod_poly_t f, flint_rand_t state, slong len,
Comment thread
vneiger marked this conversation as resolved.
const fmpz_mod_ctx_t ctx)
{
slong i;
Expand All @@ -31,7 +31,7 @@ void fmpz_mod_poly_randtest(fmpz_mod_poly_t f, flint_rand_t state, slong len,
_fmpz_mod_poly_normalise(f);
}

void fmpz_mod_poly_randtest_monic(fmpz_mod_poly_t f, flint_rand_t state,
void fmpz_mod_poly_rand_monic(fmpz_mod_poly_t f, flint_rand_t state,
slong len, const fmpz_mod_ctx_t ctx)
{
slong i;
Expand All @@ -48,6 +48,53 @@ void fmpz_mod_poly_randtest_monic(fmpz_mod_poly_t f, flint_rand_t state,
_fmpz_mod_poly_set_length(f, len);
}

void fmpz_mod_poly_rand_irreducible(fmpz_mod_poly_t f, flint_rand_t state,
slong len, const fmpz_mod_ctx_t ctx)
{
if (len == 0)
{
flint_throw(FLINT_ERROR, "Exception (fmpz_mod_poly_rand_irreducible). len == 0.\n");
}

do {
fmpz_mod_poly_rand(f, state, len, ctx);
} while (fmpz_mod_poly_is_zero(f, ctx) ||
!fmpz_mod_poly_is_irreducible(f, ctx));
}

void fmpz_mod_poly_randtest(fmpz_mod_poly_t f, flint_rand_t state, slong len,
const fmpz_mod_ctx_t ctx)
{
slong i;

fmpz_mod_poly_fit_length(f, len, ctx);

for (i = 0; i < len; i++) {
fmpz_randtest_mod(f->coeffs + i, state, fmpz_mod_ctx_modulus(ctx));
}

_fmpz_mod_poly_set_length(f, len);
_fmpz_mod_poly_normalise(f);
}

void fmpz_mod_poly_randtest_monic(fmpz_mod_poly_t f, flint_rand_t state,
slong len, const fmpz_mod_ctx_t ctx)
{
slong i;

FLINT_ASSERT(len > 0);

fmpz_mod_poly_fit_length(f, len, ctx);

for (i = 0; i < len - 1; i++) {
fmpz_randtest_mod(f->coeffs + i, state, fmpz_mod_ctx_modulus(ctx));
}

fmpz_one(f->coeffs + len - 1);

_fmpz_mod_poly_set_length(f, len);
}

static void
fmpz_mod_poly_randtest_monic_sparse(fmpz_mod_poly_t poly, flint_rand_t state,
slong len, slong nonzero, const fmpz_mod_ctx_t ctx)
Expand All @@ -56,10 +103,11 @@ fmpz_mod_poly_randtest_monic_sparse(fmpz_mod_poly_t poly, flint_rand_t state,

fmpz_mod_poly_fit_length(poly, len, ctx);
_fmpz_vec_zero(poly->coeffs, len);
fmpz_randm(poly->coeffs + 0, state, fmpz_mod_ctx_modulus(ctx));
for (i = 1; i < nonzero; i++)
fmpz_randm(poly->coeffs + n_randint(state, len - 1) + 1,
state, fmpz_mod_ctx_modulus(ctx));
fmpz_randtest_mod(poly->coeffs + 0, state, fmpz_mod_ctx_modulus(ctx));
for (i = 1; i < nonzero; i++) {
ulong random_idx = n_randint(state, len - 1);
fmpz_randtest_mod(poly->coeffs + random_idx + 1, state, fmpz_mod_ctx_modulus(ctx));
}
fmpz_set_ui(poly->coeffs + len - 1, 1);
_fmpz_mod_poly_set_length(poly, len);
}
Expand Down Expand Up @@ -127,22 +175,23 @@ void fmpz_mod_poly_randtest_trinomial(fmpz_mod_poly_t poly,
ulong k;
fmpz_mod_poly_fit_length(poly, len, ctx);
_fmpz_vec_zero(poly->coeffs, len);
fmpz_randm(poly->coeffs, state, fmpz_mod_ctx_modulus(ctx));
fmpz_randtest_mod(poly->coeffs, state, fmpz_mod_ctx_modulus(ctx));
k = (n_randtest(state) % (len - 2)) + 1;
fmpz_randm(poly->coeffs + k, state, fmpz_mod_ctx_modulus(ctx));
fmpz_randtest_mod(poly->coeffs + k, state, fmpz_mod_ctx_modulus(ctx));
fmpz_one(poly->coeffs + len - 1);
_fmpz_mod_poly_set_length(poly, len);
}

void fmpz_mod_poly_randtest_pentomial(fmpz_mod_poly_t poly,
flint_rand_t state, slong len, const fmpz_mod_ctx_t ctx)
{
const fmpz * modulus = fmpz_mod_ctx_modulus(ctx);
fmpz_mod_poly_fit_length(poly, len, ctx);
_fmpz_vec_zero(poly->coeffs, len);
fmpz_randm(poly->coeffs, state, fmpz_mod_ctx_modulus(ctx));
fmpz_randm(poly->coeffs + 1, state, fmpz_mod_ctx_modulus(ctx));
fmpz_randm(poly->coeffs + 2, state, fmpz_mod_ctx_modulus(ctx));
fmpz_randm(poly->coeffs + 3, state, fmpz_mod_ctx_modulus(ctx));
fmpz_randtest_mod(poly->coeffs, state, modulus);
fmpz_randtest_mod(poly->coeffs + 1, state, modulus);
fmpz_randtest_mod(poly->coeffs + 2, state, modulus);
fmpz_randtest_mod(poly->coeffs + 3, state, modulus);
fmpz_one(poly->coeffs + len - 1);
_fmpz_mod_poly_set_length(poly, len);
}
Expand Down
2 changes: 2 additions & 0 deletions src/mpn_mod.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ int mpn_mod_neg_one(nn_ptr res, gr_ctx_t ctx);
int mpn_mod_set_mpn(nn_ptr res, nn_srcptr x, slong xn, gr_ctx_t ctx);
int mpn_mod_set_fmpz(nn_ptr res, const fmpz_t x, gr_ctx_t ctx);
int mpn_mod_set_other(nn_ptr res, gr_ptr v, gr_ctx_t v_ctx, gr_ctx_t ctx);
int mpn_mod_rand(nn_ptr res, flint_rand_t state, gr_ctx_t ctx);
int mpn_mod_randtest(nn_ptr res, flint_rand_t state, gr_ctx_t ctx);
int mpn_mod_write(gr_stream_t out, nn_srcptr x, gr_ctx_t ctx);

Expand Down Expand Up @@ -191,6 +192,7 @@ int mpn_mod_div(nn_ptr res, nn_srcptr x, nn_srcptr y, gr_ctx_t ctx);
int _mpn_mod_vec_zero(nn_ptr res, slong len, gr_ctx_t ctx);
int _mpn_mod_vec_clear(nn_ptr FLINT_UNUSED(res), slong FLINT_UNUSED(len), gr_ctx_t FLINT_UNUSED(ctx));
int _mpn_mod_vec_set(nn_ptr res, nn_srcptr x, slong len, gr_ctx_t ctx);
int _mpn_mod_vec_rand(nn_ptr res, flint_rand_t state, slong len, gr_ctx_t ctx);
void _mpn_mod_vec_swap(nn_ptr vec1, nn_ptr vec2, slong len, gr_ctx_t ctx);
int _mpn_mod_vec_neg(nn_ptr res, nn_srcptr x, slong len, gr_ctx_t ctx);
int _mpn_mod_vec_add(nn_ptr res, nn_srcptr x, nn_srcptr y, slong len, gr_ctx_t ctx);
Expand Down
12 changes: 12 additions & 0 deletions src/mpn_mod/ring.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,18 @@ mpn_mod_set_other(nn_ptr res, gr_ptr v, gr_ctx_t v_ctx, gr_ctx_t ctx)
return gr_generic_set_other(res, v, v_ctx, ctx);
}

int
mpn_mod_rand(nn_ptr res, flint_rand_t state, gr_ctx_t ctx)
{
fmpz_t t;
fmpz_init(t);
fmpz_set_ui_array(t, MPN_MOD_CTX_MODULUS(ctx), MPN_MOD_CTX_NLIMBS(ctx));
fmpz_randm(t, state, t);
GR_IGNORE(mpn_mod_set_fmpz(res, t, ctx));
fmpz_clear(t);
return GR_SUCCESS;
}

int
mpn_mod_randtest(nn_ptr res, flint_rand_t state, gr_ctx_t ctx)
{
Expand Down
13 changes: 13 additions & 0 deletions src/mpn_mod/vec.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ _mpn_mod_vec_set(nn_ptr res, nn_srcptr x, slong len, gr_ctx_t ctx)
return GR_SUCCESS;
}

int
_mpn_mod_vec_rand(nn_ptr res, flint_rand_t state, slong len, gr_ctx_t ctx)
{
slong n = MPN_MOD_CTX_NLIMBS(ctx);
slong i;

for (i = 0; i < len; i++)
if (mpn_mod_rand(res + i * n, state, ctx) != GR_SUCCESS)
return GR_UNABLE;

return GR_SUCCESS;
}

void
_mpn_mod_vec_swap(nn_ptr vec1, nn_ptr vec2, slong len, gr_ctx_t ctx)
{
Expand Down
4 changes: 4 additions & 0 deletions src/nmod_poly.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,10 @@ int nmod_poly_is_monic(const nmod_poly_t poly)

/* Randomisation ************************************************************/

void nmod_poly_rand(nmod_poly_t poly, flint_rand_t state, slong len);
void nmod_poly_rand_monic(nmod_poly_t poly, flint_rand_t state, slong len);
void nmod_poly_rand_irreducible(nmod_poly_t poly, flint_rand_t state, slong len);

void nmod_poly_randtest(nmod_poly_t poly, flint_rand_t state, slong len);

NMOD_POLY_INLINE
Expand Down
28 changes: 28 additions & 0 deletions src/nmod_poly/randtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,34 @@
#include "nmod_poly.h"
#include "nmod_poly_factor.h"

/* rand functions -> coefficients generated uniformly */
void
nmod_poly_rand(nmod_poly_t poly, flint_rand_t state, slong len)
{
nmod_poly_fit_length(poly, len);
_nmod_vec_rand(poly->coeffs, state, len, poly->mod);
poly->length = len;
_nmod_poly_normalise(poly);
}

void
nmod_poly_rand_monic(nmod_poly_t poly, flint_rand_t state, slong len)
{
nmod_poly_fit_length(poly, len);
_nmod_vec_rand(poly->coeffs, state, len - 1, poly->mod);
poly->coeffs[len - 1] = 1;
poly->length = len;
}

void
nmod_poly_rand_irreducible(nmod_poly_t poly, flint_rand_t state, slong len)
{
do {
nmod_poly_rand(poly, state, len);
} while (nmod_poly_is_zero(poly) || !(nmod_poly_is_irreducible(poly)));
}

// Randtest functions -> dense/sparse polynomials
void
nmod_poly_randtest(nmod_poly_t poly, flint_rand_t state, slong len)
{
Expand Down
1 change: 1 addition & 0 deletions src/nmod_poly_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ void nmod_poly_mat_one(nmod_poly_mat_t mat);

/* Random matrices ***********************************************************/

void nmod_poly_mat_rand(nmod_poly_mat_t A, flint_rand_t state, slong len);
void nmod_poly_mat_randtest(nmod_poly_mat_t mat, flint_rand_t state,
slong len);

Expand Down
11 changes: 11 additions & 0 deletions src/nmod_poly_mat/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,17 @@
#include "nmod_poly.h"
#include "nmod_poly_mat.h"

void
nmod_poly_mat_rand(nmod_poly_mat_t A, flint_rand_t state, slong len)
{
slong i, j;

for (i = 0; i < A->r; i++)
for (j = 0; j < A->c; j++)
nmod_poly_rand(nmod_poly_mat_entry(A, i, j), state, len);
}


void
nmod_poly_mat_randtest(nmod_poly_mat_t A, flint_rand_t state, slong len)
{
Expand Down
1 change: 0 additions & 1 deletion src/nmod_vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ void _nmod_vec_clear(nn_ptr vec)
}

void _nmod_vec_randtest(nn_ptr vec, flint_rand_t state, slong len, nmod_t mod);

void _nmod_vec_rand(nn_ptr vec, flint_rand_t state, slong len, nmod_t mod);

NMOD_VEC_INLINE
Expand Down
Loading