-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
Strange behaviour of map and filter for sparse matrices (vectors) #6918
Comments
(Just a technical note: |
No problem... but it looks like a bug. It would nice be have general |
Well, the julia> map(b -> if b<0.2 0.0 else b end,a)
1000000x1 sparse matrix with 779 Float64 entries:
[69 , 1] = 0.816295
[859 , 1] = 0.620671
[7695 , 1] = 0.65508
[8110 , 1] = 0.402167
[8112 , 1] = 0.461852
[9201 , 1] = 0.432456
[9705 , 1] = 0.421811
⋮ |
But why we are not trying to perform |
That doesn't work in general. Imagine if you had |
Maybe at least error should be more explanatory and we need to check the mapping function for inconsistencies first! |
You may be interested in the discussion in #6548. |
Sounds attractive. Only |
Here is a minimal example: julia> A=sparse([1.0]); filter(x->x==0, A)
ERROR: BoundsError()
in getindex at sparse/sparsematrix.jl:718
in getindex at abstractarray.jl:368
in filter at array.jl:1216
julia> A=sparse([1.0]); filter(x->x==1, A)
1x1 sparse matrix with 1 Float64 entries:
[1, 1] = 1.0 For some reason, |
The first case involves the call |
Bump. Sparse lacks indexing with a single index, except for linear indexing with an Int. Here we need |
Cc @tanmaykm too. |
We should try fix this for 0.3 |
I think that the same discussion as we had on Thanks for kicking the tires on all this. These have always been open questions, and these discussions are getting some answers and clarity on behaviour. |
My consideration is due to very slow executions of |
For the sparse indexing stuff, I have opened #7023 |
I really think the only thing we can do here for |
That seems reasonable. |
+1 for @StefanKarpinski's solution |
+1 |
Wise solution, +1 |
Doesn't Stefan's solution assume that your function is pure? While this is statistically reasonable, it seems potentially problematic to me. |
Fair point, but I'm not sure calling |
I agree that it's weird, but I'm not convinced having Imagine that you have a function like |
Interesting point; a function like that is kind of reasonable to use with |
Yes, I prefer not supporting |
That's fine with me. We should provide a useful error message though and not just remove the method. |
To play devil's advocate here, if you use a random function like that to map over a sparse array, then you randomly get an error or a result, which is kind of what you were asking for, no? The common case here is clearly applying a pure function that takes zero to zero, in which case my proposal does exactly what you want. |
My take is that we usually want to ensure that we don't fail silently in the worst case, even if this makes the common case a little more difficult. |
Also, worth noting that this proposal would make |
Applying a function with a random behavior using |
I can definitely see why you might want to allow this behavior for convenience, but the proposal at hand is to make |
The underlying sparse indexing issue causing the failure reported here is now fixed by #7047. For a discussion on |
@johnmyleswhite, that depends on what you consider the semantics of map to be. Does |
Replying to @StefanKarpinski here although the response might belong in #7157. I totally agree that you could define the meaning of |
I have |
I have to point out that I did not consider side effects when I implemented |
The concept of stored values shouldn't leak through to high-level functions like these. |
@mlubin It doesn't have to, as the difference is invisible as long as the passed function is pure. |
Please consider the following:
The same reproduces perfectly for matrices as well!
The text was updated successfully, but these errors were encountered: