Skip to content

Keep the sign of a zero on a Diagonal argument of a sparse broadcast - #1085

Merged
ViralBShah merged 1 commit into
mainfrom
vs/broadcast-diagonal-negzero
Oct 13, 2026
Merged

ViralBShah merged 1 commit into
mainfrom
vs/broadcast-diagonal-negzero

Conversation

@ViralBShah

@ViralBShah ViralBShah commented Oct 13, 2026 •

Copy link
Copy Markdown
Member

A Diagonal argument of a sparse broadcast loses the sign of a -0.0 on its diagonal, so the result differs from the dense one.

Before

julia> sparse(ones(2, 2)) ./ Diagonal([-0.0, 1.0])
2×2 Matrix{Float64}:
 Inf  Inf
 Inf   1.0

julia> ones(2, 2) ./ Diagonal([-0.0, 1.0])
2×2 Matrix{Float64}:
 -Inf  Inf
  Inf   1.0

After

julia> sparse(ones(2, 2)) ./ Diagonal([-0.0, 1.0])
2×2 Matrix{Float64}:
 -Inf  Inf
  Inf   1.0

Broadcast and map convert a structured argument with _sparsifystructured. For a Diagonal that went through SparseMatrixCSC(D), which drops the zeros of the diagonal, and the kernel then read the dropped -0.0 as a structural +0.0. Bidiagonal, Tridiagonal and SymTridiagonal arguments already convert with their whole band stored, zeros included, so they were not affected. _sparsifystructured(::Diagonal) now stores the whole diagonal as well. SparseMatrixCSC(D) and sparse(D) are unchanged.

The stored pattern of a zero-preserving result does not change: the kernels store only the nonzero values they compute, so S .* D, S .+ D, S .- D, map(+, S, D) and broadcast!(*, Z, S, D) with zeros (of either sign) on the diagonal have the same nnz and pattern as on main.

Touches dispatch: adds the internal method _sparsifystructured(::Diagonal).

Split from #1022. A bug fix with no change of result type, so a backport candidate.

Written by Claude Code.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@codecov

codecov Bot commented Oct 13, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.79%. Comparing base (c0f75f0) to head (6129598).
⚠️ Report is 27 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1085      +/-   ##
==========================================
+ Coverage   94.75%   94.79%   +0.03%     
==========================================
  Files          16       16              
  Lines        9885     9886       +1     
==========================================
+ Hits         9367     9371       +4     
+ Misses        518      515       -3     

☔ 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 left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed by Codex at 6129598.

No actionable findings. Materializing the complete diagonal preserves stored signed zeros for the broadcast kernel while leaving implicit off-diagonal zeros implicit.

@ViralBShah ViralBShah added the backport 1.13 Change should be backported to release-1.13 label Oct 13, 2026
@ViralBShah
ViralBShah merged commit e57a4f6 into main Oct 13, 2026
10 checks passed
@ViralBShah
ViralBShah deleted the vs/broadcast-diagonal-negzero branch October 13, 2026 22:45
@ViralBShah ViralBShah removed the backport 1.13 Change should be backported to release-1.13 label Oct 27, 2026
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.

1 participant