The Trace Operator
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns# Plot style
sns.set()
%pylab inline
pylab.rcParams['figure.figsize'] = (4, 4)Populating the interactive namespace from numpy and matplotlib
The Trace Operator¶
This chapter is very light! I can assure you that you will read it in 1 minute! It is nice after the last two chapters that were quite big! We will see what is the Trace of a matrix. It will be needed for the last chapter on the Principal Component Analysis (PCA).
The Trace Operator¶
The trace of matrixThe trace is the sum of all values in the diagonal of a square matrix.
Numpy provides the function trace() to calculate it:
A = np.array([[2, 9, 8], [4, 7, 1], [8, 2, 5]])
Aarray([[2, 9, 8],
[4, 7, 1],
[8, 2, 5]])A_tr = np.trace(A)
A_tr14GoodFellow et al. explain that the trace can be used to specify the Frobenius norm of a matrix (see 2.5). The Frobenius norm is the equivalent of the norm for matrices. It is defined by:
Take the square of all elements and sum them. Take the square root of the result. This norm can also be calculated with:
We can check this. The first way to compute the norm can be done with the simple command np.linalg.norm():
np.linalg.norm(A)17.549928774784245The Frobenius norm of is 17.549928774784245.
With the trace the result is identical:
np.sqrt(np.trace(A.dot(A.T)))17.549928774784245Since the transposition of a matrix doesn’t change the diagonal, the trace of the matrix is equal to the trace of its transpose:
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))531np.trace(C.dot(A).dot(B))531np.trace(B.dot(C).dot(A))531