Skip to content

Commit

Permalink
Allow pushing to vector after 3-arg ldiv! (#43510)
Browse files Browse the repository at this point in the history
Co-authored-by: Daniel Karrasch <[email protected]>
  • Loading branch information
simonbyrne and dkarrasch committed Mar 29, 2022
1 parent f86b4ef commit e2ea152
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 3 deletions.
17 changes: 14 additions & 3 deletions stdlib/LinearAlgebra/src/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,16 +114,27 @@ end
/(B::TransposeAbsVec, adjF::Adjoint{<:Any,<:Factorization}) = adjoint(adjF.parent \ adjoint(B))


# support the same 3-arg idiom as in our other in-place A_*_B functions:
function ldiv!(Y::AbstractVecOrMat, A::Factorization, B::AbstractVecOrMat)
function ldiv!(Y::AbstractVector, A::Factorization, B::AbstractVector)
require_one_based_indexing(Y, B)
m, n = size(A, 1), size(A, 2)
if m > n
Bc = copy(B)
ldiv!(A, Bc)
return copyto!(Y, 1, Bc, 1, n)
else
return ldiv!(A, copyto!(Y, B))
end
end
function ldiv!(Y::AbstractMatrix, A::Factorization, B::AbstractMatrix)
require_one_based_indexing(Y, B)
m, n = size(A, 1), size(A, 2)
if m > n
Bc = copy(B)
ldiv!(A, Bc)
return copyto!(Y, view(Bc, 1:n, :))
else
return ldiv!(A, copyto!(Y, view(B, 1:m, :)))
copyto!(view(Y, 1:m, :), view(B, 1:m, :))
return ldiv!(A, Y)
end
end

Expand Down
9 changes: 9 additions & 0 deletions stdlib/LinearAlgebra/test/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -426,4 +426,13 @@ end
end
end

@testset "can push to vector after 3-arg ldiv! (#43507)" begin
u = rand(3)
A = rand(3,3)
b = rand(3)
ldiv!(u,lu(A),b)
push!(b,4.0)
@test length(b) == 4
end

end # module TestLU
8 changes: 8 additions & 0 deletions stdlib/LinearAlgebra/test/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,19 @@ end
b = randn(3)
b0 = copy(b)
c = randn(2)
B = randn(3,3)
B0 = copy(B)
C = randn(2,3)
@test A \b ldiv!(c, qr(A ), b)
@test b == b0
@test A \B ldiv!(C, qr(A ), B)
@test B == B0
c0 = copy(c)
C0 = copy(C)
@test Ac\c ldiv!(b, qr(Ac, ColumnNorm()), c)
@test c0 == c
@test Ac\C ldiv!(B, qr(Ac, ColumnNorm()), C)
@test C0 == C
end

@testset "Issue reflector of zero-length vector" begin
Expand Down

0 comments on commit e2ea152

Please sign in to comment.