6.5 Built-In Functions

The functions described in the table of section 2.5 also work with sparse matrix arguments. The difference is that for a sparse matrix only the nonzero entries are considered.

len( x)
If x is a spmatrix, returns the number of nonzero entries in x.

bool( [x])
If x is a spmatrix, returns False if x has at least one nonzero entry; False otherwise.

max( x)
If x is a spmatrix, returns the maximum nonzero entry of x.

min( x)
If x is a spmatrix, returns the minimum nonzero entry of x.

abs( x)
If x is a spmatrix, returns a sparse matrix with the absolute value of the elements of x and the same sparsity pattern.

sum( x[, start=0.0])
If x is a spmatrix, returns the sum of start and the elements of x.

The functions list(), tuple(), zip(), map(), filter() also take sparse matrix arguments. They work as for dense matrices, again with the difference that only the nonzero entries are considered.

In the following example we square the entries of the matrix (6.2).

>>> A = spmatrix([2,1,2,2,1,3,4], [1,2,0,2,3,0,2], [0,0,1,1,2,3,3]) 
>>> B = spmatrix(map(lambda x: x**2, A), A.I, A.J)
>>> print B
SIZE: (4,4)
(1, 0)  4.0000e+00
(2, 0)  1.0000e+00
(0, 1)  4.0000e+00
(2, 1)  4.0000e+00
(3, 2)  1.0000e+00
(0, 3)  9.0000e+00
(2, 3)  1.6000e+01

The expression "x in A" returns True if a nonzero entry of A is equal to x and False otherwise.