From 5ad620beca7c3c7655469457fe96812c8dec92ea Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Sat, 29 Sep 2018 20:38:47 +0200 Subject: [PATCH] stdlib/SparseArrays: add rmul! and lmul! of sparse matrix with Diagonal (#29296) * Add rmul! and lmul! of sparse matrix with Diagonal (cherry picked from commit 9454c8dc42e5be2682b47aeeffa388b8c527135f) --- stdlib/SparseArrays/src/linalg.jl | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/stdlib/SparseArrays/src/linalg.jl b/stdlib/SparseArrays/src/linalg.jl index ad8ffb2b14f63f..f368eb277b162a 100644 --- a/stdlib/SparseArrays/src/linalg.jl +++ b/stdlib/SparseArrays/src/linalg.jl @@ -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)