Skip to content

fix: stop rowEchelon mutating its input matrix - #2036

Open
Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-row-echelon-no-mutate
Open

Aditya2375 wants to merge 2 commits into
TheAlgorithms:masterfrom
Aditya2375:fix-row-echelon-no-mutate

Conversation

@Aditya2375

Copy link
Copy Markdown

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

What: rowEchelon() mutates the caller's matrix in place. result aliases the input (let result = matrix) and every row operation (pivot select, scalar multiply, row subtract) writes through it.

Why: a reduction function should not destroy its argument. rowEchelon([[1,2,3],[4,5,6],[7,8,10]]) returns the correct row echelon form, but the caller's original matrix is gone afterwards - the returned value is the same object as the input.

Change: work on a copy (matrix.map((row) => [...row])) and add a regression test asserting the input matrix is unchanged and the result is a different object. The new test fails on the current implementation; all existing RowEchelon tests still pass.

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
  • All new algorithms have a URL in their comments that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 85.91%. Comparing base (5c39e87) to head (29d8b6e).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #2036   +/-   ##
=======================================
  Coverage   85.91%   85.91%           
=======================================
  Files         379      379           
  Lines       19778    19778           
  Branches     3016     3017    +1     
=======================================
  Hits        16993    16993           
  Misses       2785     2785           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants