Skip to content
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

Minor fixes for nmod #78

Merged
merged 1 commit into from
Sep 9, 2023
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
8 changes: 8 additions & 0 deletions src/flint/test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1279,6 +1279,7 @@ def test_nmod():
assert G(1,2) != G(0,2)
assert G(0,2) != G(0,3)
assert G(3,5) == G(8,5)
assert isinstance(hash(G(3, 5)), int)
assert raises(lambda: G([], 3), TypeError)
#assert G(3,5) == 8 # do we want this?
#assert 8 == G(3,5)
Expand All @@ -1300,9 +1301,14 @@ def test_nmod():
assert G(3,17) / G(2,17) == G(10,17)
assert G(3,17) / 2 == G(10,17)
assert 3 / G(2,17) == G(10,17)
assert G(0,3) / G(1,3) == G(0,3)
assert G(3,17) * flint.fmpq(11,5) == G(10,17)
assert G(3,17) / flint.fmpq(11,5) == G(6,17)
assert G(1,3) ** 2 == G(1,3)
assert G(2,3) ** flint.fmpz(2) == G(1,3)
assert G(flint.fmpq(2, 3), 5) == G(4,5)
assert raises(lambda: G(2,5) ** G(2,5), TypeError)
assert raises(lambda: flint.fmpz(2) ** G(2,5), TypeError)
assert raises(lambda: G(flint.fmpq(2, 3), 3), ZeroDivisionError)
assert raises(lambda: G(2,5) / G(0,5), ZeroDivisionError)
assert raises(lambda: G(2,5) / 0, ZeroDivisionError)
Expand All @@ -1314,10 +1320,12 @@ def test_nmod():
assert raises(lambda: G(2,5) - [], TypeError)
assert raises(lambda: G(2,5) * [], TypeError)
assert raises(lambda: G(2,5) / [], TypeError)
assert raises(lambda: G(2,5) ** [], TypeError)
assert raises(lambda: [] + G(2,5), TypeError)
assert raises(lambda: [] - G(2,5), TypeError)
assert raises(lambda: [] * G(2,5), TypeError)
assert raises(lambda: [] / G(2,5), TypeError)
assert raises(lambda: [] ** G(2,5), TypeError)
assert G(3,17).modulus() == 17
assert str(G(3,5)) == "3"
assert G(3,5).repr() == "nmod(3, 5)"
Expand Down
20 changes: 20 additions & 0 deletions src/flint/types/nmod.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ from flint.types.fmpz cimport fmpz
from flint.types.fmpq cimport fmpq

from flint.flintlib.fmpz cimport fmpz_t
from flint.flintlib.nmod cimport nmod_pow_fmpz, nmod_inv
from flint.flintlib.nmod_vec cimport *
from flint.flintlib.fmpz cimport fmpz_fdiv_ui, fmpz_init, fmpz_clear
from flint.flintlib.fmpz cimport fmpz_set_ui, fmpz_get_ui
Expand Down Expand Up @@ -89,6 +90,9 @@ cdef class nmod(flint_scalar):
return not res
return NotImplemented

def __hash__(self):
return hash((int(self.val), self.modulus))

def __nonzero__(self):
return self.val != 0

Expand Down Expand Up @@ -178,6 +182,8 @@ cdef class nmod(flint_scalar):
return NotImplemented
if tval == 0:
raise ZeroDivisionError("%s is not invertible mod %s" % (tval, mod.n))
if not s:
return s
# XXX: check invertibility?
x = nmod_div(sval, tval, mod)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change makes it so that nmod(0, p) / nmod(a, p) gives nmod(0, p).

About invertibility: in the context of SymPy's use the modulus here will always be prime. Otherwise though if tval and mod are not coprime then this gives a core dump:

In [3]: flint.nmod(1, 6) / flint.nmod(3, 6)
Flint exception (Impossible inverse):
    Cannot invert modulo 3*0
Aborted (core dumped)

That is not very friendly in Python land but I am not sure if adding checks here for is worth it: you would need to compute gcd(tval, mod) and that calculation would always be unnecessary for prime mod. Ideally Flint should provide an efficient function that could return an error code rather than aborting.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is such a function: n_gcdinv.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. I've added this in #79

if x == 0:
Expand All @@ -195,3 +201,17 @@ cdef class nmod(flint_scalar):

def __invert__(self):
return (1 / self) # XXX: speed up

def __pow__(self, exp):
cdef nmod r
e = any_as_fmpz(exp)
if e is NotImplemented:
return NotImplemented
r = nmod.__new__(nmod)
r.mod = self.mod
r.val = self.val
if e < 0:
r.val = nmod_inv(r.val, self.mod)
e = -e
r.val = nmod_pow_fmpz(r.val, (<fmpz>e).val, self.mod)
return r