SparseCSR

class sisl.SparseCSR(arg1, dim=1, dtype=None, nnzpr=20, nnz=None, **kwargs)[source]

A compressed sparse row matrix, slightly different than scipy.sparse.csr_matrix.

This class holds all required information regarding the CSR matrix format.

Note that this sparse matrix of data does not retain the number of columns in the matrix, i.e. it has no way of determining whether the input is correct.

Attributes

data Data contained in the sparse matrix (numpy array of elements)
dim The extra dimensionality of the sparse matrix (elements per matrix element)
dkind The data-type in the sparse matrix (in str)
dtype The data-type in the sparse matrix
finalized Whether the contained data is finalized and non-used elements have been removed
nnz Number of non-zero elements in the sparse matrix
shape The shape of the sparse matrix

Methods

__init__(arg1[, dim, dtype, nnzpr, nnz]) Initialize a new sparse CSR matrix
align(other) Aligns this sparse matrix with the sparse elements of the other sparse matrix
copy([dims, dtype]) Returns an exact copy of the sparse matrix
delete_columns(columns[, keep_shape]) Delete all columns in columns
diags(diagonals[, offsets, dim, dtype]) Create a SparseCSR with diagonal elements with the same shape as the routine
edges(row[, exclude]) Retrieve edges (connections) of a given row or list of row‘s
eliminate_zeros() Remove all zero elememts from the sparse matrix
empty([keep_nnz]) Delete all sparse information from the sparsity pattern
finalize([sort]) Finalizes the sparse matrix by removing all non-set elements
iter_nnz([row]) Iterations of the non-zero elements, returns a tuple of row and column with non-zero elements
nonzero([row, only_col]) Row and column indices where non-zero elements exists
remove(indices) Return a new sparse CSR matrix with all the indices removed
spsame(other) Check whether two sparse matrices have the same non-zero elements
sub(indices) Return a new sparse CSR matrix with the data only for the given indices
sum([axis]) Calculate the sum, if axis is None the sum of all elements are returned, else a new sparse matrix is returned
tocsr([dim]) Return the data in scipy.sparse.csr_matrix format
translate_columns(old, new) Takes all old columns and translates them to new.
align(other)[source]

Aligns this sparse matrix with the sparse elements of the other sparse matrix

Routine for ensuring that all non-zero elements in other are also in this object.

I.e. this will, possibly, change the sparse elements in-place.

A ValueError will be raised if the shapes are not mergeable.

Parameters:

other : SparseCSR

the other sparse matrix to align.

copy(dims=None, dtype=None)[source]

Returns an exact copy of the sparse matrix

Parameters:

dims: array-like, optional

which dimensions to store in the copy, defaults to all.

dtype : numpy.dtype

this defaults to the dtype of the object, but one may change it if supplied.

data

Data contained in the sparse matrix (numpy array of elements)

delete_columns(columns, keep_shape=False)[source]

Delete all columns in columns

Parameters:

columns : int or array_like

columns to delete from the sparse pattern

keep_shape : bool, optional

whether the shape of the object should be retained, if True all higher columns will be shifted according to the number of columns deleted below, if False, only the elements will be deleted.

diags(diagonals, offsets=0, dim=None, dtype=None)[source]

Create a SparseCSR with diagonal elements with the same shape as the routine

Parameters:

diagonals : scalar or array_like

the diagonal values, if scalar the shape must be present.

offsets : scalar or array_like

the offsets from the diagonal for each of the components (defaults to the diagonal)

dim : int, optional

the extra dimension of the new diagonal matrix (default to the current extra dimension)

dtype : numpy.dtype, optional

the data-type to create (default to numpy.float64)

dim

The extra dimensionality of the sparse matrix (elements per matrix element)

dkind

The data-type in the sparse matrix (in str)

dtype

The data-type in the sparse matrix

edges(row, exclude=None)[source]

Retrieve edges (connections) of a given row or list of row‘s

The returned edges are unique and sorted (see numpy.unique).

Parameters:

row : int or list of int

the edges are returned only for the given row

exclude : int or list of int, optional

remove edges which are in the exclude list. Default to row.

eliminate_zeros()[source]

Remove all zero elememts from the sparse matrix

This is an in-place operation

empty(keep_nnz=False)[source]

Delete all sparse information from the sparsity pattern

Essentially this deletes all entries.

Parameters:

keep_nnz: boolean, optional

if True keeps the sparse elements as is. I.e. it will merely set the stored sparse elements to zero. This may be advantagegous when re-constructing a new sparse matrix from an old sparse matrix

finalize(sort=True)[source]

Finalizes the sparse matrix by removing all non-set elements

One may still interact with the sparse matrix as one would previously.

NOTE: This is mainly an internal used routine to ensure data structure when converting to scipy.sparse.csr_matrix

Parameters:

sort: bool, optional

sort the column indices for each row

finalized

Whether the contained data is finalized and non-used elements have been removed

iter_nnz(row=None)[source]

Iterations of the non-zero elements, returns a tuple of row and column with non-zero elements

An iterator returning the current row index and the corresponding column index.

>>> for r, c in self: 

In the above case r and c are rows and columns such that

>>> self[r, c] 

returns the non-zero element of the sparse matrix.

Parameters:

row : int or array_like of int

only loop on the given row(s) default to all rows

nnz

Number of non-zero elements in the sparse matrix

nonzero(row=None, only_col=False)[source]

Row and column indices where non-zero elements exists

Parameters:

row : int or array_like of int, optional

only return the tuples for the requested rows, default is all rows

only_col : bool, optional

only return then non-zero columns

remove(indices)[source]

Return a new sparse CSR matrix with all the indices removed

Parameters:

indices : array_like

the indices of the rows and columns that are removed in the sparse pattern

shape

The shape of the sparse matrix

spsame(other)[source]

Check whether two sparse matrices have the same non-zero elements

Parameters:other : SparseCSR
Returns:True if the same non-zero elements are in the matrices (but not necessarily the same values)
sub(indices)[source]

Return a new sparse CSR matrix with the data only for the given indices

Parameters:

indices : array_like

the indices of the rows and columns that are retained in the sparse pattern

sum(axis=None)[source]

Calculate the sum, if axis is None the sum of all elements are returned, else a new sparse matrix is returned

Parameters:

axis : int, optional

which axis to perform the sum of. If None the element sum is returned, if either 0 or 1 is passed a vector is returned, and for 2 it returns a new sparse matrix with the last dimension reduced to 1 (summed).

Raises:

NotImplementedError : when axis = 1

tocsr(dim=0, **kwargs)[source]

Return the data in scipy.sparse.csr_matrix format

Parameters:

dim: int, optional

the dimension of the data to create the sparse matrix

**kwargs:

arguments passed to the scipy.sparse.csr_matrix routine

translate_columns(old, new)[source]

Takes all old columns and translates them to new.

Parameters:

old : int or array_like

old column indices

new : int or array_like

new column indices