Sparse integer matrices.¶
AUTHORS:
- William Stein (2007-02-21)
- Soroosh Yazdani (2007-02-21)
-
class
sage.matrix.matrix_integer_sparse.
Matrix_integer_sparse
¶ Bases:
sage.matrix.matrix_sparse.Matrix_sparse
Create a sparse matrix over the integers.
INPUT:
parent
– a matrix spaceentries
– can be one of the following:- a Python dictionary whose items have the
form
(i, j): x
, where0 <= i < nrows
,0 <= j < ncols
, andx
is coercible to an integer. Thei,j
entry ofself
is set tox
. Thex
‘s can be0
. - Alternatively, entries can be a list of all the entries of the sparse matrix, read row-by-row from top to bottom (so they would be mostly 0).
- a Python dictionary whose items have the
form
copy
– ignoredcoerce
– ignored
-
elementary_divisors
(algorithm='pari')¶ 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 – matrix
- algorithm – (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
-
hermite_form
(algorithm='default', cutoff=0, **kwds)¶ Return the echelon form of self.
Note
This row reduction does not use division if the matrix is not over a field (e.g., if the matrix is over the integers). If you want to calculate the echelon form using division, then use
rref()
, which assumes that the matrix entries are in a field (specifically, the field of fractions of the base ring of the matrix).INPUT:
algorithm
– string. Which algorithm to use. Choices are'default'
: Let Sage choose an algorithm (default).'classical'
: Gauss elimination.'strassen'
: use a Strassen divide and conquer algorithm (if available)
cutoff
– integer. Only used if the Strassen algorithm is selected.transformation
– boolean. Whether to also return the transformation matrix. Some matrix backends do not provide this information, in which case this option is ignored.
OUTPUT:
The reduced row echelon form of
self
, as an immutable matrix. Note that self is not changed by this command. Useechelonize()
to changeself
in place.If the optional parameter
transformation=True
is specified, the output consists of a pair \((E,T)\) of matrices where \(E\) is the echelon form ofself
and \(T\) is the transformation matrix.EXAMPLES:
sage: MS = MatrixSpace(GF(19),2,3) sage: C = MS.matrix([1,2,3,4,5,6]) sage: C.rank() 2 sage: C.nullity() 0 sage: C.echelon_form() [ 1 0 18] [ 0 1 2]
The matrix library used for \(\ZZ/p\)-matrices does not return the transformation matrix, so the
transformation
option is ignored:sage: C.echelon_form(transformation=True) [ 1 0 18] [ 0 1 2] sage: D = matrix(ZZ, 2, 3, [1,2,3,4,5,6]) sage: D.echelon_form(transformation=True) ( [1 2 3] [ 1 0] [0 3 6], [ 4 -1] ) sage: E, T = D.echelon_form(transformation=True) sage: T*D == E True
-
rational_reconstruction
(N)¶ Use rational reconstruction to lift self to a matrix over the rational numbers (if possible), where we view self as a matrix modulo N.
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
()¶ Returns matrices S, U, and V such that S = U*self*V, and S is in Smith normal form. Thus S is diagonal with diagonal entries the ordered elementary divisors of S.
This version is for sparse matrices and simply makes the matrix dense and calls the version for dense integer matrices.
Warning
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 1 0] [ 0 -1 1] [-1 2 -1] sage: V [-1 4 1] [ 1 -3 -2] [ 0 0 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 1 0] [ 0 -1 1] [-1 2 -1] sage: V [-1 3] [ 1 -2] sage: U * A * V [1 0] [0 2] [0 0]
The examples above show that trac ticket #10626 has been implemented.
See also