Skip to content

Commit 4968cff

Browse files
authored
ldiv! for Diagonal and a sparse vector (#613)
This improves performance: ```julia julia> using LinearAlgebra, SparseArrays julia> D = Diagonal(rand(3000)); julia> S = sprand(size(D,1), 0.01); julia> @Btime ldiv!($D, $S); 30.053 μs (0 allocations: 0 bytes) # master 1.585 μs (0 allocations: 0 bytes) # PR ```
1 parent 5062034 commit 4968cff

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/linalg.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -1312,7 +1312,7 @@ function rdiv!(A::AbstractSparseMatrixCSC{T}, D::Diagonal{T}) where T
13121312
A
13131313
end
13141314

1315-
function ldiv!(D::Diagonal{T}, A::AbstractSparseMatrixCSC{T}) where {T}
1315+
function ldiv!(D::Diagonal{T}, A::Union{AbstractSparseMatrixCSC{T}, AbstractSparseVector{T}}) where {T}
13161316
# require_one_based_indexing(A)
13171317
if size(A, 1) != length(D.diag)
13181318
throw(DimensionMismatch("diagonal matrix is $(length(D.diag)) by $(length(D.diag)) but right hand side has $(size(A, 1)) rows"))

test/linalg.jl

+3
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ end
338338
@test lmul!(transpose(copy(D)), copy(b)) transpose(MD)*bd
339339
@test lmul!(adjoint(copy(D)), copy(b)) MD'*bd
340340
end
341+
342+
v = sprand(eltype(D), size(D,1), 0.1)
343+
@test ldiv!(D, copy(v)) == D \ Array(v)
341344
end
342345
end
343346

0 commit comments

Comments
 (0)