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

added extrema(A,dims) #15550

Merged
merged 1 commit into from
Mar 22, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ Library improvements

* Improve performance of `quantile` ([#14413]).

* `extrema` can now operate over a region ([#15550]).

* The new `Base.StackTraces` module makes stack traces easier to use programmatically. ([#14469])

Deprecated or removed
Expand Down
7 changes: 0 additions & 7 deletions base/docs/helpdb/Base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2082,13 +2082,6 @@ appended to an internal buffer of backtraces.
"""
:@profile

"""
extrema(itr)

Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple.
"""
extrema

"""
isdigit(c::Union{Char,AbstractString}) -> Bool

Expand Down
42 changes: 42 additions & 0 deletions base/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ minabs(a) = mapreduce(AbsFun(), MinFun(), a)
extrema(r::Range) = (minimum(r), maximum(r))
extrema(x::Real) = (x, x)

"""
extrema(itr) -> Tuple

Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple.
"""
function extrema(itr)
s = start(itr)
done(itr, s) && throw(ArgumentError("collection must be non-empty"))
Expand All @@ -353,6 +358,43 @@ function extrema(itr)
return (vmin, vmax)
end

"""
extrema(A,dims) -> Array{Tuple}

Compute the minimum and maximum elements of an array over the given dimensions.
"""
function extrema(A::AbstractArray, dims)
sz = [size(A)...]
sz[[dims...]] = 1
B = Array{Tuple{eltype(A),eltype(A)}}(sz...)
extrema!(B, A)
end

@generated function extrema!{T,N}(B, A::Array{T,N})
quote
sA = size(A)
sB = size(B)
@nloops $N i B begin
AI = @nref $N A i
(@nref $N B i) = (AI, AI)
end
Bmax = sB
Istart = ones(Int,ndims(A))
Istart[([sB...].==1) & ([sA...].!=1)] = 2
@inbounds @nloops $N i d->(Istart[d]:size(A,d)) begin
AI = @nref $N A i
@nexprs $N d->(j_d = min(Bmax[d], i_{d}))
BJ = @nref $N B j
if AI < BJ[1]
(@nref $N B j) = (AI, BJ[2])
elseif AI > BJ[2]
(@nref $N B j) = (BJ[1], AI)
end
end
B
end
end

## all & any

any(itr) = any(IdFun(), itr)
Expand Down
8 changes: 7 additions & 1 deletion doc/stdlib/collections.rst
Original file line number Diff line number Diff line change
Expand Up @@ -299,12 +299,18 @@ Iterable Collections

Compute the minimum value of ``A`` over the singleton dimensions of ``r``\ , and write results to ``r``\ .

.. function:: extrema(itr)
.. function:: extrema(itr) -> Tuple

.. Docstring generated from Julia source

Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple.

.. function:: extrema(A,dims) -> Array{Tuple}

.. Docstring generated from Julia source

Compute the minimum and maximum elements of an array over the given dimensions.

.. function:: indmax(itr) -> Integer

.. Docstring generated from Julia source
Expand Down
4 changes: 4 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,10 @@ prod2(itr) = invoke(prod, Tuple{Any}, itr)
@test maximum(collect(Int16(1):Int16(100))) === Int16(100)
@test maximum(Int32[1,2]) === Int32(2)

@test extrema(reshape(1:24,2,3,4),1) == reshape([(1,2),(3,4),(5,6),(7,8),(9,10),(11,12),(13,14),(15,16),(17,18),(19,20),(21,22),(23,24)],1,3,4)
@test extrema(reshape(1:24,2,3,4),2) == reshape([(1,5),(2,6),(7,11),(8,12),(13,17),(14,18),(19,23),(20,24)],2,1,4)
@test extrema(reshape(1:24,2,3,4),3) == reshape([(1,19),(2,20),(3,21),(4,22),(5,23),(6,24)],2,3,1)

# any & all

@test any(Bool[]) == false
Expand Down