From 3a97dbbb1187b941e5dce656f86a890e9db00f46 Mon Sep 17 00:00:00 2001 From: "Viral B. Shah" Date: Tue, 13 Oct 2026 03:20:05 +0000 Subject: [PATCH] Return the dense result of a broadcast whose index type cannot address every entry Co-Authored-By: Claude Opus 5.5 --- src/higherorderfns.jl | 6 +++++- test/higherorderfns.jl | 3 +++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/higherorderfns.jl b/src/higherorderfns.jl index 04166135..f85756fd 100644 --- a/src/higherorderfns.jl +++ b/src/higherorderfns.jl @@ -1349,8 +1349,12 @@ end function _densenonzero(bc::Broadcasted, f::F, args::Vararg{Any,N}) where {F,N} T = Base.promote_typejoin_union(Base.promote_op(f, map(eltype, args)...)) (isconcretetype(T) && T !== Bool) || return _densebroadcast(bc) - return _storedasdense(_copy(f, map(_sparsifystructured, args)...)) + As = map(_sparsifystructured, args) + # the kernels store every entry, which the index type of the arguments may not address + _addresses(_promote_indtype(As...), map(length, axes(bc))) || return _densebroadcast(bc) + return _storedasdense(_copy(f, As...)) end +_addresses(::Type{Ti}, shape) where {Ti} = !(Ti <: Base.BitInteger) || prod(Int64, shape) < typemax(Ti) _storedasdense(C::Union{SparseVector,SparseMatrixCSC}) = length(getnzval(C)) == nnz(C) == length(C) ? reshape(getnzval(C), size(C)) : Array(C) _storedasdense(C) = Array(C) diff --git a/test/higherorderfns.jl b/test/higherorderfns.jl index f64a71a7..09fca5b9 100644 --- a/test/higherorderfns.jl +++ b/test/higherorderfns.jl @@ -865,6 +865,9 @@ end B = sparse(BigFloat[1 0; 0 2]) @test @inferred(broadcast(*, B, B))::SparseMatrixCSC == Array(B) .* Array(B) @test cos.(B)::Matrix{BigFloat} == cos.(Array(B)) + # an index type that cannot address every entry of the dense result + S8 = sparse(Int8[1], Int8[1], [1.0], 12, 12) + @test cos.(S8)::Matrix{Float64} == cos.(Array(S8)) # a function that throws at zero is left to the sparse kernels: only a matrix with a # structural zero evaluates it there shifted(x) = sqrt(x - 1)