Skip to content

Match dense isequal with a lazy adjoint of a sparse array - #1189

Merged
ViralBShah merged 1 commit into
mainfrom
vs/isequal-lazy-adjoint
Nov 22, 2026
Merged

ViralBShah merged 1 commit into
mainfrom
vs/isequal-lazy-adjoint

Conversation

@ViralBShah

Copy link
Copy Markdown
Member

Fixes #1185. isequal with a lazy adjoint of a sparse matrix or vector disagrees with dense isequal in both directions, and is true for some pairs whose hashes differ. Julia 1.13 agrees with dense; 1.14.0-beta1 and nightly do not, since #985.

Before

julia> Z = spzeros(ComplexF64, 2, 2);

julia> isequal(Z, Z'), isequal(spzeros(2, 2), Z')         # dense: (false, false)
(true, true)

julia> A = sparse([1], [1], [1]); B = sparse([1], [1], [1.0 + 0.0im]);

julia> isequal(A', B), isequal(B, A'), isequal(A', B')    # dense: (true, true, false)
(false, false, true)

julia> x = sparsevec([1], [1]); y = sparsevec([1], [1.0 + 0.0im]);

julia> isequal(x', y')                                     # dense: false
true

After

julia> isequal(Z, Z'), isequal(spzeros(2, 2), Z')
(false, false)

julia> isequal(A', B), isequal(B, A'), isequal(A', B')
(true, true, false)

julia> isequal(x', y')
false

The adjoint of a complex zero is 0.0 - 0.0im, which isequal tells from 0.0 + 0.0im, and isequal(1, 1.0 + 0.0im) holds but isequal(1, 1.0 - 0.0im) does not. Three places conjugated the wrong side. The comparison of a matrix with a lazy adjoint took the implicit zero of the adjoint's parent, in its check and in the merge it falls back to. Its search over the adjoint's stored entries conjugated the other matrix's entries instead of the adjoint's own. And isequal with an adjoint as the first argument, of matrices or of vectors, compared A' with B', conjugating both sides. Now a wrapper applies to its own side only: to the implicit zero of its parent, and to its own entries, while the other matrix is only indexed with its indices swapped. isequal with an adjoint first compares A' and B' by isequal of the adjoints of their entries. ==, which conjugating both sides does not change, keeps its path.

Over every pair of 30 small sparse matrices with Int, Float64, ComplexF64 and ComplexF32 entries, signed zeros, NaN and stored zeros, each without a wrapper, under adjoint or transpose, or under both nested, isequal and == now agree with dense: 972 of the 88,200 comparisons differed on main. In a smaller sweep, the 40 pairs that were isequal with different hashes no longer are.

With a complex eltype isequal(A, B') now finds the implicit zeros unequal, so it merges A with the sparse transpose of B' instead of searching B' for each stored entry. That allocates a copy of B, and for 10⁴×10⁴ matrices with 10⁵ stored entries it takes 0.29 ms where the search took 3.2 ms. The other comparisons keep their path and time.

Should be backported to 1.14, which has #985.

Left out: ==, and isequal of real matrices, still compare a matrix with a lazy adjoint by that search, which on those matrices takes 3.1 ms where the transpose and a full merge take 0.48 ms.

Written with Claude Code.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ViralBShah
ViralBShah force-pushed the vs/isequal-lazy-adjoint branch from 877c3cf to 193b6d1 Compare November 22, 2026 14:42
@codecov

codecov Bot commented Nov 22, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 94.73684% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 98.96%. Comparing base (a31bcc2) to head (193b6d1).

Files with missing lines Patch % Lines
src/sparsematrix.jl 94.11% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1189      +/-   ##
==========================================
- Coverage   98.97%   98.96%   -0.01%     
==========================================
  Files          16       16              
  Lines       10759    10770      +11     
==========================================
+ Hits        10649    10659      +10     
- Misses        110      111       +1     

☔ 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 355782e into main Nov 22, 2026
11 checks passed
@ViralBShah
ViralBShah deleted the vs/isequal-lazy-adjoint branch November 22, 2026 14:54
@ViralBShah ViralBShah mentioned this pull request Nov 22, 2026
10 tasks done
ViralBShah added a commit that referenced this pull request Nov 22, 2026
Fixes #1185. `isequal` with a lazy adjoint of a sparse matrix or vector disagrees with dense `isequal` in both directions, and is true for some pairs whose hashes differ. Julia 1.13 agrees with dense; 1.14.0-beta1 and nightly do not, since #985.

**Before**
```julia
julia> Z = spzeros(ComplexF64, 2, 2);

julia> isequal(Z, Z'), isequal(spzeros(2, 2), Z')         # dense: (false, false)
(true, true)

julia> A = sparse([1], [1], [1]); B = sparse([1], [1], [1.0 + 0.0im]);

julia> isequal(A', B), isequal(B, A'), isequal(A', B')    # dense: (true, true, false)
(false, false, true)

julia> x = sparsevec([1], [1]); y = sparsevec([1], [1.0 + 0.0im]);

julia> isequal(x', y')                                     # dense: false
true
```

**After**
```julia
julia> isequal(Z, Z'), isequal(spzeros(2, 2), Z')
(false, false)

julia> isequal(A', B), isequal(B, A'), isequal(A', B')
(true, true, false)

julia> isequal(x', y')
false
```

The adjoint of a complex zero is `0.0 - 0.0im`, which `isequal` tells from `0.0 + 0.0im`, and `isequal(1, 1.0 + 0.0im)` holds but `isequal(1, 1.0 - 0.0im)` does not. Three places conjugated the wrong side. The comparison of a matrix with a lazy adjoint took the implicit zero of the adjoint's parent, in its check and in the merge it falls back to. Its search over the adjoint's stored entries conjugated the other matrix's entries instead of the adjoint's own. And `isequal` with an adjoint as the first argument, of matrices or of vectors, compared `A'` with `B'`, conjugating both sides. Now a wrapper applies to its own side only: to the implicit zero of its parent, and to its own entries, while the other matrix is only indexed with its indices swapped. `isequal` with an adjoint first compares `A'` and `B'` by `isequal` of the adjoints of their entries. `==`, which conjugating both sides does not change, keeps its path.

Over every pair of 30 small sparse matrices with `Int`, `Float64`, `ComplexF64` and `ComplexF32` entries, signed zeros, `NaN` and stored zeros, each without a wrapper, under `adjoint` or `transpose`, or under both nested, `isequal` and `==` now agree with dense: 972 of the 88,200 comparisons differed on `main`. In a smaller sweep, the 40 pairs that were `isequal` with different hashes no longer are.

With a complex eltype `isequal(A, B')` now finds the implicit zeros unequal, so it merges `A` with the sparse transpose of `B'` instead of searching `B'` for each stored entry. That allocates a copy of `B`, and for 10⁴×10⁴ matrices with 10⁵ stored entries it takes 0.29 ms where the search took 3.2 ms. The other comparisons keep their path and time.

Should be backported to 1.14, which has #985.

Left out: `==`, and `isequal` of real matrices, still compare a matrix with a lazy adjoint by that search, which on those matrices takes 3.1 ms where the transpose and a full merge take 0.48 ms.

Written with Claude Code.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

(cherry picked from commit 355782e)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

1 participant