Base class for sparse matrices

class sage.matrix.matrix_sparse.Matrix_sparse
__copy__()

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

EXAMPLES:

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]
__hash__()
x.__hash__() <==> hash(x)
static __new__()
T.__new__(S, ...) -> a new object with type S, a subtype of T
_derivative()

Differentiate with respect to var by differentiating each element with respect to var.

See also

derivative()

EXAMPLES:

sage: m = matrix(2, [x^i for i in range(4)], sparse=True)
sage: m._derivative(x)
[    0     1]
[  2*x 3*x^2]
_elementwise_product()

Returns the elementwise product of two sparse matrices with identical base rings.

This routine assumes that self and right are both matrices, both sparse, with identical sizes and with identical base rings. It is “unsafe” in the sense that these conditions are not checked and no sensible errors are raised.

This routine is meant to be called from the meth:~sage.matrix.matrix2.Matrix.elementwise_product method, which will ensure that this routine receives proper input. More thorough documentation is provided there.

EXAMPLE:

sage: A = matrix(ZZ, 2, range(6), sparse=True)
sage: B = matrix(ZZ, 2, [1,0,2,0,3,0], sparse=True)
sage: A._elementwise_product(B)
[ 0  0  4]
[ 0 12  0]

AUTHOR:

  • Rob Beezer (2009-07-14)
_multiply_classical()

EXAMPLES:

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.

EXAMPLES:

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()
antitranspose()
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:
sparse – False to make the output a dense matrix; default True
  • phi - arbitrary Python function or callable object
  • R - (optional) ring

OUTPUT: a matrix over R

EXAMPLES:

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

If self is subdivided, the result will be as well:

sage: m = matrix(2, 2, [0, 0, 3, 0])
sage: m.subdivide(None, 1); m
[0|0]
[3|0]
sage: m.apply_map(lambda x: x*x)
[0|0]
[9|0]

If the map sends zero to a non-zero value, then it may be useful to get the result as a dense matrix.

sage: m = matrix(ZZ, 3, 3, [0] * 7 + [1,2], sparse=True); m
[0 0 0]
[0 0 0]
[0 1 2]
sage: parent(m)
Full MatrixSpace of 3 by 3 sparse matrices over Integer Ring
sage: n = m.apply_map(lambda x: x+polygen(QQ), sparse=False); n
[    x     x     x]
[    x     x     x]
[    x x + 1 x + 2]
sage: parent(n)
Full MatrixSpace of 3 by 3 dense matrices over Univariate Polynomial Ring in x over Rational Field

TESTS:

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

sage: m.apply_map(lambda x: x*x, sparse=False).parent()
Full MatrixSpace of 0 by 0 dense matrices over Integer Ring

Check that we don’t unnecessarily apply phi to 0 in the sparse case:

sage: m = matrix(QQ, 2, 2, range(1, 5), sparse=True)
sage: m.apply_map(lambda x: 1/x)
[  1 1/2]
[1/3 1/4]

Test subdivisions when phi maps 0 to non-zero:

sage: m = matrix(2, 2, [0, 0, 3, 0])
sage: m.subdivide(None, 1); m
[0|0]
[3|0]
sage: m.apply_map(lambda x: x+1)
[1|1]
[4|1]
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

EXAMPLES:

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
change_ring()

Return the matrix obtained by coercing the entries of this matrix into the given ring.

Always returns a copy (unless self is immutable, in which case returns self).

EXAMPLES:

sage: A = matrix(QQ[‘x,y’], 2, [0,-1,2*x,-2], sparse=True); A [ 0 -1] [2*x -2] sage: A.change_ring(QQ[‘x,y,z’]) [ 0 -1] [2*x -2]

Subdivisions are preserved when changing rings:

sage: A.subdivide([2],[]); A
[  0  -1]
[2*x  -2]
[-------]
sage: A.change_ring(RR['x,y'])
[                 0  -1.00000000000000]
[2.00000000000000*x  -2.00000000000000]
[-------------------------------------]
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.

EXAMPLES:

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
matrix_from_rows_and_columns()

Return the matrix constructed from self from the given rows and columns.

EXAMPLES:

sage: M = MatrixSpace(Integers(8),3,3, sparse=True)
sage: A = M(range(9)); A
[0 1 2]
[3 4 5]
[6 7 0]
sage: A.matrix_from_rows_and_columns([1], [0,2])
[3 5]
sage: A.matrix_from_rows_and_columns([1,2], [1,2])
[4 5]
[7 0]

Note that row and column indices can be reordered or repeated:

sage: A.matrix_from_rows_and_columns([2,1], [2,1])
[0 7]
[5 4]

For example here we take from row 1 columns 2 then 0 twice, and do this 3 times.

sage: A.matrix_from_rows_and_columns([1,1,1],[2,0,0])
[5 3 3]
[5 3 3]
[5 3 3]

We can efficiently extract large submatrices:

sage: A=random_matrix(ZZ,100000,density=.00005,sparse=True)
sage: B=A[50000:,:50000]                                   
sage: len(B.nonzero_positions())
14047              # 32-bit
100550             # 64-bit

We must pass in a list of indices:

sage: A=random_matrix(ZZ,100,density=.02,sparse=True)
sage: A.matrix_from_rows_and_columns(1,[2,3])
...
TypeError: rows must be a list of integers
sage: A.matrix_from_rows_and_columns([1,2],3)  
...
TypeError: columns must be a list of integers

AUTHORS:

  • Jaap Spies (2006-02-18)
  • Didier Deshommes: some Pyrex speedups implemented
  • Jason Grout: sparse matrix optimizations
transpose()

Returns the transpose of self, without changing self.

EXAMPLES: 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]

Previous topic

Base class for dense matrices

Next topic

Dense Matrices over a general ring

This Page