Skip to content

Fixed error in matrix multiplication that didn't multiple non-square … #898

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

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
15 changes: 11 additions & 4 deletions matrix/matrix_multiplication_addition.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@ def scalarMultiply(matrix , n):

def multiply(matrix_a, matrix_b):
matrix_c = []
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

with numpy arrays, no need to define matrix_c at the beginning of the function. Also, since I can't access the beginning of this module would need to add "import numpy as np".

Below is a suggestion to reduce the number of code lines. Maybe I'm missing the point of this module, but using numpy would be a lot more efficient. I know the rest of the doc has no docstring, but I figured I would add one in the suggestion.

You can the entire function with just 8 lines of code.

Suggested change
matrix_c = []
"""
:param matrix_a: matrix as a list or array, 1st matrix that defines #columns for matrix rule [a1 x a2]
:type matrix_a: list of int or list of float
:param matrix_b: matrix as a list or array, 2nd matrix that defines #rows for matrix rule [b1 x b2]
:type matrix_b: list of int or list of float
:return: numpy array [a1 x b2]
"""
matrix_a = np.array(matrix_a)
matrix_b = np.array(matrix_b)
try:
matrix_c = np.dot(matrix_a, matrix_b)
return matrix_c
except ValueError:
raise

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definitely works. In fact, a lot of stuff in this project can be done using in-built/library functions, but I think the idea of this project is to show how the core algorithms work.

Using numpy.dot would definitely make it work in lesser lines of code, and possibly even be faster, but that level of abstraction would hide what exactly is happening.

I may be wrong though, and if what we're going for is smaller code and speed, your suggestion would work (Although I think numpy.matmul would be a better choice)

Copy link
Contributor

@StephenGemin StephenGemin Jun 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That makes sense. You're right on both points ☺. Best leave your initial commit than.

I never heard of numpy.matmul, now I know.
https://www.numpy.org/devdocs/reference/generated/numpy.dot.html

TLDR: If both a and b are 2-D arrays, it is matrix multiplication, but using matmul or a @ b is preferred.

Appreciate the feedback!

n = len(matrix_a)
for i in range(n):
num_rows_a = len(matrix_a)
num_cols_a = len(matrix_a[0])
num_rows_b = len(matrix_b)
num_cols_b = len(matrix_b[0])

if num_cols_a != num_rows_b :
raise ValueError('Cannot multiply matrix of dimensions {},{} and {},{}'.format(num_rows_a,num_cols_a,num_rows_b,num_cols_b))

for i in range(num_rows_a):
list_1 = []
for j in range(n):
for j in range(num_cols_b):
val = 0
for k in range(n):
for k in range(num_rows_a):
val = val + matrix_a[i][k] * matrix_b[k][j]
list_1.append(val)
matrix_c.append(list_1)
Expand Down