diff --git a/base/deprecated.jl b/base/deprecated.jl index 10185fbe576e7..3893bbe42b8da 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1000,4 +1000,7 @@ macro vectorize_2arg(S,f) end export @vectorize_1arg, @vectorize_2arg +# Deprecate manually vectorized abs2 methods in favor of compact broadcast syntax +@deprecate abs2(x::AbstractSparseVector) abs2.(x) + # End deprecations scheduled for 0.6 diff --git a/base/sparse/sparsevector.jl b/base/sparse/sparsevector.jl index ce610778e0a2d..6b5aaaa78ac1d 100644 --- a/base/sparse/sparsevector.jl +++ b/base/sparse/sparsevector.jl @@ -1,4 +1,4 @@ -# This file is a part of Julia. License is MIT: http://julialang.org/license +9# This file is a part of Julia. License is MIT: http://julialang.org/license ### Common definitions @@ -912,7 +912,8 @@ hvcat{T}(rows::Tuple{Vararg{Int}}, xs::_TypedDenseConcatGroup{T}...) = Base.type ### Unary Map # zero-preserving functions (z->z, nz->nz) -for op in [:abs, :abs2, :conj] +broadcast(::typeof(abs2), x::AbstractSparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), abs2.(nonzeros(x))) +for op in [:abs, :conj] @eval begin $(op)(x::AbstractSparseVector) = SparseVector(length(x), copy(nonzeroinds(x)), $(op).(nonzeros(x))) diff --git a/test/sparsedir/sparsevector.jl b/test/sparsedir/sparsevector.jl index 0aaf4975a29fe..140a9aacbdb95 100644 --- a/test/sparsedir/sparsevector.jl +++ b/test/sparsedir/sparsevector.jl @@ -560,7 +560,7 @@ let x = spv_x1, x2 = x2 = spv_x2 # abs and abs2 @test exact_equal(abs(x), SparseVector(8, [2, 5, 6], abs.([1.25, -0.75, 3.5]))) - @test exact_equal(abs2(x), SparseVector(8, [2, 5, 6], abs2.([1.25, -0.75, 3.5]))) + @test exact_equal(abs2.(x), SparseVector(8, [2, 5, 6], abs2.([1.25, -0.75, 3.5]))) # plus and minus xa = SparseVector(8, [1,2,5,6,7], [3.25,5.25,-0.75,-2.0,-6.0]) @@ -581,7 +581,7 @@ let x = spv_x1, x2 = x2 = spv_x2 # multiplies xm = SparseVector(8, [2, 6], [5.0, -19.25]) - @test exact_equal(x .* x, abs2(x)) + @test exact_equal(x .* x, abs2.(x)) @test exact_equal(x .* x2, xm) @test exact_equal(x2 .* x, xm)