How do I calculate the middle eigenvalue in numpy?
Is this a correct way to do it?
import numpy as np
(1/a.ndim)*np.trace(a)
a is a numpy matrix.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
That code will not calculate the middle eigenvalue for most definitions of the word “middle” (though it will work by chance on a 2×2 matrix).
If you want the mean of the eigenvalues (which is what the code you have written is closest to) then you could use:
assuming that a is an NxN matrix. This is because the trace is the sum of the eigenvalues, so if we divide by the size of the matrix (not its dimensionality, which should be two always) we get the mean.
If you want the median eigenvalue (which in the case where the matrix has an odd size is one of the eigenvalues; otherwise it is half way between two values) you can use:
Note that for 2×2 matrices these two values are the same.