Skip to main content

Basic linear algebra using numpy

Dot product

import numpy as np
x = np.array([3, 4, 1])
y = np.array([2, -3, 4])
np.dot(x, y)

Angle between two vectors:

cos(θ)=xyxy\cos(\theta) = \frac{\bf{x} \cdot \bf{y}}{|\bf{x}| |\bf{y}|}
np.dot(x, y)/(np.linalg.norm(x) * np.linalg.norm(y))

Matrix multiplication

The inner dimensions of matrices must match for multiplication. The dimension or output matrix is the outer dimensions, e.g., a 2×32 \times 3 multiplied with a 3×33 \times 3 matrix will result in a 2×32 \times 3 matrix.

Matrix multiplication is not commutative (in general): ABBAAB \neq BA

A = np.array([[1, -2, 1], [2, -4, 5]]); A
B = np.array([[3, 4, 2], [1, -2, 0], [2, 1, -3]]); B
np.matmul(A, B)

Transpose

(AB)T=BTAT(AB)^T = B^T A^T
AT = A.transpose(); AT
(np.matmul(A, B)).transpose()
np.matmul(B.transpose(), A.transpose())

If uu, v ϵ Rdv~\epsilon~\rm{R}^d; uv=uTv\bf{u} \cdot \bf{v} = u^Tv

u = np.array([3, 4, 1]); u
v = np.array([1, 5, 3]); v
np.matmul(u.transpose(), v)
np.dot(u, v)

MM is symmetric if M=MTM = M^T Diagonal matrices are symmetric.

Determinant

For a 2×22 \times 2 matrix A=(abcd)A = \begin{pmatrix}a & b\\ c & d \end{pmatrix}, det(A)=adbcdet (A) = ad -bc

For a diagonal matrix, det(A)det(A) is the product of diagonal elements.

det(AB)=det(A)det(B)det(AB) = det(A) det(B)
A = np.array([[1, 2, 4], [-2, 0, 3], [5, -1, -2]]); A
np.linalg.det(A)

Inverse

BB is inverse of AA (AA must be a square matrix) if AB=BA=IAB = BA = I

B=A1B = A^{-1}

A matrix for which inverse does not exist is called singular matrix. AA is invertible if and only if A0|A| \neq 0.

np.linalg.inv(A)

System of linear equations

The central problem of linear algebra is solving the system of linear equations. There are two main methods to solve linear equations: (1) method of elimination and (2) Cramer's rule: method of determinants.

Let us consider the case of nn linear equations with nn unknowns. In case of elimination, the multiples of first equation is subtracted from the remaining equations to eliminate the first unknown. This leaves a smaller system of (n1)(n-1) equations with (n1)(n-1) unknowns. The method is repeated until we are left with one equation with one unknown. Then we substitute the solution and find the other unknowns in reversed ordered. This method is generally used in practice to solve system of linear equation, and known as Gaussian elimination.

2x1+x2+x3=12 x_1 + x_2 + x_3 = 1 6x1+2x2+x3=16 x_1 + 2 x_2 + x_3 = -1 2x1+2x2+x3=7-2 x_1 + 2 x_2 + x_3 = 7

Matrix equation: Ax=bA\textbf{x} = \textbf{b}

Solution: x=A1b\textbf{x} = A^{-1} \textbf{b}

[211621221][x1x2x3]=[117]\begin{bmatrix}2 & 1 & 1 \\ 6 & 2 & 1 \\ -2 & 2 & 1 \end{bmatrix} \begin{bmatrix} x_1 \\ x_2 \\ x_3 \end{bmatrix} = \begin{bmatrix} 1 \\ -1 \\ 7 \end{bmatrix}
A = np.array([[2, 1, 1], [6, 2, 1], [-2, 2, 1]])
b = np.array([1, -1, 7])
x = np.linalg.solve(A, b)
print(x)

LU factorization

After the Gauss elimination, we are left with an upper triangular matrix (UU).

Ux=cU\textbf{x} = \textbf{c}

The matrix operation that relates AA and UU (b\textbf{b} and c\textbf{c}) is found to be a lower triangular matrix LL.

A=LUA = LU Ax=LUx=Lc=bA\textbf{x} = LU\textbf{x} = L\textbf{c} = \textbf{b}
import scipy.linalg as la
p, l, u = la.lu(A)
print("L=", l)
print("U=", u)
np.matmul(l, u)

Notice that the order of the rows changed and this information is contained in the permutaion matrix PP.

The diagonal elements of LL are always 1. There is another form of decomposition A=LDUA = LDU, where both LL and UU has diagonal elements as 1, and DD is the diagonal matrix of pivots. For a nonsingular matrix AA, the LULU and LDULDU factorization are unique.

Rank of a matrix is the number of pivots or the number of non-zero rows in UU. It represents the number of independent rows in the matrix. Note that the same number of columns in AA are linearly independent as well.

Projections

The projection of a vector b\textbf{b} onto a\textbf{a} is:

p=aTbaTaap = \frac{a^T b}{a^T a} a

The projection matrix P=aaTaTaP = \frac{a a^T}{a^T a} so that p=Pbp = Pb.

PP is a symmetric matrix, and P2=PP^2 = P

Eigenvalues and eigenvectors

Ax=λxA \textbf{x} = \lambda \textbf{x}

where the numbers λ\lambda are the eigenvalue of AA. x\textbf{x} are the special vectors that does not change direction after multiplied by AA.

  • det(AλI)=0\det(A - \lambda I) = 0

  • If Ax=λxA \textbf{x} = \lambda \textbf{x} then: A2x=λ2xA^2 \textbf{x} = \lambda^2 \textbf{x} and A1x=λ1xA^{-1} \textbf{x} = \lambda^{-1} \textbf{x}

  • det(A)=(λ1)(λ2)(λn)\det(A) = (\lambda_1) (\lambda_2) \cdots (\lambda_n)

Resources

  • Linear Algebra and Its Applications by Gilbert Strang.
  • Matrix Analysis and Applied Linear Algebra by Carl D. Meyer.