From 54b212c62c649f756982d381182f058ce704a190 Mon Sep 17 00:00:00 2001 From: simeonschaub Date: Mon, 28 Oct 2019 08:45:48 +0100 Subject: [PATCH] spezialized conj of Transpose/Adjoint (#33609) * specialized conj for Adjoint/Transpose * add tests * implement @dkarrasch's suggestions * add test case where M' != Adjoint(M) --- stdlib/LinearAlgebra/src/adjtrans.jl | 4 ++++ stdlib/LinearAlgebra/test/adjtrans.jl | 21 +++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/stdlib/LinearAlgebra/src/adjtrans.jl b/stdlib/LinearAlgebra/src/adjtrans.jl index c980e500adde0..b822be0c6e36d 100644 --- a/stdlib/LinearAlgebra/src/adjtrans.jl +++ b/stdlib/LinearAlgebra/src/adjtrans.jl @@ -281,3 +281,7 @@ pinv(v::TransposeAbsVec, tol::Real = 0) = pinv(conj(v.parent)).parent /(u::TransposeAbsVec, A::AbstractMatrix) = transpose(transpose(A) \ u.parent) /(u::AdjointAbsVec, A::Transpose{<:Any,<:AbstractMatrix}) = adjoint(conj(A.parent) \ u.parent) # technically should be adjoint(copy(adjoint(copy(A))) \ u.parent) /(u::TransposeAbsVec, A::Adjoint{<:Any,<:AbstractMatrix}) = transpose(conj(A.parent) \ u.parent) # technically should be transpose(copy(transpose(copy(A))) \ u.parent) + +## complex conjugate +conj(A::Transpose) = adjoint(A.parent) +conj(A::Adjoint) = transpose(A.parent) diff --git a/stdlib/LinearAlgebra/test/adjtrans.jl b/stdlib/LinearAlgebra/test/adjtrans.jl index d0e82aafa78bf..16b4db98911ac 100644 --- a/stdlib/LinearAlgebra/test/adjtrans.jl +++ b/stdlib/LinearAlgebra/test/adjtrans.jl @@ -504,4 +504,25 @@ using .Main.OffsetArrays @test_throws BoundsError s[1, 4] end +@testset "specialized conj of Adjoint/Transpose" begin + realmat = [1 2; 3 4] + complexmat = ComplexF64[1+im 2; 3 4-im] + nested = [[complexmat] [-complexmat]; [0complexmat] [3complexmat]] + @testset "AdjOrTrans{...,$(typeof(i))}" for i in ( + realmat, vec(realmat), + complexmat, vec(complexmat), + nested, vec(nested), + ) + for (t,type) in ((transpose, Adjoint), (adjoint, Transpose)) + M = t(i) + @test conj(M) isa type + @test conj(M) == conj(collect(M)) + @test conj(conj(M)) === M + end + end + # test if `conj(transpose(::Hermitian))` is a no-op + hermitian = Hermitian([1 2+im; 2-im 3]) + @test conj(transpose(hermitian)) === hermitian +end + end # module TestAdjointTranspose