@@ -40,7 +40,6 @@ from flint.flintlib.nmod_mat cimport (
40
40
nmod_mat_randtest,
41
41
)
42
42
43
- from flint.utils.conversion cimport matrix_to_str
44
43
from flint.utils.typecheck cimport typecheck
45
44
from flint.types.fmpz_mat cimport any_as_fmpz_mat
46
45
from flint.types.fmpz_mat cimport fmpz_mat
@@ -50,6 +49,8 @@ from flint.types.nmod_poly cimport nmod_poly
50
49
from flint.pyflint cimport global_random_state
51
50
from flint.flint_base.flint_context cimport thectx
52
51
52
+ from flint.flint_base.flint_base cimport flint_mat
53
+
53
54
54
55
ctx = thectx
55
56
@@ -69,10 +70,12 @@ cdef any_as_nmod_mat(obj, nmod_t mod):
69
70
return NotImplemented
70
71
71
72
72
- cdef class nmod_mat:
73
+ cdef class nmod_mat(flint_mat) :
73
74
"""
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.
76
79
"""
77
80
78
81
# cdef nmod_mat_t val
@@ -177,18 +180,6 @@ cdef class nmod_mat:
177
180
entries = ' , ' .join(map (str , self .entries()))
178
181
return f" nmod_mat({m}, {n}, [{entries}], {self.modulus()})"
179
182
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
-
192
183
def entries (self ):
193
184
cdef long i, j, m, n
194
185
cdef nmod t
@@ -202,13 +193,6 @@ cdef class nmod_mat:
202
193
L[i* n + j] = t
203
194
return L
204
195
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
-
212
196
def __getitem__ (self , index ):
213
197
cdef long i, j
214
198
cdef nmod x
@@ -229,21 +213,6 @@ cdef class nmod_mat:
229
213
else :
230
214
raise TypeError (" cannot set item of type %s " % type (value))
231
215
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
-
247
216
def __pos__ (self ):
248
217
return self
249
218
@@ -391,7 +360,29 @@ cdef class nmod_mat:
391
360
def __div__ (s , t ):
392
361
return nmod_mat._div_(s, t)
393
362
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
+
394
375
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
+ """
395
386
cdef nmod_mat u
396
387
if not nmod_mat_is_square(self .val):
397
388
raise ValueError (" matrix must be square" )
@@ -486,6 +477,16 @@ cdef class nmod_mat:
486
477
rank = nmod_mat_rref((< nmod_mat> res).val)
487
478
return res, rank
488
479
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
+
489
490
def nullspace (self ):
490
491
"""
491
492
Computes a basis for the nullspace of self. Returns (X, nullity)
0 commit comments