Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

zero(::AbstractSparseArray) full of structural nonzeros set to zero #31835

Closed
tkluck opened this issue Apr 25, 2019 · 1 comment · Fixed by #34209
Closed

zero(::AbstractSparseArray) full of structural nonzeros set to zero #31835

tkluck opened this issue Apr 25, 2019 · 1 comment · Fixed by #34209
Labels
sparse Sparse arrays

Comments

@tkluck
Copy link
Contributor

tkluck commented Apr 25, 2019

An obvious implementation for zero(a::AbstractSparseArray) would be

zero(a::AbstractSparseArray) = spzeros(eltype(a), size(a)...)

However, in reality there's no specialization, and (the abstract fallback uses fill!:

zero(x::AbstractArray{T}) where {T} = fill!(similar(x), zero(T))

In particular, that means that it will be filled with non-structural zeros.

The reason why I haven't sent the obvious pull request is that this change is backwards incompatible where it relates to object identity. Namely, a side-effect of using fill! is that for mutable objects, every zero has the same identity. Example:

julia> using SparseArrays

julia> y = zero(BigInt[1,2,3]); y[1] === y[2]
true

julia> y = zero(sparse(BigInt[1,2,3])); y[1] === y[2]
true

julia> Base.zero(a::AbstractSparseArray) = spzeros(eltype(a), size(a)...)

julia> y = zero(sparse(BigInt[1,2,3])); y[1] === y[2]
false

What do you think? Can we make this change in a minor release? If so, I'll gladly send in a pull request.

@tkluck
Copy link
Contributor Author

tkluck commented Apr 25, 2019

PS apologies for butchering terminology. I'm always confused about what counts as the "structural zeros" -- the ones that are explicitly stored or the ones that aren't.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants