diff --git a/stdlib/SparseArrays/src/sparsematrix.jl b/stdlib/SparseArrays/src/sparsematrix.jl index 85478ca6cc831..9195176c9376d 100644 --- a/stdlib/SparseArrays/src/sparsematrix.jl +++ b/stdlib/SparseArrays/src/sparsematrix.jl @@ -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}, diff --git a/stdlib/SparseArrays/test/sparse.jl b/stdlib/SparseArrays/test/sparse.jl index 613b957738f35..5d52ba1bd62de 100644 --- a/stdlib/SparseArrays/test/sparse.jl +++ b/stdlib/SparseArrays/test/sparse.jl @@ -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