Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow pushing to vector after 3-arg ldiv! #43510

Merged
merged 3 commits into from
Mar 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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