-
-
Notifications
You must be signed in to change notification settings - Fork 50k
Implement Strassen's matrix multiplication algorithm #13755
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
base: master
Are you sure you want to change the base?
Conversation
This file implements Strassen's matrix multiplication algorithm, which is faster than the standard O(n^3) method for large matrices. It includes helper functions for matrix operations and benchmarks the performance of Strassen's algorithm against standard multiplication.
Implement Strassen's matrix multiplication algorithm
There was a problem hiding this comment.
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 reviewto trigger the checks for only added pull request files@algorithms-keeper review-allto 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.
| """ | ||
|
|
||
| # type Matrix = list[list[int]] # psf/black currently fails on this line | ||
| Matrix = list[list[int]] |
There was a problem hiding this comment.
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: Matrix
| return result | ||
|
|
||
|
|
||
| def pad_matrix(matrix: Matrix, target_size: int) -> Matrix: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file matrix/strassen.py, please provide doctest for the function pad_matrix
| return padded_matrix | ||
|
|
||
|
|
||
| def unpad_matrix(matrix: Matrix, original_size: int) -> Matrix: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As there is no test file in this pull request nor any test function or class in the file matrix/strassen.py, please provide doctest for the function unpad_matrix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull request overview
Adds a new matrix multiplication implementation using Strassen’s divide-and-conquer algorithm (with padding to power-of-two sizes), including helper utilities, doctests, and an optional benchmark.
Changes:
- Introduces
strassen()with recursive Strassen multiplication and automatic zero-padding/unpadding for non-power-of-two sizes. - Adds supporting helpers (add/subtract, split/combine, pad/unpad) plus sample matrices for doctests.
- Provides a
__main__doctest runner and a small benchmark comparing against iterative multiplication.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if not matrix_a or not matrix_b: | ||
| return [] | ||
|
|
||
| if not ( | ||
| len(matrix_a) == len(matrix_b) and is_square(matrix_a) and is_square(matrix_b) | ||
| ): | ||
| raise ValueError("Matrices must be square and of the same dimensions") |
Copilot
AI
Jan 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
strassen() returns [] whenever either input is empty (if not matrix_a or not matrix_b). This means calls like strassen([], [[1]]) silently return an empty result even though the matrices are not the same dimensions and cannot be multiplied. Either tighten the condition to only treat the ( [], [] ) case as a valid empty multiply (and raise ValueError otherwise), or explicitly document/test that any empty input returns [] despite dimension mismatch.
| def split_matrix(matrix: Matrix) -> tuple[Matrix, Matrix, Matrix, Matrix]: | ||
| """ | ||
| Splits a given matrix into four equal quadrants. | ||
| >>> a, b, c, d = split_matrix(matrix_count_up) | ||
| >>> a | ||
| [[1, 2], [5, 6]] | ||
| >>> b | ||
| [[3, 4], [7, 8]] | ||
| >>> c | ||
| [[9, 10], [13, 14]] | ||
| >>> d | ||
| [[11, 12], [15, 16]] | ||
| """ | ||
| n = len(matrix) // 2 | ||
| a11 = [row[:n] for row in matrix[:n]] | ||
| a12 = [row[n:] for row in matrix[:n]] | ||
| a21 = [row[:n] for row in matrix[n:]] | ||
| a22 = [row[n:] for row in matrix[n:]] | ||
| return a11, a12, a21, a22 |
Copilot
AI
Jan 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
split_matrix() claims it splits into four equal quadrants, but for odd-sized matrices it will produce uneven quadrant sizes (because it uses n = len(matrix) // 2 without validation). Since this is a public helper in the module, either validate that the input dimension is even (or power-of-two) and raise a clear error, or update the docstring to state the precondition explicitly.
| next_power_of_2 = 1 << n.bit_length() | ||
| a = pad_matrix(matrix_a, next_power_of_2) | ||
| b = pad_matrix(matrix_b, next_power_of_2) | ||
| n = next_power_of_2 |
Copilot
AI
Jan 28, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Variable n is not used.
| n = next_power_of_2 |
Describe your change:
Checklist: