Vectors

A vector is just a point in some finite-dimensional space.

In Python, we can represent a vector as a list of numbers:


In [2]:
height_weight_age = [70, # inches
                     170, # pounds
                     40] # years
 
grades = [95, # exam1
          80, # exam2
          75, # exam3
          62] # exam4

Now we will build functions to perform vector arithmetic. Vectors add componentwise meaning they must line up side-by-side and elements are added to adjacent elements.


In [3]:
def vector_add(v, w):
    """adds two vectors together v + w"""
    return [v_i + w_i for v_i, w_i in zip(v, w)]

vector_add(grades, grades)


Out[3]:
[190, 160, 150, 124]

In [4]:
def vector_subtract(v, w):
    """subtracts one vector from another v - w"""
    return [v_i - w_i for v_i, w_i in zip(v, w)]

vector_subtract(grades, grades)


Out[4]:
[0, 0, 0, 0]

In [5]:
def vector_sum(vectors):
    """sums a list of vectors"""
    vectors = list(vectors)
    result = vectors[0]
    for vector in vectors[1:]:
        result = vector_add(result, vector)
    return result

vector_sum([grades, grades, grades])


Out[5]:
[285, 240, 225, 186]

In [6]:
def scalar_multiply(c, v):
    """multiply elements in vector v by scalar c"""
    return [c * v_i for v_i in v]

scalar_multiply(1.5, grades)


Out[6]:
[142.5, 120.0, 112.5, 93.0]

Dot Product: the sum of two vectors' componentwise products; measures how far vector v extends in the w direction.


In [7]:
def dot(v, w):
    """v_1 * w_1 + ... v_n * w_n"""
    return sum(v_i * w_i for v_i, w_i in zip(v, w))

dot(grades, grades)


Out[7]:
24894

In [8]:
def sum_of_squares(v):
    """v_1 * v_1 + ... + v_n * v_n"""
    return dot(v, v)

sum_of_squares(grades)


Out[8]:
24894

In [9]:
import math

def magnitude(v):
    return math.sqrt(sum_of_squares(v))

magnitude(grades)


Out[9]:
157.77832550765646

We can now use the above to compute the distance between two vectors: $$\sqrt{(v_i - w_1)^2 + ... + (v_n - w_n)^2}$$


In [10]:
def squared_distance(v, w):
    """(v_1 - w_1) ** 2 + ... + (v_n - w_n) ** 2"""
    return sum_of_squares(vector_subtract(v, w))

def distance(v, w):
    return math.sqrt(squared_distance(v, w))

distance(grades, [90, 75, 70, 60])


Out[10]:
8.888194417315589

Matrices

A matrix is a two-dimensional list of numbers.


In [11]:
# 2 x 3 matrix
A = [[1, 2, 3],
     [4, 5, 6]]
# 3 x 2 matrix
B = [[1, 2],
     [3, 4],
     [5, 6]]

In [12]:
def shape(A):
    num_rows = len(A)
    num_cols = len(A[0]) if A else 0
    return num_rows, num_cols

shape(A), shape(B)


Out[12]:
((2, 3), (3, 2))

In [13]:
def get_row(A, i):
    return A[i]

def get_column(A, j):
    return [A_i[j] for A_i in A]

get_row(A, 1), get_column(A, 1)


Out[13]:
([4, 5, 6], [2, 5])

In [14]:
def make_matrix(num_rows, num_cols, entry_fn):
    """returns a num_rows x num_cols matrix"""
    return [[entry_fn(i, j) for j in range(num_cols)] for i in range(num_rows)]

make_matrix(2, 2, lambda i, j: i + j)


Out[14]:
[[0, 1], [1, 2]]

In [15]:
def diagonal(i, j):
    """given row i and col j, returns 1 for diagonal, 0 otherwise"""
    return 1 if i == j else 0

make_matrix(5, 5, diagonal)


Out[15]:
[[1, 0, 0, 0, 0],
 [0, 1, 0, 0, 0],
 [0, 0, 1, 0, 0],
 [0, 0, 0, 1, 0],
 [0, 0, 0, 0, 1]]

In [ ]: