Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 1 addition & 2 deletions doc/source/fmpz.rst
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ should call :func:`flint_rand_clear` to clean up.
.. function:: void fmpz_randtest_unsigned(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)
void fmpz_randtest(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)

Generates a random integer whose absolute value has a number of bits which
is random from `0` up to ``bits`` inclusive.
Generates a random (unsigned) integer whose absolute value has a number of bits which is random from `0` up to ``bits`` inclusive.

.. function:: void fmpz_randtest_not_zero(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits)

Expand Down
13 changes: 12 additions & 1 deletion doc/source/fmpz_mod_mat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,18 @@ Random generation
.. function:: void fmpz_mod_mat_randtest(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx)

Generate a random matrix with the existing dimensions and entries in
`[0, n)` where ``n`` is the modulus.
`[0, n)` where ``n`` is the modulus. A sparse matrix is
generated with increased probability.

.. function:: void fmpz_mod_mat_randfull(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx)

Sets the element to random numbers in `[0, n)`, likely to be close to the modulus ``n``
of the matrix. This is used to test potential overflow-related bugs.

.. function:: void fmpz_mod_mat_rand(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx)

Sets the element to random numbers in `[0, n)`, likely to be close to the modulus ``n``
of the matrix.


Windows and concatenation
Expand Down
12 changes: 10 additions & 2 deletions doc/source/nmod_mat.rst
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ Memory management
Swaps two matrices by swapping the individual entries rather than swapping
the contents of the structs.

.. function:: void nmod_mat_set_mod(nmod_mat_t mat, ulong n);
.. function:: nmod_t nmod_mat_mod(const nmod_mat_t mat)

Returns the modulus of a matrix.

.. function:: void nmod_mat_set_mod(nmod_mat_t mat, ulong n)

Sets the modulus of an already initialized matrix ``mat`` to be `n`. Row
and column dimensions are unchanged, and allocated memory is unaffected.
Expand Down Expand Up @@ -196,9 +200,13 @@ Random matrix generation

.. function:: void nmod_mat_randfull(nmod_mat_t mat, flint_rand_t state)

Sets the element to random numbers likely to be close to the modulus
Sets the element to random numbers in `[0, n)`, likely to be close to the modulus ``n``
of the matrix. This is used to test potential overflow-related bugs.

.. function:: void nmod_mat_rand(nmod_mat_t mat, flint_rand_t state)

Sets the element to uniformly generated random numbers in `[0, n)`, where `n` is the modulus of the matrix.

.. function:: int nmod_mat_randpermdiag(nmod_mat_t mat, flint_rand_t state, nn_srcptr diag, slong n)

Sets ``mat`` to a random permutation of the diagonal matrix
Expand Down
5 changes: 4 additions & 1 deletion doc/source/nmod_vec.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ Random functions
Sets ``vec`` to a random vector of the given length with entries
reduced modulo ``mod.n``.

.. function:: void _nmod_vec_rand(nn_ptr vec, flint_rand_t state, slong len, nmod_t mod)

Sets ``vec`` to a vector of the given length with entries picked uniformly at random in `[0, mod.n)`.


Basic manipulation and comparison
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -224,4 +228,3 @@ performed at the very end of the computation.
Same specification as ``_nmod_vec_dot_bound_limbs``, but uses the additional
input ``params`` to reduce the amount of computations; for correctness
``params`` must have been computed for the specified ``len`` and ``mod``.

27 changes: 23 additions & 4 deletions src/fmpz_mat/randtest.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,31 @@
void
fmpz_mat_randtest(fmpz_mat_t mat, flint_rand_t state, flint_bitcnt_t bits)
{
slong r, c, i, j;
// Adapted from nmod_vec_randtest
slong r, c, i, j, len;
slong sparseness;

r = mat->r;
c = mat->c;
len = r*c;

for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
fmpz_randtest(fmpz_mat_entry(mat, i, j), state, bits);
if (n_randint(state, 2))
{
for (i = 0; i < r; i++)
for (j = 0; j < c; j++)
fmpz_randtest(fmpz_mat_entry(mat, i, j), state, bits);
}
else
{
sparseness = 1 + n_randint(state, FLINT_MAX(2, len));
for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++) {
if (n_randint(state, sparseness))
fmpz_zero(fmpz_mat_entry(mat, i, j));
else
fmpz_randtest(fmpz_mat_entry(mat, i, j), state, bits);
}
}
}
}
4 changes: 4 additions & 0 deletions src/fmpz_mod_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ FMPZ_MOD_MAT_INLINE void _fmpz_mod_mat_reduce(fmpz_mod_mat_t mat, const fmpz_mod

void fmpz_mod_mat_randtest(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx);

void fmpz_mod_mat_randfull(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx);

void fmpz_mod_mat_rand(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx);

void fmpz_mod_mat_randrank(fmpz_mod_mat_t mat, flint_rand_t state, slong rank, const fmpz_mod_ctx_t ctx);

void fmpz_mod_mat_randtril(fmpz_mod_mat_t mat, flint_rand_t state, int unit, const fmpz_mod_ctx_t ctx);
Expand Down
19 changes: 19 additions & 0 deletions src/fmpz_mod_mat/rand.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "fmpz.h"
#include "fmpz_mod_mat.h"

void fmpz_mod_mat_rand(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx)
{
fmpz* e;
slong i, j, r, c;
r = fmpz_mod_mat_nrows(mat, ctx);
c = fmpz_mod_mat_nrows(mat, ctx);

for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
e = fmpz_mod_mat_entry(mat, i, j);
fmpz_randm(e, state, ctx->n);
}
}
}
21 changes: 21 additions & 0 deletions src/fmpz_mod_mat/randfull.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include "fmpz.h"
#include "fmpz_mod_mat.h"

void fmpz_mod_mat_randfull(fmpz_mod_mat_t mat, flint_rand_t state, const fmpz_mod_ctx_t ctx)
{
fmpz* e;
slong i, j, r, c;
r = fmpz_mod_mat_nrows(mat, ctx);
c = fmpz_mod_mat_nrows(mat, ctx);

for (i = 0; i < r; i++)
{
for (j = 0; j < c; j++)
{
e = fmpz_mod_mat_entry(mat, i, j);
fmpz_randm(e, state, ctx->n);
if (fmpz_cmp_ui(e, 0))
fmpz_one(e);
}
}
}
12 changes: 12 additions & 0 deletions src/nmod_mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ ulong * nmod_mat_entry_ptr(const nmod_mat_t mat, slong i, slong j)
return &nmod_mat_entry(mat, i, j);
}

NMOD_MAT_INLINE
ulong * nmod_mat_row_ptr(const nmod_mat_t mat, slong i)
{
return &nmod_mat_entry(mat, i, 0);
}
/* See inlines.c */

NMOD_MAT_INLINE
Expand All @@ -56,6 +61,12 @@ slong nmod_mat_ncols(const nmod_mat_t mat)

void nmod_mat_set_mod(nmod_mat_t mat, ulong n);

NMOD_MAT_INLINE
nmod_t nmod_mat_mod(const nmod_mat_t mat)
{
return mat->mod;
}

/* Memory management */
void nmod_mat_init(nmod_mat_t mat, slong rows, slong cols, ulong n);
void nmod_mat_init_set(nmod_mat_t mat, const nmod_mat_t src);
Expand Down Expand Up @@ -106,6 +117,7 @@ void nmod_mat_concat_vertical(nmod_mat_t res,
/* Random matrix generation */
void nmod_mat_randtest(nmod_mat_t mat, flint_rand_t state);
void nmod_mat_randfull(nmod_mat_t mat, flint_rand_t state);
void nmod_mat_rand(nmod_mat_t mat, flint_rand_t state);
int nmod_mat_randpermdiag(nmod_mat_t mat, flint_rand_t state,
nn_srcptr diag, slong n);
void nmod_mat_randrank(nmod_mat_t, flint_rand_t state, slong rank);
Expand Down
12 changes: 11 additions & 1 deletion src/nmod_mat/randomisation.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ nmod_mat_randfull(nmod_mat_t mat, flint_rand_t state)
nmod_mat_entry(mat, i, j) = FLINT_MAX(1, n_randint(state, mat->mod.n));
}

void
nmod_mat_rand(nmod_mat_t mat, flint_rand_t state)
{
slong i;
for (i = 0; i < mat->r; i++)
_nmod_vec_rand(nmod_mat_row_ptr(mat, i), state, mat->c, mat->mod);
}

void
nmod_mat_randops(nmod_mat_t mat, flint_rand_t state, slong count)
{
Expand Down Expand Up @@ -123,7 +131,9 @@ nmod_mat_randrank(nmod_mat_t mat, flint_rand_t state, slong rank)
void
nmod_mat_randtest(nmod_mat_t mat, flint_rand_t state)
{
_nmod_vec_randtest(mat->entries, state, mat->r * mat->c, mat->mod);
slong i;
for (i = 0; i < mat->r; i++)
_nmod_vec_randtest(nmod_mat_row_ptr(mat, i), state, mat->c, mat->mod);
}

void
Expand Down
2 changes: 2 additions & 0 deletions src/nmod_vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ 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
void _nmod_vec_zero(nn_ptr vec, slong len)
{
Expand Down
7 changes: 7 additions & 0 deletions src/nmod_vec/randtest.c → src/nmod_vec/rand.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@

#include "nmod_vec.h"

void _nmod_vec_rand(nn_ptr vec, flint_rand_t state, slong len, nmod_t mod)
{
slong i;
for (i = 0; i < len; i++)
vec[i] = n_randint(state, mod.n);
}

void _nmod_vec_randtest(nn_ptr vec, flint_rand_t state, slong len, nmod_t mod)
{
slong i, sparseness;
Expand Down