32.11 Base class for sparse matrices

Module: sage.matrix.matrix_sparse

Base class for sparse matrices

Class: Matrix_sparse

class Matrix_sparse

Functions: antitranspose,$ \,$ apply_map,$ \,$ apply_morphism,$ \,$ change_ring,$ \,$ charpoly,$ \,$ transpose

apply_map( )

Apply the given map phi (an arbitrary Python function or callable object) to this matrix. If R is not given, automatically determine the base ring of the resulting matrix.

Input:

phi
- arbitrary Python function or callable object
R
- (optional) ring

Output: a matrix over R

sage: m = matrix(ZZ, 10000, {(1,2): 17}, sparse=True)
sage: k.<a> = GF(9)
sage: f = lambda x: k(x)
sage: n = m.apply_map(f)
sage: n.parent()
Full MatrixSpace of 10000 by 10000 sparse matrices over Finite Field in a
of size 3^2
sage: n[1,2]
2

An example where the codomain is explicitly specified.

sage: n = m.apply_map(lambda x:x%3, GF(3))
sage: n.parent()
Full MatrixSpace of 10000 by 10000 sparse matrices over Finite Field of
size 3
sage: n[1,2]
2

If we didn't specify the codomain, the resulting matrix in the above case ends up over ZZ again:

sage: n = m.apply_map(lambda x:x%3)
sage: n.parent()
Full MatrixSpace of 10000 by 10000 sparse matrices over Integer Ring
sage: n[1,2]
2

TESTS:

sage: m = matrix([])
sage: m.apply_map(lambda x: x*x) == m
True

apply_morphism( )

Apply the morphism phi to the coefficients of this sparse matrix.

The resulting matrix is over the codomain of phi.

Input:

phi
- a morphism, so phi is callable and phi.domain() and phi.codomain() are defined. The codomain must be a ring.

Output: a matrix over the codomain of phi

sage: m = matrix(ZZ, 3, range(9), sparse=True)
sage: phi = ZZ.hom(GF(5))
sage: m.apply_morphism(phi)
[0 1 2]
[3 4 0]
[1 2 3]
sage: m.apply_morphism(phi).parent()
Full MatrixSpace of 3 by 3 sparse matrices over Finite Field of size 5

charpoly( )

Return the characteristic polynomial of this matrix.

Note - the generic sparse charpoly implementation in Sage is to just compute the charpoly of the corresponding dense matrix, so this could use a lot of memory. In particular, for this matrix, the charpoly will be computed using a dense algorithm.

sage: A = matrix(ZZ, 4, range(16), sparse=True)
sage: A.charpoly()
x^4 - 30*x^3 - 80*x^2
sage: A.charpoly('y')
y^4 - 30*y^3 - 80*y^2
sage: A.charpoly()
x^4 - 30*x^3 - 80*x^2

transpose( )

Returns the transpose of self, without changing self.

We create a matrix, compute its transpose, and note that the original matrix is not changed.

sage: M = MatrixSpace(QQ,  2, sparse=True)
sage: A = M([1,2,3,4])
sage: B = A.transpose()
sage: print B
[1 3]
[2 4]
sage: print A
[1 2]
[3 4]

Special Functions: __copy__,$ \,$ _multiply_classical,$ \,$ _multiply_classical_with_cache,$ \,$ _pickle,$ \,$ _unpickle_generic

__copy__( )

Return a copy of this matrix. Changing the entries of the copy will not change the entries of this matrix.

sage: A = matrix(QQ['x,y'], 2, [0,-1,2,-2], sparse=True); A
[ 0 -1]
[ 2 -2]
sage: B = copy(A); B
[ 0 -1]
[ 2 -2]
sage: B is A
False
sage: B[0,0]=10; B
[10 -1]
[ 2 -2]
sage: A
[ 0 -1]
[ 2 -2]

_multiply_classical( )

sage: A = matrix(QQ['x,y'], 2, [0,-1,2,-2], sparse=True)
sage: type(A)
<type 'sage.matrix.matrix_generic_sparse.Matrix_generic_sparse'>
sage: B = matrix(QQ['x,y'], 2, [-1,-1,-2,-2], sparse=True)
sage: A*B
[2 2]
[2 2]

_multiply_classical_with_cache( )

This function computes the locations of the end of the rows/columns in the non-zero entries list once O(rows+cols) time and space, then uses these values in the inner loops. For large matrices this can be a 2x or more speedup, but the matrices can no longer be arbitrarily large as the runtime and space requirements are no longer functions of the total number of entries only.

sage: A = matrix(QQ['x,y'], 2, [0,-1,2,-2], sparse=True)
sage: type(A)
<type 'sage.matrix.matrix_generic_sparse.Matrix_generic_sparse'>
sage: B = matrix(QQ['x,y'], 2, [-1,-1,-2,-2], sparse=True)
sage: A._multiply_classical_with_cache(B)
[2 2]
[2 2]

_pickle( )

_unpickle_generic( )

See About this document... for information on suggesting changes.