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 u=[u1,,un] and v=[v1,,vn] is

uv=uvT=i=1nuivi

Problem Statement. Find the dot product of vectors

u=[9242]T,v=[4310]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 p1 is a real number, then the p-norm of vector x=[x1,,xn] is

xp(i=1n|xi|p)1/p

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

x2(i=1n|xi|2)1/2=x12+x22++xn2

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

x1(i=1n|xi|1)1/1=|x1|+|x2|++|xn|

When p, the infinity norm, or maximum norm, is the maximum value of the absolute value of the component

xmaxi|xi|

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

u=[9242]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 AB where

A=[14512313],B=[262642021156]
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]])