From a09e3f89f6d6089378bef5792fa8c053af4fa1c0 Mon Sep 17 00:00:00 2001 From: davidschlegel Date: Tue, 10 Jan 2023 15:59:37 +0100 Subject: [PATCH 1/3] replace !iszero(x) with x !== zero(x) To work with symbolic numbers. --- src/sparsearray.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sparsearray.jl b/src/sparsearray.jl index 025d4af..3b94ca0 100644 --- a/src/sparsearray.jl +++ b/src/sparsearray.jl @@ -31,7 +31,7 @@ Base.@propagate_inbounds Base.getindex(a::SparseArray{T,N}, I::Vararg{Int,N}) wh @inline function Base.setindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N} @boundscheck checkbounds(a, I) - if !iszero(v) + if v !== zero(v) a.data[I] = v else delete!(a.data, I) # does not do anything if there was no key corresponding to I @@ -44,14 +44,14 @@ Base.@propagate_inbounds Base.setindex!(a::SparseArray{T,N}, @inline function increaseindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N} @boundscheck checkbounds(a, I) - iszero(v) && return + v !== zero(v) && return h = a.data index = Base.ht_keyindex2!(h, I) @inbounds begin if index > 0 currentv = h.vals[index] newv = currentv + convert(T, v) - if iszero(newv) + if v !== zero(newv) Base._delete!(h, index) else h.age += 1 @@ -110,7 +110,7 @@ SparseArray{T}(a::AbstractArray{<:Any,N}) where {T,N} = SparseArray{T,N}(a) function SparseArray{T,N}(a::AbstractArray{<:Any,N}) where {T,N} d = SparseArray{T,N}(undef, size(a)) for I in CartesianIndices(a) - iszero(a[I]) && continue + v !== zero(a[I]) && continue d[I] = a[I] end return d From 0495ba6fccaab916b40f6bb3cab9d4f26a960d1b Mon Sep 17 00:00:00 2001 From: davidschlegel Date: Tue, 10 Jan 2023 16:46:40 +0100 Subject: [PATCH 2/3] fix typos --- src/sparsearray.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/sparsearray.jl b/src/sparsearray.jl index 3b94ca0..b2ea9cb 100644 --- a/src/sparsearray.jl +++ b/src/sparsearray.jl @@ -44,14 +44,14 @@ Base.@propagate_inbounds Base.setindex!(a::SparseArray{T,N}, @inline function increaseindex!(a::SparseArray{T,N}, v, I::CartesianIndex{N}) where {T,N} @boundscheck checkbounds(a, I) - v !== zero(v) && return + v == zero(v) && return h = a.data index = Base.ht_keyindex2!(h, I) @inbounds begin if index > 0 currentv = h.vals[index] newv = currentv + convert(T, v) - if v !== zero(newv) + if v == zero(newv) Base._delete!(h, index) else h.age += 1 @@ -89,7 +89,7 @@ function Base._unsafe_getindex(::IndexCartesian, a::SparseArray{T,N}, b = SparseArray{T}(undef, length.(Base.index_shape(indices...))) for (k, v) in a.data newI = _newindices(k.I, indices) - if newI !== nothing + if newI == nothing b[newI...] = v end end @@ -110,7 +110,7 @@ SparseArray{T}(a::AbstractArray{<:Any,N}) where {T,N} = SparseArray{T,N}(a) function SparseArray{T,N}(a::AbstractArray{<:Any,N}) where {T,N} d = SparseArray{T,N}(undef, size(a)) for I in CartesianIndices(a) - v !== zero(a[I]) && continue + v == zero(a[I]) && continue d[I] = a[I] end return d From 8ece5cf05a2a62e22abe8fcd27eb827e111792dd Mon Sep 17 00:00:00 2001 From: davidschlegel Date: Tue, 10 Jan 2023 16:49:34 +0100 Subject: [PATCH 3/3] fix typo --- src/sparsearray.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sparsearray.jl b/src/sparsearray.jl index b2ea9cb..ef959a4 100644 --- a/src/sparsearray.jl +++ b/src/sparsearray.jl @@ -89,7 +89,7 @@ function Base._unsafe_getindex(::IndexCartesian, a::SparseArray{T,N}, b = SparseArray{T}(undef, length.(Base.index_shape(indices...))) for (k, v) in a.data newI = _newindices(k.I, indices) - if newI == nothing + if newI !== nothing b[newI...] = v end end