-
-
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
Fix find() when called with function and non-StridedArray #8323
Conversation
The previous code expected testfun to be vectorized, which does not match the behavior of other methods, nor the documentation. Especially visible when passing a sparse matrix. Always return an Array rather than an object similar to the input, because it makes no sense to return a sparse vector. Remove the method matching any object so that an error is raised rather than returning incorrect results on non-AbstractArray (e.g. Set).
I realize this change has the drawback that scalars are only supported if they are julia> find(s -> ismatch(r"a", s), ["az"])
1-element Array{Int64,1}:
1
julia> find(s -> ismatch(r"a", s), "az")
ERROR: `find` has no method matching find(::Function, ::ASCIIString) But if |
See also my answer to the other post about the problem.
|
@@ -1082,7 +1082,7 @@ function find(A::StridedArray) | |||
end | |||
|
|||
find(x::Number) = x == 0 ? Array(Int,0) : [1] | |||
find(testf::Function, x) = find(testf(x)) | |||
find(testf::Function, x::Number) = testf(x) == 0 ? Array(Int,0) : [1] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be !testf(x)
instead of testf(x)==0
, but false == 0 so this works.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, not pretty. :-/ I could have fixed that. Worth another commit?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure, but a commit is cheap: 6b3276f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but for me it implies bothering somebody with a pull request.
While we're tweaking style, how about writing it testf(x) ? [1] : Array(Int,0)
instead? :-p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or
testf(x) ? Int[1] : Int[]
Which will also make the function type stable. (Sorry! Array constructors confuse me)
I don't think Pull requests is bothering for small changes like this, that are obviously correct. A unrelated discussion that goes on and on, is a different matter tough.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or testf(x) ? [1] : Int[]
?!
Fix find() when called with function and non-StridedArray
The previous code expected testfun to be vectorized, which does not match the behavior of other methods, nor the documentation. Especially visible when passing a sparse matrix. Always return an Array rather than an object similar to the input, because it makes no sense to return a sparse vector. Remove the method matching any object so that an error is raised rather than returning incorrect results on non-AbstractArray (e.g. Set). Backport of: ae641c9 Issue: #8323
Backported in f0214c3 |
The previous code expected testfun to be vectorized, which does not match
the behavior of other methods, nor the documentation. Especially visible
when passing a sparse matrix. Always return an Array rather than an object
similar to the input, because it makes no sense to return a sparse vector.
Remove the method matching any object so that an error is raised rather
than returning incorrect results on non-AbstractArray (e.g. Set).
This fixes the bug reported by i.pallikarakis-11 on the mailing list, namely:
I'm not completely sure about the
AbstractArray
requirement, but it doesn't make sense to me to return indexes for non-arrays. The previous code would happily work onSet
s, but would return nonsensical results.(BTW, I'm aware that calling
find(testf, x)
withx
a sparse matrix is not very efficient, but at least it must not give wrong results.)