Skip to content

Commit

Permalink
getindex and setindex of transposed matrix (#30)
Browse files Browse the repository at this point in the history
  • Loading branch information
guimarqu authored Mar 30, 2022
1 parent 3533de5 commit 56f94ce
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/operations.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ struct Transposed{T}
end

Base.transpose(matrix::DynamicSparseMatrix) = Transposed(matrix)
Base.setindex!(m::Transposed, val, row, col) = setindex!(m.array, val, col, row)
Base.getindex(m::Transposed, row, col) = getindex(m.array, col, row)

function Base.:(*)(matrix::DynamicSparseMatrix{K,L,T}, v::PackedMemoryArray{L,T}) where {K,L,T}
return _mult(matrix.colmajor, v)
Expand Down
9 changes: 9 additions & 0 deletions test/unit/spmv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ function test_spmv_2()
V2 = [1, 1, 1]
vec = dynamicsparsevec(I2, V2)

transposed_matrix = transpose(matrix)
for (i,j,v) in Iterators.zip(I,J,V)
@test transposed_matrix[j,i] == matrix[i,j] == v
end

result = transpose(matrix) * vec

# The multiplication returns a Dict.
Expand All @@ -42,6 +47,10 @@ function test_spmv_2()
@test result[3] == 2
@test result[4] == 3
@test result[5] == 1


transposed_matrix[2, 'e'] = 5
@test transposed_matrix[2, 'e'] == matrix['e', 2] == 5
return
end

Expand Down

0 comments on commit 56f94ce

Please sign in to comment.