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 3 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)
if length(x) != length(y)
matbesancon marked this conversation as resolved.
Show resolved Hide resolved
throw(

Check warning on line 634 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L634

Added line #L634 was not covered by tests
DimensionMismatch("Vector x has a length $(length(x)) but y has a length $(length(y))")
)
end
d = Q.diag
nzvals = nonzeros(x)
nzinds = nonzeroinds(x)
s = zero(Base.promote_eltype(x, Q, y))
matbesancon marked this conversation as resolved.
Show resolved Hide resolved
@inbounds for nzidx in eachindex(nzvals)
s += dot(nzvals[nzidx], d[nzinds[nzidx]], y[nzinds[nzidx]])
end
return s
end

function dot(a::AbstractSparseVector, Q::Diagonal, b::AbstractSparseVector)
n = length(a)
if length(b) != n
throw(

Check warning on line 651 in src/linalg.jl

View check run for this annotation

Codecov / codecov/patch

src/linalg.jl#L651

Added line #L651 was not covered by tests
DimensionMismatch("Vector a has a length $n but b has a length $(length(b))")
)
end
anzind = nonzeroinds(a)
bnzind = nonzeroinds(b)
anzval = nonzeros(a)
bnzval = nonzeros(b)
s = zero(Base.promote_eltype(a, Q, b))

if isempty(anzind) || isempty(bnzind)
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

a_idx = 1
b_idx = 1
a_idx_last = length(anzind)
b_idx_last = length(bnzind)

# go through the nonzero indices of a and b simultaneously
@inbounds while a_idx <= a_idx_last && b_idx <= b_idx_last
ia = anzind[a_idx]
ib = bnzind[b_idx]
if ia == ib
s += dot(anzval[a_idx], Q.diag[ia], bnzval[b_idx])
a_idx += 1
b_idx += 1
elseif ia < ib
a_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
b_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
11 changes: 11 additions & 0 deletions test/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,17 @@ 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)
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