Skip to content

Commit

Permalink
make findmin/findmax behavior match min/max (fix #23209) (#23227)
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC authored and StefanKarpinski committed Aug 31, 2017
1 parent 88b6487 commit 6210e24
Show file tree
Hide file tree
Showing 7 changed files with 428 additions and 95 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,10 @@ This section lists changes that do not have deprecation warnings.
`Tridiagonal{T,V<:AbstractVector{T}}` and `SymTridiagonal{T,V<:AbstractVector{T}}`
respectively ([#22718], [#22925], [#23035], [#23154]).

* When called with an argument that contains `NaN` elements, `findmin` and `findmax` now return the
first `NaN` found and its corresponding index. Previously, `NaN` elements were ignored.
The new behavior matches that of `min`, `max`, `minimum`, and `maximum`.

* `isapprox(x,y)` now tests `norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`
rather than `norm(x-y) <= atol + ...`, and `rtol` defaults to zero
if an `atol > 0` is specified ([#22742]).
Expand Down
36 changes: 16 additions & 20 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,6 @@ function fill!(a::Array{T}, x) where T<:Union{Integer,AbstractFloat}
return a
end


"""
fill(x, dims)
Expand Down Expand Up @@ -1020,7 +1019,6 @@ function _prepend!(a, ::IteratorSize, iter)
a
end


"""
resize!(a::Vector, n::Integer) -> Vector
Expand Down Expand Up @@ -1539,7 +1537,6 @@ function reverse!(v::AbstractVector, s=first(linearindices(v)), n=last(linearind
return v
end


# concatenations of homogeneous combinations of vectors, horizontal and vertical

vcat() = Array{Any,1}(0)
Expand Down Expand Up @@ -1584,7 +1581,6 @@ end

cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x)))


## find ##

"""
Expand Down Expand Up @@ -2054,8 +2050,9 @@ end
findmax(itr) -> (x, index)
Returns the maximum element of the collection `itr` and its index. If there are multiple
maximal elements, then the first one will be returned. `NaN` values are ignored, unless
all elements are `NaN`. Other than the treatment of `NaN`, the result is in line with `max`.
maximal elements, then the first one will be returned.
If any data element is `NaN`, this element is returned.
The result is in line with `max`.
The collection must not be empty.
Expand All @@ -2068,7 +2065,7 @@ julia> findmax([1,7,7,6])
(7, 2)
julia> findmax([1,7,7,NaN])
(7.0, 2)
(NaN, 4)
```
"""
function findmax(a)
Expand All @@ -2079,10 +2076,10 @@ function findmax(a)
mi = i = 1
m, s = next(a, s)
while !done(a, s)
m != m && break
ai, s = next(a, s)
i += 1
ai != ai && continue # assume x != x => x is a NaN
if m != m || isless(m, ai)
if ai != ai || isless(m, ai)
m = ai
mi = i
end
Expand All @@ -2094,8 +2091,9 @@ end
findmin(itr) -> (x, index)
Returns the minimum element of the collection `itr` and its index. If there are multiple
minimal elements, then the first one will be returned. `NaN` values are ignored, unless
all elements are `NaN`. Other than the treatment of `NaN`, the result is in line with `min`.
minimal elements, then the first one will be returned.
If any data element is `NaN`, this element is returned.
The result is in line with `min`.
The collection must not be empty.
Expand All @@ -2108,7 +2106,7 @@ julia> findmin([7,1,1,6])
(1, 2)
julia> findmin([7,1,1,NaN])
(1.0, 2)
(NaN, 4)
```
"""
function findmin(a)
Expand All @@ -2119,10 +2117,10 @@ function findmin(a)
mi = i = 1
m, s = next(a, s)
while !done(a, s)
m != m && break
ai, s = next(a, s)
i += 1
ai != ai && continue
if m != m || isless(ai, m)
if ai != ai || isless(ai, m)
m = ai
mi = i
end
Expand All @@ -2134,8 +2132,7 @@ end
indmax(itr) -> Integer
Returns the index of the maximum element in a collection. If there are multiple maximal
elements, then the first one will be returned. `NaN` values are ignored, unless all
elements are `NaN`.
elements, then the first one will be returned.
The collection must not be empty.
Expand All @@ -2148,7 +2145,7 @@ julia> indmax([1,7,7,6])
2
julia> indmax([1,7,7,NaN])
2
4
```
"""
indmax(a) = findmax(a)[2]
Expand All @@ -2157,8 +2154,7 @@ indmax(a) = findmax(a)[2]
indmin(itr) -> Integer
Returns the index of the minimum element in a collection. If there are multiple minimal
elements, then the first one will be returned. `NaN` values are ignored, unless all
elements are `NaN`.
elements, then the first one will be returned.
The collection must not be empty.
Expand All @@ -2171,7 +2167,7 @@ julia> indmin([7,1,1,6])
2
julia> indmin([7,1,1,NaN])
2
4
```
"""
indmin(a) = findmin(a)[2]
Expand Down
Loading

0 comments on commit 6210e24

Please sign in to comment.