forked from tiyd-python-2015-05/vectors-and-matrices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix_math.py
72 lines (46 loc) · 1.51 KB
/
matrix_math.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
import math
class ShapeException(Exception):
pass
def vector_add(a,b):
if shape(a) != shape(b):
raise ShapeException()
return [a[i] + b[i] for i in range(len(a))]
def vector_sub(a,b):
if shape(a) != shape(b):
raise ShapeException()
return [a[i] - b[i] for i in range(len(a))]
def vector_sum(*vectors):
size = len(vectors[1])
for v in vectors:
if len(v) != size:
raise ShapeException()
return [sum(i) for i in zip(*vectors)]
def dot(a, b):
if shape(a) != shape(b):
raise ShapeException()
return sum([(a[i] * b[i]) for i in range(len(a))])
def vector_multiply(a, scalar):
return [a[i] * scalar for i in range(len(a))]
def vector_mean(*vectors):
return [sum(i)/len(i) for i in zip(*vectors)]
def magnitude(a):
return math.sqrt(sum([a[i]**2 for i in range(len(a))]))
def shape(a):
if type(a[0]) == int:
return (len(a),)
else:
return (len(a),len(a[1]))
def matrix_row(a, row):
return a[row]
def matrix_col(a, column):
return [l[column] for l in a]
def matrix_scalar_multiply(a, scalar):
return[vector_multiply(i,scalar) for i in a]
def matrix_vector_multiply(matrix,vector):
if len(vector) != len(matrix[1]):
raise ShapeException()
return[dot(matrix[i], vector) for i in range(len(matrix))]
def matrix_matrix_multiply(a,b):
if len(a[0]) != len(b):
raise ShapeException()
return [[dot(a[i],matrix_col(b, j)) for j in range(len(b[0]))] for i in range(len(a))]