Vector and matrix operations#
Teng-Jui Lin
Content adapted from UW AMATH 301, Beginning Scientific Computing, in Spring 2020.
Dot product by
@
Norm by
numpy.linalg.norm()
Matrix multiplication by
@
Dot product#
The dot product of row vectors
Problem Statement. Find the dot product of vectors
import numpy as np
import scipy
u = np.array([-9, 2, 4, 2])
v = np.array([4, 3, 1, 0])
# dot product
u@v
-26
Norms#
If
For example, when
When
When
Problem Statement. Find the 2-norm, 1-norm, and infinity norm of
# 2-norm, Frobenius norm
np.linalg.norm(u)
10.246950765959598
# 1-norm
np.linalg.norm(u, 1)
17.0
# infinity norm, maximum norm
np.linalg.norm(u, np.inf)
9.0
Matrix multiplication#
Problem Statement. Find the matrix product
A = np.array([[1, 4, 5, -1], [2, -3, 1, 3]])
B = np.array([[2, 6, 2], [6, 4, 2], [0, 2, -1], [1, 5, 6]])
# matrix multiplication
A@B
array([[ 25, 27, -1],
[-11, 17, 15]])