Skip to content

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

Closed

Description

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    sparseSparse arrays

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions