Skip to main content

Linear Algebra with SageMath Learn math with open-source software

Section 6.4 Trace and Norm

The trace of a square matrix \(A = [a_{ij}]\) of order \(n \times n\text{,}\) denoted by \(\mathrm{tr}(A)\text{,}\) is defined as the sum of the entries along its main diagonal: \(\mathrm{tr}(A) = \sum_{i=1}^{n} a_{ii}\text{.}\)
The built-in Sage method trace() is used to compute the trace of a square matrix as shown in the example below.
Sage’s method norm() calculates different norms of a given matrix. By default, it computes the 2-norm \(L_2\) (also known as the spectral norm).
The \(L_2\) norm of a matrix \(A\) is defined as the largest singular value of \(A\text{,}\) which is the square root of the largest eigenvalue of \(A^T A\text{.}\) We can verify that the value computed by Sage is indeed the largest singular value of \(A\) as follows.

Note 6.4.1. Default Norm of Matrix in Sage.

Sage documentation states that the norm() method computes the Euclidean norm by default, whereas the actual value being returned is for the spectral norm (\(L_2\) norm). Sage documentation also implies that Euclidean norm is different from the Frobenius norm, which is not the case. In fact, the Euclidean norm and the Frobenius norm are the same and they are both different from the \(L_2\) norm.
For a matrix \(A_{n \times m}\text{,}\) the Euclidean Norm (also known as Frobenius norm or the Hilbert-Schmidt norm) is a vector-induced norm defined as the square root of the sum of the squares of all its entrees \(|A|_F = \sqrt{\sum_{i=1}^{n}\sum_{j=1}^{m} |a_{ij}|^2}\text{.}\)
Which is equivalent to the Euclidean norm of the flattened matrix as a vector in \(\mathbb{R}^{nm}\text{.}\)
To compute the Frobenius norm in Sage directly, we need to explicitly pass frob as argument to the norm() method.
The Frobenius norm can also be computed using the trace of the product of the matrix and its transpose \(|A|_F = \sqrt{\mathrm{tr(A^T A)}}\text{.}\)