-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Add matrix multiplication function with validation #13934
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| """ | ||
| Matrix Multiplication Algorithm | ||
|
|
||
| This function performs matrix multiplication on two valid matrices. | ||
| It follows the mathematical definition: | ||
| If A is an m×n matrix and B is an n×p matrix, | ||
| then their product C is an m×p matrix. | ||
|
|
||
| Raises: | ||
| ValueError: if matrices have invalid structure or incompatible sizes. | ||
|
|
||
| Sources: | ||
| https://en.wikipedia.org/wiki/Matrix_multiplication | ||
|
|
||
| Examples: | ||
| >>> A = [[1, 2], [3, 4]] | ||
| >>> B = [[5, 6], [7, 8]] | ||
| >>> matrix_multiply(A, B) | ||
| [[19, 22], [43, 50]] | ||
|
|
||
| >>> matrix_multiply([[1, 2, 3]], [[4], [5], [6]]) | ||
| [[32]] | ||
|
|
||
| # Invalid structure | ||
| >>> matrix_multiply([[1, 2], [3]], [[1, 2]]) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Invalid matrix structure | ||
|
|
||
| # Incompatible sizes | ||
| >>> matrix_multiply([[1, 2]], [[1, 2]]) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: Incompatible matrix sizes | ||
| """ | ||
|
|
||
| from typing import List | ||
|
|
||
|
|
||
| def matrix_multiply(A: List[List[float]], B: List[List[float]]) -> List[List[float]]: | ||
| if not _is_valid_matrix(A) or not _is_valid_matrix(B): | ||
| raise ValueError("Invalid matrix structure") | ||
|
|
||
| rows_A = len(A) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
| cols_A = len(A[0]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
| rows_B = len(B) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
| cols_B = len(B[0]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Variable and function names should follow the |
||
|
|
||
| if cols_A != rows_B: | ||
| raise ValueError("Incompatible matrix sizes") | ||
|
|
||
| result = [[0.0 for _ in range(cols_B)] for _ in range(rows_A)] | ||
|
|
||
| for i in range(rows_A): | ||
| for j in range(cols_B): | ||
| for k in range(cols_A): | ||
| result[i][j] += A[i][k] * B[k][j] | ||
|
|
||
| return result | ||
|
|
||
|
|
||
| def _is_valid_matrix(M: List[List[float]]) -> bool: | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please provide descriptive name for the parameter: |
||
| if not isinstance(M, list) or not M: | ||
| return False | ||
| first_length = len(M[0]) | ||
| return all(isinstance(row, list) and len(row) == first_length for row in M) | ||
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.
Please provide descriptive name for the parameter:
APlease provide descriptive name for the parameter:
B