nmod_mat – matrices over integers mod n

class flint.nmod_mat

The nmod_mat type represents dense matrices over Z/nZ for word-size n (see fmpz_mod_mat for larger moduli).

Some operations may assume that n is a prime.

charpoly(self)

Return the characteristic polynomial of a matrix.

>>> from flint import nmod_mat
>>> M = nmod_mat([[1, 2], [3, 4]], 11)
>>> M.charpoly()
x^2 + 6*x + 9
det(self)

Returns the determinant of self as an nmod.

>>> nmod_mat(2,2,[1,2,3,4],17).det()
15
entries(self)
inv(self)

Returns the inverse of self.

>>> from flint import nmod_mat
>>> A = nmod_mat(2,2,[1,2,3,4],17)
>>> A.inv()
[15, 1]
[10, 8]
minpoly(self)

Return the minimal polynomial of a matrix.

>>> from flint import nmod_mat
>>> M = nmod_mat([[2, 1, 0], [0, 2, 0], [0, 0, 2]], 7)
>>> M.charpoly()
x^3 + x^2 + 5*x + 6
>>> M.minpoly()
x^2 + 3*x + 4
modulus(self) mp_limb_t
ncols(self) long
nrows(self) long
nullspace(self)

Computes a basis for the nullspace of self. Returns (X, nullity) where nullity is the rank of the nullspace of self and X is a matrix whose first (nullity) columns are linearly independent, and such that self * X = 0.

>>> A = nmod_mat(3,5,range(1,16),23)
>>> X, nullity = A.nullspace()
>>> A.rank(), nullity, X.rank()
(2, 3, 3)
>>> A * X
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
[0, 0, 0, 0, 0]
>>> X
[ 1,  2,  3, 0, 0]
[21, 20, 19, 0, 0]
[ 1,  0,  0, 0, 0]
[ 0,  1,  0, 0, 0]
[ 0,  0,  1, 0, 0]
classmethod randtest(cls, ulong m, ulong n, ulong mod)

Returns a random (m, n) matrix with non-uniformly chosen entries. Zero entries are generated with increased probability.

rank(self)

Return the rank of a matrix.

>>> from flint import nmod_mat
>>> M = nmod_mat([[1, 2], [3, 4]], 11)
>>> M.rank()
2
repr(self)
rref(self, inplace=False)

Computes the reduced row echelon form (rref) of self, either returning a new copy or modifying self in-place. Returns (rref, rank).

>>> A = nmod_mat(3,3,range(9),23)
>>> A.rref()
([1, 0, 22]
[0, 1,  2]
[0, 0,  0], 2)
>>> A.rref(inplace=True)
([1, 0, 22]
[0, 1,  2]
[0, 0,  0], 2)
>>> A
[1, 0, 22]
[0, 1,  2]
[0, 0,  0]
solve(self, other)

Given self = A and other = B, returns a matrix X such that A*X = B, assuming that self is square and invertible.

>>> A = nmod_mat(2, 2, [1,4,8,3], 11)
>>> B = nmod_mat(2, 3, range(6), 11)
>>> X = A.solve(B)
>>> X
[8,  5, 2]
[9, 10, 0]
>>> A*X == B
True
>>> nmod_mat(2, 2, [1,0,2,0], 11).solve(B)
Traceback (most recent call last):
  ...
ZeroDivisionError: singular matrix in solve()
>>> A.solve(nmod_mat(1, 2, [2,3], 11))
Traceback (most recent call last):
  ...
ValueError: need a square system and compatible right hand side
str(self, *args, **kwargs)
table(self)
tolist()

flint_mat.table(self)

transpose(self)

Returns the transpose of self.

>>> nmod_mat(2,3,range(6),7).transpose()
[0, 3]
[1, 4]
[2, 5]