Skip to content

Commit

Permalink
reducedims for arrays with abstract element types. fix JuliaLang#28227
Browse files Browse the repository at this point in the history
  • Loading branch information
KlausC authored and JeffBezanson committed Jul 26, 2018
1 parent 9e7ae17 commit 6f87a00
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 5 deletions.
4 changes: 3 additions & 1 deletion base/reducedim.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,9 @@ for (f1, f2, initval) in ((:min, :max, :Inf), (:max, :min, :(-Inf)))
# but NaNs need to be avoided as intial values
v0 = v0 != v0 ? typeof(v0)($initval) : v0

return reducedim_initarray(A, region, v0)
T = promote_union(eltype(A))
Tr = v0 isa T ? T : typeof(v0)
return reducedim_initarray(A, region, v0, Tr)
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1613,9 +1613,10 @@ end

# In general, output of sparse matrix reductions will not be sparse,
# and computing reductions along columns into SparseMatrixCSC is
# non-trivial, so use Arrays for output
Base.reducedim_initarray(A::SparseMatrixCSC, region, v0, ::Type{R}) where {R} =
fill(v0, Base.reduced_indices(A,region))
# non-trivial, so use Arrays for output. Array element type is given by `R`.
function Base.reducedim_initarray(A::SparseMatrixCSC, region, v0, ::Type{R}) where {R}
fill!(Array{R}(undef, Base.to_shape(Base.reduced_indices(A, region))), v0)
end

# General mapreduce
function _mapreducezeros(f, op, ::Type{T}, nzeros::Int, v0) where T
Expand Down
3 changes: 2 additions & 1 deletion stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -523,8 +523,9 @@ end

@testset "reductions" begin
pA = sparse(rand(3, 7))
p28227 = sparse(Real[0 0.5])

for arr in (se33, sA, pA)
for arr in (se33, sA, pA, p28227)
for f in (sum, prod, minimum, maximum)
farr = Array(arr)
@test f(arr) f(farr)
Expand Down

0 comments on commit 6f87a00

Please sign in to comment.