Skip to content

Commit c24508f

Browse files
jlapeyreKristofferC
authored andcommitted
fix #28369, transpose of SparseMatrixCSC is not recursive (#28376)
1 parent 441ef92 commit c24508f

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

stdlib/SparseArrays/src/sparsematrix.jl

+7-2
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,13 @@ function ftranspose(A::SparseMatrixCSC{Tv,Ti}, f::Function) where {Tv,Ti}
867867
end
868868
adjoint(A::SparseMatrixCSC) = Adjoint(A)
869869
transpose(A::SparseMatrixCSC) = Transpose(A)
870-
Base.copy(A::Adjoint{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, conj)
871-
Base.copy(A::Transpose{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, identity)
870+
Base.copy(A::Adjoint{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, x -> copy(adjoint(x)))
871+
Base.copy(A::Transpose{<:Any,<:SparseMatrixCSC}) = ftranspose(A.parent, x -> copy(transpose(x)))
872+
function Base.permutedims(A::SparseMatrixCSC, (a,b))
873+
(a, b) == (2, 1) && return ftranspose(A, identity)
874+
(a, b) == (1, 2) && return copy(A)
875+
throw(ArgumentError("no valid permutation of dimensions"))
876+
end
872877

873878
"""
874879
unchecked_noalias_permute!(X::SparseMatrixCSC{Tv,Ti},

stdlib/SparseArrays/test/sparse.jl

+19
Original file line numberDiff line numberDiff line change
@@ -2253,4 +2253,23 @@ end
22532253
@test SparseMatrixCSC(transpose(A)) isa SparseMatrixCSC
22542254
end
22552255

2256+
@testset "Issue #28369" begin
2257+
M = reshape([[1 2; 3 4], [9 10; 11 12], [5 6; 7 8], [13 14; 15 16]], (2,2))
2258+
MP = reshape([[1 2; 3 4], [5 6; 7 8], [9 10; 11 12], [13 14; 15 16]], (2,2))
2259+
S = sparse(M)
2260+
SP = sparse(MP)
2261+
@test isa(transpose(S), Transpose)
2262+
@test transpose(S) == copy(transpose(S))
2263+
@test Array(transpose(S)) == copy(transpose(M))
2264+
@test permutedims(S) == SP
2265+
@test permutedims(S, (2,1)) == SP
2266+
@test permutedims(S, (1,2)) == S
2267+
@test permutedims(S, (1,2)) !== S
2268+
MC = reshape([[(1+im) 2; 3 4], [9 10; 11 12], [(5 + 2im) 6; 7 8], [13 14; 15 16]], (2,2))
2269+
SC = sparse(MC)
2270+
@test isa(adjoint(SC), Adjoint)
2271+
@test adjoint(SC) == copy(adjoint(SC))
2272+
@test adjoint(MC) == copy(adjoint(SC))
2273+
end
2274+
22562275
end # module

0 commit comments

Comments
 (0)