Skip to content

Commit 1d9a627

Browse files
committed
more efficient implementation of submatrix
1 parent 0535854 commit 1d9a627

1 file changed

Lines changed: 17 additions & 6 deletions

File tree

src/fematrix.jl

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -620,18 +620,29 @@ $(TYPEDSIGNATURES)
620620
Generates an ExtendableSparseMatrix from the submatrix
621621
for the given row and col numbers
622622
"""
623-
function submatrix(A::AbstractExtendableSparseMatrixCSC{Tv, Ti}, srows, scols) where {Tv, Ti}
623+
function submatrix(A::AbstractExtendableSparseMatrixCSC{Tv, Ti}, srows, scols; factor = 1) where {Tv, Ti}
624624
cscmat::SparseMatrixCSC{Tv, Ti} = A.cscmatrix
625625
rows::Array{Ti, 1} = rowvals(cscmat)
626+
valsA = cscmat.nzval
627+
nrowsA = size(A,1)
628+
ncolsA = size(A,2)
626629
S = ExtendableSparseMatrix{Tv, Ti}(length(srows), length(scols))
627630
@assert maximum(srows) <= size(A, 1) "rows exceeds rowcount of A"
628631
@assert maximum(scols) <= size(A, 2) "cols exceeds colcount of A"
629-
for col in 1:length(scols)
630-
scol = scols[col]
632+
ncols = length(scols)
633+
nrows = length(srows)
634+
newrows = zeros(Int, nrowsA)
635+
newcols = zeros(Int, ncolsA)
636+
newrows[srows] = 1:nrows
637+
newcols[scols] = 1:ncols
638+
minrow, maxrow = minimum(srows), maximum(srows)
639+
640+
for scol in scols
631641
for r in nzrange(cscmat, scol)
632-
j = findfirst(==(rows[r]), srows)
633-
if j !== nothing
634-
S[j, col] = A[rows[r], scol]
642+
if newrows[rows[r]] > 0
643+
_addnz(S, newrows[rows[r]], newcols[scol], valsA[r], factor)
644+
else
645+
break
635646
end
636647
end
637648
end

0 commit comments

Comments
 (0)