1. What is the trace of a matrix?
The trace is the sum of all values on the diagonal of a square matrix. Principal Component Analysis (PCA) will use it.
Below is such a matrix A. Then its trace is 2+7+5=14.
Numpy provides the function trace() to calculate it:
A = np.array([[2, 9, 8], [4, 7, 1], [8, 2, 5]])
A_tr = np.trace(A)
Get 14.
2. Properties of L2 norm and trace
Specifies the Frobenius norm of the matrix. The Frobenius norm is equivalent to the L2 norm of a matrix (discussed in the previous chapter).
It is defined as:
It can also be calculated by this formula:
test with python
np.linalg.norm(A)
You get 17.549928774784245, which means that the L2 norm of A is 17.549928774784245.
Use traces to calculate
np.sqrt(np.trace(A.dot(A.T)))
The result is 17.549928774784245.
Property 1: Since the transpose of a matrix does not change the diagonal, the trace of a matrix is equal to the trace of its transpose:
Property 2:
3. Examples
There are the following three matrices
Calculate the dot product of three matrices
A = np.array([[4, 12], [7, 6]])
B = np.array([[1, -3], [4, 3]])
C = np.array([[6, 6], [2, 5]])
np.trace(A.dot(B).dot(C))
np.trace(C.dot(A).dot(B))
np.trace(B.dot(C).dot(A))
All traces are 531.