Skip to content

Commit 2bf7818

Browse files
committed
Fix bug in findmin! and findmax!
Also added tests/docs for those functions and exported them
1 parent 4f9e506 commit 2bf7818

File tree

4 files changed

+43
-10
lines changed

4 files changed

+43
-10
lines changed

base/exports.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,8 @@ export
521521
findin,
522522
findmax,
523523
findmin,
524+
findmin!,
525+
findmax!,
524526
findn,
525527
findnext,
526528
findprev,

base/reducedim.jl

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,12 +325,18 @@ function findminmax!{T,N}(f, Rval, Rind, A::AbstractArray{T,N})
325325
Rval, Rind
326326
end
327327

328-
# findmin
328+
329+
"""
330+
findmin!(rval, rind, A, [init=true]) -> (minval, index)
331+
332+
Find the minimum of `A` and the corresponding linear index along singleton
333+
dimensions of `rval` and `rind`, and store the results in `rval` and `rind`.
334+
"""
329335
function findmin!{R}(rval::AbstractArray{R},
330336
rind::AbstractArray,
331337
A::AbstractArray;
332338
init::Bool=true)
333-
findminmax!(LessFun(), initarray!(rval, typemax(R), init), rind, A)
339+
findminmax!(LessFun(), initarray!(rval, MinFun(), init), rind, A)
334340
end
335341

336342
function findmin{T}(A::AbstractArray{T}, region)
@@ -342,12 +348,17 @@ function findmin{T}(A::AbstractArray{T}, region)
342348
zeros(Int, reduced_dims0(A, region)), A)
343349
end
344350

345-
# findmax
351+
"""
352+
findmax!(rval, rind, A, [init=true]) -> (maxval, index)
353+
354+
Find the maximum of `A` and the corresponding linear index along singleton
355+
dimensions of `rval` and `rind`, and store the results in `rval` and `rind`.
356+
"""
346357
function findmax!{R}(rval::AbstractArray{R},
347358
rind::AbstractArray,
348359
A::AbstractArray;
349360
init::Bool=true)
350-
findminmax!(MoreFun(), initarray!(rval, typemin(R), init), rind, A)
361+
findminmax!(MoreFun(), initarray!(rval, MaxFun(), init), rind, A)
351362
end
352363

353364
function findmax{T}(A::AbstractArray{T}, region)

doc/stdlib/collections.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,18 @@ Iterable Collections
347347
348348
For an array input, returns the value and index of the minimum over the given dimensions.
349349

350+
.. function:: findmax!(rval, rind, A, [init=true]) -> (maxval, index)
351+
352+
.. Docstring generated from Julia source
353+
354+
Find the maximum of ``A`` and the corresponding linear index along singleton dimensions of ``rval`` and ``rind``\ , and store the results in ``rval`` and ``rind``\ .
355+
356+
.. function:: findmin!(rval, rind, A, [init=true]) -> (minval, index)
357+
358+
.. Docstring generated from Julia source
359+
360+
Find the minimum of ``A`` and the corresponding linear index along singleton dimensions of ``rval`` and ``rind``\ , and store the results in ``rval`` and ``rind``\ .
361+
350362
.. function:: maxabs(itr)
351363

352364
.. Docstring generated from Julia source

test/reducedim.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,23 @@ R = reducedim((a,b) -> a+b, [1 2; 3 4], 2, 0.0)
113113
rt = Base.return_types(reducedim, Tuple{Function, Array{Float64, 3}, Int, Float64})
114114
@test length(rt) == 1 && rt[1] == Array{Float64, 3}
115115

116+
116117
## findmin/findmax
117118
A = [1.0 3.0 6.0;
118119
5.0 2.0 4.0]
119-
@test findmin(A, (1,)) == ([1.0 2.0 4.0], [1 4 6])
120-
@test findmin(A, (2,)) == (reshape([1.0,2.0], 2, 1), reshape([1,4], 2, 1))
121-
@test findmin(A, (1,2)) == (fill(1.0,1,1),fill(1,1,1))
122-
@test findmax(A, (1,)) == ([5.0 3.0 6.0], [2 3 5])
123-
@test findmax(A, (2,)) == (reshape([6.0,5.0], 2, 1), reshape([5,2], 2, 1))
124-
@test findmax(A, (1,2)) == (fill(6.0,1,1),fill(5,1,1))
120+
for (tup, rval, rind) in [((1,), [1.0 2.0 4.0], [1 4 6]),
121+
((2,), reshape([1.0,2.0], 2, 1), reshape([1,4], 2, 1)),
122+
((1,2), fill(1.0,1,1),fill(1,1,1))]
123+
@test findmin(A, tup) == (rval, rind)
124+
@test findmin!(similar(rval), similar(rind), A) == (rval, rind)
125+
end
126+
127+
for (tup, rval, rind) in [((1,), [5.0 3.0 6.0], [2 3 5]),
128+
((2,), reshape([6.0,5.0], 2, 1), reshape([5,2], 2, 1)),
129+
((1,2), fill(6.0,1,1),fill(5,1,1))]
130+
@test findmax(A, tup) == (rval, rind)
131+
@test findmax!(similar(rval), similar(rind), A) == (rval, rind)
132+
end
125133

126134
# issue #6672
127135
@test sum(Real[1 2 3; 4 5.3 7.1], 2) == reshape([6, 16.4], 2, 1)

0 commit comments

Comments
 (0)