Bug in matrix times subarray with decreasing index

Simple MWE:

A = ones(2,2)
v = [1.0; 2.0]
A * v # [3.0; 3.0]
A * v[2:-1:1] # [3.0; 3.0]
A * view(v, 2:-1:1) # [2.0; 2.0] ????

Is this a bug?

2 Likes

Yes. The matrix multiplication ultimately dispatches to BLAS gemv:

https://github.com/JuliaLang/julia/blob/099e826241fca365a120df9bac9a9fede6e7bae4/stdlib/LinearAlgebra/src/blas.jl#L564

via

https://github.com/JuliaLang/julia/blob/099e826241fca365a120df9bac9a9fede6e7bae4/stdlib/LinearAlgebra/src/matmul.jl#L346

BLAS gemv probably assumes that the stride is nonnegative. LinearAlgebra.generic_matvecmul! does do the right thing though, so the latter method could just check for a negative stride and fall back to that.

5 Likes

Poster the issue. Thanks.

1 Like