@@ -40,7 +40,6 @@ from flint.flintlib.nmod_mat cimport (
4040 nmod_mat_randtest,
4141)
4242
43- from flint.utils.conversion cimport matrix_to_str
4443from flint.utils.typecheck cimport typecheck
4544from flint.types.fmpz_mat cimport any_as_fmpz_mat
4645from flint.types.fmpz_mat cimport fmpz_mat
@@ -50,6 +49,8 @@ from flint.types.nmod_poly cimport nmod_poly
5049from flint.pyflint cimport global_random_state
5150from flint.flint_base.flint_context cimport thectx
5251
52+ from flint.flint_base.flint_base cimport flint_mat
53+
5354
5455ctx = thectx
5556
@@ -69,10 +70,12 @@ cdef any_as_nmod_mat(obj, nmod_t mod):
6970 return NotImplemented
7071
7172
72- cdef class nmod_mat:
73+ cdef class nmod_mat(flint_mat) :
7374 """
74- The nmod_mat type represents dense matrices over Z/nZ for
75- word-size n. Some operations may assume that n is a prime.
75+ The nmod_mat type represents dense matrices over Z/nZ for word-size n (see
76+ fmpz_mod_mat for larger moduli).
77+
78+ Some operations may assume that n is a prime.
7679 """
7780
7881# cdef nmod_mat_t val
@@ -177,18 +180,6 @@ cdef class nmod_mat:
177180 entries = ' , ' .join(map (str , self .entries()))
178181 return f" nmod_mat({m}, {n}, [{entries}], {self.modulus()})"
179182
180- def str (self ):
181- return matrix_to_str(self .table())
182-
183- def __str__ (self ):
184- return self .str()
185-
186- def __repr__ (self ):
187- if ctx.pretty:
188- return self .str()
189- else :
190- return self .repr()
191-
192183 def entries (self ):
193184 cdef long i, j, m, n
194185 cdef nmod t
@@ -202,13 +193,6 @@ cdef class nmod_mat:
202193 L[i* n + j] = t
203194 return L
204195
205- def table (self ):
206- cdef long i, m, n
207- m = self .nrows()
208- n = self .ncols()
209- L = self .entries()
210- return [L[i* n:(i+ 1 )* n] for i in range (m)]
211-
212196 def __getitem__ (self , index ):
213197 cdef long i, j
214198 cdef nmod x
@@ -229,21 +213,6 @@ cdef class nmod_mat:
229213 else :
230214 raise TypeError (" cannot set item of type %s " % type (value))
231215
232- def det (self ):
233- """
234- Returns the determinant of self as an nmod.
235-
236- >>> nmod_mat(2,2,[1,2,3,4],17).det()
237- 15
238-
239- """
240- if not nmod_mat_is_square(self .val):
241- raise ValueError (" matrix must be square" )
242- return nmod(nmod_mat_det(self .val), self .modulus())
243-
244- def rank (self ):
245- return nmod_mat_rank(self .val)
246-
247216 def __pos__ (self ):
248217 return self
249218
@@ -391,7 +360,29 @@ cdef class nmod_mat:
391360 def __div__ (s , t ):
392361 return nmod_mat._div_(s, t)
393362
363+ def det (self ):
364+ """
365+ Returns the determinant of self as an nmod.
366+
367+ >>> nmod_mat(2,2,[1,2,3,4],17).det()
368+ 15
369+
370+ """
371+ if not nmod_mat_is_square(self .val):
372+ raise ValueError (" matrix must be square" )
373+ return nmod(nmod_mat_det(self .val), self .modulus())
374+
394375 def inv (self ):
376+ """
377+ Returns the inverse of self.
378+
379+ >>> from flint import nmod_mat
380+ >>> A = nmod_mat(2,2,[1,2,3,4],17)
381+ >>> A.inv()
382+ [15, 1]
383+ [10, 8]
384+
385+ """
395386 cdef nmod_mat u
396387 if not nmod_mat_is_square(self .val):
397388 raise ValueError (" matrix must be square" )
@@ -486,6 +477,16 @@ cdef class nmod_mat:
486477 rank = nmod_mat_rref((< nmod_mat> res).val)
487478 return res, rank
488479
480+ def rank (self ):
481+ """ Return the rank of a matrix.
482+
483+ >>> from flint import nmod_mat
484+ >>> M = nmod_mat([[1, 2], [3, 4]], 11)
485+ >>> M.rank()
486+ 2
487+ """
488+ return nmod_mat_rank(self .val)
489+
489490 def nullspace (self ):
490491 """
491492 Computes a basis for the nullspace of self. Returns (X, nullity)
0 commit comments