Skip to content
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

getindex and setindex of transposed matrix #30

Merged
merged 1 commit into from
Mar 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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