-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
stdlib/SparseArrays: add rmul! and lmul! of sparse matrix with Diagonal #29296
Changes from 2 commits
0537f6b
b93376c
43af178
2efc646
2edf05f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be possible to gain a bit of speed here by manually hoisting the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm sorry I can't follow. I did the change @StefanKarpinski suggested. Was that just (potentially) unnecessary or even "wrong"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I benchmarked the previous ("my") against the latest version ("Stefan's") on these:
and Stefan's won with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm, might be neglible. For
(an order of magnitude larger) and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The question is if the compiler tries to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like it doesn't reload, at least it doesn't show up in performance loss. Should I, for better readability, then merge the two loops again as before, but leave the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't matter that much but perhaps it makes sense to at least have the two methods in this PR be consistent with eachother. |
||
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) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
T
is never used in the function body so I think you can make this signaturefunction rmul!(A::SparseMatrixCSC, D::Diagonal)
. (same for lmul)