Repository navigation
fix: correct matrix size validation and handle rectangular matrix operations - #15604
hardikkaurani wants to merge 1 commit into
Conversation
…rations - Fix index transposition in _verify_matrix_sizes where rows_a was compared with cols_b and cols_a with rows_b, which incorrectly raised ValueError on valid rectangular matrix addition and subtraction. - In multiply(), compare inner dimensions cols_a == rows_b directly instead of calling _verify_matrix_sizes, allowing rectangular matrix multiplication with distinct outer dimensions (e.g., 2x3 by 3x4). - In multiply(), raise TypeError instead of causing an UnboundLocalError when invalid non-matrix inputs are passed. - In subtract(), call _verify_matrix_sizes as a statement to eliminate ty warning[redundant-condition] on truthiness of a 2-element tuple. - Update matrix/tests/test_matrix_operation.py to test rectangular matrix addition, subtraction, multiplication, and fix copy-paste calls in test_multiplication and test_subtraction.
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
|
Hi @cclauss, I've submitted this pull request to fix an index transposition in Quick overview of the fixes:
All 33 unit tests and doctests pass ( Whenever you have a chance, could you please take a look? Thank you! |
Describe your change
Problem & Context
_verify_matrix_sizes:In
matrix/matrix_operation.py,_shape(matrix_a) + _shape(matrix_b)produces(rows_a, cols_a, rows_b, cols_b). The conditionshape[0] != shape[3] or shape[1] != shape[2]inadvertently comparedrows_awithcols_bandcols_awithrows_b. This caused valid addition and subtraction of identical rectangular (non-square) matrices (e.g.,add([[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]])) to erroneously raiseValueError: operands could not be broadcast together with shape ((2, 3)), ((2, 3)).multiply():In
multiply(), calling_verify_matrix_sizeswas incorrect because matrix multiplication does not require operands to have identical shapes; it requires inner dimensions to match (cols_a == rows_b). Calling_verify_matrix_sizesrejected valid multiplications whererows_a != cols_b(e.g. multiplying a 2x3 matrix with a 3x4 matrix). Furthermore, passing non-matrix inputs caused anUnboundLocalErroroncolsinstead of raisingTypeError("Expected a matrix, got int/list instead")likeadd()andsubtract().tywarning on redundant condition insubtract():Calling
and _verify_matrix_sizes(matrix_a, matrix_b)inside theifcondition triggeredty'swarning[redundant-condition]: A 2-element tuple is always truthy.Changes Made
_verify_matrix_sizes(), corrected the condition toshape[0] != shape[2] or shape[1] != shape[3].subtract(), invoked_verify_matrix_sizes(matrix_a, matrix_b)as a statement, matchingadd().multiply(), compared inner dimensionscols_a == rows_bdirectly and raisedTypeErrorwhen inputs are not matrices.matrix/tests/test_matrix_operation.pyto test rectangular matrices and corrected copy-pasted helper calls (matop.add->matop.multiplyintest_multiplication).Verification
uvx --with numpy pytest matrix/tests/test_matrix_operation.py matrix/matrix_operation.py --doctest-modules: 33 passed.uvx ty check matrix/matrix_operation.py: All checks passed (0 diagnostics, 0 warnings).uvx ruff check matrix/matrix_operation.py matrix/tests/test_matrix_operation.py: All checks passed.uvx ruff format --check matrix/matrix_operation.py matrix/tests/test_matrix_operation.py: Clean.Checklist