Skip to content

Add matrix multiplication function with validation - #13934

Closed
ArsiyaTasleem90250 wants to merge 3 commits into
TheAlgorithms:masterfrom
ArsiyaTasleem90250:patch-1
Closed

ArsiyaTasleem90250 wants to merge 3 commits into
TheAlgorithms:masterfrom
ArsiyaTasleem90250:patch-1

Conversation

@ArsiyaTasleem90250

Copy link
Copy Markdown

Implement matrix multiplication function with validation.

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Add or change doctests? -- Note: Please avoid changing both code and tests in a single pull request.
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the description above includes the issue number(s) with a closing keyword: "Fixes #ISSUE-NUMBER".

Implement matrix multiplication function with validation.
@algorithms-keeper algorithms-keeper Bot added the require descriptive names This PR needs descriptive function and/or variable names label Sep 16, 2026

@algorithms-keeper algorithms-keeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

if not _is_valid_matrix(A) or not _is_valid_matrix(B):
raise ValueError("Invalid matrix structure")

rows_A = len(A)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: rows_A

raise ValueError("Invalid matrix structure")

rows_A = len(A)
cols_A = len(A[0])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: cols_A


rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: rows_B

rows_A = len(A)
cols_A = len(A[0])
rows_B = len(B)
cols_B = len(B[0])

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: cols_B

from typing import List


def matrix_multiply(A: List[List[float]], B: List[List[float]]) -> List[List[float]]:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: A

Please provide descriptive name for the parameter: B

return result


def _is_valid_matrix(M: List[List[float]]) -> bool:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please provide descriptive name for the parameter: M

@algorithms-keeper algorithms-keeper Bot added the awaiting reviews This PR is ready to be reviewed label Sep 16, 2026
@algorithms-keeper algorithms-keeper Bot added the tests are failing Do not merge until tests pass label Sep 16, 2026
@cclauss

cclauss commented Sep 16, 2026

Copy link
Copy Markdown
Member

@priya-sundaram-dev Filename is matrix/matrix_diagonal_sum.py, but the algorithm is matrix multiplication.
Are they two different algorithms??

@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

@cclauss you're right — they're mismatched. The file is named matrix/matrix_diagonal_sum.py (and DIRECTORY.md lists it as "Matrix Diagonal Sum"), but the code inside is matrix_multiply() — plain O(n³) matrix multiplication, nothing about a diagonal sum. So the name/content don't agree.

@ArsiyaTasleem90250 two things to sort out before this can go in:

  1. It's a duplicate. We already have matrix multiplication in the repo: matrix/matrix_operation.py has multiply(matrix_a, matrix_b), and there's also matrix/matrix_multiplication_recursion.py. Per CONTRIBUTING we don't add a second implementation of an existing algorithm. If you actually meant to add a diagonal sum (that's a genuinely different, not-yet-present algorithm), rename the function to match the file and implement that instead — e.g. sum of the primary (and optionally secondary) diagonal.

  2. If you do resubmit, a few style fixes the CI will want anyway: use built-in generics (list[list[float]]) rather than typing.List, and snake_case names — A/B will trip ruff's N806, use matrix_a/matrix_b.

Happy to re-review once it's pointed at a non-duplicate algorithm. Thanks for the PR!

@cclauss cclauss closed this Sep 16, 2026
@priya-sundaram-dev

Copy link
Copy Markdown
Contributor

@cclauss good catch — yes, these look like two different things. The file is named matrix/matrix_diagonal_sum.py and the DIRECTORY.md entry says "Matrix Diagonal Sum", but the module docstring and the function (matrix_multiply) both implement matrix multiplication, not a diagonal sum. So the name and the algorithm don't match.

And matrix multiplication is already in the repo, so as written this is a duplicate:

  • matrix/matrix_operation.pymultiply(matrix_a, matrix_b)
  • matrix/matrix_multiplication_recursion.py

Two ways forward for @-the author:

  1. If the intent was a diagonal sum algorithm (which isn't in the repo yet), keep the filename and rewrite the body to actually sum the main/anti-diagonals — that would be a genuinely new algorithm.
  2. If the intent was matrix multiplication, this overlaps existing code and would likely be closed as a duplicate.

Two smaller style notes for whichever direction is taken (both will trip the ruff CI here):

  • Use built-in generics list[list[float]] instead of typing.List (repo targets 3.10+, and from typing import List is flagged).
  • Rename the uppercase locals/args (A, B, M) to lowercase (matrix_a, etc.) — ruff N803/N806 flags uppercase names.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants