From abc56aca9c009daf7409475f74cc5dedc1b35a43 Mon Sep 17 00:00:00 2001 From: Alexey Stukalov Date: Thu, 9 Aug 2018 11:55:04 +0200 Subject: [PATCH] maximum(abs, T[]) should throw an error fix maximum(abs/abs2, ::AbstractSparseVector) --- base/reduce.jl | 3 --- stdlib/SparseArrays/src/sparsevector.jl | 2 ++ test/reduce.jl | 6 ++++-- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/base/reduce.jl b/base/reduce.jl index 0fbcc5a8a2a45..bfcebb352e8e5 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -252,9 +252,6 @@ mapreduce_empty(::typeof(identity), op, T) = reduce_empty(op, T) mapreduce_empty(::typeof(abs), op, T) = abs(reduce_empty(op, T)) mapreduce_empty(::typeof(abs2), op, T) = abs2(reduce_empty(op, T)) -mapreduce_empty(f::typeof(abs), ::typeof(max), T) = abs(zero(T)) -mapreduce_empty(f::typeof(abs2), ::typeof(max), T) = abs2(zero(T)) - mapreduce_empty_iter(f, op, itr, ::HasEltype) = mapreduce_empty(f, op, eltype(itr)) mapreduce_empty_iter(f, op::typeof(&), itr, ::EltypeUnknown) = true mapreduce_empty_iter(f, op::typeof(|), itr, ::EltypeUnknown) = false diff --git a/stdlib/SparseArrays/src/sparsevector.jl b/stdlib/SparseArrays/src/sparsevector.jl index e7f0e1a2db26a..375566838e3b5 100644 --- a/stdlib/SparseArrays/src/sparsevector.jl +++ b/stdlib/SparseArrays/src/sparsevector.jl @@ -1323,6 +1323,8 @@ for f in [:sum, :maximum, :minimum], op in [:abs, :abs2] SV = :AbstractSparseVector if f == :minimum @eval ($f)(::typeof($op), x::$SV{T}) where {T<:Number} = nnz(x) < length(x) ? ($op)(zero(T)) : ($f)($op, nonzeros(x)) + elseif f == :maximum + @eval ($f)(::typeof($op), x::$SV{T}) where {T<:Number} = length(x) > 0 && nnz(x) == 0 ? ($op)(zero(T)) : ($f)($op, nonzeros(x)) else @eval ($f)(::typeof($op), x::$SV) = ($f)($op, nonzeros(x)) end diff --git a/test/reduce.jl b/test/reduce.jl index f2bdd30d21684..c35457be7682e 100644 --- a/test/reduce.jl +++ b/test/reduce.jl @@ -50,8 +50,10 @@ using .Main.OffsetArrays @test mapreduce(-, +, Vector(range(1.0, stop=10000.0, length=10000))) == -50005000.0 # empty mr @test mapreduce(abs2, +, Float64[]) === 0.0 -@test mapreduce(abs2, max, Float64[]) === 0.0 -@test mapreduce(abs, max, Float64[]) === 0.0 +@test_throws ArgumentError mapreduce(abs, max, Float64[]) +@test_throws ArgumentError mapreduce(abs2, max, Float64[]) +@test_throws ArgumentError mapreduce(abs, min, Float64[]) +@test_throws ArgumentError mapreduce(abs2, min, Float64[]) @test_throws ArgumentError mapreduce(abs2, &, Float64[]) @test_throws ArgumentError mapreduce(abs2, |, Float64[])