user1407199
user1407199

Reputation: 183

How to print all the eigen values using np.linalg.eigh(A)?

I have symmetric matrix of size 2200 * 2200. I use the following command in numpy to diagonalize :

np.linalg.eigh(A)

It gives output as follows:

(array([ -1.93221186e-14,  -1.53743240e-14,  -3.58303176e-15, ...,
     4.95098104e+01,   5.06117042e+01,   5.07858517e+01]).

Please suggest me a way , so that it can print all the 2200 eigen values. Thanks for your reply in advance

Upvotes: 2

Views: 931

Answers (2)

wim
wim

Reputation: 363233

Just use tuple unpacking:

eigenvalues, eigenvectors = np.linalg.eigh(A)

Then you will have a 1-d array eigenvalues with len(eigenvalues) == 2200. You can iterate and print that as usual if you like.

for eigenvalue in eigenvalues:
    print(eigenvalue)

Upvotes: 3

Bálint Aradi
Bálint Aradi

Reputation: 3812

As suggested before, you could use tuple unpacking to store your eigenvalues in a separate array. Then you could use the np.savetxt routine to write out your array:

import sys
import numpy as np

eigvals, eigvecs = np.linalg.eigh(A)
np.savetxt(sys.stdout, eigvals, delimiter=" ", fmt="%15.8E")

Nice thing here is, if you decide to write your arrays into a file instead to the screen, you can use a file handler (or even a file name) instead of sys.stdout.

Upvotes: 0

Related Questions