Skip to content

Expect a floating-point solution from sparse \ with a Hermitian Rational matrix - #1188

Merged
ViralBShah merged 1 commit into
mainfrom
vs/rational-hermitian-solve
Nov 22, 2026
Merged

ViralBShah merged 1 commit into
mainfrom
vs/rational-hermitian-solve

Conversation

@ViralBShah

Copy link
Copy Markdown
Member

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 symmetric Rational matrix through LinearAlgebra's Hermitian solve, so it now returns Float64 as 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)

julia> A = sparse(Rational{Int}[2 1; 1 2]); A \ Rational{Int}[1, 0]
2-element Vector{Rational{Int64}}:
  2//3
 -1//3

julia> Hermitian(Matrix(A)) \ Rational{Int}[1, 0]
2-element Vector{Rational{Int64}}:
  2//3
 -1//3

After (LinearAlgebra with #1882)

julia> A = sparse(Rational{Int}[2 1; 1 2]); A \ Rational{Int}[1, 0]
2-element Vector{Float64}:
  0.6666666666666666
 -0.3333333333333333

julia> Hermitian(Matrix(A)) \ Rational{Int}[1, 0]
2-element Vector{Float64}:
  0.6666666666666666
 -0.3333333333333333

This follows LinearAlgebra: the test expects what Hermitian(Matrix(A)) \ b gives, and the code is unchanged. The comment on _hermitian_solve now says that LinearAlgebra's generic Hermitian factorization is in floating point, where it said Rational{BigInt} for an integer eltype, and that a Rational one no longer stays exact.

Written with Claude Code.

🤖 Generated with Claude Code

…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
ViralBShah force-pushed the vs/rational-hermitian-solve branch from 05ef757 to c5c9536 Compare November 22, 2026 14:28
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.97%. Comparing base (0d4a8f6) to head (c5c9536).
⚠️ Report is 2 commits behind head on main.

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.
📢 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.

@ViralBShah
ViralBShah merged commit a31bcc2 into main Nov 22, 2026
11 checks passed
@ViralBShah
ViralBShah deleted the vs/rational-hermitian-solve branch November 22, 2026 14:41
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)
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.

1 participant