Skip to content

Commit

Permalink
fix #28369, transpose of SparseMatrixCSC is not recursive (#28376)
Browse files Browse the repository at this point in the history
  • Loading branch information
jlapeyre authored and andreasnoack committed Aug 1, 2018
1 parent c05fd20 commit b526c52
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions stdlib/SparseArrays/src/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,13 @@ function ftranspose(A::SparseMatrixCSC{Tv,Ti}, f::Function) where {Tv,Ti}
end
adjoint(A::SparseMatrixCSC) = Adjoint(A)
transpose(A::SparseMatrixCSC) = Transpose(A)
Base.copy(A::Adjoint{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, conj)
Base.copy(A::Transpose{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, identity)
Base.copy(A::Adjoint{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, x -> copy(adjoint(x)))
Base.copy(A::Transpose{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, x -> copy(transpose(x)))
function Base.permutedims(A::SparseMatrixCSC, (a,b))
(a, b) == (2, 1) && return ftranspose(A, identity)
(a, b) == (1, 2) && return copy(A)
throw(ArgumentError("no valid permutation of dimensions"))
end

"""
unchecked_noalias_permute!(X::SparseMatrixCSC{Tv,Ti},
Expand Down
19 changes: 19 additions & 0 deletions stdlib/SparseArrays/test/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2253,4 +2253,23 @@ end
@test SparseMatrixCSC(transpose(A)) isa SparseMatrixCSC
end

@testset "Issue #28369" begin
M = reshape([[1 2; 3 4], [9 10; 11 12], [5 6; 7 8], [13 14; 15 16]], (2,2))
MP = reshape([[1 2; 3 4], [5 6; 7 8], [9 10; 11 12], [13 14; 15 16]], (2,2))
S = sparse(M)
SP = sparse(MP)
@test isa(transpose(S), Transpose)
@test transpose(S) == copy(transpose(S))
@test Array(transpose(S)) == copy(transpose(M))
@test permutedims(S) == SP
@test permutedims(S, (2,1)) == SP
@test permutedims(S, (1,2)) == S
@test permutedims(S, (1,2)) !== S
MC = reshape([[(1+im) 2; 3 4], [9 10; 11 12], [(5 + 2im) 6; 7 8], [13 14; 15 16]], (2,2))
SC = sparse(MC)
@test isa(adjoint(SC), Adjoint)
@test adjoint(SC) == copy(adjoint(SC))
@test adjoint(MC) == copy(adjoint(SC))
end

end # module

0 comments on commit b526c52

Please sign in to comment.