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

sparse vector divide by float gives full vector #14111

Closed
augustinyiptong opened this issue Nov 23, 2015 · 5 comments
Closed

sparse vector divide by float gives full vector #14111

augustinyiptong opened this issue Nov 23, 2015 · 5 comments
Labels
sparse Sparse arrays

Comments

@augustinyiptong
Copy link
Contributor

Hi

I have:

julia> s = sparsevec([1.0, 2.0, 3.0])
Sparse vector of length 3, with 3 Float64 nonzero entries:
  [1]  =  1.0
  [2]  =  2.0
  [3]  =  3.0

I do:

julia> s * 0.5
Sparse vector of length 3, with 3 Float64 nonzero entries:
  [1]  =  0.5
  [2]  =  1.0
  [3]  =  1.5

So far so good. If i do:

julia> s/2.0
 3-element Array{Float64,1}:
 0.5
 1.0
 1.5

Why does it get converted to a full vector?

I am using Julia Version 0.5.0-dev+1158

Thanks, Gus

@yuyichao yuyichao added the sparse Sparse arrays label Nov 23, 2015
@nalimilan
Copy link
Member

Looks like an oversight, as sparsity is preserved for sparse matrices.

@augustinyiptong If you call @which s.*1.0 and @less s.*1.0, you'll see that multiplication is defined very simply by calling scale. This can be a good workaround at least, multiplying by 1/a.

A better fix would be to add a function like this:

import Base.SparseArrays.nonzeroinds
./(x::AbstractSparseVector, a::Number) = SparseVector(length(x), copy(nonzeroinds(x)), nonzeros(x)./a)

@tkelman
Copy link
Contributor

tkelman commented Nov 24, 2015

be sure to check for a zero (edit: or NaN) denominator.

@ViralBShah
Copy link
Member

We can do the obvious fix for now - but I think sparse solvers can generally do better with sparse RHS. I'm not sure what the state of the art is on this question today though.

@tkelman
Copy link
Contributor

tkelman commented Nov 24, 2015

Unless your matrix is diagonal, solving a linear system of equations doesn't generally preserve sparsity in the right hand side. Simplex is the usual poster child algorithm where sparse vectors play a crucial role.

This issue is just about division by a scalar though, so "solvers" aren't so relevant here.

@tkelman
Copy link
Contributor

tkelman commented Mar 25, 2016

closed by #15579

@tkelman tkelman closed this as completed Mar 25, 2016
tkelman pushed a commit to tkelman/julia that referenced this issue Mar 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
sparse Sparse arrays
Projects
None yet
Development

No branches or pull requests

5 participants