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

Don't read destination indices when copying structured matrices #55322

Merged
merged 2 commits into from
Aug 6, 2024
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
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ function _copyto_banded!(A::Bidiagonal, B::Bidiagonal)
if A.uplo == B.uplo
A.ev .= B.ev
elseif iszero(B.ev) # diagonal source
A.ev .= zero.(A.ev)
A.ev .= B.ev
else
zeroband = istriu(A) ? "lower" : "upper"
uplo = A.uplo
Expand Down
14 changes: 7 additions & 7 deletions stdlib/LinearAlgebra/src/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,20 +320,20 @@ function copyto!(dest::BandedMatrix, src::BandedMatrix)
end
function _copyto_banded!(T::Tridiagonal, D::Diagonal)
T.d .= D.diag
T.dl .= zero.(T.dl)
T.du .= zero.(T.du)
T.dl .= view(D, diagind(D, -1, IndexStyle(D)))
T.du .= view(D, diagind(D, 1, IndexStyle(D)))
return T
end
function _copyto_banded!(SymT::SymTridiagonal, D::Diagonal)
issymmetric(D) || throw(ArgumentError("cannot copy a non-symmetric Diagonal matrix to a SymTridiagonal"))
SymT.dv .= D.diag
_ev = _evview(SymT)
_ev .= zero.(_ev)
_ev .= view(D, diagind(D, 1, IndexStyle(D)))
return SymT
end
function _copyto_banded!(B::Bidiagonal, D::Diagonal)
B.dv .= D.diag
B.ev .= zero.(B.ev)
B.ev .= view(D, diagind(D, B.uplo == 'U' ? 1 : -1, IndexStyle(D)))
return B
end
function _copyto_banded!(D::Diagonal, B::Bidiagonal)
Expand Down Expand Up @@ -361,18 +361,18 @@ function _copyto_banded!(T::Tridiagonal, B::Bidiagonal)
T.d .= B.dv
if B.uplo == 'U'
T.du .= B.ev
T.dl .= zero.(T.dl)
T.dl .= view(B, diagind(B, -1, IndexStyle(B)))
else
T.dl .= B.ev
T.du .= zero.(T.du)
T.du .= view(B, diagind(B, 1, IndexStyle(B)))
end
return T
end
function _copyto_banded!(SymT::SymTridiagonal, B::Bidiagonal)
issymmetric(B) || throw(ArgumentError("cannot copy a non-symmetric Bidiagonal matrix to a SymTridiagonal"))
SymT.dv .= B.dv
_ev = _evview(SymT)
_ev .= zero.(_ev)
_ev .= B.ev
return SymT
end
function _copyto_banded!(B::Bidiagonal, T::Tridiagonal)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1040,7 +1040,7 @@ function _copyto_banded!(A::Tridiagonal, B::SymTridiagonal)
return A
end
function _copyto_banded!(A::SymTridiagonal, B::Tridiagonal)
issymmetric(B) || throw(ArgumentError("cannot copy a non-symmetric Tridiagonal matrix to a SymTridiagonal"))
issymmetric(B) || throw(ArgumentError("cannot copy an asymmetric Tridiagonal matrix to a SymTridiagonal"))
A.dv .= B.d
_evview(A) .= B.du
return A
Expand Down
17 changes: 9 additions & 8 deletions stdlib/LinearAlgebra/test/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,8 @@ end
@testset "from Diagonal" begin
D = Diagonal(d)
@testset "to Bidiagonal" begin
BU = Bidiagonal(zero(d), oneunit.(du), :U)
BL = Bidiagonal(zero(d), oneunit.(dl), :L)
BU = Bidiagonal(similar(d, BigInt), similar(du, BigInt), :U)
BL = Bidiagonal(similar(d, BigInt), similar(dl, BigInt), :L)
for B in (BL, BU)
copyto!(B, D)
@test B == D
Expand All @@ -573,7 +573,7 @@ end
end
end
@testset "to Tridiagonal" begin
T = Tridiagonal(oneunit.(dl), zero(d), oneunit.(du))
T = Tridiagonal(similar(dl, BigInt), similar(d, BigInt), similar(du, BigInt))
copyto!(T, D)
@test T == D

Expand All @@ -586,8 +586,8 @@ end
end
end
@testset "to SymTridiagonal" begin
for du2 in (oneunit.(du), oneunit.(d))
S = SymTridiagonal(zero(d), du2)
for du2 in (similar(du, BigInt), similar(d, BigInt))
S = SymTridiagonal(similar(d), du2)
copyto!(S, D)
@test S == D
end
Expand Down Expand Up @@ -630,13 +630,14 @@ end
end
end
@testset "to Tridiagonal" begin
T = Tridiagonal(oneunit.(dl), zero(d), oneunit.(du))
T = Tridiagonal(similar(dl, BigInt), similar(d, BigInt), similar(du, BigInt))
for B in (BL, BU, BLones, BUones)
copyto!(T, B)
@test T == B
end

@testset "mismatched size" begin
T = Tridiagonal(oneunit.(dl), zero(d), oneunit.(du))
for uplo in (:L, :U)
T .= 0
copyto!(T, Bidiagonal([1], Int[], uplo))
Expand All @@ -647,8 +648,8 @@ end
end
end
@testset "to SymTridiagonal" begin
for du2 in (oneunit.(du), oneunit.(d))
S = SymTridiagonal(zero(d), du2)
for du2 in (similar(du, BigInt), similar(d, BigInt))
S = SymTridiagonal(similar(d, BigInt), du2)
for B in (BL, BU)
copyto!(S, B)
@test S == B
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/test/tridiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ end
@test copyto!(zero(S), T) == T

T2 = Tridiagonal(ones(length(ev)), zero(dv), zero(ev))
@test_throws "cannot copy a non-symmetric Tridiagonal matrix to a SymTridiagonal" copyto!(zero(S), T2)
@test_throws "cannot copy an asymmetric Tridiagonal matrix to a SymTridiagonal" copyto!(zero(S), T2)

@testset "mismatched sizes" begin
dv2 = [4; @view dv[2:end]]
Expand Down