Skip to content

Commit e6e7449

Browse files
committed
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 aefab75 commit e6e7449

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
@@ -599,14 +599,15 @@ end
599599
copytrito!(dest, U, U isa UpperOrUnitUpperTriangular ? 'L' : 'U')
600600
return dest
601601
end
602-
@propagate_inbounds function _copyto!(dest::StridedMatrix, U::UpperOrLowerTriangular{<:Any, <:StridedMatrix})
602+
@propagate_inbounds function _copyto!(dest::StridedMatrix,
603+
U::Union{UnitUpperOrUnitLowerTriangular, UpperOrLowerTriangular{<:Any, <:StridedMatrix}})
603604
U2 = Base.unalias(dest, U)
604605
copyto_unaliased!(dest, U2)
605606
return dest
606607
end
607608
# for strided matrices, we explicitly loop over the arrays to improve cache locality
608609
# This fuses the copytrito! for the two halves
609-
@inline function copyto_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular{<:Any, <:StridedMatrix})
610+
@inline function copyto_unaliased!(dest::StridedMatrix, U::UpperOrUnitUpperTriangular)
610611
@boundscheck checkbounds(dest, axes(U)...)
611612
isunit = U isa UnitUpperTriangular
612613
for col in axes(dest,2)
@@ -619,7 +620,7 @@ end
619620
end
620621
return dest
621622
end
622-
@inline function copyto_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular{<:Any, <:StridedMatrix})
623+
@inline function copyto_unaliased!(dest::StridedMatrix, L::LowerOrUnitLowerTriangular)
623624
@boundscheck checkbounds(dest, axes(L)...)
624625
isunit = L isa UnitLowerTriangular
625626
for col in axes(dest,2)

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)