Skip to content

Commit e2ea152

Browse files
Allow pushing to vector after 3-arg ldiv! (#43510)
Co-authored-by: Daniel Karrasch <[email protected]>
1 parent f86b4ef commit e2ea152

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

stdlib/LinearAlgebra/src/factorization.jl

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,16 +114,27 @@ end
114114
/(B::TransposeAbsVec, adjF::Adjoint{<:Any,<:Factorization}) = adjoint(adjF.parent \ adjoint(B))
115115

116116

117-
# support the same 3-arg idiom as in our other in-place A_*_B functions:
118-
function ldiv!(Y::AbstractVecOrMat, A::Factorization, B::AbstractVecOrMat)
117+
function ldiv!(Y::AbstractVector, A::Factorization, B::AbstractVector)
118+
require_one_based_indexing(Y, B)
119+
m, n = size(A, 1), size(A, 2)
120+
if m > n
121+
Bc = copy(B)
122+
ldiv!(A, Bc)
123+
return copyto!(Y, 1, Bc, 1, n)
124+
else
125+
return ldiv!(A, copyto!(Y, B))
126+
end
127+
end
128+
function ldiv!(Y::AbstractMatrix, A::Factorization, B::AbstractMatrix)
119129
require_one_based_indexing(Y, B)
120130
m, n = size(A, 1), size(A, 2)
121131
if m > n
122132
Bc = copy(B)
123133
ldiv!(A, Bc)
124134
return copyto!(Y, view(Bc, 1:n, :))
125135
else
126-
return ldiv!(A, copyto!(Y, view(B, 1:m, :)))
136+
copyto!(view(Y, 1:m, :), view(B, 1:m, :))
137+
return ldiv!(A, Y)
127138
end
128139
end
129140

stdlib/LinearAlgebra/test/lu.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,4 +426,13 @@ end
426426
end
427427
end
428428

429+
@testset "can push to vector after 3-arg ldiv! (#43507)" begin
430+
u = rand(3)
431+
A = rand(3,3)
432+
b = rand(3)
433+
ldiv!(u,lu(A),b)
434+
push!(b,4.0)
435+
@test length(b) == 4
436+
end
437+
429438
end # module TestLU

stdlib/LinearAlgebra/test/qr.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,11 +270,19 @@ end
270270
b = randn(3)
271271
b0 = copy(b)
272272
c = randn(2)
273+
B = randn(3,3)
274+
B0 = copy(B)
275+
C = randn(2,3)
273276
@test A \b ldiv!(c, qr(A ), b)
274277
@test b == b0
278+
@test A \B ldiv!(C, qr(A ), B)
279+
@test B == B0
275280
c0 = copy(c)
281+
C0 = copy(C)
276282
@test Ac\c ldiv!(b, qr(Ac, ColumnNorm()), c)
277283
@test c0 == c
284+
@test Ac\C ldiv!(B, qr(Ac, ColumnNorm()), C)
285+
@test C0 == C
278286
end
279287

280288
@testset "Issue reflector of zero-length vector" begin

0 commit comments

Comments
 (0)