Sparse integer matrices¶
AUTHORS:
William Stein (2007-02-21)
Soroosh Yazdani (2007-02-21)
- class sage.matrix.matrix_integer_sparse.Matrix_integer_sparse[source]¶
Bases:
Matrix_sparseCreate a sparse matrix over the integers.
INPUT:
parent– a matrix space overZZentries– seematrix()copy– ignored (for backwards compatibility)coerce– ifFalse, assume without checking that the entries are of typeInteger
- charpoly(var='x', algorithm=None)[source]¶
Return the characteristic polynomial of this matrix.
INPUT:
var– (default:'x') the name of the variable of the polynomialalgorithm– (default:None) one ofNone,'linbox', or an algorithm accepted bysage.matrix.matrix_sparse.Matrix_sparse.charpoly()
EXAMPLES:
sage: M = MatrixSpace(ZZ, 4, sparse=True) sage: m = M() sage: m[0,0] = m[1,2] = m[2,3] = m[3,3] = 1 sage: m[0,2] = m[1,3] = m[2,0] = m[3,0] = -3 sage: m[1,1] = 2 sage: m [ 1 0 -3 0] [ 0 2 1 -3] [-3 0 0 1] [-3 0 0 1] sage: m.charpoly() x^4 - 4*x^3 - 4*x^2 + 16*x
- elementary_divisors(algorithm='pari')[source]¶
Return the elementary divisors of self, in order.
The elementary divisors are the invariants of the finite abelian group that is the cokernel of left multiplication by this matrix. They are ordered in reverse by divisibility.
INPUT:
self– matrixalgorithm– (default:'pari')‘pari’: works robustly, but is slower
‘linbox’ – use linbox (currently off, broken)
OUTPUT: list of integers
EXAMPLES:
sage: matrix(3, range(9),sparse=True).elementary_divisors() [1, 3, 0] sage: M = matrix(ZZ, 3, [1,5,7, 3,6,9, 0,1,2], sparse=True) sage: M.elementary_divisors() [1, 1, 6]
This returns a copy, which is safe to change:
sage: edivs = M.elementary_divisors() sage: edivs.pop() 6 sage: M.elementary_divisors() [1, 1, 6]
See also
- minpoly(var='x', algorithm=None)[source]¶
Return the minimal polynomial of this matrix.
INPUT:
var– (default:'x') the name of the variable of the polynomialalgorithm– (default:None) one ofNone,'linbox', or an algorithm accepted bysage.matrix.matrix_sparse.Matrix_sparse.minpoly()
EXAMPLES:
sage: M = MatrixSpace(ZZ, 4, sparse=True) sage: m = M({(0, 0):2, (1, 1):1, (2, 1):-8, (2, 2):2, (2, 3):-1, (3, 3):1}) sage: m [ 2 0 0 0] [ 0 1 0 0] [ 0 -8 2 -1] [ 0 0 0 1] sage: m.minpoly() x^2 - 3*x + 2
- rank(algorithm=None)[source]¶
Compute the rank of this matrix.
INPUT:
algorithm– (optional) one ofNone,'linbox'or'generic'
EXAMPLES:
sage: M = MatrixSpace(ZZ, 3, 2, sparse=True) sage: m = M([1, 0, 2, 3, -1, 0]) sage: m.rank() 2
- rational_reconstruction(N)[source]¶
Use rational reconstruction to lift
selfto a matrix over the rational numbers (if possible), where we viewselfas a matrix modulo .EXAMPLES:
sage: A = matrix(ZZ, 3, 4, [(1/3)%500, 2, 3, (-4)%500, ....: 7, 2, 2, 3, ....: 4, 3, 4, (5/7)%500], sparse=True) sage: A.rational_reconstruction(500) [1/3 2 3 -4] [ 7 2 2 3] [ 4 3 4 5/7]
- smith_form(transformation=True, integral=None)[source]¶
Return the smith normal form of this matrix, that is the diagonal matrix
with diagonal entries the ordered elementary divisors of this matrix.INPUT:
transformation– boolean (default:True); whether to return the transformation matrices and such thatintegral– a subring of the base ring orTrue(default:None); ignored for matrices with integer entries
This version is for sparse matrices and simply makes the matrix dense and calls the version for dense integer matrices.
Note
The
elementary_divisors()function, which returns the diagonal entries of S, is VASTLY faster than this function.The elementary divisors are the invariants of the finite abelian group that is the cokernel of this matrix. They are ordered in reverse by divisibility.
EXAMPLES:
sage: A = MatrixSpace(IntegerRing(), 3, sparse=True)(range(9)) sage: D, U, V = A.smith_form() sage: D [1 0 0] [0 3 0] [0 0 0] sage: U [ 0 2 -1] [ 0 -1 1] [ 1 -2 1] sage: V [ 0 0 1] [-1 2 -2] [ 1 -1 1] sage: U*A*V [1 0 0] [0 3 0] [0 0 0]
It also makes sense for nonsquare matrices:
sage: A = Matrix(ZZ,3,2,range(6), sparse=True) sage: D, U, V = A.smith_form() sage: D [1 0] [0 2] [0 0] sage: U [ 0 2 -1] [ 0 -1 1] [ 1 -2 1] sage: V [-1 1] [ 1 0] sage: U * A * V [1 0] [0 2] [0 0]
The examples above show that Issue #10626 has been implemented.
See also