Skip to content

Commit 7d27fa8

Browse files
authored
SparseArrays: Speed up right-division by diagonal matrices (#35533)
1 parent 86ee57c commit 7d27fa8

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

stdlib/SparseArrays/src/linalg.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ function (*)(A::AbstractSparseMatrixCSC, D::Diagonal)
174174
T = Base.promote_op(*, eltype(D), eltype(A))
175175
mul!(LinearAlgebra.copy_oftype(A, T), A, D)
176176
end
177+
function (/)(A::AbstractSparseMatrixCSC, D::Diagonal)
178+
T = typeof(oneunit(eltype(A))/oneunit(eltype(D)))
179+
rdiv!(LinearAlgebra.copy_oftype(A, T), D)
180+
end
177181

178182
# Sparse matrix multiplication as described in [Gustavson, 1978]:
179183
# http://dl.acm.org/citation.cfm?id=355796

stdlib/SparseArrays/test/sparse.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,9 @@ dA = Array(sA)
519519
@test lmul!(Diagonal(bi), copy(dA)) ldiv!(Diagonal(b), copy(sA))
520520
@test lmul!(Diagonal(bi), copy(dA)) ldiv!(transpose(Diagonal(b)), copy(sA))
521521
@test lmul!(Diagonal(conj(bi)), copy(dA)) ldiv!(adjoint(Diagonal(b)), copy(sA))
522+
Aob = Diagonal(b) \ sA
523+
@test Aob == ldiv!(Diagonal(b), copy(sA))
524+
@test issparse(Aob)
522525
@test_throws DimensionMismatch ldiv!(Diagonal(fill(1., length(b)+1)), copy(sA))
523526
@test_throws LinearAlgebra.SingularException ldiv!(Diagonal(zeros(length(b))), copy(sA))
524527

@@ -527,6 +530,9 @@ dA = Array(sA)
527530
@test rmul!(copy(dAt), Diagonal(bi)) rdiv!(copy(sAt), Diagonal(b))
528531
@test rmul!(copy(dAt), Diagonal(bi)) rdiv!(copy(sAt), transpose(Diagonal(b)))
529532
@test rmul!(copy(dAt), Diagonal(conj(bi))) rdiv!(copy(sAt), adjoint(Diagonal(b)))
533+
Atob = sAt / Diagonal(b)
534+
@test Atob == rdiv!(copy(dAt), Diagonal(b))
535+
@test issparse(Atob)
530536
@test_throws DimensionMismatch rdiv!(copy(sAt), Diagonal(fill(1., length(b)+1)))
531537
@test_throws LinearAlgebra.SingularException rdiv!(copy(sAt), Diagonal(zeros(length(b))))
532538
end

0 commit comments

Comments
 (0)