Vector and matrix operations#

Teng-Jui Lin

Content adapted from UW AMATH 301, Beginning Scientific Computing, in Spring 2020.

Dot product#

The dot product of row vectors \(\mathbf{u} = [u_1, \dots, u_n]\) and \(\mathbf{v} = [v_1, \dots, v_n]\) is

\[ \mathbf{u}\cdot\mathbf{v} = \mathbf{u}\mathbf{v}^T = \sum\limits_{i=1}^n u_iv_i \]

Problem Statement. Find the dot product of vectors

\[ \mathbf{u} = \begin{bmatrix} -9 & 2 & 4 & 2 \end{bmatrix}^T, \mathbf{v} = \begin{bmatrix} 4 & 3 & 1 & 0 \end{bmatrix}^T \]
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

\[ \Vert\mathbf{x}\Vert_p \equiv \left( \sum\limits_{i=1}^n |x_i|^p \right)^{1/p} \]

For example, when \(p=2\), the 2-norm, or Euclidean norm, Frobenius norm, is

\[ \Vert\mathbf{x}\Vert_2 \equiv \left( \sum\limits_{i=1}^n |x_i|^2 \right)^{1/2} = \sqrt{x_1^2 + x_2^2 + \cdots + x_n^2} \]

When \(p=1\), the 1-norm, or Manhattan norm, is the sum of absolute value of the components

\[ \Vert\mathbf{x}\Vert_1 \equiv \left( \sum\limits_{i=1}^n |x_i|^1 \right)^{1/1} = |x_1| + |x_2| + \cdots + |x_n| \]

When \(p\to \infty\), the infinity norm, or maximum norm, is the maximum value of the absolute value of the component

\[ \Vert\mathbf{x}\Vert_\infty \equiv \max\limits_i |x_i| \]

Problem Statement. Find the 2-norm, 1-norm, and infinity norm of

\[ \mathbf{u} = \begin{bmatrix} -9 & 2 & 4 & 2 \end{bmatrix}^T \]
# 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

\[\begin{split} \mathbf{A} = \begin{bmatrix} 1 & 4 & 5 & -1 \\ 2 & -3 & 1 & 3 \end{bmatrix}, \mathbf{B} = \begin{bmatrix} 2 & 6 & 2 \\ 6 & 4 & 2 \\ 0 & 2 & -1 \\ 1 & 5 & 6 \\ \end{bmatrix} \end{split}\]
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]])