def nonzerodiag(nrow, colind, rowptr): """ Given the sparsity pattern of a square sparse matrix in compressed row (csr) format, attempt to find a *row* permutation so the row-permuted matrix has a nonzero diagonal, if this is possible. This function assumes that the matrix indexing is 0-based. Note that in general, this function does not preserve symmetry. The method used is a depth-first search with lookahead described in [Duff81a]_ and [Duff81b]_. :parameters: :nrow: The number of rows of the input matrix. :colind: An integer array (or list) of length nnz giving the column indices of the nonzero elements in each row. :rowptr: An integer array (or list) of length nrow+1 giving the indices of the first element of each row in colind. :returns: :perm: An integer array of length nrow giving the variable permutation. If irow and jcol are two integer arrays describing the pattern of the input matrix in triple format, perm[irow] and jcol describe the permuted matrix. :nzdiag: The number of nonzeros on the diagonal of the permuted matrix. """ lenrows = rowptr[1:] - rowptr[:-1] perm, nzdiag = _mc21.mc21ad(colind + 1, rowptr[:-1] + 1, lenrows) return (perm - 1, nzdiag)
def test_spec_sheet(self): """Solve example from the spec sheet. Ordering http://www.hsl.rl.ac.uk/specs/mc21.pdf """ n = 4 icn = np.array([1, 4, 3, 4, 1, 4, 2, 4], dtype=np.int32) ip = np.array([1, 3, 5, 7], dtype=np.int32) lenr = np.array([2, 2, 2, 2], dtype=np.int32) iperm, numnz = _mc21.mc21ad(icn, ip, lenr) assert np.array_equal(iperm, np.array([1, 4, 2, 3]))
import numpy as np from hsl.ordering import _mc21 n = 4 icn = np.array([1, 4, 3, 4, 1, 4, 2, 4], dtype=np.int32) ip = np.array([1, 3, 5, 7], dtype=np.int32) lenr = np.array([2, 2, 2, 2], dtype=np.int32) iperm, numnz = _mc21.mc21ad(icn, ip, lenr) print 'iperm = ', iperm, ' (1-based)' print 'numnz = ', numnz