From 56f94ce7152aa1a8dbf6c2e78c9d23d962578eed Mon Sep 17 00:00:00 2001 From: Guillaume Marques Date: Wed, 30 Mar 2022 15:26:25 +0200 Subject: [PATCH] getindex and setindex of transposed matrix (#30) --- src/operations.jl | 2 ++ test/unit/spmv.jl | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/src/operations.jl b/src/operations.jl index 4568387..550a72b 100644 --- a/src/operations.jl +++ b/src/operations.jl @@ -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) diff --git a/test/unit/spmv.jl b/test/unit/spmv.jl index 984e37b..5c3808f 100644 --- a/test/unit/spmv.jl +++ b/test/unit/spmv.jl @@ -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. @@ -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