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

Incorrect filtering of subarrays #4763

Closed
magistere opened this issue Nov 9, 2013 · 10 comments
Closed

Incorrect filtering of subarrays #4763

magistere opened this issue Nov 9, 2013 · 10 comments

Comments

@magistere
Copy link
Contributor

filter produces incorrect output for subarrays:

julia> a = [1:10]
10-element Array{Int32,1}:
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10

julia> b = sub(a, 5:8)
4-element SubArray{Int32,1,Array{Int32,1},(Range1{Int32},)}:
 5
 6
 7
 8

julia> filter(x -> x < 7, b)
4-element Array{Int32,1}:
 5
 5
 4
 4

julia> filter(x -> x < 7, b[:])
2-element Array{Int32,1}:
 5
 6
@timholy
Copy link
Sponsor Member

timholy commented Nov 9, 2013

The issue seems to be that we're missing an implementation of logical indexing for SubArrays. It's being interpreted as b([1,1,0,0]), and because of the lack of bounds-checking on SubArray indexing no error is thrown.

@ivarne
Copy link
Sponsor Member

ivarne commented Nov 9, 2013

@timholy beat me to it.

The problem is that filter creates a boolean array that is used for indexing. Subarray does not support a boolean array for indexing.

The method that gets invoked for boolean array indexing is

function getindex{T,S<:Integer}(s::SubArray{T,1}, I::AbstractVector{S})

because Bool is a subtype of integer. I think this will be fixed if we define

function getindex{T,S<:Integer}(s::SubArray{T,1}, I::AbstractVector{Bool})

@timholy
Copy link
Sponsor Member

timholy commented Nov 9, 2013

Agreed. I'll let you beat me this time, if you're game :-).

@ivarne
Copy link
Sponsor Member

ivarne commented Nov 9, 2013

I'll give it a try, but probably it will not be good enough for Stefan.

@ivarne
Copy link
Sponsor Member

ivarne commented Nov 9, 2013

And I will not have time to read subarray.jl and understand how arrays and dimensions work until Monday. An implementation just raising an error might be good before we release 0.2.

@StefanKarpinski
Copy link
Sponsor Member

I'll give it a try, but probably it will not be good enough for Stefan.

I'm not sure where I got this stern reputation from. Is it the viking horns?

@magistere
Copy link
Contributor Author

Yeah, horns are awesome :)

@magistere
Copy link
Contributor Author

Tim and Ivar, thank you for pointing out the root of the problem.

@ivarne
Copy link
Sponsor Member

ivarne commented Nov 10, 2013

@StefanKarpinski I mentioned you because I often see you saying NO to slow implementations and new language features with too specific use cases. Too me it looks like you're doing a terrific job to make the language pleasant, consistent and easy to learn.

The subarray code is also performance critical, so it is highly likely that there is a better way to implement it than what I would end up with.

@StefanKarpinski
Copy link
Sponsor Member

Ah, yes. Saying "no" is definitely a huge part of my job :-)

The current SubArray code is not exactly immaculate, so I'm a little less protective of it. The whole thing is badly in need of an overhaul.

ivarne added a commit to ivarne/julia that referenced this issue Nov 18, 2013
timholy added a commit that referenced this issue Nov 18, 2013
Implement logical indexing for SubArray. Fixes #4763
ivarne added a commit to ivarne/julia that referenced this issue Mar 4, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants