In linear algebra, the trace of an n-by-n square matrix A is defined to be the sum of the elements on the main diagonal (the diagonal from the upper left to the lower right) of A.
<img src="../images/traceofmat.png", style="width:50%;">
The sum of Eigen Values corresponds to the trace of the matrix.
In Linear Algebra, determinant of a matrix is a special number that can be calculated from a square matrix.
For a 2×2 matrix:
A=$\left[\begin{array}{cc}a & b \\c & d\\\end{array}\right]$
The determinant is found by,
**|A| = (a * d) − (b * c)**
For a 3x3 matrix:
A=$\left[\begin{array}{cc}a & b & c \\d & e &f\\ g &h&i\\\end{array}\right]$ The determinant is found by, **|A| = a((e * i) − (f * h)) − b((d * i) − (f * g)) + c((d * h) − (e * g))**
Determinant of a matrix can also be found by product of its eigenvalues.
Find determinent of a matrix using "numpy.linalg.det" in python and store its value in variable called d.
a = np.array([[5, 4], [1, 2]])
Use the np.linalg.det function in numpy module and pass the array a as an argument to find out the determinant of a
b=np.linalg.det(a)
b
6.0
ref_tmp_var = False
try:
import numpy as np
if np.all(b == 6.0) :
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions. ')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions. ')
assert ref_tmp_var
continue
Decomposition means breaking of a large entity into smaller ones. Similarly, matrix decompositon or matrix factorization means decomposing or factorizing a large matrix into smaller ones. The usefullness of decompostion is that it helps us in learning about some special properties of the matrix which we couldn't have done if we hadn't decomposed it. Here we will talk about decomposition of a matrix into Eigen Vectors and Eigen Values
In practical applications, many problems are presented in the form of Eigenvalue problem:
A·v = λ·v
In this equation A is an n-by-n matrix, v is a non-zero n-by-1 vector and λ is a scalar (which may be either real or complex). Any value of λ for which this equation has a solution is known as an eigenvalue of the matrix A. It is sometimes also called the characteristic value. The vector, v, which corresponds to this value is called an eigenvector. The eigenvalue problem can be rewritten as:
(A·v) - (λ·v) = 0
|A - (λ·I)| = 0
This equation is called the characteristic equation of A, and is an nth order polynomial in λ with n roots.
|A - (λ·I)| = (λ1-λ)(λ2-λ)…(λn-λ)
These roots are called the eigenvalues of A. For each eigenvalue there will be an eigenvector for which the eigenvalue equation is true. In python we can find eigen values and eigen vectors using "linalg" package from scipy.
As seen in the topics above, Eigen Decomposition is the factorization of a matrix into a canonical form, whereby the matrix is represented in terms of its eigenvalues and eigenvectors.
Lets see an example:
Let's see the eigendecomposition for the matrix:
A=$\left[ \begin{array}{cccc} 1 & 1 \\ 0 & 3 \\ \end{array} \right]$
From above we can find determinant of the matrix as:
det$\left( \begin{array}{cccc} 1-\lambda & 0 \\ 1 & 3-\lambda \\ \end{array} \right)$
(1−λ)(3−λ)=0
we get directly λ1 =1 and λ2=3. The above expression is usually referred as the characteristic polynomial or characteristic equation of a matrix.
Putting λ1 in the equation, we get:
$\left[\begin{array}{cc}1 & 0\\1 & 3\\\end{array}\right]\left[\begin{array}{c}v_{11}\\v_{12}\\\end{array}\right]$ = 1 $\left[\begin{array}{c}v_{11}\\v_{12}\\\end{array}\right]$
from which we get
v11=−2v12.
That is, any vector v1=[v11,v12] where v11=−2v12 is an eigenvector of A with eigenvalue 1. Putting λ2 into equation, we get:
$\left[\begin{array}{cc}1 & 0\\1 & 3\\\end{array}\right]\left[\begin{array}{c}v_{21}\\v_{22}\\\end{array}\right]= 3 \left[\begin{array}{c}v_{21}\\v_{22}\\\end{array}\right]$
from which we get v_21 = 0 and v_22 ∈R . That is, any vector v_2 = [v_21, v_22] where v_21 = 0 is an eigenvector of A with eigenvalue 3.
From our previous example, we can use eigen values and eigen vectors, join them in a single matrix equation.
A$\left[\mathbf{v_1 v_2}\right]$ = $\left[\begin{array}{cc}1 & 0\\1 & 3\\\end{array}\right]$ $\left[\begin{array}{cc}v_{11} & v_{21}\\ v_{12} & v_{22}\\\end{array}\right]$ = $\left[\begin{array}{cc}v_{11} & v_{21}\\v_{12} & v_{22}\\\end{array}\right]$ $\left[\begin{array}{cc}\lambda_1 & 0\\0 & \lambda_2\\\end{array}\right]$ = $\left[\mathbf{v_1 v_2}\right]$ $\left[\begin{array}{cc}\lambda_1 & 0\\0 & \lambda_2\\\end{array}\right]$
If we replace the value,
λ=$\left[\begin{array}{cc}\lambda_1 & 0\\0 & \lambda_2\\\end{array}\right]$
V = $\left[\mathbf{v_1 v_2}\right]$
it becomes,
AV=VΛ
A=VΛV−1
Eigendecomposition decomposes a matrix A into a multiplication of a matrix of eigenvectors V and a diagonal matrix of eigenvalues Λ as A=VΛV−1
If all eigenvalues are positive, then the matrix is positive definite. If all eigenvalues are positive or zero-valued, then the matrix is positive semi-definite. Similar is the case negative definite and negative semi-definite. There are benefits to knowing that a matrix is positive definite, positive semi-definite, negative definite and negative semi-definite. Many useful facts can be gather using this -
Eigen decomposition can be performed on a matrix using the 'linalg.eig' function from linear algebra sub-module from scipy library. The linalg.eig function returns two outputs - the eigen values of the input matrix and the right eigen vectors of the matrix. Hence, the function output is generally captured by using two variables, For e.g., B,C = linalg.eig(A). In this way, the eigen values of Matrix A are assigned to variable B and right eigen vectors are assigned to variable C.
Given matrix $A=\left[\begin{array}{cc}2 & 3\\4 & 6\\\end{array}\right]$.
from scipy import linalg as LA
import numpy as np
A = np.array([2,3,4,6]).reshape(2,2)
B, C= LA.eig(A)
print("Eigen Values: ",B,"\nEigen Vectors: ",C)
Eigen Values: [ 0.+0.j 8.+0.j] Eigen Vectors: [[-0.83205029 -0.4472136 ] [ 0.5547002 -0.89442719]]
ref_tmp_var = False
try:
import numpy as np
if np.all(B == [ 0.+0.j , 8.+0.j]) :
ref_assert_var = True
ref_tmp_var = True
else:
ref_assert_var = False
print('Please follow the instructions given and use the same variables provided in the instructions. ')
except Exception:
print('Please follow the instructions given and use the same variables provided in the instructions. ')
assert ref_tmp_var
continue