Skip to content

fix(matrix): use adjoint_matrix for 3x3 inverse computation#14838

Open
maitriupadhyay03-cell wants to merge 6 commits into
TheAlgorithms:masterfrom
maitriupadhyay03-cell:fix/matrix-inverse-3x3-use-adjoint
Open

fix(matrix): use adjoint_matrix for 3x3 inverse computation#14838
maitriupadhyay03-cell wants to merge 6 commits into
TheAlgorithms:masterfrom
maitriupadhyay03-cell:fix/matrix-inverse-3x3-use-adjoint

Conversation

@maitriupadhyay03-cell

Copy link
Copy Markdown

Describe your change:

Fix a bug in matrix/inverse_of_matrix.py where the 3x3 inverse computation returned the transpose of the correct inverse instead of the actual inverse.

Root cause: The function correctly computed adjoint_matrix as the transpose of cofactor_matrix, but then initialized inverse_matrix from cofactor_matrix instead of adjoint_matrix. This meant inverse_matrix was 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_matrix from adjoint_matrix (not cofactor_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

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

Checklist:

  • I have read CONTRIBUTING.md.
  • * [x] This pull request is all my own work -- I have not plagiarized.
  • * [x] I know that pull requests will not be merged if they fail the automated tests.

…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
@algorithms-keeper algorithms-keeper Bot added enhancement This PR modified some existing files awaiting reviews This PR is ready to be reviewed tests are failing Do not merge until tests pass labels Jun 21, 2026
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
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 enhancement This PR modified some existing files tests are failing Do not merge until tests pass

Projects

None yet

Development

Successfully merging this pull request may close these issues.

matrix/inverse_of_matrix: 3x3 inverse returns the transpose of the true inverse (adjoint computed but unused)

1 participant