Skip to content

Fix copy for partly initialized unit triangular #1350

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

Merged
merged 2 commits into from
May 13, 2025
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
7 changes: 4 additions & 3 deletions src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -623,7 +623,8 @@ end
copytrito!(dest, U, U isa UpperOrUnitUpperTriangular ? 'L' : 'U')
return dest
end
@propagate_inbounds function _copy!(dest::StridedMatrix, U::UpperOrLowerTriangular{<:Any, <:StridedMatrix})
@propagate_inbounds function _copy!(dest::StridedMatrix,
U::Union{UnitUpperOrUnitLowerTriangular, UpperOrLowerTriangularStrided})
U2 = Base.unalias(dest, U)
copy_unaliased!(dest, U2)
return dest
Expand Down Expand Up @@ -656,7 +657,7 @@ end
end
dest
end
@inline function copy_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular{<:Any, <:StridedMatrix})
@inline function copy_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular)
@boundscheck checkbounds(dest, axes(U)...)
for col in axes(dest,2)
@inbounds copy_unaliased_stored!(dest, U, col)
Expand All @@ -666,7 +667,7 @@ end
end
return dest
end
@inline function copy_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular{<:Any, <:StridedMatrix})
@inline function copy_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular)
@boundscheck checkbounds(dest, axes(L)...)
for col in axes(dest,2)
for row in firstindex(dest,1):col-1
Expand Down
11 changes: 11 additions & 0 deletions test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")

using Test, LinearAlgebra, Random
using LinearAlgebra: errorbounds, transpose!, BandIndex
using Test: GenericArray

const BASE_TEST_PATH = joinpath(Sys.BINDIR, "..", "share", "julia", "test")

Expand Down Expand Up @@ -650,6 +651,16 @@ end
@test_throws "cannot set index in the upper triangular part" copyto!(A, B)
end
end

@testset "partly initialized unit triangular" begin
for T in (UnitUpperTriangular, UnitLowerTriangular)
isupper = T == UnitUpperTriangular
M = Matrix{BigFloat}(undef, 2, 2)
M[1+!isupper,1+isupper] = 3
U = T(GenericArray(M))
@test copyto!(similar(M), U) == U
end
end
end

@testset "getindex with Integers" begin
Expand Down