Skip to content

Commit 751387e

Browse files
authored
Optimize row-indexing in AdjOrTrans of sparse matrix (#734)
This uses both optimized column-indexing and `map!`, as suggested by #628 (comment) @SobhanMP. Fixes #628.
1 parent 7abbefa commit 751387e

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

src/sparsevector.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,11 @@ function getindex(x::AbstractSparseMatrixCSC, I::AbstractUnitRange, j::Integer)
683683
return @if_move_fixed x SparseVector(length(I), [rowvals(x)[i] - first(I) + 1 for i = r1:r2], nonzeros(x)[r1:r2])
684684
end
685685

686+
getindex(M::AdjOrTrans{<:Any,<:AbstractSparseMatrixCSC}, i::Integer, ::Colon) =
687+
map!(wrapperop(M), M.parent[:,i])
688+
getindex(M::AdjOrTrans{<:Any,<:AbstractSparseMatrixCSC}, i::AbstractVector, ::Colon) =
689+
copy(wrapperop(M)(M.parent[:,i]))
690+
686691
# In the general case, we piggy back upon SparseMatrixCSC's optimized solution
687692
@inline getindex(A::AbstractSparseMatrixCSC, I::AbstractVector, J::Integer) =
688693
let M = A[I, [J]]

test/sparsevector.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,14 @@ end
294294
bIv[I] .= true
295295
@test Array(r) == Array(x)[bIv]
296296
end
297+
298+
let x = sprand(ComplexF64, 10, 10, 0.5)
299+
for t in (transpose, adjoint)
300+
xt = t(x)
301+
@test xt[1,:] == t.(x[:,1])
302+
@test xt[2:4,:] == t(x[:,2:4])
303+
end
304+
end
297305
end
298306
@testset "index with colon" begin
299307
@test issparse(spzeros(0)[:])

0 commit comments

Comments
 (0)