How to do it…

Before getting into the details of checking whether a matrix is diagonalizable or not, let us see how to convert any 1D array into a diagonal matrix.

  1. Import the relevant packages:
import numpy as np 
  1. Initialize a 1D matrix:
a= np.array([1,2,3]) 
  1. Diagonalize the matrix:
np.diag(a)
  1. The output of the preceding code is:
array([[1, 0, 0],       [0, 2, 0],       [0, 0, 3]]) 

You should note that all the elements within the one-dimensional array form the diagonal of the output matrix generated by the diag function within the numpy package. Moreover, the other elements of the output array (apart from the diagonal elements) are 0. In the following code, we will look into checking whether a matrix is diagonalizable or not.

As discussed earlier, a matrix A is diagonalizable if there exists another matrix P that satisfies the following condition:

In this example, we will consider the eigenvector of the given matrix as our P matrix.

  1. We can calculate the P matrix as follows: 
la, v = linalg.eig(a)
print(la)
print(v)
[ 3.+0.j -1.+0.j -3.+0.j]
[[ 0.70710678 -0.70710678 0. ]
[ 0.70710678 0.70710678 0. ]
[ 0. 0. 1. ]]

As seen earlier in the eigenvector calculation, the matrix v would be the eigenvector (which is the P matrix for us). 

  1. Once we have the P matrix, P−1AP  is calculated as follows:
np.dot(np.dot(np.linalg.inv(v),a),v) 

The output of preceding code is: 

array([[  3.00000000e+00,   5.05623488e-16,   0.00000000e+00],      
[ 6.57231935e-16, -1.00000000e+00, 0.00000000e+00],
[ 0.00000000e+00, 0.00000000e+00, -3.00000000e+00]])
  1. Diagonalizing the eigenvalues of our matrix is done as follows: 
np.diag(la) 

The output of preceding code will be: 

array([[ 3.+0.j,  0.+0.j,  0.+0.j],      
[ 0.+0.j, -1.+0.j, 0.+0.j],
[ 0.+0.j, 0.+0.j, -3.+0.j]])
  1. Compare the difference between the outputs in step 2 and step 3:
np.dot(np.dot(np.linalg.inv(v),a),v)-np.diag(la) 
array([[ -4.44089210e-16+0.j, 5.05623488e-16+0.j, 0.00000000e+00+0.j],
[ 6.57231935e-16+0.j, -3.33066907e-16+0.j, 0.00000000e+00+0.j],
[ 0.00000000e+00+0.j, 0.00000000e+00+0.j, 0.00000000e+00+0.j]])

In the preceding calculation, note that the difference between the two outputs is very small and, hence, satisfies our originally laid out objective; P−1AP  is a diagonal matrix.

Note that the diagonal elements within the diagonal matrix are the eigenvalues of the original matrix.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset