Skip to content

Commit 33e5c64

Browse files
authored
Fix neutral_element for AbstractArrays (#320)
1 parent a12311e commit 33e5c64

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

src/reduce.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ reduce_op(op::AddSubMul) = add_sub_op(op)
2222

2323
reduce_op(::typeof(add_dot)) = +
2424

25-
neutral_element(::typeof(+), T::Type) = zero(T)
25+
neutral_element(::typeof(+), ::Type{T}) where {T} = zero(T)
26+
neutral_element(::typeof(+), ::Type{T}) where {T<:AbstractArray} = Zero()
2627

2728
map_op(::AddSubMul) = *
2829

test/interface.jl

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,3 +215,21 @@ end
215215
@test ret == reshape([3.0], 1, 1)
216216
@test y == reshape([1.0], 1, 1, 1)
217217
end
218+
219+
@testset "issue_318_neutral_element" begin
220+
a = rand(3)
221+
A = [rand(2, 2) for _ in 1:3]
222+
@test_throws DimensionMismatch MA.operate(LinearAlgebra.dot, a, A)
223+
y = a' * A
224+
@test isapprox(MA.fused_map_reduce(MA.add_mul, a', A), y)
225+
z = MA.operate(LinearAlgebra.dot, Int[], Int[])
226+
@test iszero(z) && z isa Int
227+
z = MA.operate(LinearAlgebra.dot, BigInt[], Int[])
228+
@test iszero(z) && z isa BigInt
229+
z = MA.operate(LinearAlgebra.dot, Int[], Float64[])
230+
@test iszero(z) && z isa Float64
231+
z = MA.operate(LinearAlgebra.dot, Matrix{Int}[], Matrix{Float64}[])
232+
@test iszero(z) && z isa Float64
233+
@test MA.fused_map_reduce(MA.add_mul, Matrix{Int}[], Float64[]) isa MA.Zero
234+
@test MA.fused_map_reduce(MA.add_mul, Float64[], Matrix{Int}[]) isa MA.Zero
235+
end

0 commit comments

Comments
 (0)