-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinear_algebra.py
88 lines (55 loc) · 1.8 KB
/
linear_algebra.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import math
from functools import reduce
def vector_add(v, w):
"""adds corresponding elements"""
return [v_i + w_i for v_i, w_i in zip(v, w)]
def vector_subtract(v, w):
"""subtracts corresponding elements"""
return [v_i - w_i for v_i, w_i in zip(v, w)]
"""
def vector_sum(vectors):
Sums all corresponding elements
result = vectors[0]
for vector in vectors:
result = vector_add(result, vector)
return result
"""
def vector_sum(vectors):
return reduce(vector_add, vectors)
# vector_sum = partial(reduce, vector_add)
def scalar_multiply(c, v):
"""c is a number, v is a vector"""
return [c * v_i for v_i in v]
def vector_mean(vectors):
"""Compute the vector whose ith element is the mean of the
ith elements of the input vectors"""
n = len(vectors)
return scalar_multiply(1 / n, vector_sum(vectors))
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))
def sum_of_squares(v):
"""v_1 * v_1 + ... + v_n * w_n"""
return dot(v, v)
def magnitude(v):
return math.sqrt(sum_of_squares(v))
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 magnitude(vector_subtract(v, w))
def shape(A):
num_rows = len(A)
num_cols = len(A[0] if A else 0)
return num_rows, num_cols
def get_row(A, i):
return A[i]
def get_column(A, j):
return [A_i[j] for A_i in A]
def make_matrix(num_rows, num_cols, entry_fn):
"""returns a num_rows x num_cols matrix
whose (i, j)th entry is entry_fn(i, j)"""
return [[entry_fn(i, j) for j in range(num_cols)] for i in range(num_rows)]
def is_diagonal(i, j):
"""1's on the diagonal, 0's else where"""
return 1 if i == j else 0