Skip to content

Commit bfdb742

Browse files
authored
Destination zeros from eltype in zero-length generic_matvecmul (#1241)
This fixes issues like ```julia julia> A = fill(@smatrix(zeros(2,2)), 4, 0) 4×0 Matrix{SMatrix{2, 2, Float64, 4}} julia> v = fill(@smatrix(zeros(2,2)), size(A,2)) SMatrix{2, 2, Float64, 4}[] julia> A * v ERROR: MethodError: Cannot `convert` an object of type Bool to an object of type SMatrix{2, 2, Float64, 4} The function `convert` exists, but no method is defined for this combination of argument types. [...] ```
1 parent 16dedb5 commit bfdb742

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/matmul.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -932,7 +932,7 @@ function __generic_matvecmul!(f::F, C::AbstractVector, A::AbstractVecOrMat, B::A
932932
@inbounds begin
933933
if length(B) == 0
934934
for k = eachindex(C)
935-
@stable_muladdmul _modify!(MulAddMul(alpha,beta), false, C, k)
935+
@stable_muladdmul _modify!(MulAddMul(alpha,beta), zero(eltype(C)), C, k)
936936
end
937937
else
938938
for k = eachindex(C)
@@ -955,7 +955,7 @@ function __generic_matvecmul!(::typeof(identity), C::AbstractVector, A::Abstract
955955
if !iszero(beta)
956956
C[i] *= beta
957957
elseif length(B) == 0
958-
C[i] = false
958+
C[i] = zero(eltype(C))
959959
else
960960
C[i] = zero(A[i]*B[1] + A[i]*B[1])
961961
end

test/matmul.jl

+17
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ using Base: rtoldefault
66
using Test, LinearAlgebra, Random
77
using LinearAlgebra: mul!, Symmetric, Hermitian
88

9+
const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")
10+
11+
isdefined(Main, :SizedArrays) || @eval Main include(joinpath($(BASE_TEST_PATH), "testhelpers", "SizedArrays.jl"))
12+
using .Main.SizedArrays
13+
914
## Test Julia fallbacks to BLAS routines
1015

1116
mul_wrappers = [
@@ -1176,4 +1181,16 @@ end
11761181
end
11771182
end
11781183

1184+
@testset "zero-length generic matvec" begin
1185+
m = SizedArrays.SizedArray{(2,2)}(ones(2,2))
1186+
A = fill(m, 2, 0)
1187+
v = fill(m, size(A,2))
1188+
w = similar(v, size(A,1))
1189+
mul!(w, A, v)
1190+
@test all(iszero, w)
1191+
A = fill(m, 0, 2)
1192+
mul!(w, A', v)
1193+
@test all(iszero, w)
1194+
end
1195+
11791196
end # module TestMatmul

0 commit comments

Comments
 (0)