Skip to content

Commit 67b29cf

Browse files
committed
Make nmod_mat subclass flint_mat
1 parent 655f392 commit 67b29cf

File tree

3 files changed

+42
-63
lines changed

3 files changed

+42
-63
lines changed

src/flint/types/nmod_mat.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ from flint.flint_base.flint_base cimport flint_mat
33
from flint.flintlib.nmod_mat cimport nmod_mat_t
44
from flint.flintlib.flint cimport mp_limb_t
55

6-
cdef class nmod_mat:
6+
cdef class nmod_mat(flint_mat):
77
cdef nmod_mat_t val
88
cpdef long nrows(self)
99
cpdef long ncols(self)

src/flint/types/nmod_mat.pyx

+39-38
Original file line numberDiff line numberDiff line change
@@ -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
4443
from flint.utils.typecheck cimport typecheck
4544
from flint.types.fmpz_mat cimport any_as_fmpz_mat
4645
from flint.types.fmpz_mat cimport fmpz_mat
@@ -50,6 +49,8 @@ from flint.types.nmod_poly cimport nmod_poly
5049
from flint.pyflint cimport global_random_state
5150
from flint.flint_base.flint_context cimport thectx
5251

52+
from flint.flint_base.flint_base cimport flint_mat
53+
5354

5455
ctx = 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)

src/flint/utils/conversion.pxd

+2-24
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,18 @@
1-
from cpython.version cimport PY_MAJOR_VERSION
2-
31
cdef inline long prec_to_dps(n):
42
return max(1, int(round(int(n)/3.3219280948873626)-1))
53

64
cdef inline long dps_to_prec(n):
75
return max(1, int(round((int(n)+1)*3.3219280948873626)))
86

97
cdef inline chars_from_str(s):
10-
if PY_MAJOR_VERSION < 3:
11-
return s
12-
else:
13-
return s.encode('ascii')
8+
return s.encode('ascii')
149

1510
cdef inline str_from_chars(s):
16-
if PY_MAJOR_VERSION < 3:
17-
return str(s)
18-
else:
19-
return bytes(s).decode('ascii')
20-
21-
cdef inline matrix_to_str(tab):
22-
if len(tab) == 0 or len(tab[0]) == 0:
23-
return "[]"
24-
tab = [[str(c) for c in row] for row in tab]
25-
widths = []
26-
for i in xrange(len(tab[0])):
27-
w = max([len(row[i]) for row in tab])
28-
widths.append(w)
29-
for i in xrange(len(tab)):
30-
tab[i] = [s.rjust(widths[j]) for j, s in enumerate(tab[i])]
31-
tab[i] = "[" + (", ".join(tab[i])) + "]"
32-
return "\n".join(tab)
11+
return bytes(s).decode('ascii')
3312

3413
cdef inline _str_trunc(s, trunc=0):
3514
if trunc > 0 and len(s) > 3 * trunc:
3615
left = right = trunc
3716
omitted = len(s) - left - right
3817
return s[:left] + ("{...%s digits...}" % omitted) + s[-right:]
3918
return s
40-

0 commit comments

Comments
 (0)