Skip to content

Commit 2362bcf

Browse files
Moelfstevengj
authored andcommitted
fix findmin/findmax (#93)
Co-authored-by: Steven G. Johnson <[email protected]>
1 parent aa51c9b commit 2362bcf

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/sparsevector.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1434,7 +1434,12 @@ for (fun, comp, word) in ((:findmin, :(<), "minimum"), (:findmax, :(>), "maximum
14341434
# we try to avoid findfirst(iszero, x)
14351435
sindex = findfirst(iszero, nzvals) # first stored zero, if any
14361436
zindex = findfirst(i -> i < nzinds[i], eachindex(nzinds)) # first non-stored zero
1437-
index = isnothing(sindex) ? zindex : min(sindex, zindex)
1437+
index = if isnothing(sindex)
1438+
# non-stored zero are contiguous and at the end
1439+
isnothing(zindex) && last(nzinds) < lastindex(x) ? last(nzinds) + 1 : zindex
1440+
else
1441+
min(sindex, zindex)
1442+
end
14381443
return zeroval, index
14391444
end
14401445
end

test/sparsevector.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,24 @@ end
898898
@test_throws ArgumentError findmin(x)
899899
@test_throws ArgumentError findmax(x)
900900
end
901+
902+
let v = spzeros(3) #Julia #44978
903+
v[1] = 2
904+
@test argmin(v) == 2
905+
@test argmax(v) == 1
906+
v[2] = 2
907+
@test argmin(v) == 3
908+
v[1] = 0
909+
v[2] = 0
910+
v[3] = 2
911+
@test argmin(v) == 1
912+
@test argmax(v) == 3
913+
end
914+
915+
let v = spzeros(3) #Julia #44978
916+
v[3] = 2
917+
@test argmax(v) == 3
918+
end
901919
end
902920

903921
### linalg

0 commit comments

Comments
 (0)