Skip to content

Commit

Permalink
added extrema(A,dims)
Browse files Browse the repository at this point in the history
  • Loading branch information
bjarthur committed Mar 18, 2016
1 parent fdbcdf7 commit 75577a6
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 8 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,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_cartesian!(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
squeeze(B, tuple(find([sB...].==1)...))
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
3 changes: 3 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ 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_cartesian([1 2 3; 4 5 6; 7 8 9], 1) == [(1,7),(2,8),(3,9)]
@test extrema_cartesian([1 2 3; 4 5 6; 7 8 9], 2) == [(1,3),(4,6),(7,9)]

# any & all

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

0 comments on commit 75577a6

Please sign in to comment.