Skip to content

Commit

Permalink
SparseArrays: Allow division by diagonal matrices
Browse files Browse the repository at this point in the history
  • Loading branch information
eschnett committed Apr 21, 2020
1 parent dd738f9 commit de7b264
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 0 deletions.
8 changes: 8 additions & 0 deletions stdlib/SparseArrays/src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ function (*)(A::AbstractSparseMatrixCSC, D::Diagonal)
T = Base.promote_op(*, eltype(D), eltype(A))
mul!(LinearAlgebra.copy_oftype(A, T), A, D)
end
function (\)(D::Diagonal, A::AbstractSparseMatrixCSC)
T = Base.promote_op(\, eltype(D), eltype(A))
ldiv!(D, LinearAlgebra.copy_oftype(A, T))
end
function (/)(A::AbstractSparseMatrixCSC, D::Diagonal)
T = Base.promote_op(/, eltype(A), eltype(D))
rdiv!(LinearAlgebra.copy_oftype(A, T), D)
end

# Sparse matrix multiplication as described in [Gustavson, 1978]:
# http://dl.acm.org/citation.cfm?id=355796
Expand Down
2 changes: 2 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -519,6 +519,7 @@ dA = Array(sA)
@test lmul!(Diagonal(bi), copy(dA)) ldiv!(Diagonal(b), copy(sA))
@test lmul!(Diagonal(bi), copy(dA)) ldiv!(transpose(Diagonal(b)), copy(sA))
@test lmul!(Diagonal(conj(bi)), copy(dA)) ldiv!(adjoint(Diagonal(b)), copy(sA))
@test Diagonal(b) \ copy(sA) == ldiv!(Diagonal(b), copy(sA))
@test_throws DimensionMismatch ldiv!(Diagonal(fill(1., length(b)+1)), copy(sA))
@test_throws LinearAlgebra.SingularException ldiv!(Diagonal(zeros(length(b))), copy(sA))

Expand All @@ -527,6 +528,7 @@ dA = Array(sA)
@test rmul!(copy(dAt), Diagonal(bi)) rdiv!(copy(sAt), Diagonal(b))
@test rmul!(copy(dAt), Diagonal(bi)) rdiv!(copy(sAt), transpose(Diagonal(b)))
@test rmul!(copy(dAt), Diagonal(conj(bi))) rdiv!(copy(sAt), adjoint(Diagonal(b)))
@test copy(sAt) / Diagonal(b) == rdiv!(copy(dAt), Diagonal(b))
@test_throws DimensionMismatch rdiv!(copy(sAt), Diagonal(fill(1., length(b)+1)))
@test_throws LinearAlgebra.SingularException rdiv!(copy(sAt), Diagonal(zeros(length(b))))
end
Expand Down

0 comments on commit de7b264

Please sign in to comment.