Skip to content

Commit 8a1ecfb

Browse files
authored
Fix copy for partly initialized unit triangular (#1350)
Currently, we forward copy for unit triangular matrices to the parents. This also copies the diagonal, which may not be initialized in the parent. This PR fixes this. After this, the following works again ```julia julia> M = Matrix{BigFloat}(undef,2,2); julia> M[2,1] = 3; julia> M' 2×2 adjoint(::Matrix{BigFloat}) with eltype BigFloat: #undef 3.0 #undef #undef julia> U = UnitUpperTriangular(M') 2×2 UnitUpperTriangular{BigFloat, Adjoint{BigFloat, Matrix{BigFloat}}}: 1.0 3.0 ⋅ 1.0 julia> copyto!(similar(M), U) 2×2 Matrix{BigFloat}: 1.0 3.0 0.0 1.0 ```
1 parent 93e97b4 commit 8a1ecfb

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

src/triangular.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -623,7 +623,8 @@ end
623623
copytrito!(dest, U, U isa UpperOrUnitUpperTriangular ? 'L' : 'U')
624624
return dest
625625
end
626-
@propagate_inbounds function _copy!(dest::StridedMatrix, U::UpperOrLowerTriangular{<:Any, <:StridedMatrix})
626+
@propagate_inbounds function _copy!(dest::StridedMatrix,
627+
U::Union{UnitUpperOrUnitLowerTriangular, UpperOrLowerTriangularStrided})
627628
U2 = Base.unalias(dest, U)
628629
copy_unaliased!(dest, U2)
629630
return dest
@@ -656,7 +657,7 @@ end
656657
end
657658
dest
658659
end
659-
@inline function copy_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular{<:Any, <:StridedMatrix})
660+
@inline function copy_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular)
660661
@boundscheck checkbounds(dest, axes(U)...)
661662
for col in axes(dest,2)
662663
@inbounds copy_unaliased_stored!(dest, U, col)
@@ -666,7 +667,7 @@ end
666667
end
667668
return dest
668669
end
669-
@inline function copy_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular{<:Any, <:StridedMatrix})
670+
@inline function copy_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular)
670671
@boundscheck checkbounds(dest, axes(L)...)
671672
for col in axes(dest,2)
672673
for row in firstindex(dest,1):col-1

test/triangular.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ isdefined(Main, :pruned_old_LA) || @eval Main include("prune_old_LA.jl")
66

77
using Test, LinearAlgebra, Random
88
using LinearAlgebra: errorbounds, transpose!, BandIndex
9+
using Test: GenericArray
910

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

@@ -650,6 +651,16 @@ end
650651
@test_throws "cannot set index in the upper triangular part" copyto!(A, B)
651652
end
652653
end
654+
655+
@testset "partly initialized unit triangular" begin
656+
for T in (UnitUpperTriangular, UnitLowerTriangular)
657+
isupper = T == UnitUpperTriangular
658+
M = Matrix{BigFloat}(undef, 2, 2)
659+
M[1+!isupper,1+isupper] = 3
660+
U = T(GenericArray(M))
661+
@test copyto!(similar(M), U) == U
662+
end
663+
end
653664
end
654665

655666
@testset "getindex with Integers" begin

0 commit comments

Comments
 (0)