Skip to content

Commit 2501f92

Browse files
LilithHafnerLilith Hafner
authored and
Lilith Hafner
committed
Fix v[i] = -0.0 and support reinterpret (#296)
* assign whenever v !== zero(eltype(sparsecollection)) * test -0.0 * allow reinterpret * test reinterpret
1 parent 4f87f88 commit 2501f92

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

src/abstractsparse.jl

-7
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,6 @@ issparse(S::AbstractSparseArray) = true
7575

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

78-
function Base.reinterpret(::Type, A::AbstractSparseArray)
79-
error("""
80-
`reinterpret` on sparse arrays is discontinued.
81-
Try reinterpreting the value itself instead.
82-
""")
83-
end
84-
8578
# The following two methods should be overloaded by concrete types to avoid
8679
# allocating the I = findall(...)
8780
_sparse_findnextnz(v::AbstractSparseArray, i) = (I = findall(_isnotzero, v); n = searchsortedfirst(I, i); n<=length(I) ? I[n] : nothing)

src/sparsematrix.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -2960,7 +2960,7 @@ function _setindex_scalar!(A::AbstractSparseMatrixCSC{Tv,Ti}, _v, _i::Integer, _
29602960
end
29612961
# Column j does not contain entry A[i,j]. If v is nonzero, insert entry A[i,j] = v
29622962
# and return. If to the contrary v is zero, then simply return.
2963-
if _isnotzero(v)
2963+
if v isa AbstractArray || v !== zero(eltype(A)) # stricter than iszero to support A[i] = -0.0
29642964
nz = getcolptr(A)[size(A, 2)+1]
29652965
# throw exception before state is partially modified
29662966
!isbitstype(Ti) || nz < typemax(Ti) ||

src/sparsevector.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ end
390390
if 1 <= k <= m && nzind[k] == i # i found
391391
nzval[k] = v
392392
else # i not found
393-
if _isnotzero(v)
393+
if v isa AbstractArray || v !== zero(eltype(x)) # stricter than iszero to support v[i] = -0.0
394394
insert!(nzind, k, i)
395395
insert!(nzval, k, v)
396396
end

test/issues.jl

+31-1
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,36 @@ end
628628
@test A[1,1] == 2
629629
end
630630

631+
@testset "-0.0 (issue #294, pr #296)" begin
632+
v = spzeros(1)
633+
v[1] = -0.0
634+
@test v[1] === -0.0
635+
636+
m = spzeros(1, 1)
637+
m[1, 1] = -0.0
638+
@test m[1, 1] === -0.0
639+
end
640+
641+
@testset "reinterpret (issue #289, pr #296)" begin
642+
s = spzeros(3)
643+
r = reinterpret(Int64, s)
644+
@test r == s
645+
646+
r[1] = Int64(12)
647+
@test r[1] === Int64(12)
648+
@test s[1] === reinterpret(Float64, Int64(12))
649+
@test r != s
650+
651+
r[2] = Int64(0)
652+
@test r[2] === Int64(0)
653+
@test s[2] === 0.0
654+
655+
z = reinterpret(Int64, -0.0)
656+
r[3] = z
657+
@test r[3] === z
658+
@test s[3] === -0.0
659+
end
660+
631661
# As part of the migration of SparseArrays.jl into its own repo,
632662
# these tests have been moved from other files in julia tests to
633663
# the SparseArrays.jl repo
@@ -753,6 +783,6 @@ g12063() = f12063(0, 0, 0, 0, 0, 0, 0.0, spzeros(0,0), Int[])
753783
@test String(take!(io)) == "transpose(sparse([1, 2, 1, 2], [1, 1, 2, 2], [1, 3, 2, 4], 2, 2))"
754784
end
755785

756-
end
786+
end # SparseTestsBase
757787

758788
end # module

test/sparsevector.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ end
463463
@test Vector(x1) == collect(x)
464464
end
465465
end
466-
@testset "vec/reinterpret/float/complex" begin
466+
@testset "vec/float/complex" begin
467467
a = SparseVector(8, [2, 5, 6], Int32[12, 35, 72])
468468
# vec
469469
@test vec(a) == a

0 commit comments

Comments
 (0)