Signs of Matlab and Numpy Graph Fourier Transform Matrix are Different

I would like to ask why I see the same numerical results but with different signs for the same demo data. I understand that the signs are arbitrary, but I’m using the transpose of this matrix in later implementation and getting severly different results over iterations because of the sign difference. Thus, I really need the Python GFT to match.

Python:

adj = dict['adj']  # given adj matrix
D = sp.diags(np.array(adj.sum(axis=0)).flatten())
# print(np.linalg.matrix_rank(D.toarray()))
L = D - adj  # Laplacian matrix
eigenvalues, psi_gft = np.linalg.eigh(L.toarray())
# sort eigenvalues by ascending order such that constant vector is in first column
idx = np.argsort(np.abs(eigenvalues))
eigenvalues = eigenvalues[idx]
psi_gft = psi_gft[:, idx]

Numpy 7×7 table:

Matlab:

L=diag(sum(adj))-adj;
[PsiGFT,e]= eigs(L, 175, 'sm');

Matlab 7×7 table:

As you can notice, the absolute values of the 7×7 sample matrices are equivalent, but I’m seeing different signs in some cells. I was wondering what I can do to the Python implementation to match the Matlab implementation.

I tried using the Numpy implementation of the eigh method but I am getting the occasional opposite sign. I am expecting the matrices to match perfectly between Python and Matlab, given it is the same demo data.

  • 1

    There are literally hundreds of questions on here about the eigendecomposition not producing the same results in MATLAB and Python. I encourage you to search for that.

    – 

  • Please include your Python imports and the actual text for the 7×7 matrices so people can test the code.

    – 

  • Hi @jared, the only imports are numpy and scipy. The matrix in question is 175×200 but I just highlighted the first 7 rows/columns to show the sign differences.

    – 

  • Hi @CrisLuengo, I searched through quite a few threads and haven’t found anything like the issue I’m dealing with. Thanks!

    – 

  • @MichaelPaglia it doesn’t look like you’re just importing numpy and SciPy since scipy.diags doesn’t exist (at least I don’t think it does). There is scipy.sparse.diags, but is your matrix sparse? Why not use np.linalg.diags? In general, I recommend including all your imports rather than leaving us to guess.

    – 




Leave a Comment