-
Notifications
You must be signed in to change notification settings - Fork 275
Mod poly and mpn_mod random generation #2448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
e6f8dcd
4ea9a57
4f0ef9b
cacfb53
bb5d986
400598a
9060505
59f5c7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -217,14 +217,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``. | ||||||
|
|
||||||
| .. function:: void nmod_poly_rand_monic(nmod_poly_t poly, flint_rand_t state, slong len) | ||||||
|
|
||||||
| Generates a random monic polynomial with length up to ``len``. | ||||||
|
|
||||||
| .. 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``. | ||||||
|
|
||||||
| .. 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 and sparse with increased probability polynomial with length up to ``len``. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| .. 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 and sparse with increased probability monic polynomial with length ``len``. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| .. function:: void nmod_poly_randtest_trinomial(nmod_poly_t poly, flint_rand_t state, slong len) | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -138,10 +138,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. | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| .. function:: void nmod_poly_mat_randtest_sparse(nmod_poly_mat_t A, flint_rand_t state, slong len, float density) | ||||||
|
|
||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,6 +71,12 @@ fmpz_randm(fmpz_t f, flint_rand_t state, const fmpz_t m) | |
| } | ||
| } | ||
|
|
||
| void fmpz_randm_nonzero(fmpz_t f, flint_rand_t state, const fmpz_t m) { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The current behaviour does not have uniform distribution (it is more likely to generate There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What I meant is that currently, So if a |
||
| fmpz_randm(f, state, m); | ||
| if (fmpz_is_zero(f)) | ||
| fmpz_one(f); | ||
| } | ||
|
|
||
| void fmpz_randprime(fmpz_t f, flint_rand_t state, flint_bitcnt_t bits, int proved) | ||
| { | ||
| if (bits <= FLINT_BITS) | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -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, | ||||||
vneiger marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| const fmpz_mod_ctx_t ctx) | ||||||
| { | ||||||
| slong i; | ||||||
|
|
@@ -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; | ||||||
|
|
@@ -48,6 +48,51 @@ 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_randtest_irreducible). len == 0.\n"); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| 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_randm_nonzero(f->coeffs + i, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe this could rely on the existing |
||||||
|
|
||||||
| _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_randm_nonzero(f->coeffs + i, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here also: maybe this could rely on the existing fmpz_randtest_mod instead of fmpz_randm_nonzero? |
||||||
|
|
||||||
| 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) | ||||||
|
|
@@ -56,9 +101,9 @@ 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)); | ||||||
| fmpz_randm_nonzero(poly->coeffs + 0, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why this change, or why using nonzero almost everywhere? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To have at least a randfull-like functionality. See my comments above. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As suggested in other functions, I think that in these |
||||||
| for (i = 1; i < nonzero; i++) | ||||||
| fmpz_randm(poly->coeffs + n_randint(state, len - 1) + 1, | ||||||
| fmpz_randm_nonzero(poly->coeffs + n_randint(state, len - 1) + 1, | ||||||
| state, fmpz_mod_ctx_modulus(ctx)); | ||||||
| fmpz_set_ui(poly->coeffs + len - 1, 1); | ||||||
| _fmpz_mod_poly_set_length(poly, len); | ||||||
|
|
@@ -127,9 +172,9 @@ 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_randm_nonzero(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_randm_nonzero(poly->coeffs + k, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
| fmpz_one(poly->coeffs + len - 1); | ||||||
| _fmpz_mod_poly_set_length(poly, len); | ||||||
| } | ||||||
|
|
@@ -139,10 +184,10 @@ void fmpz_mod_poly_randtest_pentomial(fmpz_mod_poly_t poly, | |||||
| { | ||||||
| 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_randm_nonzero(poly->coeffs, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
| fmpz_randm_nonzero(poly->coeffs + 1, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
| fmpz_randm_nonzero(poly->coeffs + 2, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
| fmpz_randm_nonzero(poly->coeffs + 3, state, fmpz_mod_ctx_modulus(ctx)); | ||||||
| fmpz_one(poly->coeffs + len - 1); | ||||||
| _fmpz_mod_poly_set_length(poly, len); | ||||||
| } | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,7 @@ mpn_mod_ctx_set_is_field(gr_ctx_t ctx, truth_t is_field) | |
|
|
||
| int gr_ctx_init_mpn_mod(gr_ctx_t ctx, const fmpz_t n); | ||
| int _gr_ctx_init_mpn_mod(gr_ctx_t ctx, nn_srcptr n, slong nlimbs); | ||
| int mpn_mod_rand(nn_ptr res, flint_rand_t state, gr_ctx_t ctx); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't this be more like the one just below, |
||
| void gr_ctx_init_mpn_mod_randtest(gr_ctx_t ctx, flint_rand_t state); | ||
|
|
||
| int mpn_mod_ctx_write(gr_stream_t out, gr_ctx_t ctx); | ||
|
|
@@ -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); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Several new things are misplaced here I think. For this specific one, there is no input
ctx, for others about they arefmpz_mod_polybut this file is aboutfmpz_poly.