Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
55 changes: 55 additions & 0 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,61 @@
return conj(dot(B, A))
end

function dot(x::AbstractSparseVector, Q::Diagonal, y::AbstractVector)
d = Q.diag
if length(x) != length(y) || length(y) != length(d)
throw(

Check warning on line 635 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L635

Added line #L635 was not covered by tests
DimensionMismatch("Vectors and matrix have different dimensions, x has a length $(length(x)), y has a length $(length(y)), Q has side dimension $(size(Q, 1))")
)
end
nzvals = nonzeros(x)
nzinds = nonzeroinds(x)
s = zero(Base.promote_eltype(x, Q, y))
@inbounds for nzidx in eachindex(nzvals)
s += dot(nzvals[nzidx], d[nzinds[nzidx]], y[nzinds[nzidx]])
end
return s
end

function dot(x::AbstractSparseVector, Q::Diagonal, y::AbstractSparseVector)
n = length(x)
if length(y) != n || n != size(Q, 1)
throw(
DimensionMismatch("Vectors and matrix have different dimensions, x has a length $(length(x)), y has a length $(length(y)), Q has side dimension $(size(Q, 1))")
)
end
xnzind = nonzeroinds(x)
ynzind = nonzeroinds(y)
xnzval = nonzeros(x)
ynzval = nonzeros(y)
s = zero(Base.promote_eltype(x, Q, y))

if isempty(xnzind) || isempty(ynzind)
return s

Check warning on line 662 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L662

Added line #L662 was not covered by tests
end

x_idx = 1
y_idx = 1
x_idx_last = length(xnzind)
y_idx_last = length(ynzind)

# go through the nonzero indices of a and b simultaneously
@inbounds while x_idx <= x_idx_last && y_idx <= y_idx_last
ix = xnzind[x_idx]
iy = ynzind[y_idx]
if ix == iy
s += dot(xnzval[x_idx], Q.diag[ix], ynzval[y_idx])
x_idx += 1
y_idx += 1
elseif ix < iy
x_idx += 1

Check warning on line 679 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L678-L679

Added lines #L678 - L679 were not covered by tests
else
y_idx += 1

Check warning on line 681 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L681

Added line #L681 was not covered by tests
end
end
return s
end

## triangular sparse handling
## triangular multiplication
function LinearAlgebra.generic_trimatmul!(C::StridedVecOrMat, uploc, isunitc, tfun::Function, A::SparseMatrixCSCUnion, B::AbstractVecOrMat)
Expand Down
14 changes: 14 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,20 @@ end
@test dot(TA,WB) ≈ dot(Matrix(TA), WB)
@test dot(TA,WC) ≈ dot(Matrix(TA), WC)
end
for M in (A, B, C)
D = Diagonal(M * M')
a = spzeros(Complex{Float64}, size(D, 1))
a[1:3] = rand(Complex{Float64}, 3)
b = spzeros(Complex{Float64}, size(D, 1))
b[1:3] = rand(Complex{Float64}, 3)
@test dot(a, D, b) ≈ dot(a, sparse(D), b)
@test dot(b, D, a) ≈ dot(b, sparse(D), a)
@test dot(b, D, a) ≈ dot(b, D, collect(a))
@test dot(b, D, a) ≈ dot(collect(b), D, a)
@test_throws DimensionMismatch dot(b, D, [a; 1])
@test_throws DimensionMismatch dot([b; 1], D, a)
@test_throws DimensionMismatch dot([b; 1], D, [a; 1])
end
end
@test_throws DimensionMismatch dot(sprand(5,5,0.2),sprand(5,6,0.2))
@test_throws DimensionMismatch dot(rand(5,5),sprand(5,6,0.2))
Expand Down
Loading