fmpq_mat – matrices over rational numbers

class flint.fmpq_mat

Represents a dense matrix over the rational numbers.

>>> A = fmpq_mat(3,3,[1,3,5,2,4,6,fmpq(2,3),2,4])
>>> A.inv()
[-3,  3/2, 3/2]
[ 3, -1/2,  -3]
[-1,    0, 3/2]
>>> A.inv() * A
[1, 0, 0]
[0, 1, 0]
[0, 0, 1]
det(self)

Returns the determinant of self as an fmpq.

>>> (fmpq_mat(2,2,[1,2,3,4]) / 5).det()
-2/25
entries(self)
hilbert(type cls, long n, long m)

Returns the n by m truncated Hilbert matrix.

>>> fmpq_mat.hilbert(2,3)
[  1, 1/2, 1/3]
[1/2, 1/3, 1/4]
inv(self)

Returns the inverse matrix of self.

>>> (fmpq_mat([[1,2],[3,4]]) / 5).inv()
[ -10,    5]
[15/2, -5/2]
>>> (fmpq_mat([[1,2],[3,6]]) / 5).inv()
Traceback (most recent call last):
  ...
ZeroDivisionError: matrix is singular
ncols(self) → long
nrows(self) → long
numer_denom(self)

Returns (A, d) where A is an fmpz_mat and d is an fmpz representing the minimal denominator such that A times d equals self.

>>> A, d = fmpq_mat.hilbert(3,3).numer_denom()
>>> A
[60, 30, 20]
[30, 20, 15]
[20, 15, 12]
>>> d
60
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 = fmpq_mat(3,3,range(9))
>>> A.rref()
([1, 0, -1]
[0, 1,  2]
[0, 0,  0], 2)
>>> A.rref(inplace=True)
([1, 0, -1]
[0, 1,  2]
[0, 0,  0], 2)
>>> A
[1, 0, -1]
[0, 1,  2]
[0, 0,  0]
solve(self, other, algorithm=None)

Given matrices A and B represented by self and other, returns an fmpq_mat X such that \(AX = B\), assuming that A is square and invertible.

Algorithm can None for a default choice, or “fflu” or “dixon” (faster for large matrices).

>>> A = fmpq_mat(2, 2, [1,4,8,3])
>>> B = fmpq_mat(2, 3, range(6))
>>> X = A.solve(B)
>>> X
[12/29, 13/29, 14/29]
[-3/29,  4/29, 11/29]
>>> A*X == B
True
>>> A.solve(B, algorithm='dixon') == X
True
>>> fmpq_mat(2, 2, [1,0,2,0]).solve(B)
Traceback (most recent call last):
  ...
ZeroDivisionError: singular matrix in solve()
>>> A.solve(fmpq_mat(1, 2, [2,3]))
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.

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