Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions stdlib/LinearAlgebra/src/blas.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,50 @@ for (fname, elty) in ((:cblas_zdotu_sub,:ComplexF64),
end
end

@inline function _dot_length_check(x,y)
n = length(x)
if n != length(y)
throw(DimensionMismatch("dot product arguments have lengths $(length(x)) and $(length(y))"))
end
n
end

for (elty, f) in ((Float32, :dot), (Float64, :dot),
(ComplexF32, :dotc), (ComplexF64, :dotc),
(ComplexF32, :dotu), (ComplexF64, :dotu))
@eval begin
function $f(x::DenseArray{$elty}, y::DenseArray{$elty})
n = _dot_length_check(x,y)
$f(n, x, 1, y, 1)
end

function $f(x::StridedVector{$elty}, y::DenseArray{$elty})
n = _dot_length_check(x,y)
xstride = stride(x,1)
ystride = stride(y,1)
x_delta = xstride < 0 ? n : 1
GC.@preserve x $f(n,pointer(x,x_delta),xstride,y,ystride)
end

function $f(x::DenseArray{$elty}, y::StridedVector{$elty})
n = _dot_length_check(x,y)
xstride = stride(x,1)
ystride = stride(y,1)
y_delta = ystride < 0 ? n : 1
GC.@preserve y $f(n,x,xstride,pointer(y,y_delta),ystride)
end

function $f(x::StridedVector{$elty}, y::StridedVector{$elty})
n = _dot_length_check(x,y)
xstride = stride(x,1)
ystride = stride(y,1)
x_delta = xstride < 0 ? n : 1
y_delta = ystride < 0 ? n : 1
GC.@preserve x y $f(n,pointer(x,x_delta),xstride,pointer(y,y_delta),ystride)
end
end
end

function dot(DX::Union{DenseArray{T},AbstractVector{T}}, DY::Union{DenseArray{T},AbstractVector{T}}) where T<:BlasReal
require_one_based_indexing(DX, DY)
n = length(DX)
Expand Down
13 changes: 13 additions & 0 deletions stdlib/LinearAlgebra/test/matmul.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,19 @@ end
@test *(Asub, adjoint(Asub)) == *(Aref, adjoint(Aref))
end

@testset "dot product of subarrays of vectors (floats, negative stride, issue #37767)" begin
for T in (Float32, Float64, ComplexF32, ComplexF64)
a = Vector{T}(3:2:7)
b = Vector{T}(1:10)
v = view(b,7:-2:3)
@test dot(a,Vector(v)) ≈ 67.0
@test dot(a,v) ≈ 67.0
@test dot(v,a) ≈ 67.0
@test dot(Vector(v),Vector(v)) ≈ 83.0
@test dot(v,v) ≈ 83.0
end
end

@testset "Complex matrix x real MatOrVec etc (issue #29224)" for T1 in (Float32,Float64)
for T2 in (Float32,Float64)
for arg1_real in (true,false)
Expand Down