Skip to content

Make sparse rational echelon forms immutable - #42601

Open
cxzhong wants to merge 1 commit into
sagemath:developfrom
cxzhong:fix-42531-sparse-echelon-immutability
Open

Make sparse rational echelon forms immutable#42601
cxzhong wants to merge 1 commit into
sagemath:developfrom
cxzhong:fix-42531-sparse-echelon-immutability

Conversation

@cxzhong

@cxzhong cxzhong commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

Fixes #42531.

Problem

Matrix_rational_sparse.echelon_form() cached and returned a mutable matrix. A caller could mutate that shared result and corrupt every later cache hit, leaving the cached echelon matrix inconsistent with the source matrix's cached pivots and rank.

The already-echelon fast path also returned a mutable self. In addition, the multimodular helper returns self for matrices with zero rows or columns, so freezing its result directly would unexpectedly freeze the caller's matrix.

Changes

  • Make computed sparse rational echelon forms immutable before caching them.
  • Return and cache an immutable copy when a mutable matrix is already in echelon form.
  • Copy zero-row and zero-column helper aliases before changing mutability.
  • Document the immutable return contract and add regression doctests for all three paths.

Validation

  • ./sage -t --long src/sage/matrix/matrix_rational_sparse.pyx (77 tests)
  • Direct cache-corruption, pivot/rank, source-mutation, and empty-shape probes
  • 42 randomized sparse shape and correctness checks against dense echelon forms
  • git diff --check

@github-actions

Copy link
Copy Markdown

Documentation preview for this PR (built with commit b17e7a4; changes) is ready! 🎉
This preview will update shortly after each push to this PR.

@cxzhong
cxzhong marked this pull request as ready for review July 30, 2026 09:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sparse echelon form over QQ is mutable and shared with the cache (#10543 was fixed for dense only)

1 participant