Re: [SciPy-Dev] Function to check if sparse matrix is symmetric (Saullo Castro)

A check "is_Hermitian" would involve basically the same function, adding something like: m = m.real and checking if the diagonal elements are all real... Saullo 2014-10-10 19:00 GMT+02:00 <scipy-dev-request@scipy.org>:
Send SciPy-Dev mailing list submissions to scipy-dev@scipy.org
To subscribe or unsubscribe via the World Wide Web, visit http://mail.scipy.org/mailman/listinfo/scipy-dev or, via email, send a message with subject or body 'help' to scipy-dev-request@scipy.org
You can reach the person managing the list at scipy-dev-owner@scipy.org
When replying, please edit your Subject line so it is more specific than "Re: Contents of SciPy-Dev digest..."
Today's Topics:
1. Function to check if sparse matrix is symmetric (Saullo Castro) 2. Re: Function to check if sparse matrix is symmetric (Nils Wagner)
----------------------------------------------------------------------
Message: 1 Date: Fri, 10 Oct 2014 12:00:06 +0200 From: Saullo Castro <saullogiovani@gmail.com> Subject: [SciPy-Dev] Function to check if sparse matrix is symmetric To: Scipy - Dev <scipy-dev@scipy.org> Message-ID: <CAHbwRz46P8zbNP= i5VH_fbMtJq0E_UDhLfEqQxNeq3c10-7cEQ@mail.gmail.com> Content-Type: text/plain; charset="utf-8"
I developed a function (shown below) to check if a sparse matrix is symmetric and would like to know if the community is interested to include in scipy.sparse.
Regards, Saullo
def is_symmetric(m): """Check if a sparse matrix is symmetric
Parameters ---------- m : array or sparse matrix A square matrix.
Returns ------- check : bool The check result.
""" if m.shape[0] != m.shape[1]: raise ValueError('m must be a square matrix')
if not isinstance(m, coo_matrix): m = coo_matrix(m)
r, c, v = m.row, m.col, m.data tril_no_diag = r > c triu_no_diag = c > r
if triu_no_diag.sum() != tril_no_diag.sum(): return False
rl = r[tril_no_diag] cl = c[tril_no_diag] vl = v[tril_no_diag] ru = r[triu_no_diag] cu = c[triu_no_diag] vu = v[triu_no_diag]
sortl = np.lexsort((cl, rl)) sortu = np.lexsort((ru, cu)) vl = vl[sortl] vu = vu[sortu]
check = np.allclose(vl, vu)
return check
participants (1)
-
Saullo Castro