Skip to content

Commit

Permalink
Fix v[i] = -0.0 and support reinterpret (#296)
Browse files Browse the repository at this point in the history
* assign whenever v !== zero(eltype(sparsecollection))

* test -0.0

* allow reinterpret

* test reinterpret
  • Loading branch information
LilithHafner authored and Lilith Hafner committed Jan 20, 2023
1 parent 4f87f88 commit 2501f92
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
7 changes: 0 additions & 7 deletions src/abstractsparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,6 @@ issparse(S::AbstractSparseArray) = true

indtype(S::AbstractSparseArray{<:Any,Ti}) where {Ti} = Ti

function Base.reinterpret(::Type, A::AbstractSparseArray)
error("""
`reinterpret` on sparse arrays is discontinued.
Try reinterpreting the value itself instead.
""")
end

# The following two methods should be overloaded by concrete types to avoid
# allocating the I = findall(...)
_sparse_findnextnz(v::AbstractSparseArray, i) = (I = findall(_isnotzero, v); n = searchsortedfirst(I, i); n<=length(I) ? I[n] : nothing)
Expand Down
2 changes: 1 addition & 1 deletion src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2960,7 +2960,7 @@ function _setindex_scalar!(A::AbstractSparseMatrixCSC{Tv,Ti}, _v, _i::Integer, _
end
# Column j does not contain entry A[i,j]. If v is nonzero, insert entry A[i,j] = v
# and return. If to the contrary v is zero, then simply return.
if _isnotzero(v)
if v isa AbstractArray || v !== zero(eltype(A)) # stricter than iszero to support A[i] = -0.0
nz = getcolptr(A)[size(A, 2)+1]
# throw exception before state is partially modified
!isbitstype(Ti) || nz < typemax(Ti) ||
Expand Down
2 changes: 1 addition & 1 deletion src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,7 @@ end
if 1 <= k <= m && nzind[k] == i # i found
nzval[k] = v
else # i not found
if _isnotzero(v)
if v isa AbstractArray || v !== zero(eltype(x)) # stricter than iszero to support v[i] = -0.0
insert!(nzind, k, i)
insert!(nzval, k, v)
end
Expand Down
32 changes: 31 additions & 1 deletion test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,36 @@ end
@test A[1,1] == 2
end

@testset "-0.0 (issue #294, pr #296)" begin
v = spzeros(1)
v[1] = -0.0
@test v[1] === -0.0

m = spzeros(1, 1)
m[1, 1] = -0.0
@test m[1, 1] === -0.0
end

@testset "reinterpret (issue #289, pr #296)" begin
s = spzeros(3)
r = reinterpret(Int64, s)
@test r == s

r[1] = Int64(12)
@test r[1] === Int64(12)
@test s[1] === reinterpret(Float64, Int64(12))
@test r != s

r[2] = Int64(0)
@test r[2] === Int64(0)
@test s[2] === 0.0

z = reinterpret(Int64, -0.0)
r[3] = z
@test r[3] === z
@test s[3] === -0.0
end

# As part of the migration of SparseArrays.jl into its own repo,
# these tests have been moved from other files in julia tests to
# the SparseArrays.jl repo
Expand Down Expand Up @@ -753,6 +783,6 @@ g12063() = f12063(0, 0, 0, 0, 0, 0, 0.0, spzeros(0,0), Int[])
@test String(take!(io)) == "transpose(sparse([1, 2, 1, 2], [1, 1, 2, 2], [1, 3, 2, 4], 2, 2))"
end

end
end # SparseTestsBase

end # module
2 changes: 1 addition & 1 deletion test/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ end
@test Vector(x1) == collect(x)
end
end
@testset "vec/reinterpret/float/complex" begin
@testset "vec/float/complex" begin
a = SparseVector(8, [2, 5, 6], Int32[12, 35, 72])
# vec
@test vec(a) == a
Expand Down

0 comments on commit 2501f92

Please sign in to comment.