Repository navigation
Expect a floating-point solution from sparse \ with a Hermitian Rational matrix - #1188
Merged
Merged
Conversation
…tional` matrix JuliaLang/LinearAlgebra.jl#1882 makes the factorization of a Hermitian matrix floating point. Sparse `\` solves a symmetric `Rational` matrix through LinearAlgebra's Hermitian solve, so it returns `Float64` as dense `Hermitian(M) \ b` does, and the test now expects that. The comments on `_hermitian_solve` describe the floating-point factorization. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ViralBShah
force-pushed
the
vs/rational-hermitian-solve
branch
from
November 22, 2026 14:28
05ef757 to
c5c9536
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #1188 +/- ##
==========================================
- Coverage 98.97% 98.97% -0.01%
==========================================
Files 16 16
Lines 10762 10759 -3
==========================================
- Hits 10652 10649 -3
Misses 110 110 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
ViralBShah
added a commit
that referenced
this pull request
Nov 22, 2026
…trix (#1190) Fixes #1179, closes #1184. Sparse `\` of a `Rational` matrix that is neither triangular nor Hermitian factorizes it in `Float64` with UMFPACK, then converts the solution back to `Rational`. The values have power-of-two denominators, so they look exact and are not, and a solution entry that no `Rational{Int}` holds throws. A tall or wide matrix gave the same, where dense `\` returns `Float64`. **Before** ```julia julia> S = sparse(Rational{Int}[2 1 0; 0 3 1; 1 0 4]); b = Rational{Int}[1, 2, 3]; julia> S \ b 3-element Vector{Rational{Int64}}: 5044031582654955//18014398509481984 3963167672086037//9007199254740992 6124895493223873//9007199254740992 julia> S \ Rational{Int}[1//10^18, 0, 0] ERROR: InexactError: Rational(4.000000000000001e-20) julia> sparse(Rational{Int}[2 0; 1 3; 0 1]) \ b # dense: Vector{Float64} 2-element Vector{Rational{Int64}}: 5711882454225997//18014398509481984 7249696961132991//9007199254740992 ``` **After** ```julia julia> S \ b 3-element Vector{Float64}: 0.27999999999999997 0.44000000000000006 0.6799999999999998 julia> S \ Rational{Int}[1//10^18, 0, 0] 3-element Vector{Float64}: 4.8e-19 4.000000000000001e-20 -1.2000000000000004e-19 julia> sparse(Rational{Int}[2 0; 1 3; 0 1]) \ b 2-element Vector{Float64}: 0.31707317073170743 0.8048780487804875 ``` The LU, QR and LQ branches of `\` convert their solution to the eltype that dense `\` gives, which is now taken in floating point. That changes nothing for floating-point and integer eltypes. For rationals the solution stays in floating point, as `lu(S) \ b` and `factorize(S) \ b` already give it, and as the Hermitian branch has given it since JuliaLang/LinearAlgebra.jl#1882 (#1188 updates that test). Triangular and diagonal matrices are still solved exactly, as dense ones are. So the result type of `\` for a `Rational` matrix still depends on its structure, as it has on `main` since the Hermitian branch went to floating point. For #1184: `det` and `logabsdet` of a `Rational` matrix that is not triangular already return floating point, from the same `Float64` LU, and `Rational{BigInt}` is rejected as `BigFloat` is, its float type. Factorizing a dense copy instead would be exact, but takes O(n²) memory, and with `Rational{Int}` it overflows on a 2D Laplacian of order 100 that the floating-point solve handles. The section on element types in the manual now says that `\`, `det` and `logabsdet` of a matrix of rationals return floating point unless it is triangular. Potential breakage: `\` of a `Rational` matrix that is neither triangular nor Hermitian returned a `Rational` array and now returns a floating-point one. Written with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
ViralBShah
added a commit
that referenced
this pull request
Nov 22, 2026
…trix (#1190) Fixes #1179, closes #1184. Sparse `\` of a `Rational` matrix that is neither triangular nor Hermitian factorizes it in `Float64` with UMFPACK, then converts the solution back to `Rational`. The values have power-of-two denominators, so they look exact and are not, and a solution entry that no `Rational{Int}` holds throws. A tall or wide matrix gave the same, where dense `\` returns `Float64`. **Before** ```julia julia> S = sparse(Rational{Int}[2 1 0; 0 3 1; 1 0 4]); b = Rational{Int}[1, 2, 3]; julia> S \ b 3-element Vector{Rational{Int64}}: 5044031582654955//18014398509481984 3963167672086037//9007199254740992 6124895493223873//9007199254740992 julia> S \ Rational{Int}[1//10^18, 0, 0] ERROR: InexactError: Rational(4.000000000000001e-20) julia> sparse(Rational{Int}[2 0; 1 3; 0 1]) \ b # dense: Vector{Float64} 2-element Vector{Rational{Int64}}: 5711882454225997//18014398509481984 7249696961132991//9007199254740992 ``` **After** ```julia julia> S \ b 3-element Vector{Float64}: 0.27999999999999997 0.44000000000000006 0.6799999999999998 julia> S \ Rational{Int}[1//10^18, 0, 0] 3-element Vector{Float64}: 4.8e-19 4.000000000000001e-20 -1.2000000000000004e-19 julia> sparse(Rational{Int}[2 0; 1 3; 0 1]) \ b 2-element Vector{Float64}: 0.31707317073170743 0.8048780487804875 ``` The LU, QR and LQ branches of `\` convert their solution to the eltype that dense `\` gives, which is now taken in floating point. That changes nothing for floating-point and integer eltypes. For rationals the solution stays in floating point, as `lu(S) \ b` and `factorize(S) \ b` already give it, and as the Hermitian branch has given it since JuliaLang/LinearAlgebra.jl#1882 (#1188 updates that test). Triangular and diagonal matrices are still solved exactly, as dense ones are. So the result type of `\` for a `Rational` matrix still depends on its structure, as it has on `main` since the Hermitian branch went to floating point. For #1184: `det` and `logabsdet` of a `Rational` matrix that is not triangular already return floating point, from the same `Float64` LU, and `Rational{BigInt}` is rejected as `BigFloat` is, its float type. Factorizing a dense copy instead would be exact, but takes O(n²) memory, and with `Rational{Int}` it overflows on a 2D Laplacian of order 100 that the floating-point solve handles. The section on element types in the manual now says that `\`, `det` and `logabsdet` of a matrix of rationals return floating point unless it is triangular. Potential breakage: `\` of a `Rational` matrix that is neither triangular nor Hermitian returned a `Rational` array and now returns a floating-point one. Written with Claude Code. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Backport to 1.14: the manual says that `\` of a Hermitian matrix of rationals stays exact, as LinearAlgebra's Hermitian solve keeps it on Julia 1.14, which does not have JuliaLang/LinearAlgebra.jl#1882. (cherry picked from commit bf4d73f)
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.
JuliaLang/LinearAlgebra.jl#1882, in Julia since JuliaLang/julia#65101 (1.15.0-DEV.864), makes the factorization of a Hermitian matrix floating point, closing JuliaLang/LinearAlgebra.jl#1871. Sparse
\solves a symmetricRationalmatrix through LinearAlgebra's Hermitian solve, so it now returnsFloat64as well, and the comprehensive test that expected an exact solution fails on nightly (test/solvers/solvers.jl:197). That is the failure left in the coverage job of #1186 once it loads.Before (LinearAlgebra without #1882)
After (LinearAlgebra with #1882)
This follows LinearAlgebra: the test expects what
Hermitian(Matrix(A)) \ bgives, and the code is unchanged. The comment on_hermitian_solvenow says that LinearAlgebra's generic Hermitian factorization is in floating point, where it saidRational{BigInt}for an integer eltype, and that aRationalone no longer stays exact.Written with Claude Code.
🤖 Generated with Claude Code