--- engine: julia --- ```{julia} #| error: false #| echo: false #| output: false using InteractiveUtils ``` # Linear Algebra in Julia ```{julia} using LinearAlgebra ``` The `LinearAlgebra` package provides, among other things: - additional subtypes of `AbstractMatrix` which are usable like other matrices, among them: - `Tridiagonal` - `SymTridiagonal` - `Symmetric` - `UpperTriangular` - additional/extended functions: `norm`, `opnorm`, `cond`, `inv`, `det`, `exp`, `tr`, `dot`, `cross`, ... - a universal solver for systems of linear equations: `\` - `x = A \ b` solves $A \mathbf{x}=\mathbf{b}$ by appropriate matrix factorization and forward/backward substitution - [Matrix factorizations](https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#man-linalg-factorizations) - `LU` - `QR` - `Cholesky` - `SVD` - ... - Computation of eigenvalues/eigenvectors - `eigen`, `eigvals`, `eigvecs` - Access to BLAS/LAPACK functions ## Special Matrix Types ```{julia} A = SymTridiagonal(fill(1.0, 4), fill(-0.3, 3)) ``` ```{julia} B = UpperTriangular(A) ``` These types are stored space-efficiently. The usual arithmetic operations are implemented: ```{julia} A + B ``` Read-only index access is possible, ```{julia} A[1,4] ``` Write operations are not necessarily possible: ```{julia} #| error: true A[1,3] = 17 ``` Conversion to a 'normal' matrix is possible using `collect()`: ```{julia} A2 = collect(A) ``` ### The Identity Matrix `I` `I` denotes an identity matrix (square, diagonal elements = 1, all others = 0) of the required size ```{julia} A + 4I ``` ## Norms To study questions such as conditioning or convergence of an algorithm, we need a metric. For linear spaces, it is appropriate to define the metric via a norm: $$ d(x,y) := \lVert x-y\rVert $$ ### $p$-Norms A simple class of norms in $ℝ^n$ are the $p$-norms $$ \lVert \mathbf{x} \rVert_p = \left(\sum |x_i|^p\right)^\frac{1}{p}, $$ which generalize the Euclidean norm $p=2$. :::{.callout-note} ## The Max-Norm $p=\infty$ Let $x_{\text{max}}$ be the component of $\mathbf{x}\in ℝ^n$ with the largest absolute value. Then always $$ \lvert x_{\text{max}}\rvert \le \lVert\mathbf{x}\rVert_p \le n^\frac{1}{p} \lvert x_{\text{max}}\rvert $$ (Consider a vector whose components are all equal to $x_{\text{max}}$ respectively a vector whose components are all equal to zero except $x_{\text{max}}$.) It follows that $$ \lim_{p \rightarrow \infty} \lVert\mathbf{x}\rVert_p = \lvert x_{\text{max}}\rvert =: \lVert\mathbf{x}\rVert_\infty. $$ ::: In Julia, the `LinearAlgebra` package defines a function `norm(v, p)`. ```{julia} v = [3, 4] w = [-1, 2, 33.2] @show norm(v) norm(v, 2) norm(v, 1) norm(v, 4) norm(w, Inf); ``` - If the 2nd argument `p` is missing, `p=2` is set. - The 2nd argument can also be `Inf` (i.e., $+\infty$). - The 1st argument can be any numerical container. The sum $\sum \lvert x_i\rvert^p$ extends over *all* elements of the container. - Thus, for a matrix `norm(A)` is equal to the _Frobenius norm_ of the matrix `A`. ```{julia} A = [1 2 3 4 5 6 7 8 9] norm(A) # Frobenius norm ``` Since norms are homogeneous under multiplication with scalars, $\lVert\lambda \mathbf{x}\rVert = |\lambda|\cdot\lVert\mathbf{x}\rVert$, they are completely determined by the specification of the unit ball. Subadditivity of the norm (triangle inequality) is equivalent to the convexity of the unit ball (code visible by clicking). ```{julia} #| code-fold: true #| fig-cap: "Unit balls in $ℝ^2$ for different $p$-norms: $p$=0.8; 1; 1.5; 2; 3.001 and 1000" using Plots colors=[:purple, :green, :red, :blue,:aqua, :black] x=LinRange(-1, 1, 1000) y=LinRange(-1, 1, 1000) fig1=plot() for p ∈ (0.8, 1, 1.5, 2, 3.001, 1000) contour!(x,y, (x,y) -> p * norm([x, y], p), levels=[p], aspect_ratio=1, cbar=false, color=[pop!(colors)], contour_labels=true, ylim=[-1.1, 1.1]) end fig1 ``` We see that, $p\ge 1$ must hold so that the unit ball is convex and $\lVert.\rVert_p$ is a norm. However, the Julia function `norm(v, p)` also works for parameters `p<1`. ### Induced Norms (Operator Norms) Matrices $A$ represent linear mappings $\mathbf{v}\mapsto A\mathbf{v}$. The matrix norm induced by a vector norm answers the question: > _"By what factor can a vector be maximally stretched by the transformation $A$?"_ Due to the homogeneity of the norm under multiplication with scalars, it is sufficient to consider the image of the unit ball under the transformation $A$. ::: {.callout-tip} ## Definition Let $V$ be a vector space with dimension $0