From 9511405d57d2ac110f1743712593f8eb137c400e Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Fri, 1 Apr 2022 09:41:01 +0200 Subject: [PATCH 1/2] maximum & minimum & extrema with init --- test/reduce.jl | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/test/reduce.jl b/test/reduce.jl index 3a8f9532b4710..7791bacc8c1ab 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -400,6 +400,15 @@ A = circshift(reshape(1:24,2,3,4), (0,1,1)) maximum([NaN;zeros(255);missing]) === missing end +@testset "maximum & minimum & extrema with init" begin + @test maximum(1:10, init=11) == 11 + @test maximum(1:10, init=0) == 10 + @test minimum(1:10, init=11) == 1 + @test minimum(1:10, init=0) == 0 + @test extrema(1:10, init=(0, 5)) == (0, 10) + @test extrema(1:10, init=(5, 11)) == (1, 11) +end + # findmin, findmax, argmin, argmax @testset "findmin(f, domain)" begin From ada79a0164c77fb77dad1eaa95ea5ee77a2feb18 Mon Sep 17 00:00:00 2001 From: Fabian Greimel Date: Fri, 1 Apr 2022 09:51:16 +0200 Subject: [PATCH 2/2] update docstrings for minimum, maximum, extrema --- base/reduce.jl | 57 ++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 46 insertions(+), 11 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index 13e1b69c79ede..fca04bd7f120b 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -724,19 +724,29 @@ Returns the largest element in a collection. The value returned for empty `itr` can be specified by `init`. It must be a neutral element for `max` (i.e. which is less than or equal to any other element) as it is unspecified whether `init` is used -for non-empty collections. +for non-empty collections. If `init > maximum(itr)`, return `init`. !!! compat "Julia 1.6" Keyword argument `init` requires Julia 1.6 or later. # Examples ```jldoctest -julia> maximum(-20.5:10) +julia> x = -20.5:10 +-20.5:1.0:9.5 + +julia> maximum(x) 9.5 -julia> maximum([1,2,3]) +julia> y = [1 2 3] +1×3 Matrix{Int64}: + 1 2 3 + +julia> maximum(y) 3 +julia> maximum(y, init=maximum(x)) == maximum(x ∪ y) == 9.5 +true + julia> maximum(()) ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer Stacktrace: @@ -756,19 +766,29 @@ Returns the smallest element in a collection. The value returned for empty `itr` can be specified by `init`. It must be a neutral element for `min` (i.e. which is greater than or equal to any other element) as it is unspecified whether `init` is used -for non-empty collections. +for non-empty collections. If `init < minimum(itr)`, return `init`. !!! compat "Julia 1.6" Keyword argument `init` requires Julia 1.6 or later. # Examples ```jldoctest -julia> minimum(-20.5:10) +julia> x = -20.5:10 +-20.5:1.0:9.5 + +julia> minimum(x) -20.5 -julia> minimum([1,2,3]) +julia> y = [1 2 3] +1×3 Matrix{Int64}: + 1 2 3 + +julia> minimum(y) 1 +julia> minimum(y, init=minimum(x)) == minimum(x ∪ y) == -20.5 +true + julia> minimum([]) ERROR: MethodError: reducing over an empty collection is not allowed; consider supplying `init` to the reducer Stacktrace: @@ -789,20 +809,35 @@ as a 2-tuple. The value returned for empty `itr` can be specified by `init`. It must be a 2-tuple whose first and second elements are neutral elements for `min` and `max` respectively (i.e. which are greater/less than or equal to any other element). As a consequence, when -`itr` is empty the returned `(mn, mx)` tuple will satisfy `mn ≥ mx`. When `init` is -specified it may be used even for non-empty `itr`. +`itr` is empty the returned `(mn, mx)` tuple will satisfy `mn ≥ mx`. If the `init` contains +a non-neutral element, this element is returned as appropriate. (If `init[1] < minimum(itr)`, +`mn == init[1]`. If `init[2] > maximum(itr)`, `mx == init[2]`.) When `init` is specified it +may be used even for non-empty `itr`. !!! compat "Julia 1.8" Keyword argument `init` requires Julia 1.8 or later. # Examples ```jldoctest -julia> extrema(2:10) -(2, 10) +julia> x = 1:5 +1:5 + +julia> extrema(x) +(1, 5) + +julia> y = [9 pi 4.5] +1×3 Matrix{Float64}: + 9.0 3.14159 4.5 -julia> extrema([9,pi,4.5]) +julia> extrema(x) +(1, 5) + +julia> extrema(y) (3.141592653589793, 9.0) +julia> extrema(y, init=extrema(x)) == extrema(x ∪ y) == (1.0, 9.0) +true + julia> extrema([]; init = (Inf, -Inf)) (Inf, -Inf) ```