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

findmin(A; dims) ignores first value if index is 0 #38660

Open
cmcaine opened this issue Dec 2, 2020 · 4 comments · May be fixed by #45822 or #55318
Open

findmin(A; dims) ignores first value if index is 0 #38660

cmcaine opened this issue Dec 2, 2020 · 4 comments · May be fixed by #45822 or #55318
Labels
bug Indicates an unexpected problem or unintended behavior correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing search & find The find* family of functions

Comments

@cmcaine
Copy link
Contributor

cmcaine commented Dec 2, 2020

Index can be zero if you're using OffsetArrays.jl or similar. Also
affects findmax, argmin, argmax.

If dims is omitted or : then you don't get this bug :)

Found while working on #35316 and I'll fix it there edit: I might fix it later, PR already too big.

Reproducible example:

julia> using OffsetArrays

julia> ov = OffsetVector([-1, 1], 0:1)
2-element OffsetArray(::Vector{Int64}, 0:1) with eltype Int64 with indices 0:1:
 -1
  1

julia> findmin(ov, dims=1) # Should be ([-1], [0])
([1], [1])
@FedericoStra
Copy link
Contributor

FedericoStra commented Dec 3, 2020

The problem appears to be that, given A = ov and ri = Base.reduced_indices0(A, 1) as defined in _findmin, we go in the else branch and findminmax! gets called with arguments

julia> Rval = fill!(similar(A, ri), first(A))
1-element OffsetArray(::Vector{Int64}, 0:0) with eltype Int64 with indices 0:0:
 -1

julia> Rind = zeros(eltype(keys(A)), ri)
1-element OffsetArray(::Vector{Int64}, 0:0) with eltype Int64 with indices 0:0:
 0

but, as the comment above findminmax! says, "The initial values of Rval are not used if the corresponding indices in Rind are 0".

Ultimately, the problem is that 0 is a valid sentinel value for an index when working with 1-based arrays, but not when working with OffsetArrays.

@cmcaine
Copy link
Contributor Author

cmcaine commented Dec 4, 2020 via email

@FedericoStra
Copy link
Contributor

FedericoStra commented Dec 4, 2020

maybe I can find a clever way around it

Making Rind::Array{Union{Nothing,Int},N} seems the most obvious and laziest thing to do in order to change the least amount of code, but almost surely there are better ways to work around it in a more efficient manner.

@N5N3 N5N3 added bug Indicates an unexpected problem or unintended behavior search & find The find* family of functions labels Jun 24, 2022
@N5N3
Copy link
Member

N5N3 commented Jun 24, 2022

Maybe we can replace zero(eltype(keys(A))) with prevind(A, first(keys(A))) to indicate the state of initialization.

@N5N3 N5N3 linked a pull request Jun 26, 2022 that will close this issue
@LilithHafner LilithHafner added the correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing label Aug 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior correctness bug ⚠ Bugs that are likely to lead to incorrect results in user code without throwing search & find The find* family of functions
Projects
None yet
4 participants