diff --git a/stdlib/LinearAlgebra/src/matmul.jl b/stdlib/LinearAlgebra/src/matmul.jl index 018ad20e538c8..5994c95851827 100644 --- a/stdlib/LinearAlgebra/src/matmul.jl +++ b/stdlib/LinearAlgebra/src/matmul.jl @@ -711,7 +711,8 @@ function _generic_matvecmul!(C::AbstractVector, tA, A::AbstractVecOrMat, B::Abst else for k = 1:mA aoffs = (k-1)*Astride - s = zero(A[aoffs + 1]*B[1] + A[aoffs + 1]*B[1]) + firstterm = transpose(A[aoffs + 1])*B[1] + s = zero(firstterm + firstterm) for i = 1:nA s += transpose(A[aoffs+i]) * B[i] end @@ -726,7 +727,8 @@ function _generic_matvecmul!(C::AbstractVector, tA, A::AbstractVecOrMat, B::Abst else for k = 1:mA aoffs = (k-1)*Astride - s = zero(A[aoffs + 1]*B[1] + A[aoffs + 1]*B[1]) + firstterm = A[aoffs + 1]'B[1] + s = zero(firstterm + firstterm) for i = 1:nA s += A[aoffs + i]'B[i] end diff --git a/stdlib/LinearAlgebra/test/matmul.jl b/stdlib/LinearAlgebra/test/matmul.jl index 86606654e911a..0c6e421b3968b 100644 --- a/stdlib/LinearAlgebra/test/matmul.jl +++ b/stdlib/LinearAlgebra/test/matmul.jl @@ -209,6 +209,18 @@ end end end +@testset "generic_matvecmul for vectors of matrices" begin + x = [1 2 3; 4 5 6] + A = reshape([x,2x,3x,4x],2,2) + b = [x, 2x] + for f in (adjoint, transpose) + c = f(A) * b + for i in eachindex(c) + @test c[i] == sum(f(A)[i, j] * b[j] for j in eachindex(b)) + end + end +end + @testset "generic_matmatmul for matrices of vectors" begin B = Matrix{Vector{Int}}(undef, 2, 2) B[1, 1] = [1, 2]