Skip to content

Instantly share code, notes, and snippets.

@Kwisses
Created January 10, 2018 04:47
Show Gist options
  • Save Kwisses/743286331d98a061b7e143a8ee59fdea to your computer and use it in GitHub Desktop.
Save Kwisses/743286331d98a061b7e143a8ee59fdea to your computer and use it in GitHub Desktop.
Matrix Multiplication - [Python 3.5]
"""Multiply two matrices using helper functions; outputs resulting matrix."""
def get_row(matrix, row):
return matrix[row]
def get_column(matrix, column_number):
column = []
for i in range(len(matrix)):
column.append(matrix[i][column_number])
return column
def dot_product(vector_one, vector_two):
total = 0
if len(vector_one) != len(vector_two):
return total
for i in range(len(vector_one)):
product = vector_one[i] * vector_two[i]
total += product
return total
def matrix_multiplication(matrix_one, matrix_two):
m_rows = len(matrix_one)
p_columns = len(matrix_two[0])
result = []
for i in range(m_rows):
row_result = []
for j in range(p_columns):
row = get_row(matrix_one, i)
column = get_column(matrix_two, j)
product = dot_product(row, column)
row_result.append(product)
result.append(row_result)
return result
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment