Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added specialized method for 3-argument dot with diagonal matrix #565

Merged
merged 9 commits into from
Oct 22, 2024
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
56 changes: 56 additions & 0 deletions src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,62 @@
return conj(dot(B, A))
end

function dot(x::AbstractSparseVector, D::Diagonal, y::AbstractVector)
dkarrasch marked this conversation as resolved.
Show resolved Hide resolved
d = D.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)), D has side dimension $(length(d))")
)
end
nzvals = nonzeros(x)
nzinds = nonzeroinds(x)
s = zero(typeof(dot(first(x), first(D), first(y))))
@inbounds for nzidx in eachindex(nzvals)
s += dot(nzvals[nzidx], d[nzinds[nzidx]], y[nzinds[nzidx]])
end
return s
end

dot(x::AbstractVector, D::Diagonal, y::AbstractSparseVector) = adjoint(dot(y, D', x))

function dot(x::AbstractSparseVector, D::Diagonal, y::AbstractSparseVector)
d = D.diag
if length(y) != length(x) || length(y) != length(d)
throw(
DimensionMismatch("Vectors and matrix have different dimensions, x has a length $(length(x)), y has a length $(length(y)), Q has side dimension $(length(d))")
)
end

Check warning on line 656 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L656

Added line #L656 was not covered by tests
xnzind = nonzeroinds(x)
ynzind = nonzeroinds(y)
xnzval = nonzeros(x)
ynzval = nonzeros(y)
s = zero(typeof(dot(first(x), first(D), first(y))))
if isempty(xnzind) || isempty(ynzind)
return s
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], d[ix], ynzval[y_idx])
x_idx += 1
y_idx += 1
elseif ix < iy
x_idx += 1
else
y_idx += 1
end
end

Check warning on line 684 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L684

Added line #L684 was not covered by tests
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