Skip to content

Commit

Permalink
Fix #29713 (ldiv! overwrites arguments) (#29715)
Browse files Browse the repository at this point in the history
* Fix for #29713

* Add tests for #29713

* Fix for #29713

* Copy the argument in the right branch

* Copy the argument in the right branch - take 2

* Update qr.jl

(cherry picked from commit 8d712f6)
  • Loading branch information
gragusa authored and KristofferC committed Dec 12, 2018
1 parent c5ecebc commit afd0525
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
6 changes: 4 additions & 2 deletions stdlib/LinearAlgebra/src/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
10 changes: 7 additions & 3 deletions stdlib/LinearAlgebra/test/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit afd0525

Please sign in to comment.