Skip to content

Code clean for extrema #74

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

Merged
merged 1 commit into from
Jan 29, 2022
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
6 changes: 2 additions & 4 deletions src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1921,10 +1921,8 @@ _mapreducezeros(f, op::Union{typeof(Base.mul_prod),typeof(*)},::Type{T}, nzeros:
nzeros == 0 ? op(one(v0), v0) : op(f(zero(T))^nzeros, v0)
_mapreducezeros(f, op::Union{typeof(min),typeof(max)}, ::Type{T}, nzeros::Integer, v0) where {T} =
nzeros == 0 ? v0 : op(v0, f(zero(T)))
if isdefined(Base, :_extrema_rf)
_mapreducezeros(f::Base.ExtremaMap, op::typeof(Base._extrema_rf), ::Type{T}, nzeros::Integer, v0) where {T} =
nzeros == 0 ? v0 : op(v0, f(zero(T)))
end
_mapreducezeros(f::Base.ExtremaMap, op::typeof(Base._extrema_rf), ::Type{T}, nzeros::Integer, v0) where {T} =
nzeros == 0 ? v0 : op(v0, f(zero(T)))

function Base._mapreduce(f, op::typeof(*), ::Base.IndexCartesian, A::AbstractSparseMatrixCSC{T}) where T
nzeros = widelength(A)-nnz(A)
Expand Down
48 changes: 18 additions & 30 deletions test/higherorderfns.jl
Original file line number Diff line number Diff line change
Expand Up @@ -709,51 +709,39 @@ end
@test extrema(f, x) == extrema(f, y)
@test extrema(spzeros(n, n)) == (0.0, 0.0)
@test extrema(spzeros(n)) == (0.0, 0.0)
# TODO: Remove the temporary skip once https://github.com/JuliaLang/julia/pull/43604 is merged
if isdefined(Base, :_extrema_rf)
@test_throws "reducing over an empty" extrema(spzeros(0, 0))
@test_throws "reducing over an empty" extrema(spzeros(0))
end
@test_throws "reducing over an empty" extrema(spzeros(0, 0))
@test_throws "reducing over an empty" extrema(spzeros(0))
@test extrema(sparse(ones(n, n))) == (1.0, 1.0)
@test extrema(sparse(ones(n))) == (1.0, 1.0)
@test extrema(A; dims=:) == extrema(B; dims=:)
@test extrema(A; dims=1) == extrema(B; dims=1)
@test extrema(A; dims=2) == extrema(B; dims=2)
@test extrema(A; dims=(1,2)) == extrema(B; dims=(1,2))
@test extrema(f, A; dims=1) == extrema(f, B; dims=1)
# TODO: Remove the temporary skip once https://github.com/JuliaLang/julia/pull/43604 is merged
if isdefined(Base, :_extrema_rf)
@test_throws "reducing over an empty" extrema(sparse(C); dims=1) == extrema(C; dims=1)
end
@test_throws "reducing over an empty" extrema(sparse(C); dims=1) == extrema(C; dims=1)
@test extrema(A; dims=[]) == extrema(B; dims=[])
@test extrema(x; dims=:) == extrema(y; dims=:)
@test extrema(x; dims=1) == extrema(y; dims=1)
@test extrema(f, x; dims=1) == extrema(f, y; dims=1)
# TODO: Remove the temporary skip once https://github.com/JuliaLang/julia/pull/43604 is merged
if isdefined(Base, :_extrema_rf)
@test_throws "reducing over an empty" extrema(sparse(z); dims=1)
end
@test_throws "reducing over an empty" extrema(sparse(z); dims=1)
@test extrema(x; dims=[]) == extrema(y; dims=[])
end

# TODO: Remove the temporary skip once https://github.com/JuliaLang/julia/pull/43604 is merged
if isdefined(Base, :_extrema_rf)
function test_extrema(a; dims_test = ((), 1, 2, (1,2), 3))
for dims in dims_test
vext = extrema(a; dims)
vmin, vmax = minimum(a; dims), maximum(a; dims)
@test all(x -> isequal(x[1], x[2:3]), zip(vext,vmin,vmax))
end
function test_extrema(a; dims_test = ((), 1, 2, (1,2), 3))
for dims in dims_test
vext = extrema(a; dims)
vmin, vmax = minimum(a; dims), maximum(a; dims)
@test all(x -> isequal(x[1], x[2:3]), zip(vext,vmin,vmax))
end
@testset "NaN test for sparse extrema" begin
for sz = (3, 10, 100)
A = sprand(sz, sz, 0.3)
A[rand(1:sz^2,sz)] .= NaN
test_extrema(A)
A = sprand(sz*sz, 0.3)
A[rand(1:sz^2,sz)] .= NaN
test_extrema(A; dims_test = ((), 1, 2))
end
end
@testset "NaN test for sparse extrema" begin
for sz = (3, 10, 100)
A = sprand(sz, sz, 0.3)
A[rand(1:sz^2,sz)] .= NaN
test_extrema(A)
A = sprand(sz*sz, 0.3)
A[rand(1:sz^2,sz)] .= NaN
test_extrema(A; dims_test = ((), 1, 2))
end
end

Expand Down