From 71872d134a7adb88e0383151dfd0d4810b49e443 Mon Sep 17 00:00:00 2001 From: Jishnu Bhattacharya Date: Thu, 12 Oct 2023 13:48:43 +0530 Subject: [PATCH] Forward `copyto!` for `Adjoint` to `adjoint!` (#51650) --- stdlib/LinearAlgebra/src/transpose.jl | 10 ++++++++++ stdlib/LinearAlgebra/test/adjtrans.jl | 10 ++++++++++ 2 files changed, 20 insertions(+) diff --git a/stdlib/LinearAlgebra/src/transpose.jl b/stdlib/LinearAlgebra/src/transpose.jl index 9d70ac3add34b..cd9123615d4bb 100644 --- a/stdlib/LinearAlgebra/src/transpose.jl +++ b/stdlib/LinearAlgebra/src/transpose.jl @@ -210,3 +210,13 @@ function copy_similar(A::TransposeAbsMat, ::Type{T}) where {T} C = similar(A, T, size(A)) transpose!(C, parent(A)) end + +function Base.copyto_unaliased!(deststyle::IndexStyle, dest::AbstractMatrix, srcstyle::IndexCartesian, src::AdjOrTransAbsMat) + if axes(dest) == axes(src) + f! = inplace_adj_or_trans(src) + f!(dest, parent(src)) + else + @invoke Base.copyto_unaliased!(deststyle::IndexStyle, dest::AbstractArray, srcstyle::IndexStyle, src::AbstractArray) + end + return dest +end diff --git a/stdlib/LinearAlgebra/test/adjtrans.jl b/stdlib/LinearAlgebra/test/adjtrans.jl index 2362ec7fb28f2..555010913660a 100644 --- a/stdlib/LinearAlgebra/test/adjtrans.jl +++ b/stdlib/LinearAlgebra/test/adjtrans.jl @@ -476,6 +476,16 @@ end @test adjoint!(b, a) === b end +@testset "copyto! uses adjoint!/transpose!" begin + for T in (Float64, ComplexF64), f in (transpose, adjoint), sz in ((5,4), (5,)) + S = rand(T, sz) + adjS = f(S) + A = similar(S') + copyto!(A, adjS) + @test A == adjS + end +end + @testset "aliasing with adjoint and transpose" begin A = collect(reshape(1:25, 5, 5)) .+ rand.().*im B = copy(A)