Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Manish's Homework #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions matrix_math.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,72 @@
import math


class ShapeException(Exception):
pass


def shape(matrix):
if isinstance(matrix[0], list):
return (len(matrix), len(matrix[1]))
elif isinstance(matrix[0], int):
return (len(matrix), )


def vector_add(v1, v2):
if shape(v1) != shape(v2):
raise ShapeException
return [x + y for x, y in zip(v1, v2)]


def vector_sub(v1, v2):
if shape(v1) != shape(v2):
raise ShapeException
return [x - y for x, y in zip(v1, v2)]


def vector_sum(*args):
vec_len = len(args[1])
for vector in args:
if len(vector) != vec_len:
raise ShapeException
return [sum(i) for i in zip(*args)]


def dot(v1, v2):
if shape(v1) != shape(v2):
raise ShapeException
return sum([v1[i] * v2[i] for i in range(len(v1))])


def vector_multiply(vector, scalar):
return [idx * scalar for idx in vector]


def vector_mean(*args):
return [sum(vectors)/len(vectors) for vectors in zip(*args)]


def magnitude(vector):
return math.sqrt(sum([vector[i]**2 for i in range(len(vector))]))


def matrix_row(matrix, row):
return matrix[row]


def matrix_col(matrix, col):
return [i[col] for i in matrix]


def matrix_scalar_multiply(matrix, scalar):
pass


def matrix_vector_multiply(m1, v1):
if len(v1) != len(m1[0]):
raise ShapeException


def matrix_matrix_multiply(m1, m2):
if len(m1[0]) != len(m2):
raise ShapeException
9 changes: 0 additions & 9 deletions test_matrix_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def test_shape_vectors():
def test_vector_add():
"""
[a b] + [c d] = [a+c b+d]

Matrix + Matrix = Matrix
"""
assert vector_add(v, w) == [1, 5, 4]
Expand All @@ -51,7 +50,6 @@ def test_vector_add_checks_shapes():
def test_vector_sub():
"""
[a b] - [c d] = [a-c b-d]

Matrix + Matrix = Matrix
"""
assert vector_sub(v, w) == [1, 1, -4]
Expand Down Expand Up @@ -80,7 +78,6 @@ def test_vector_sum_checks_shapes():
def test_dot():
"""
dot([a b], [c d]) = a * c + b * d

dot(Vector, Vector) = Scalar
"""
assert dot(w, y) == 160
Expand All @@ -97,7 +94,6 @@ def test_dot_checks_shapes():
def test_vector_multiply():
"""
[a b] * Z = [a*Z b*Z]

Vector * Scalar = Vector
"""
assert vector_multiply(v, 0.5) == [0.5, 1.5, 0]
Expand All @@ -107,7 +103,6 @@ def test_vector_multiply():
def test_vector_mean():
"""
mean([a b], [c d]) = [mean(a, c) mean(b, d)]

mean(Vector) = Vector
"""
assert vector_mean(m, n) == [4, 2]
Expand All @@ -120,7 +115,6 @@ def test_vector_mean():
def test_magnitude():
"""
magnitude([a b]) = sqrt(a^2 + b^2)

magnitude(Vector) = Scalar
"""
assert magnitude(m) == 5
Expand Down Expand Up @@ -181,7 +175,6 @@ def test_matrix_scalar_multiply():
"""
[[a b] * Z = [[a*Z b*Z]
[c d]] [c*Z d*Z]]

Matrix * Scalar = Matrix
"""
assert matrix_scalar_multiply(C, 3) == [[3, 6],
Expand All @@ -194,7 +187,6 @@ def test_matrix_vector_multiply():
[[a b] * [x = [a*x+b*y
[c d] y] c*x+d*y
[e f] e*x+f*y]

Matrix * Vector = Vector
"""
assert matrix_vector_multiply(A, [2, 5, 4]) == [2, 5, 4]
Expand All @@ -215,7 +207,6 @@ def test_matrix_matrix_multiply():
[[a b] * [[w x] = [[a*w+b*y a*x+b*z]
[c d] [y z]] [c*w+d*y c*x+d*z]
[e f] [e*w+f*y e*x+f*z]]

Matrix * Matrix = Matrix
"""
assert matrix_matrix_multiply(A, B) == A
Expand Down