Skip to main content

Section 5.5 Python recap

Eigenvalues and eigenvectors. SciPy has its own function to evaluate eigenvalues and eigenvectors of a matrix. Given a \(n\times n\) matrix A, one can fill up a vector e with the list of all eigenvalues with the command e=linalg.eigvals(A).

By using, instead, e=linalg.eig(A), then e is a matrix (i.e. an array of arrays) whose first entry is an array containing all eigenvalues while the second array is a matrix whose rows are the corresponding eigenvectors.
Norms. In this Chapter's code we evaluated explicitly the Euclidean and Taxicab norms of a vector v. We could have accomplish the same thing using SciPy's own function linalg.norm(v). In general, linalg.norm(v,ord=p) gives the \(p\)-norm
\begin{equation*} \left[|v_1|^p+\dots+|v_n|^p\right]^{(1/p)}. \end{equation*}
In particular, the taxicab norm is given by linalg.norm(v,ord=1) and the Euclidean one by linalg.norm(v,ord=2). The same function also applies to matrices. In this case, ord=2 gives the spectral norm (the highest eigenvalues modulus) while ord=1 gives the maximum sum of moduli of entries on a row of the matrix. Below we re-write the power method code using this function:

QR decomposition. SciPy can evaluate QR decompositions. The command Q,R=linalg.qr(A) gives the standard QR decomposition of a square matrix \(A\text{,}\) namely \(A=QR\text{.}\) The command Q,R,P=qr(A, pivoting=True) gives the 'column pivoting' QR decomposition of a square matrix \(A\text{.}\) In this case, \(AP=QR\text{,}\) where \(P\) is a permutation matrix. It makes sense to use a pivoted decomposition when \(A\) is close to being degenerate.