def sp_bandwidth(A): """ Returns the max(mb), lower(lb), and upper(ub) bandwidths of a sparse CSR/CSC matrix. If the matrix is symmetric then the upper and lower bandwidths are identical. Diagonal matrices have a bandwidth equal to one. Parameters ---------- A : csr_matrix, csc_matrix Input matrix Returns ------- mb : int Maximum bandwidth of matrix. lb : int Lower bandwidth of matrix. ub : int Upper bandwidth of matrix. """ nrows = A.shape[0] ncols = A.shape[1] if A.getformat() == 'csr': return _sparse_bandwidth(A.indices, A.indptr, nrows) elif A.getformat() == 'csc': # Normal output is mb,lb,ub but since CSC # is transpose of CSR switch lb and ub mb, ub, lb = _sparse_bandwidth(A.indices, A.indptr, ncols) return mb, lb, ub else: raise Exception('Invalid sparse input format.')