Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,45 @@ end

### Generic functions operating on AbstractSparseVector

## Explicit efficient comparisons with vectors

function ==(A::AbstractCompressedVector,
B::AbstractCompressedVector)
# Different sizes are always different
size(A) ≠ size(B) && return false
# Compare nonzero elements
i, j = 1, 1
@inbounds while i <= nnz(A) && j <= nnz(B)
if nonzeroinds(A)[i] == nonzeroinds(B)[j]
nonzeros(A)[i] == nonzeros(B)[j] || return false
i += 1
j += 1
elseif nonzeroinds(A)[i] <= nonzeroinds(B)[j]
iszero(nonzeros(A)[i]) || return false
i += 1
else # nonzeroinds(A)[i] >= nonzeroinds(B)[j]
iszero(nonzeros(B)[j]) || return false
j += 1
end
end

@inbounds for k in i:nnz(A)
iszero(nonzeros(A)[k]) || return false
end

@inbounds for k in j:nnz(B)
iszero(nonzeros(B)[k]) || return false
end

return true
end

==(A::Transpose{<:Any,<:AbstractCompressedVector},
B::Transpose{<:Any,<:AbstractCompressedVector}) = transpose(A) == transpose(B)

==(A::Adjoint{<:Any,<:AbstractCompressedVector},
B::Adjoint{<:Any,<:AbstractCompressedVector}) = adjoint(A) == adjoint(B)

### getindex

function _spgetindex(m::Int, nzind::AbstractVector{Ti}, nzval::AbstractVector{Tv}, i::Integer) where {Tv,Ti}
Expand Down
3 changes: 2 additions & 1 deletion test/higherorderfns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,8 @@ end
@test ((_, x) -> x).(Int, spzeros(3)) == spzeros(3)
@test ((_, _, x) -> x).(Int, Int, spzeros(3)) == spzeros(3)
@test ((_, _, _, x) -> x).(Int, Int, Int, spzeros(3)) == spzeros(3)
@test_broken ((_, _, _, _, x) -> x).(Int, Int, Int, Int, spzeros(3)) == spzeros(3)
@test ((_, _, _, _, x) -> x).(Int, Int, Int, Int, spzeros(3)) == spzeros(3)
@test_broken typeof(((_, _, _, _, x) -> x).(Int, Int, Int, Int, spzeros(3))) == typeof(spzeros(3))
end

using SparseArrays.HigherOrderFns: SparseVecStyle, SparseMatStyle
Expand Down
2 changes: 1 addition & 1 deletion test/issues.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ include("simplesmatrix.jl")
@testset "Issue #15" begin
s = sparse([1, 2], [1, 2], [10, missing])
d = Matrix(s)

s2 = sparse(d)

@test s2[1, 1] == 10
Expand Down
30 changes: 30 additions & 0 deletions test/sparsematrix_ops.jl
Original file line number Diff line number Diff line change
Expand Up @@ -530,4 +530,34 @@ Base.transpose(x::Counting) = Counting(transpose(x.elt))
end
end


@testset "Issue #246" begin
for t in [Int, UInt8, Float64]
a = Counting.(sprand(t, 100, 0.5))
b = Counting.(sprand(t, 100, 0.5))

c = if nnz(a) != 0
c = copy(a)
nonzeros(c)[1] = 0
c
else
c = copy(a)
push!(nonzeros(c), zero(t))
push!(nonzerosinds(c), 1)
c
end
d = dropzeros(c)

for m in [identity, transpose, adjoint]
ma, mb, mc, md = m.([a, b, c, d])

resetcounter()
ma == mb
@test getcounter() <= nnz(a) + nnz(b)

@test (mc == md) == (Array(mc) == Array(md))
end
end
end

end # module