From 39234a3ee3ac346ec66a2e659842ae8b336f447d Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Tue, 13 Jun 2023 14:23:33 +0530 Subject: [PATCH] `Adjoint`/`transpose` for `OneElementMatrix` preserves type (#258) * Adjoint/transpose for OneElementMatrix preserves type * tests for vector adjoint/transpose --- src/oneelement.jl | 5 +++++ test/runtests.jl | 22 ++++++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/oneelement.jl b/src/oneelement.jl index 8ab1c77f..c371ebe9 100644 --- a/src/oneelement.jl +++ b/src/oneelement.jl @@ -144,3 +144,8 @@ end function mul!(C::AbstractMatrix, A::AbstractFillMatrix, B::OneElementMatrix, alpha::Number, beta::Number) _mulonel!(C, A, B, alpha, beta) end + +# adjoint/transpose + +adjoint(A::OneElementMatrix) = OneElement(adjoint(A.val), reverse(A.ind), reverse(A.axes)) +transpose(A::OneElementMatrix) = OneElement(transpose(A.val), reverse(A.ind), reverse(A.axes)) diff --git a/test/runtests.jl b/test/runtests.jl index c1d649ad..5386d0c0 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1713,6 +1713,28 @@ end @test Base.setindex(Zeros(5,3), 2, 2, 3) ≡ OneElement(2.0, (2,3), (5,3)) @test_throws BoundsError Base.setindex(Zeros(5), 2, 6) + @testset "adjoint/transpose" begin + A = OneElement(3im, (2,4), (4,6)) + @test A' === OneElement(-3im, (4,2), (6,4)) + @test transpose(A) === OneElement(3im, (4,2), (6,4)) + + A = OneElement(3im, 2, 3) + @test A' isa Adjoint + @test transpose(A) isa Transpose + @test A' == OneElement(-3im, (1,2), (1,3)) + @test transpose(A) == OneElement(3im, (1,2), (1,3)) + + A = OneElement(3, (2,2), (4,4)) + @test adjoint(A) === A + @test transpose(A) === A + + A = OneElement(3, 2, 4) + @test transpose(A) isa Transpose + @test adjoint(A) isa Adjoint + @test transpose(A) == OneElement(3, (1,2), (1,4)) + @test adjoint(A) == OneElement(3, (1,2), (1,4)) + end + @testset "matmul" begin A = reshape(Float64[1:9;], 3, 3) testinds(w::AbstractArray) = testinds(size(w))