Skip to content

Latest commit

 

History

History
129 lines (92 loc) · 2.94 KB

08-linear-algebra.md

File metadata and controls

129 lines (92 loc) · 2.94 KB

1.8 Linear Algebra Refresher

Slides

Notes

Linear Algebra Refresher

  • Vector operations
  • Multiplication
    • Vector-vector multiplication
    • Matrix-vector multiplication
    • Matrix-matrix multiplication
  • Identity matrix
  • Inverse

Vector operations

u = np.array([2, 7, 5, 6])
v = np.array([3, 4, 8, 6])

# addition 
u + v

# subtraction 
u - v

# scalar multiplication 
2 * v

Multiplication

Vector-vector multiplication
def vector_vector_multiplication(u, v):
    assert u.shape[0] == v.shape[0]
    
    n = u.shape[0]
    
    result = 0.0

    for i in range(n):
        result = result + u[i] * v[i]
    
    return result
Matrix-vector multiplication
def matrix_vector_multiplication(U, v):
    assert U.shape[1] == v.shape[0]
    
    num_rows = U.shape[0]
    
    result = np.zeros(num_rows)
    
    for i in range(num_rows):
        result[i] = vector_vector_multiplication(U[i], v)
    
    return result
Matrix-matrix multiplication
def matrix_matrix_multiplication(U, V):
    assert U.shape[1] == V.shape[0]
    
    num_rows = U.shape[0]
    num_cols = V.shape[1]
    
    result = np.zeros((num_rows, num_cols))
    
    for i in range(num_cols):
        vi = V[:, i]
        Uvi = matrix_vector_multiplication(U, vi)
        result[:, i] = Uvi
    
    return result

Identity matrix

I = np.eye(3)

Inverse

V = np.array([
    [1, 1, 2],
    [0, 0.5, 1], 
    [0, 2, 1],
])
inv = np.linalg.inv(V)

Add notes here (PRs are welcome).

⚠️ The notes are written by the community.
If you see an error here, please create a PR with a fix.

Links

Navigation