diff --git a/stdlib/LinearAlgebra/src/lu.jl b/stdlib/LinearAlgebra/src/lu.jl index 54d421441b9de..9262b517f5906 100644 --- a/stdlib/LinearAlgebra/src/lu.jl +++ b/stdlib/LinearAlgebra/src/lu.jl @@ -432,11 +432,11 @@ end (/)(adjA::Adjoint{<:Any,<:AbstractMatrix}, F::Adjoint{<:Any,<:LU}) = adjoint(F.parent \ adjA.parent) function (/)(trA::Transpose{<:Any,<:AbstractVector}, F::Adjoint{<:Any,<:LU}) T = promote_type(eltype(trA), eltype(F)) - return adjoint(ldiv!(F.parent, convert(AbstractVector{T}, conj(trA.parent)))) + return adjoint(ldiv!(F.parent, conj!(copy_oftype(trA.parent, T)))) end function (/)(trA::Transpose{<:Any,<:AbstractMatrix}, F::Adjoint{<:Any,<:LU}) T = promote_type(eltype(trA), eltype(F)) - return adjoint(ldiv!(F.parent, convert(AbstractMatrix{T}, conj(trA.parent)))) + return adjoint(ldiv!(F.parent, conj!(copy_oftype(trA.parent, T)))) end function det(F::LU{T}) where T diff --git a/stdlib/LinearAlgebra/test/lu.jl b/stdlib/LinearAlgebra/test/lu.jl index 1f5f2892947dc..02ebb55b6e7f2 100644 --- a/stdlib/LinearAlgebra/test/lu.jl +++ b/stdlib/LinearAlgebra/test/lu.jl @@ -360,4 +360,17 @@ end end end +@testset "transpose(A) / lu(B)' should not overwrite A (#36657)" begin + for elty in (Float16, Float64, ComplexF64) + A = randn(elty, 5, 5) + B = randn(elty, 5, 5) + C = copy(A) + a = randn(elty, 5) + c = copy(a) + @test transpose(A) / lu(B)' ≈ transpose(A) / B' + @test transpose(a) / lu(B)' ≈ transpose(a) / B' + @test A == C + @test a == c + end +end end # module TestLU