diff --git a/stdlib/LinearAlgebra/src/factorization.jl b/stdlib/LinearAlgebra/src/factorization.jl index e90b21401bb1f3..129d4ca121a5db 100644 --- a/stdlib/LinearAlgebra/src/factorization.jl +++ b/stdlib/LinearAlgebra/src/factorization.jl @@ -100,12 +100,14 @@ function ldiv!(Y::AbstractVecOrMat, A::Factorization, B::AbstractVecOrMat) @assert !has_offset_axes(Y, B) m, n = size(A, 1), size(A, 2) if m > n - ldiv!(A, B) - return copyto!(Y, view(B, 1:n, :)) + Bc = copy(B) + ldiv!(A, Bc) + return copyto!(Y, view(Bc, 1:n, :)) else return ldiv!(A, copyto!(Y, view(B, 1:m, :))) end end + function ldiv!(Y::AbstractVecOrMat, adjA::Adjoint{<:Any,<:Factorization}, B::AbstractVecOrMat) checksquare(adjA) return ldiv!(adjA, copyto!(Y, B)) diff --git a/stdlib/LinearAlgebra/test/qr.jl b/stdlib/LinearAlgebra/test/qr.jl index 28e923867f9406..d78d7fd02e2197 100644 --- a/stdlib/LinearAlgebra/test/qr.jl +++ b/stdlib/LinearAlgebra/test/qr.jl @@ -220,12 +220,16 @@ end end @testset "Issue Test Factorization fallbacks for rectangular problems" begin - A = randn(3,2) + A = randn(3,2) Ac = copy(A') - b = randn(3) - c = randn(2) + b = randn(3) + b0 = copy(b) + c = randn(2) @test A \b ≈ ldiv!(c, qr(A ), b) + @test b == b0 + c0 = copy(c) @test Ac\c ≈ ldiv!(b, qr(Ac, Val(true)), c) + @test c0 == c end end # module TestQR