Basic linear algebra using numpy
Dot product
- Input
- Output
import numpy as np
x = np.array([3, 4, 1])
y = np.array([2, -3, 4])
np.dot(x, y)
-2
Angle between two vectors:
- Input
- Output
np.dot(x, y)/(np.linalg.norm(x) * np.linalg.norm(y))
-0.07283570407292297
Matrix multiplication
The inner dimensions of matrices must match for multiplication. The dimension or output matrix is the outer dimensions, e.g., a multiplied with a matrix will result in a matrix.
Matrix multiplication is not commutative (in general):
- Input
- Output
A = np.array([[1, -2, 1], [2, -4, 5]]); A
array([[ 1, -2, 1],
[ 2, -4, 5]])
- Input
- Output
B = np.array([[3, 4, 2], [1, -2, 0], [2, 1, -3]]); B
array([[ 3, 4, 2],
[ 1, -2, 0],
[ 2, 1, -3]])
- Input
- Output
np.matmul(A, B)
array([[ 3, 9, -1],
[ 12, 21, -11]])
Transpose
- Input
- Output
AT = A.transpose(); AT
array([[ 1, 2],
[-2, -4],
[ 1, 5]])
- Input
- Output
(np.matmul(A, B)).transpose()
array([[ 3, 12],
[ 9, 21],
[ -1, -11]])
- Input
- Output
np.matmul(B.transpose(), A.transpose())
array([[ 3, 12],
[ 9, 21],
[ -1, -11]])
If , ;
- Input
- Output
u = np.array([3, 4, 1]); u
array([3, 4, 1])
- Input
- Output
v = np.array([1, 5, 3]); v
array([1, 5, 3])
- Input
- Output
np.matmul(u.transpose(), v)
26
- Input
- Output
np.dot(u, v)
26
is symmetric if Diagonal matrices are symmetric.
Determinant
For a matrix ,
For a diagonal matrix, is the product of diagonal elements.
- Input
- Output
A = np.array([[1, 2, 4], [-2, 0, 3], [5, -1, -2]]); A
array([[ 1, 2, 4],
[-2, 0, 3],
[ 5, -1, -2]])
- Input
- Output
np.linalg.det(A)
33.000000000000014
Inverse
is inverse of ( must be a square matrix) if
A matrix for which inverse does not exist is called singular matrix. is invertible if and only if .
- Input
- Output
np.linalg.inv(A)
array([[ 9.09090909e-02, -2.22044605e-17, 1.81818182e-01],
[ 3.33333333e-01, -6.66666667e-01, -3.33333333e-01],
[ 6.06060606e-02, 3.33333333e-01, 1.21212121e-01]])
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 linear equations with 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 equations with 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.
Matrix equation:
Solution:
- Input
- Output
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)
[-1. 2. 1.]
LU factorization
After the Gauss elimination, we are left with an upper triangular matrix ().
The matrix operation that relates and ( and ) is found to be a lower triangular matrix .
- Input
- Output
import scipy.linalg as la
p, l, u = la.lu(A)
print("L=", l)
print("U=", u)
L= [[ 1. 0. 0. ]
[-0.33333333 1. 0. ]
[ 0.33333333 0.125 1. ]]
U= [[6. 2. 1. ]
[0. 2.66666667 1.33333333]
[0. 0. 0.5 ]]
- Input
- Output
np.matmul(l, u)
array([[ 6., 2., 1.],
[-2., 2., 1.],
[ 2., 1., 1.]])
Notice that the order of the rows changed and this information is contained in the permutaion matrix .
The diagonal elements of are always 1. There is another form of decomposition , where both and has diagonal elements as 1, and is the diagonal matrix of pivots. For a nonsingular matrix , the and factorization are unique.
Rank of a matrix is the number of pivots or the number of non-zero rows in . It represents the number of independent rows in the matrix. Note that the same number of columns in are linearly independent as well.
Projections
The projection of a vector onto is:
The projection matrix so that .
is a symmetric matrix, and
Eigenvalues and eigenvectors
where the numbers are the eigenvalue of . are the special vectors that does not change direction after multiplied by .
-
-
If then: and
-
Resources
- Linear Algebra and Its Applications by Gilbert Strang.
- Matrix Analysis and Applied Linear Algebra by Carl D. Meyer.