Skip to content

Commit

Permalink
stdlib/SparseArrays: add rmul! and lmul! of sparse matrix with Diagon…
Browse files Browse the repository at this point in the history
…al (#29296)

* Add rmul! and lmul! of sparse matrix with Diagonal
  • Loading branch information
dkarrasch authored and Sacha0 committed Sep 29, 2018
1 parent 201cba5 commit 9454c8d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions stdlib/SparseArrays/src/linalg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1017,11 +1017,33 @@ function rmul!(A::SparseMatrixCSC, b::Number)
rmul!(A.nzval, b)
return A
end

function lmul!(b::Number, A::SparseMatrixCSC)
lmul!(b, A.nzval)
return A
end

function rmul!(A::SparseMatrixCSC, D::Diagonal)
m, n = size(A)
(n == size(D, 1)) || throw(DimensionMismatch())
Anzval = A.nzval
@inbounds for col = 1:n, p = A.colptr[col]:(A.colptr[col + 1] - 1)
Anzval[p] *= D.diag[col]
end
return A
end

function lmul!(D::Diagonal, A::SparseMatrixCSC)
m, n = size(A)
(m == size(D, 2)) || throw(DimensionMismatch())
Anzval = A.nzval
Arowval = A.rowval
@inbounds for col = 1:n, p = A.colptr[col]:(A.colptr[col + 1] - 1)
Anzval[p] *= D.diag[Arowval[p]]
end
return A
end

function \(A::SparseMatrixCSC, B::AbstractVecOrMat)
@assert !has_offset_axes(A, B)
m, n = size(A)
Expand Down

0 comments on commit 9454c8d

Please sign in to comment.