Eigenvalues & Eigenvectors (NumPy)
Definition
Let \(A\) be an \(n\times n\) matrix (i.e. a square matrix). A non-zero vector \(\vec{v}\) is an eigenvector of \(A\) with eigenvalue \(\lambda\) if
Rewriting this equation, we see that \(\vec{v}\) is a solution of the homogeneous system of equations
where \(I\) is the identity matrix of size \(n\). Non-trivial solutions exists only when the matrix \(A-\lambda I\) is noninvertible (singular). That is, when \(\operatorname{det}(A-\lambda I) =0\). Therefore, the eigenvalues are the roots of the characteristic polynomial
Here are three examples that we will consider. In each case, we have pre-computed the eigenvalues and eigenvectors (check them yourself).
Notice, for matrix \(D\) there is one eigenvalue that has two associated eigenvectors.
Warning
Eigenvectors are not unique, any scalar multiple of an eigenvector is an eigenvector. Therefore, some caution is needed when having SciPy compute eigenvectors. The values returned will be in floating point form, if you expected an eigenvector with integer or rational number entries you may need to rescale it to get it into this form. We'll see examples of this below.
scipy.linalg.eig
The function scipy.linalg.eig
computes eigenvalues and eigenvectors of a square matrix \(A\).
It returns a 2-tuple, the first part is a 1D array of eigenvalues, the second part is a 2D array where the columns are the (normalized) eigenvectors associated to each eigenvalue. The eigenvalues are returned as complex numbers, so if you expect real eigenvalues then check to see if the imaginary part is \(0\). This will be the case in all our examples below.
Example
Determine the eigenvalues and eigenvectors of
The eigenvalues are \(1\) and \(4\), and the corresponding eigenvalues are \(\begin{bmatrix}-0.89442719 \\ 0.4472136 \end{bmatrix}\) and \(\begin{bmatrix} -0.70710678 \\ -0.70710678\end{bmatrix}\). Notice, in the first vector it looks like the first component is twice the second component. Whereas in the second vector both components are equal. Therefore, these vectors are scalar multiples of \(\begin{bmatrix}-2 \\ 1 \end{bmatrix}\) and \(\begin{bmatrix} 1 \\ 1\end{bmatrix}\).
To check this directly in python we can use the tuple assignment to capture the arrays for eigenvalues and eigenvectors, and then pick off the two vectors.
Example
Determine the eigenvalues and eigenvectors of
Example
Determine the eigenvalues and eigenvectors of
(array([-3.+0.j, 4.+0.j, 2.+0.j]),
array([[ 5.77350269e-01, -8.33283831e-16, -7.07106781e-01],
[ 5.77350269e-01, -7.07106781e-01, -7.07106781e-01],
[ 5.77350269e-01, -7.07106781e-01, 3.33066907e-16]]))
The eigenvalues are \(-3\), \(4\) and \(2\), and the corresponding eigenvectors are
\(\begin{bmatrix} 5.77350269e-01 \\ 5.77350269e-01 \\ 5.77350269e-01 \end{bmatrix}\), \(\begin{bmatrix} -8.33283831e-16 \\ -7.07106781e-01 \\ -7.07106781e-01 \end{bmatrix}\), and
\(\begin{bmatrix} -7.07106781e-01 \\ -7.07106781e-01 \\ 3.33066907e-16 \end{bmatrix}\). The entry \(3.33066907e-16\) is very very small compared to the other entries (e-16
means \(\cdot 10^{-16}\)), which leads us to believe it should be \(0\). Therefore, these vectors can be rescaled to
\(\begin{bmatrix} 1 \\ 1 \\ 1 \end{bmatrix}\), \(\begin{bmatrix} 0 \\ 1 \\ 1 \end{bmatrix}\), and
\(\begin{bmatrix} 1 \\ 1 \\ 0 \end{bmatrix}\).
Example
Determine the eigenvalues and eigenvectors of
[ 3.+0.j -3.+0.j 3.+0.j]
[[ 0.81649658 0.57735027 0.22645541]
[ 0.40824829 -0.57735027 0.79259392]
[-0.40824829 0.57735027 0.56613852]]
The eigenvalue \(3\) appears twice, therefore the first and third vectors both correspond to eigenvlaue \(3\).
Eignevalue \(3\) has eigenvectors \(\begin{bmatrix} 0.81649658 \\ 0.40824829 \\ -0.40824829 \end{bmatrix}\) and \(\begin{bmatrix} 0.22645541 \\ 0.79259392 \\ 0.56613852 \end{bmatrix}\).
Eignevalue \(-3\) has eigenvector \(\begin{bmatrix} 0.57735027 \\ -0.57735027 \\ 0.57735027 \end{bmatrix}\), which rescales to \(\begin{bmatrix} 1 \\ -1 \\ 1 \end{bmatrix}\).
To rescale the two eigenvectors corresponding to eigenvalue \(3\) we do the following.
- select the first column of the eigenvectors array.
- rescale by dividing through by the second entry.
- select the third column of the eigenvectors array.
- rescale by dividing through by the first entry (since it is the smallest). Once we do this we'll see that the numbers are fractions ending in \(0.5\), so we scale by \(2\).
Notice that these two eigenvectors are not orthogonal. One way to fix this is to use .eigh
instead of eig
since the matrix \(A\) is symmetric.
[-3. 3. 3.]
[[ 0.57735027 0. 0.81649658]
[-0.57735027 0.70710678 0.40824829]
[ 0.57735027 0.70710678 -0.40824829]]
Rescaling the vectors corresponding to eigenvalue \(3\) we get \(\left[\begin{matrix} 0\\1\\1\\ \end{matrix}\right]\) and \(\left[\begin{matrix} 2\\1\\-1\\ \end{matrix}\right]\).