Matrix eigenvectors and eigenvalues

Time:2021-10-24

Recently, when learning LDA, we need to calculate eigenvalues and eigenvectors, so we re learned a wave

The calculation of eigenvalues is relatively simple in Python, and the linalg calculation of numpy needs to be imported.
Linalg is the abbreviation of linear algebra.

Import numpy first

import numpy as np

Randomly generate a matrix A

A = np.random.rand(4, 4)
A
array([[0.14978175, 0.60689778, 0.02583363, 0.46816227],
       [0.28508934, 0.74476942, 0.48711273, 0.75551799],
       [0.54103663, 0.57551838, 0.16542061, 0.06687122],
       [0.99511415, 0.07225251, 0.67671701, 0.80672535]])

We use lambda to represent eigenvalues and $W $to represent eigenvectors
$$\lambda (Lambda)$$

Lambda,W = np.linalg.eig(A)

We note that both eigenvalues and eigenvariables contain complex parts

Lambda
array([ 1.90910151+0.j        , -0.16902995+0.48364592j,
       -0.16902995-0.48364592j,  0.29565552+0.j        ])
W
array([[-0.37740651+0.j        , -0.0403403 +0.46883807j,
        -0.0403403 -0.46883807j,  0.17922515+0.j        ],
       [-0.62172554+0.j        , -0.30567755+0.1341112j ,
        -0.30567755-0.1341112j , -0.39087808+0.j        ],
       [-0.34506145+0.j        ,  0.61011431+0.j        ,
         0.61011431-0.j        , -0.67479126+0.j        ],
       [-0.59325734+0.j        , -0.09427735-0.5348002j ,
        -0.09427735+0.5348002j ,  0.59979116+0.j        ]])

Let sigma be a diagonal matrix, and the values on the diagonal are eigenvalues
$$\Sigma (Sigma)$$

Sigma = np.array(np.identity(4),dtype=complex)
for i in range(4):
  Sigma[i,i] = Lambda[i]

herenp.identity(4)Represents the production of an identity matrix,dtype=complexThis matrix contains complex parts.

Sigma
array([[ 1.90910151+0.j        ,  0.        +0.j        ,
         0.        +0.j        ,  0.        +0.j        ],
       [ 0.        +0.j        , -0.16902995+0.48364592j,
         0.        +0.j        ,  0.        +0.j        ],
       [ 0.        +0.j        ,  0.        +0.j        ,
        -0.16902995-0.48364592j,  0.        +0.j        ],
       [ 0.        +0.j        ,  0.        +0.j        ,
         0.        +0.j        ,  0.29565552+0.j        ]])

This diagonal matrix $\ \ sigma $has a magical property, which is:
$$W\Sigma W^{-1} = A$$
Matrix $a can be restored$
Where $W $is the matrix composed of eigenvectors and $W ^ {- 1} $is the inverse of $W $.

np.dot(np.dot(W,Sigma),np.linalg.inv(W))
array([[0.14732831+0.j, 0.41608559+0.j, 0.3469616 +0.j, 0.2353639 +0.j],
       [0.94427989+0.j, 0.54550629+0.j, 0.4839118 +0.j, 0.67664038+0.j],
       [0.83886785+0.j, 0.10101667+0.j, 0.0850258 +0.j, 0.65483286+0.j],
       [0.73646205+0.j, 0.94155591+0.j, 0.92273078+0.j, 0.37602319+0.j]])

In addition, if the matrix $a $is multiplied by a scalar, the eigenvalue changes and the eigenvector matrix remains unchanged.

np.linalg.eig(A)
(array([ 2.04788686+0.j, -0.73295712+0.j, -0.1776856 +0.j,  0.01663945+0.j]),
 array([[ 0.27874918-0.j, -0.21558825-0.j, -0.59281181+0.j,
         -0.13927731+0.j],
        [ 0.59156016-0.j,  0.23053778-0.j,  0.11428477+0.j,
         -0.6623556 +0.j],
        [ 0.36976417-0.j,  0.70290387+0.j, -0.1166957 +0.j,
          0.70647971+0.j],
        [ 0.66002268+0.j, -0.6374168 -0.j,  0.78860336+0.j,
          0.20681712+0.j]]))
          
np.linalg.eig(3*A)
np.linalg.eig(3*A)

(array([ 6.14366059+0.j, -2.19887136+0.j, -0.53305679+0.j,  0.04991834+0.j]),
 array([[ 0.27874918-0.j, -0.21558825-0.j, -0.59281181+0.j,
         -0.13927731+0.j],
        [ 0.59156016-0.j,  0.23053778-0.j,  0.11428477+0.j,
         -0.6623556 +0.j],
        [ 0.36976417-0.j,  0.70290387+0.j, -0.1166957 +0.j,
          0.70647971+0.j],
        [ 0.66002268+0.j, -0.6374168 -0.j,  0.78860336+0.j,
          0.20681712+0.j]]))

We can see that the eigenvalue of $3A $is three times that of $a $, but the eigenvector is constant.