From f488610e4b851d8fbaa8f868d74a2494562c3272 Mon Sep 17 00:00:00 2001 From: Sacha Verweij Date: Sat, 31 Dec 2016 00:30:11 -0800 Subject: [PATCH] Deprecate a few remaining vectorized methods over SparseVectors. (And associated cleanup.) --- base/sparse/higherorderfns.jl | 1 - base/sparse/sparsevector.jl | 33 +++++++++++++-------------------- test/sparse/sparsevector.jl | 8 ++++---- 3 files changed, 17 insertions(+), 25 deletions(-) diff --git a/base/sparse/higherorderfns.jl b/base/sparse/higherorderfns.jl index 2887eec75291e..9effb8666ee60 100644 --- a/base/sparse/higherorderfns.jl +++ b/base/sparse/higherorderfns.jl @@ -141,7 +141,6 @@ end return SparseMatrixCSC(shape..., pointers, storedinds, storedvals) end # Ambiguity killers, TODO: nix conflicting specializations -broadcast(::typeof(abs), x::SparseVector) = _noshapecheck_map(abs, x) # base/sparse/sparevectors.jl: 921 ambiguityfunnel{Tf}(f::Tf, x, y) = _aresameshape(x, y) ? _noshapecheck_map(f, x, y) : _diffshape_broadcast(f, x, y) broadcast(::typeof(+), x::SparseVector, y::SparseVector) = ambiguityfunnel(+, x, y) # base/sparse/sparsevectors.jl:1266 broadcast(::typeof(-), x::SparseVector, y::SparseVector) = ambiguityfunnel(-, x, y) # base/sparse/sparsevectors.jl:1266 diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index a63beb7ac3310..f5bad384f9148 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -918,14 +918,8 @@ hvcat{T}(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) = Base.type ### Unary Map # zero-preserving functions (z->z, nz->nz) -broadcast(::typeof(abs), x::AbstractSparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), abs.(nonzeros(x))) -for op in [:conj] - @eval begin - $(op)(x::AbstractSparseVector) = - SparseVector(length(x), copy(nonzeroinds(x)), $(op).(nonzeros(x))) - end -end --(x::AbstractSparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), -(nonzeros(x))) +conj(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), conj(nonzeros(x))) +-(x::SparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), -(nonzeros(x))) # functions f, such that # f(x) can be zero or non-zero when x != 0 @@ -1271,18 +1265,17 @@ end # definition of other binary functions -for (op, TF, mode) in [(:max, :Real, 2), - (:min, :Real, 2), - (:complex, :Real, 1)] - @eval begin - $(op){Tx<:$(TF),Ty<:$(TF)}(x::AbstractSparseVector{Tx}, y::AbstractSparseVector{Ty}) = - _binarymap($(op), x, y, $mode) - $(op){Tx<:$(TF),Ty<:$(TF)}(x::StridedVector{Tx}, y::AbstractSparseVector{Ty}) = - _binarymap($(op), x, y, $mode) - $(op){Tx<:$(TF),Ty<:$(TF)}(x::AbstractSparseVector{Tx}, y::StridedVector{Ty}) = - _binarymap($(op), x, y, $mode) - end -end +broadcast{Tx<:Real,Ty<:Real}(::typeof(min), x::SparseVector{Tx}, y::SparseVector{Ty}) = _binarymap(min, x, y, 2) +broadcast{Tx<:Real,Ty<:Real}(::typeof(min), x::StridedVector{Tx}, y::SparseVector{Ty}) = _binarymap(min, x, y, 2) +broadcast{Tx<:Real,Ty<:Real}(::typeof(min), x::SparseVector{Tx}, y::SparseVector{Ty}) = _binarymap(min, x, y, 2) + +broadcast{Tx<:Real,Ty<:Real}(::typeof(max), x::SparseVector{Tx}, y::SparseVector{Ty}) = _binarymap(max, x, y, 2) +broadcast{Tx<:Real,Ty<:Real}(::typeof(max), x::StridedVector{Tx}, y::SparseVector{Ty}) = _binarymap(max, x, y, 2) +broadcast{Tx<:Real,Ty<:Real}(::typeof(max), x::SparseVector{Tx}, y::SparseVector{Ty}) = _binarymap(max, x, y, 2) + +complex{Tx<:Real,Ty<:Real}(x::StridedVector{Tx}, y::AbstractSparseVector{Ty}) = _binarymap(complex, x, y, 1) +complex{Tx<:Real,Ty<:Real}(x::AbstractSparseVector{Tx}, y::StridedVector{Ty}) = _binarymap(complex, x, y, 1) +complex{Tx<:Real,Ty<:Real}(x::AbstractSparseVector{Tx}, y::AbstractSparseVector{Ty}) = _binarymap(complex, x, y, 1) ### Reduction diff --git a/test/sparse/sparsevector.jl b/test/sparse/sparsevector.jl index 08639cd4f167d..80ef3784bc313 100644 --- a/test/sparse/sparsevector.jl +++ b/test/sparse/sparsevector.jl @@ -617,11 +617,11 @@ let x = spv_x1, x2 = spv_x2 @test x .* Array(x2) == Array(xm) # max & min - @test exact_equal(max(x, x), x) - @test exact_equal(min(x, x), x) - @test exact_equal(max(x, x2), + @test exact_equal(max.(x, x), x) + @test exact_equal(min.(x, x), x) + @test exact_equal(max.(x, x2), SparseVector(8, Int[1, 2, 6], Float64[3.25, 4.0, 3.5])) - @test exact_equal(min(x, x2), + @test exact_equal(min.(x, x2), SparseVector(8, Int[2, 5, 6, 7], Float64[1.25, -0.75, -5.5, -6.0])) end