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 \(\mathbf{u} = [u_1, \dots, u_n]\) and \(\mathbf{v} = [v_1, \dots, v_n]\) is
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 \(p \ge 1\) is a real number, then the \(p\)-norm of vector \(\mathbf{x} = [x_1, \dots, x_n]\) is
For example, when \(p=2\), the 2-norm, or Euclidean norm, Frobenius norm, is
When \(p=1\), the 1-norm, or Manhattan norm, is the sum of absolute value of the components
When \(p\to \infty\), the infinity norm, or maximum norm, is the maximum value of the absolute value of the component
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 \(\mathbf{AB}\) where
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]])