-
Notifications
You must be signed in to change notification settings - Fork 34
Begin adding fmpz_mod_poly #87
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
Changes from 6 commits
fea7def
6b1611a
6797150
e6b28d4
4e64490
36724e3
0a84867
6122937
4827066
00b87f5
a3fec08
586307a
b2e2894
b060b23
c30ad7a
a2a7c1f
857ee26
9cabaa2
024a09c
9a01c5d
87a77b7
85f82a4
428bece
bc85cd3
cb60fe7
e57a071
69b2e50
a793997
8fd39d1
3614a42
9dfd197
1945859
57b8346
6a3b447
52dee64
b2543b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| from flint.flintlib.flint cimport fmpz_struct, slong, flint_rand_t | ||
| from flint.flintlib.fmpz cimport fmpz_t | ||
| from flint.flintlib.fmpz_mod cimport fmpz_mod_ctx_t | ||
| from flint.flintlib.fmpz_factor cimport fmpz_factor_t | ||
| from flint.flintlib.fmpz_mod_poly cimport fmpz_mod_poly_struct, fmpz_mod_poly_t | ||
|
|
||
| # unimported types {'fmpz_mod_poly_factor_t', 'void', 'fmpz_mod_poly_t'} | ||
|
|
||
| cdef extern from "flint/fmpz_mod_poly_factor.h": | ||
| ctypedef struct fmpz_mod_poly_factor_struct: | ||
| fmpz_mod_poly_struct * poly | ||
| slong *exp | ||
| slong num | ||
| slong alloc | ||
| ctypedef fmpz_mod_poly_factor_struct fmpz_mod_poly_factor_t[1] | ||
|
|
||
| # Parsed from here | ||
| void fmpz_mod_poly_factor_init(fmpz_mod_poly_factor_t fac, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_clear(fmpz_mod_poly_factor_t fac, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_realloc(fmpz_mod_poly_factor_t fac, slong alloc, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_fit_length(fmpz_mod_poly_factor_t fac, slong len, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_set(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_factor_t fac, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_print(const fmpz_mod_poly_factor_t fac, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_insert(fmpz_mod_poly_factor_t fac, const fmpz_mod_poly_t poly, slong exp, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_concat(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_factor_t fac, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_pow(fmpz_mod_poly_factor_t fac, slong exp, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_is_irreducible(const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_is_irreducible_ddf(const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_is_irreducible_rabin(const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_is_irreducible_rabin_f(fmpz_t r, const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| int _fmpz_mod_poly_is_squarefree(const fmpz_struct * f, slong len, const fmpz_mod_ctx_t ctx) | ||
| int _fmpz_mod_poly_is_squarefree_f(fmpz_t fac, const fmpz_struct * f, slong len, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_is_squarefree(const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_is_squarefree_f(fmpz_t fac, const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_factor_equal_deg_prob(fmpz_mod_poly_t factor, flint_rand_t state, const fmpz_mod_poly_t pol, slong d, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_equal_deg(fmpz_mod_poly_factor_t factors, const fmpz_mod_poly_t pol, slong d, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_distinct_deg(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_t poly, slong * const *degs, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_distinct_deg_threaded(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_t poly, slong * const *degs, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_squarefree(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_cantor_zassenhaus(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_kaltofen_shoup(fmpz_mod_poly_factor_t res, const fmpz_mod_poly_t poly, const fmpz_mod_ctx_t ctx) | ||
| void fmpz_mod_poly_factor_berlekamp(fmpz_mod_poly_factor_t factors, const fmpz_mod_poly_t f, const fmpz_mod_ctx_t ctx) | ||
| void _fmpz_mod_poly_interval_poly_worker(void* arg_ptr) | ||
| void fmpz_mod_poly_roots(fmpz_mod_poly_factor_t r, const fmpz_mod_poly_t f, int with_multiplicity, const fmpz_mod_ctx_t ctx) | ||
| int fmpz_mod_poly_roots_factored(fmpz_mod_poly_factor_t r, const fmpz_mod_poly_t f, int with_multiplicity, const fmpz_factor_t n, const fmpz_mod_ctx_t ctx) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1813,6 +1813,85 @@ def test_fmpz_mod_dlog(): | |
| x = g.discrete_log(a) | ||
| assert g**x == a | ||
|
|
||
| def test_fmpz_mod_poly(): | ||
| from flint import fmpz_poly, fmpz_mod_poly_ctx, fmpz_mod_ctx, fmpz | ||
|
|
||
| # fmpz_mod_poly_ctx tests | ||
| F = fmpz_mod_ctx(11) | ||
| R1 = fmpz_mod_poly_ctx(F) | ||
| R2 = fmpz_mod_poly_ctx(11) | ||
| R3 = fmpz_mod_poly_ctx(13) | ||
|
|
||
| assert raises(lambda: fmpz_mod_ctx("AAA"), TypeError) | ||
| assert raises(lambda: fmpz_mod_ctx(-1), ValueError) | ||
| assert (R1 == R1) is True | ||
| assert (R1 == R2) is True | ||
| assert (R1 != R3) is True | ||
| assert (R1 != "AAA") is True | ||
|
|
||
| assert (hash(R1) == hash(R1)) is True | ||
| assert (hash(R1) == hash(R2)) is True | ||
| assert (hash(R1) != hash(R3)) is True | ||
|
|
||
| assert str(R1) == "Context for fmpz_mod_poly with modulus: 11" | ||
| assert str(R1) == str(R2) | ||
| assert repr(R3) == "fmpz_mod_poly_ctx(13)" | ||
|
|
||
| assert R1.modulus() == 11 | ||
| assert R1([0,1]) == R1.gen() | ||
|
|
||
| # Conversion tests | ||
| F = fmpz_mod_ctx(11) | ||
| R = fmpz_mod_poly_ctx(F) | ||
|
|
||
| f1 = R([int(-1),int(-2),int(-3)]) | ||
| f2 = R([fmpz(-1),fmpz(-2),fmpz(-3)]) | ||
| f3 = R([F(-1),F(-2),F(-3)]) | ||
| f4 = R(fmpz_poly([-1, -2, -3])) | ||
|
|
||
| assert str(f1) == "8*x^2 + 9*x + 10" | ||
| assert str(f2) == "8*x^2 + 9*x + 10" | ||
| assert str(f3) == "8*x^2 + 9*x + 10" | ||
| assert str(f4) == "8*x^2 + 9*x + 10" | ||
|
|
||
| f1 = R(5) | ||
| f2 = R(fmpz(6)) | ||
| f3 = R(F(7)) | ||
| assert str(f1) == "5" | ||
| assert str(f2) == "6" | ||
| assert str(f3) == "7" | ||
|
|
||
| # Printing | ||
| f = R([5, 6, 7, 8]) | ||
| assert str(f) == "8*x^3 + 7*x^2 + 6*x + 5" | ||
| # assert repr(f) == "fmpz_mod_poly([5, 6, 7, 8], fmpz_mod_poly_ctx(11))" | ||
|
Collaborator
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. I guess we need to decide what this is supposed to do. I would also include a
Contributor
Author
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. Yeah I'm not really happy with even the
Collaborator
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. There is a bit of inconsistency: In [20]: f(2)
Out[20]: fmpz_mod(17, 163)
In [21]: f
Out[21]: 3*x^2 + 2*x + 1
Collaborator
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. Personally I think that In [28]: print(str(gmpy2.mpz(2)))
2
In [29]: print(repr(gmpy2.mpz(2)))
mpz(2)Basically that is the purpose of In [30]: print([gmpy2.mpz(2)])
[mpz(2)]One way to control this is just to have I think it is something that should be decided on in general for python-flint and we should have a separate issue to discuss it. For now I would just make sure that there is a test covering repr with its current behaviour along with a comment noting that the tested output is potentially not ideal.
Contributor
Author
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. Yeah at the moment for the polynomial
Collaborator
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 What |
||
|
|
||
| # Get and Set tests | ||
| f = R([5, 6, 7, 8]) | ||
| assert f[0] == 5 | ||
| assert repr(f[0]) == "fmpz_mod(5, 11)" | ||
| f[0] = 7 | ||
| assert repr(f[0]) == "fmpz_mod(7, 11)" | ||
| assert str(f) == "8*x^3 + 7*x^2 + 6*x + 7" | ||
|
|
||
| # Comparisons | ||
| f1 = R([1,2,3]) | ||
| f2 = R([12,13,14]) | ||
| f3 = R([4,5,6]) | ||
| f4 = R([3]) | ||
|
|
||
| assert (f1 == f2) is True | ||
| assert (f1 != f3) is True | ||
| assert (f1 != "1") is True | ||
| assert (f4 == 3) is True | ||
|
|
||
| f1 = R([0]) | ||
| f2 = R([1]) | ||
| f3 = R([0, 1]) | ||
|
|
||
| assert f1.is_zero() is True | ||
| assert f2.is_one() is True | ||
| assert f3.is_gen() is True | ||
|
|
||
|
|
||
| all_tests = [ | ||
|
|
@@ -1834,5 +1913,6 @@ def test_fmpz_mod_dlog(): | |
| test_nmod_mat, | ||
| test_arb, | ||
| test_fmpz_mod, | ||
| test_fmpz_mod_dlog | ||
| test_fmpz_mod_dlog, | ||
| test_fmpz_mod_poly | ||
| ] | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| from flint.flintlib.fmpz_mod_poly cimport * | ||
|
|
||
| from flint.flint_base.flint_base cimport flint_poly | ||
| from flint.types.fmpz_mod cimport fmpz_mod_ctx | ||
|
|
||
|
|
||
| cdef class fmpz_mod_poly_ctx: | ||
| cdef fmpz_mod_ctx mod | ||
| cdef any_as_fmpz_mod_poly(self, obj) | ||
| cdef set_any_as_fmpz_mod_poly(self, fmpz_mod_poly_t poly, obj) | ||
| cdef set_list_as_fmpz_mod_poly(self, fmpz_mod_poly_t poly, val) | ||
|
|
||
| cdef class fmpz_mod_poly(flint_poly): | ||
| cdef fmpz_mod_poly_t val | ||
| cdef fmpz_mod_poly_ctx ctx | ||
| cpdef long length(self) | ||
| cpdef long degree(self) |
Uh oh!
There was an error while loading. Please reload this page.