Skip to content

Commit e5ee8da

Browse files
KlausCandreasnoack
authored andcommitted
BUGFIX: genmatmul! for empty input matrices (#33874)
* correct genmatmul! for empty input matrices * tests for getmatmul!
1 parent 1e39c69 commit e5ee8da

3 files changed

Lines changed: 11 additions & 4 deletions

File tree

stdlib/LinearAlgebra/src/generic.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ julia> C
7575
end
7676

7777
@inline function _rmul_or_fill!(C::AbstractArray, beta::Number)
78+
if isempty(C)
79+
return C
80+
end
7881
if iszero(beta)
7982
fill!(C, zero(eltype(C)))
8083
else

stdlib/LinearAlgebra/src/matmul.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -738,10 +738,8 @@ function _generic_matmatmul!(C::AbstractVecOrMat{R}, tA, tB, A::AbstractVecOrMat
738738
if size(C,1) != mA || size(C,2) != nB
739739
throw(DimensionMismatch("result C has dimensions $(size(C)), needs ($mA,$nB)"))
740740
end
741-
if isempty(A) || isempty(B)
742-
return C
743-
end
744-
if iszero(_add.alpha)
741+
742+
if iszero(_add.alpha) || isempty(A) || isempty(B)
745743
return _rmul_or_fill!(C, _add.beta)
746744
end
747745

stdlib/LinearAlgebra/test/matmul.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,4 +618,10 @@ end
618618
@test_throws MethodError zero(eltype(C))
619619
end
620620

621+
@testset "Issue #33873: genmatmul! with empty operands" begin
622+
@test Matrix{Any}(undef, 0, 2) * Matrix{Any}(undef, 2, 3) == Matrix{Any}(undef, 0, 3)
623+
@test_throws MethodError Matrix{Any}(undef, 2, 0) * Matrix{Any}(undef, 0, 3)
624+
@test Matrix{Int}(undef, 2, 0) * Matrix{Int}(undef, 0, 3) == zeros(Int, 2, 3)
625+
end
626+
621627
end # module TestMatmul

0 commit comments

Comments
 (0)