Skip to content

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

Open
maitriupadhyay03-cell wants to merge 7 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 7 commits into
TheAlgorithms:masterfrom
maitriupadhyay03-cell:fix/matrix-inverse-3x3-use-adjoint

Conversation

@maitriupadhyay03-cell

@maitriupadhyay03-cell maitriupadhyay03-cell commented Jun 21, 2026

Copy link
Copy Markdown

Describe your change:

Fixed a bug in matrix/inverse_of_matrix.py — the 3x3 inverse was coming out wrong (it was returning the transpose of the actual inverse). Turned out the code was building inverse_matrix from cofactor_matrix instead of adjoint_matrix, so the transpose step was getting skipped. One-line fix: use adjoint_matrix where it should've been all along. Also updated the doctest since it was asserting the wrong expected 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

@diusazzad diusazzad left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey @maitriupadhyay03-cell! 👋 Thanks for this fix. It looks like the tests are currently failing. Could you check the CI logs to see which test case is causing the error? If you need help debugging the logic, let me know!

@algorithms-keeper algorithms-keeper Bot removed the tests are failing Do not merge until tests pass label Jun 25, 2026
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

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)

2 participants