Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #14111 dividing sparse vector by constant should give sparse vector #14963

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion base/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ using Base: Func, AddFun, OrFun, ConjFun, IdFun
using Base.Sort: Forward
using Base.LinAlg: AbstractTriangular, PosDefException

import Base: +, -, *, \, &, |, $, .+, .-, .*, ./, .\, .^, .<, .!=, ==
import Base: +, -, *, /, \, &, |, $, .+, .-, .*, ./, .\, .^, .<, .!=, ==
import Base: A_mul_B!, Ac_mul_B, Ac_mul_B!, At_mul_B, At_mul_B!, At_ldiv_B, Ac_ldiv_B, A_ldiv_B!
import Base.LinAlg: At_ldiv_B!, Ac_ldiv_B!

Expand Down
9 changes: 9 additions & 0 deletions base/sparse/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,15 @@ scale(a::Complex, x::AbstractSparseVector) = scale(x, a)
.*(x::AbstractSparseVector, a::Number) = scale(x, a)
.*(a::Number, x::AbstractSparseVector) = scale(x, a)

function /(x::AbstractSparseVector, a::Number)
if a == 0 || isnan(a)
throw(ArgumentError("Cannot divide sparse vector by provided argument"))
end
SparseVector(length(x), copy(x.nzind), x.nzval/a)
end

./(x::AbstractSparseVector, a::Number) = /(x,a)


# dot

Expand Down
22 changes: 22 additions & 0 deletions test/sparsedir/sparsevector.jl
Original file line number Diff line number Diff line change
Expand Up @@ -838,3 +838,25 @@ x = sparsevec(1:7, [3., 2., -1., 1., -2., -3., 3.], 15)
@test collect(sort(x, by=abs)) == sort(collect(x), by=abs)
@test collect(sort(x, by=sign)) == sort(collect(x), by=sign)
@test collect(sort(x, by=inv)) == sort(collect(x), by=inv)

# Issue 14111
# test vector with no zeros
s14111 = sparsevec([1., 2.5, -3., 8., -4])
a14111 = sparsevec([0.4, 1., -1.2, 3.2, -1.6])
@test exact_equal(s14111/2.5, a14111)
@test exact_equal(s14111./2.5, a14111)
# test vector with all zeros
s14111 = sparsevec(Int64[], Float64[], 10)
a14111 = sparsevec(Int64[], Float64[], 10)
@test exact_equal(s14111/5., a14111)
@test exact_equal(s14111./5., a14111)
# test vector with both zero and nonzero
s14111 = sparsevec([7., 0., -6., 3., 0., 2])
a14111 = sparsevec([1, 3, 4, 6], [2.8, -2.4, 1.2, 0.8], 6)
@test exact_equal(s14111/2.5, a14111)
@test exact_equal(s14111./2.5, a14111)
# test for division by illegal arguments
@test_throws ArgumentError s14111/0.
@test_throws ArgumentError s14111./0.
@test_throws ArgumentError s14111/NaN
@test_throws ArgumentError s14111./NaN