Skip to content

Commit 8d712f6

Browse files
gragusaandreasnoack
authored andcommitted
Fix #29713 (ldiv! overwrites arguments) (#29715)
* 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
1 parent e9d32a6 commit 8d712f6

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

stdlib/LinearAlgebra/src/factorization.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,12 +100,14 @@ function ldiv!(Y::AbstractVecOrMat, A::Factorization, B::AbstractVecOrMat)
100100
@assert !has_offset_axes(Y, B)
101101
m, n = size(A, 1), size(A, 2)
102102
if m > n
103-
ldiv!(A, B)
104-
return copyto!(Y, view(B, 1:n, :))
103+
Bc = copy(B)
104+
ldiv!(A, Bc)
105+
return copyto!(Y, view(Bc, 1:n, :))
105106
else
106107
return ldiv!(A, copyto!(Y, view(B, 1:m, :)))
107108
end
108109
end
110+
109111
function ldiv!(Y::AbstractVecOrMat, adjA::Adjoint{<:Any,<:Factorization}, B::AbstractVecOrMat)
110112
checksquare(adjA)
111113
return ldiv!(adjA, copyto!(Y, B))

stdlib/LinearAlgebra/test/qr.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,12 +220,16 @@ end
220220
end
221221

222222
@testset "Issue Test Factorization fallbacks for rectangular problems" begin
223-
A = randn(3,2)
223+
A = randn(3,2)
224224
Ac = copy(A')
225-
b = randn(3)
226-
c = randn(2)
225+
b = randn(3)
226+
b0 = copy(b)
227+
c = randn(2)
227228
@test A \b ldiv!(c, qr(A ), b)
229+
@test b == b0
230+
c0 = copy(c)
228231
@test Ac\c ldiv!(b, qr(Ac, Val(true)), c)
232+
@test c0 == c
229233
end
230234

231235
end # module TestQR

0 commit comments

Comments
 (0)