From 0537f6bb65c20001526373a835c85aea6442e1f4 Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Fri, 21 Sep 2018 09:14:19 +0200 Subject: [PATCH 1/4] Add rmul! and lmul! of sparse matrix with Diagonal --- 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 ad8ffb2b14f63..083cfed01eb3d 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{T}) where T + m, n = size(A) + (n == size(D, 1)) || throw(DimensionMismatch()) + Anzval = A.nzval + for col = 1:n, p = A.colptr[col]:(A.colptr[col+1]-1) + @inbounds Anzval[p] *= D.diag[col] + end + return A +end + +function lmul!(D::Diagonal{T}, A::SparseMatrixCSC) where T + m, n = size(A) + (m == size(D, 2)) || throw(DimensionMismatch()) + Anzval = A.nzval + Arowval = A.rowval + for col = 1:n, p = A.colptr[col]:(A.colptr[col+1]-1) + @inbounds 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) From 43af17821d6283642360b148014590d384b82fc5 Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Fri, 21 Sep 2018 16:40:09 +0200 Subject: [PATCH 2/4] Some optimization --- stdlib/SparseArrays/src/linalg.jl | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/stdlib/SparseArrays/src/linalg.jl b/stdlib/SparseArrays/src/linalg.jl index 083cfed01eb3d..20513f8934de2 100644 --- a/stdlib/SparseArrays/src/linalg.jl +++ b/stdlib/SparseArrays/src/linalg.jl @@ -1027,8 +1027,11 @@ function rmul!(A::SparseMatrixCSC, D::Diagonal{T}) where T m, n = size(A) (n == size(D, 1)) || throw(DimensionMismatch()) Anzval = A.nzval - for col = 1:n, p = A.colptr[col]:(A.colptr[col+1]-1) - @inbounds Anzval[p] *= D.diag[col] + @inbounds for col = 1:n + scale = D.diag[col] + for p = A.colptr[col]:(A.colptr[col + 1] - 1) + Anzval[p] *= scale + end end return A end @@ -1038,8 +1041,8 @@ function lmul!(D::Diagonal{T}, A::SparseMatrixCSC) where T (m == size(D, 2)) || throw(DimensionMismatch()) Anzval = A.nzval Arowval = A.rowval - for col = 1:n, p = A.colptr[col]:(A.colptr[col+1]-1) - @inbounds Anzval[p] *= D.diag[Arowval[p]] + @inbounds for col = 1:n, p = A.colptr[col]:(A.colptr[col + 1] - 1) + Anzval[p] *= D.diag[Arowval[p]] end return A end From 2efc6463b2326186f0924dfa081280675cd0298f Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Fri, 21 Sep 2018 17:43:21 +0200 Subject: [PATCH 3/4] merge loops again for consistency --- stdlib/SparseArrays/src/linalg.jl | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/stdlib/SparseArrays/src/linalg.jl b/stdlib/SparseArrays/src/linalg.jl index 20513f8934de2..c61c64bb3a0e9 100644 --- a/stdlib/SparseArrays/src/linalg.jl +++ b/stdlib/SparseArrays/src/linalg.jl @@ -1027,11 +1027,8 @@ function rmul!(A::SparseMatrixCSC, D::Diagonal{T}) where T m, n = size(A) (n == size(D, 1)) || throw(DimensionMismatch()) Anzval = A.nzval - @inbounds for col = 1:n - scale = D.diag[col] - for p = A.colptr[col]:(A.colptr[col + 1] - 1) - Anzval[p] *= scale - end + @inbounds for col = 1:n, p = A.colptr[col]:(A.colptr[col + 1] - 1) + Anzval[p] *= D.diag[col] end return A end From 2edf05f8575fdc2af2e2ea794eda84a671d9119e Mon Sep 17 00:00:00 2001 From: Daniel Karrasch Date: Fri, 28 Sep 2018 16:47:22 +0200 Subject: [PATCH 4/4] remove unused parametrized type --- stdlib/SparseArrays/src/linalg.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stdlib/SparseArrays/src/linalg.jl b/stdlib/SparseArrays/src/linalg.jl index c61c64bb3a0e9..f368eb277b162 100644 --- a/stdlib/SparseArrays/src/linalg.jl +++ b/stdlib/SparseArrays/src/linalg.jl @@ -1023,7 +1023,7 @@ function lmul!(b::Number, A::SparseMatrixCSC) return A end -function rmul!(A::SparseMatrixCSC, D::Diagonal{T}) where T +function rmul!(A::SparseMatrixCSC, D::Diagonal) m, n = size(A) (n == size(D, 1)) || throw(DimensionMismatch()) Anzval = A.nzval @@ -1033,7 +1033,7 @@ function rmul!(A::SparseMatrixCSC, D::Diagonal{T}) where T return A end -function lmul!(D::Diagonal{T}, A::SparseMatrixCSC) where T +function lmul!(D::Diagonal, A::SparseMatrixCSC) m, n = size(A) (m == size(D, 2)) || throw(DimensionMismatch()) Anzval = A.nzval