fix(matrix): use adjoint_matrix for 3x3 inverse computation#14838
Open
maitriupadhyay03-cell wants to merge 6 commits into
Open
fix(matrix): use adjoint_matrix for 3x3 inverse computation#14838maitriupadhyay03-cell wants to merge 6 commits into
maitriupadhyay03-cell wants to merge 6 commits into
Conversation
…gorithms#7854) The split() function in treap.py uses `<` comparison but the docstring states that the right subtree should contain values "greater or equal" to the split value. This fix changes `elif value < root.value:` to `elif value <= root.value:` so that equal values go to the right subtree as documented. Fixes TheAlgorithms#7854
The 3x3 inverse computation had a bug: it was using cofactor_matrix instead of adjoint_matrix when seeding inverse_matrix (line ~148). The adjoint is correctly computed as the transpose of the cofactor matrix, but then discarded. This fix uses adjoint_matrix as required by the formula: inverse = (1/det) * adjoint. Also updates the 3x3 doctest to assert the correct inverse values. Fixes TheAlgorithms#14813
The inorder() function previously used print() to output values, which made its doctests thread-unsafe (pytest-run-parallel does not support doctests that use print). This fix: - Changes inorder() to return a comma-separated string instead of printing - Updates all interact_treap doctests to assert the returned string value - Adds a doctest to inorder() itself Fixes the CI failure: FAILED data_structures/binary_tree/treap.py::data_structures.binary_tree.treap.interact_treap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Describe your change:
Fix a bug in
matrix/inverse_of_matrix.pywhere the 3x3 inverse computation returned the transpose of the correct inverse instead of the actual inverse.Root cause: The function correctly computed
adjoint_matrixas the transpose ofcofactor_matrix, but then initializedinverse_matrixfromcofactor_matrixinstead ofadjoint_matrix. This meantinverse_matrixwas seeded with the un-transposed cofactor data, producing a result equal to(1/det) * cofactor— which is the transpose of the true inverse.Fix: Seed
inverse_matrixfromadjoint_matrix(notcofactor_matrix), as required by the formula:A⁻¹ = (1/det(A)) * adj(A).Also updated: The 3x3 doctest which was asserting the previously wrong (transposed) values.
Fixes #14813
Checklist: