<jupyter_start><jupyter_text>$$\newcommand\norm[1]{\left\lVert1\right\rVert} \DeclareMathOperator{\Tr}{Tr}\newcommand\bs[1]{\boldsymbol{1}}$$     This content is part of a series following the chapter 2 on linear algebra from the [Deep Learning Book](http://www.deeplearningbook.org/) by <NAME>., <NAME>., and <NAME>. (2016). It aims to provide intuitions/drawings/python code on mathematical theories and is constructed as my understanding of these concepts. You can check the syllabus in the [introduction post](https://hadrienj.github.io/posts/Deep-Learning-Book-Series-Introduction/).  IntroductionThis 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).  2.10 The Trace Operator The trace of matrixThe trace is the sum of all values in the diagonal of a square matrix.$$\bs{A}=\begin{bmatrix}    2 & 9 & 8 \\\\    4 & 7 & 1 \\\\    8 & 2 & 5\end{bmatrix}$$$$\mathrm{Tr}(\bs{A}) = 2 + 7 + 5 = 14$$Numpy provides the function `trace()` to calculate it:<jupyter_code>A = np.array([[2, 9, 8], [4, 7, 1], [8, 2, 5]])
A
A_tr = np.trace(A)
A_tr<jupyter_output><empty_output><jupyter_text>GoodFellow et al. explain that the trace can be used to specify the Frobenius norm of a matrix (see [2.5](https://hadrienj.github.io/posts/Deep-Learning-Book-Series-2.5-Norms/)). The Frobenius norm is the equivalent of the $L^2$ norm for matrices. It is defined by:$$\norm{\bs{A}}_F=\sqrt{\sum_{i,j}A^2_{i,j}}$$Take the square of all elements and sum them. Take the square root of the result. This norm can also be calculated with:$$\norm{\bs{A}}_F=\sqrt{\Tr({\bs{AA}^T})}$$We can check this. The first way to compute the norm can be done with the simple command `np.linalg.norm()`:<jupyter_code>np.linalg.norm(A)<jupyter_output><empty_output><jupyter_text>The Frobenius norm of $\bs{A}$ is 17.549928774784245.With the trace the result is identical:<jupyter_code>np.sqrt(np.trace(A.dot(A.T)))<jupyter_output><empty_output><jupyter_text>Since the transposition of a matrix doesn't change the diagonal, the trace of the matrix is equal to the trace of its transpose:$$\Tr(\bs{A})=\Tr(\bs{A}^T)$$  Trace of a product$$\Tr(\bs{ABC}) = \Tr(\bs{CAB}) = \Tr(\bs{BCA})$$ Example 1.Let's see an example of this property.$$\bs{A}=\begin{bmatrix}    4 & 12 \\\\    7 & 6\end{bmatrix}$$$$\bs{B}=\begin{bmatrix}    1 & -3 \\\\    4 & 3\end{bmatrix}$$$$\bs{C}=\begin{bmatrix}    6 & 6 \\\\    2 & 5\end{bmatrix}$$<jupyter_code>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))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.16761565836298933, 'fraction_numerical': 0.030604982206405694, 'mean_word_length': 4.426640926640927, 'pattern_counts': {'":': 0, '<': 20, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '38330', 'license': 'MIT', 'n_tokens_mistral': 1043, 'n_tokens_neox': 956, 'n_words': 314, 'path': '2.10 The Trace Operator/2.10 The Trace Operator.ipynb', 'repo_name': 'anantmittal/deepLearningBook-Notes', 'size': '2826'}