Skip to content

Commit 6210e24

Browse files
KlausCStefanKarpinski
authored andcommitted
make findmin/findmax behavior match min/max (fix #23209) (#23227)
1 parent 88b6487 commit 6210e24

File tree

7 files changed

+428
-95
lines changed

7 files changed

+428
-95
lines changed

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ This section lists changes that do not have deprecation warnings.
145145
`Tridiagonal{T,V<:AbstractVector{T}}` and `SymTridiagonal{T,V<:AbstractVector{T}}`
146146
respectively ([#22718], [#22925], [#23035], [#23154]).
147147

148+
* When called with an argument that contains `NaN` elements, `findmin` and `findmax` now return the
149+
first `NaN` found and its corresponding index. Previously, `NaN` elements were ignored.
150+
The new behavior matches that of `min`, `max`, `minimum`, and `maximum`.
151+
148152
* `isapprox(x,y)` now tests `norm(x-y) <= max(atol, rtol*max(norm(x), norm(y)))`
149153
rather than `norm(x-y) <= atol + ...`, and `rtol` defaults to zero
150154
if an `atol > 0` is specified ([#22742]).

base/array.jl

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@ function fill!(a::Array{T}, x) where T<:Union{Integer,AbstractFloat}
332332
return a
333333
end
334334

335-
336335
"""
337336
fill(x, dims)
338337
@@ -1020,7 +1019,6 @@ function _prepend!(a, ::IteratorSize, iter)
10201019
a
10211020
end
10221021

1023-
10241022
"""
10251023
resize!(a::Vector, n::Integer) -> Vector
10261024
@@ -1539,7 +1537,6 @@ function reverse!(v::AbstractVector, s=first(linearindices(v)), n=last(linearind
15391537
return v
15401538
end
15411539

1542-
15431540
# concatenations of homogeneous combinations of vectors, horizontal and vertical
15441541

15451542
vcat() = Array{Any,1}(0)
@@ -1584,7 +1581,6 @@ end
15841581

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

1587-
15881584
## find ##
15891585

15901586
"""
@@ -2054,8 +2050,9 @@ end
20542050
findmax(itr) -> (x, index)
20552051
20562052
Returns the maximum element of the collection `itr` and its index. If there are multiple
2057-
maximal elements, then the first one will be returned. `NaN` values are ignored, unless
2058-
all elements are `NaN`. Other than the treatment of `NaN`, the result is in line with `max`.
2053+
maximal elements, then the first one will be returned.
2054+
If any data element is `NaN`, this element is returned.
2055+
The result is in line with `max`.
20592056
20602057
The collection must not be empty.
20612058
@@ -2068,7 +2065,7 @@ julia> findmax([1,7,7,6])
20682065
(7, 2)
20692066
20702067
julia> findmax([1,7,7,NaN])
2071-
(7.0, 2)
2068+
(NaN, 4)
20722069
```
20732070
"""
20742071
function findmax(a)
@@ -2079,10 +2076,10 @@ function findmax(a)
20792076
mi = i = 1
20802077
m, s = next(a, s)
20812078
while !done(a, s)
2079+
m != m && break
20822080
ai, s = next(a, s)
20832081
i += 1
2084-
ai != ai && continue # assume x != x => x is a NaN
2085-
if m != m || isless(m, ai)
2082+
if ai != ai || isless(m, ai)
20862083
m = ai
20872084
mi = i
20882085
end
@@ -2094,8 +2091,9 @@ end
20942091
findmin(itr) -> (x, index)
20952092
20962093
Returns the minimum element of the collection `itr` and its index. If there are multiple
2097-
minimal elements, then the first one will be returned. `NaN` values are ignored, unless
2098-
all elements are `NaN`. Other than the treatment of `NaN`, the result is in line with `min`.
2094+
minimal elements, then the first one will be returned.
2095+
If any data element is `NaN`, this element is returned.
2096+
The result is in line with `min`.
20992097
21002098
The collection must not be empty.
21012099
@@ -2108,7 +2106,7 @@ julia> findmin([7,1,1,6])
21082106
(1, 2)
21092107
21102108
julia> findmin([7,1,1,NaN])
2111-
(1.0, 2)
2109+
(NaN, 4)
21122110
```
21132111
"""
21142112
function findmin(a)
@@ -2119,10 +2117,10 @@ function findmin(a)
21192117
mi = i = 1
21202118
m, s = next(a, s)
21212119
while !done(a, s)
2120+
m != m && break
21222121
ai, s = next(a, s)
21232122
i += 1
2124-
ai != ai && continue
2125-
if m != m || isless(ai, m)
2123+
if ai != ai || isless(ai, m)
21262124
m = ai
21272125
mi = i
21282126
end
@@ -2134,8 +2132,7 @@ end
21342132
indmax(itr) -> Integer
21352133
21362134
Returns the index of the maximum element in a collection. If there are multiple maximal
2137-
elements, then the first one will be returned. `NaN` values are ignored, unless all
2138-
elements are `NaN`.
2135+
elements, then the first one will be returned.
21392136
21402137
The collection must not be empty.
21412138
@@ -2148,7 +2145,7 @@ julia> indmax([1,7,7,6])
21482145
2
21492146
21502147
julia> indmax([1,7,7,NaN])
2151-
2
2148+
4
21522149
```
21532150
"""
21542151
indmax(a) = findmax(a)[2]
@@ -2157,8 +2154,7 @@ indmax(a) = findmax(a)[2]
21572154
indmin(itr) -> Integer
21582155
21592156
Returns the index of the minimum element in a collection. If there are multiple minimal
2160-
elements, then the first one will be returned. `NaN` values are ignored, unless all
2161-
elements are `NaN`.
2157+
elements, then the first one will be returned.
21622158
21632159
The collection must not be empty.
21642160
@@ -2171,7 +2167,7 @@ julia> indmin([7,1,1,6])
21712167
2
21722168
21732169
julia> indmin([7,1,1,NaN])
2174-
2
2170+
4
21752171
```
21762172
"""
21772173
indmin(a) = findmin(a)[2]

0 commit comments

Comments
 (0)