Skip to content

Commit ed6694f

Browse files
committed
Test nmod_mat_ctx
1 parent bfe4869 commit ed6694f

File tree

4 files changed

+27
-25
lines changed

4 files changed

+27
-25
lines changed

src/flint/test/test_all.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,7 @@ def set_bad(i):
11231123
for _ in range(2):
11241124
A = Q(flint.fmpz_mat.randtest(30, 30, 10))
11251125
if A.det() == 0:
1126-
continue
1126+
continue # pragma: no cover
11271127
B = Q(flint.fmpz_mat.randtest(30, 1, 10))
11281128
X = A.solve(B)
11291129
assert A*X == B
@@ -1548,6 +1548,8 @@ def test_nmod_mat():
15481548
assert raises(lambda: M([1], 5), TypeError)
15491549
assert raises(lambda: M([[1],[2,3]], 5), ValueError)
15501550
assert raises(lambda: M([[1],[2]], 0), ValueError)
1551+
assert raises(lambda: M([[1, 2], [3, 4.0]], 5), TypeError)
1552+
assert raises(lambda: M(2, 2, [1, 2, 3, 4.0], 5), TypeError)
15511553
assert raises(lambda: M(None), TypeError)
15521554
assert raises(lambda: M(None,17), TypeError)
15531555
assert M(2,3,17) == M(2,3,[0,0,0,0,0,0],17)
@@ -1629,29 +1631,36 @@ def test_nmod_series():
16291631

16301632
def test_nmod_contexts():
16311633
# XXX: Generalise this test to cover fmpz_mod, fq_default, etc.
1632-
C = flint.nmod_ctx
1634+
CS = flint.nmod_ctx
16331635
CP = flint.nmod_poly_ctx
1634-
G = flint.nmod
1636+
CM = flint.nmod_mat_ctx
1637+
S = flint.nmod
16351638
P = flint.nmod_poly
1639+
M = flint.nmod_mat
16361640

1637-
for c, name in [(C, 'nmod'), (CP, 'nmod_poly')]:
1641+
for c, name in [(CS, 'nmod'), (CP, 'nmod_poly'), (CM, 'nmod_mat')]:
16381642
ctx = c.new(17)
16391643
assert ctx.modulus() == 17
16401644
assert str(ctx) == f"Context for {name} with modulus: 17"
16411645
assert repr(ctx) == f"{name}_ctx(17)"
16421646
assert raises(lambda: c(3), TypeError)
16431647
assert raises(lambda: ctx.new(3.0), TypeError)
16441648

1645-
ctx = C.new(17)
1646-
assert ctx(3) == G(3,17) == G(3, ctx)
1649+
ctx = CS.new(17)
1650+
assert ctx(3) == S(3,17) == S(3, ctx)
16471651
assert raises(lambda: ctx(3.0), TypeError)
1648-
assert raises(lambda: G(3, []), TypeError)
1652+
assert raises(lambda: S(3, []), TypeError)
16491653

16501654
ctx_poly = CP.new(17)
16511655
assert ctx_poly([1,2,3]) == P([1,2,3],17) == P([1,2,3], ctx_poly)
16521656
assert raises(lambda: ctx_poly([1,2.0,3]), TypeError)
16531657
assert raises(lambda: P([1,2,3], []), TypeError)
16541658

1659+
ctx_mat = CM.new(17)
1660+
assert ctx_mat([[1,2],[3,4]]) == M([[1,2],[3,4]],17) == M([[1,2],[3,4]], ctx_mat)
1661+
assert raises(lambda: ctx_mat([[1,2.0],[3,4]]), TypeError)
1662+
assert raises(lambda: M([[1,2],[3,4]], []), TypeError)
1663+
16551664

16561665
def test_arb():
16571666
A = flint.arb
@@ -2596,6 +2605,7 @@ def FQ_DEFAULT(n, k):
25962605
NMOD(9),
25972606
NMOD(16),
25982607
FMPZ_MOD(164),
2608+
FMPZ_MOD(9),
25992609
FMPZ_MOD(2**127),
26002610
FMPZ_MOD(2**255),
26012611
]
@@ -2795,7 +2805,7 @@ def setbad(obj, i, val):
27952805
assert P([1, 1]) % 2 == P([1, 1])
27962806
assert P([2, 2]) / 2 == P([1, 1])
27972807
assert raises(lambda: P([1, 2]) / 2, DomainError)
2798-
else:
2808+
elif characteristic.gcd(2) != 1 or type(P(1)) is flint.nmod_poly:
27992809
# Z/nZ for n not prime
28002810
assert raises(lambda: P([1, 1]) // 2, DomainError)
28012811
assert raises(lambda: P([1, 1]) % 2, DomainError)
@@ -2898,7 +2908,7 @@ def setbad(obj, i, val):
28982908
if type(x) is flint.fmpz_mod_poly:
28992909
assert (1 + x).inverse_series_trunc(4) == 1 - x + x**2 - x**3
29002910
if characteristic.gcd(3) != 1:
2901-
assert (3 + x).inverse_series_trunc(4) == 1 - x + x**2 - x**3
2911+
assert raises(lambda: (3 + x).inverse_series_trunc(4), DomainError)
29022912
else:
29032913
assert (3 + x).inverse_series_trunc(4)\
29042914
== S(1)/3 - S(1)/9*x + S(1)/27*x**2 - S(1)/81*x**3

src/flint/types/fmpz_mod_poly.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1451,7 +1451,7 @@ cdef class fmpz_mod_poly(flint_poly):
14511451
fmpz_clear(f)
14521452

14531453
if not is_one:
1454-
raise ValueError(
1454+
raise DomainError(
14551455
f"Cannot compute inverse series of {self} modulo x^{n}"
14561456
)
14571457

src/flint/types/nmod_mat.pxd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ cdef class nmod_mat_ctx:
1818
cdef nmod_poly_ctx poly_ctx
1919

2020
@staticmethod
21-
cdef any_as_nmod_mat_ctx(obj)
21+
cdef nmod_mat_ctx any_as_nmod_mat_ctx(obj)
2222
@staticmethod
2323
cdef nmod_mat_ctx _get_ctx(int mod)
2424
@staticmethod

src/flint/types/nmod_mat.pyx

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -76,18 +76,18 @@ cdef class nmod_mat_ctx:
7676
@staticmethod
7777
def new(mod):
7878
"""Get an ``nmod_poly`` context with modulus ``mod``."""
79-
return nmod_mat_ctx._get_ctx(mod)
79+
return nmod_mat_ctx.any_as_nmod_mat_ctx(mod)
8080

8181
@staticmethod
82-
cdef any_as_nmod_mat_ctx(obj):
82+
cdef nmod_mat_ctx any_as_nmod_mat_ctx(obj):
8383
"""Convert an ``nmod_mat_ctx`` or ``int`` to an ``nmod_mat_ctx``."""
8484
if typecheck(obj, nmod_mat_ctx):
8585
return obj
8686
if typecheck(obj, int):
8787
return nmod_mat_ctx._get_ctx(obj)
8888
elif typecheck(obj, fmpz):
8989
return nmod_mat_ctx._get_ctx(int(obj))
90-
return NotImplemented
90+
raise TypeError("nmod_mat: expected last argument to be an nmod_mat_ctx or an integer")
9191

9292
@staticmethod
9393
cdef nmod_mat_ctx _get_ctx(int mod):
@@ -265,12 +265,7 @@ cdef class nmod_mat(flint_mat):
265265
mod = args[-1]
266266
args = args[:-1]
267267

268-
c = nmod_mat_ctx.any_as_nmod_mat_ctx(mod)
269-
if c is NotImplemented:
270-
raise TypeError("nmod_mat: expected last argument to be an nmod_mat_ctx or an integer")
271-
272-
ctx = c
273-
self.ctx = ctx
268+
self.ctx = ctx = nmod_mat_ctx.any_as_nmod_mat_ctx(mod)
274269

275270
if mod == 0:
276271
raise ValueError("modulus must be nonzero")
@@ -427,7 +422,7 @@ cdef class nmod_mat(flint_mat):
427422
return t
428423
tv = &(<nmod_mat>t).val[0]
429424
if sv.mod.n != tv.mod.n:
430-
raise ValueError("cannot add nmod_mats with different moduli")
425+
raise ValueError("cannot add nmod_mats with different moduli") # pragma: no cover
431426
if sv.r != tv.r or sv.c != tv.c:
432427
raise ValueError("incompatible shapes for matrix addition")
433428
r = s.ctx.new_nmod_mat(sv.r, sv.c)
@@ -461,7 +456,7 @@ cdef class nmod_mat(flint_mat):
461456
return t
462457
tv = &(<nmod_mat>t).val[0]
463458
if sv.mod.n != tv.mod.n:
464-
raise ValueError("cannot subtract nmod_mats with different moduli")
459+
raise ValueError("cannot subtract nmod_mats with different moduli") # pragma: no cover
465460
if sv.r != tv.r or sv.c != tv.c:
466461
raise ValueError("incompatible shapes for matrix subtraction")
467462
r = s.ctx.new_nmod_mat(sv.r, sv.c)
@@ -541,9 +536,6 @@ cdef class nmod_mat(flint_mat):
541536
def __truediv__(s, t):
542537
return nmod_mat._div_(s, t)
543538

544-
def __div__(s, t):
545-
return nmod_mat._div_(s, t)
546-
547539
def det(self):
548540
"""
549541
Returns the determinant of self as an nmod.

0 commit comments

Comments
 (0)