Skip to content

Commit

Permalink
Fix indexing triangular matrices with undef elements in the parent (#…
Browse files Browse the repository at this point in the history
…52530)

I'm not totally sure about specializing`getindex` for numeric arrays,
but this lets us display a triangular matrix where the parent is not
initialized.
```julia
julia> M = Matrix{BigFloat}(undef, 4, 4)
4×4 Matrix{BigFloat}:
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef
 #undef  #undef  #undef  #undef

julia> UpperTriangular(M)
4×4 UpperTriangular{BigFloat, Matrix{BigFloat}}:
 #undef  #undef  #undef  #undef
    ⋅    #undef  #undef  #undef
    ⋅       ⋅    #undef  #undef
    ⋅       ⋅       ⋅    #undef
```
  • Loading branch information
jishnub authored Jan 26, 2024
1 parent 4919dd7 commit 1e45aba
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,11 @@ Base.isstored(A::UpperTriangular, i::Int, j::Int) =
@propagate_inbounds getindex(A::UnitLowerTriangular{T}, i::Integer, j::Integer) where {T} =
i > j ? A.data[i,j] : ifelse(i == j, oneunit(T), zero(T))
@propagate_inbounds getindex(A::LowerTriangular, i::Integer, j::Integer) =
i >= j ? A.data[i,j] : zero(A.data[j,i])
i >= j ? A.data[i,j] : _zero(A.data,j,i)
@propagate_inbounds getindex(A::UnitUpperTriangular{T}, i::Integer, j::Integer) where {T} =
i < j ? A.data[i,j] : ifelse(i == j, oneunit(T), zero(T))
@propagate_inbounds getindex(A::UpperTriangular, i::Integer, j::Integer) =
i <= j ? A.data[i,j] : zero(A.data[j,i])
i <= j ? A.data[i,j] : _zero(A.data,j,i)

@propagate_inbounds function setindex!(A::UpperTriangular, x, i::Integer, j::Integer)
if i > j
Expand Down
13 changes: 13 additions & 0 deletions stdlib/LinearAlgebra/test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,14 @@ end
end
end

@testset "indexing partly initialized matrices" begin
M = Matrix{BigFloat}(undef, 2, 2)
U = UpperTriangular(M)
@test iszero(U[2,1])
L = LowerTriangular(M)
@test iszero(L[1,2])
end

@testset "special printing of Lower/UpperTriangular" begin
@test occursin(r"3×3 (LinearAlgebra\.)?LowerTriangular{Int64, Matrix{Int64}}:\n 2 ⋅ ⋅\n 2 2 ⋅\n 2 2 2",
sprint(show, MIME"text/plain"(), LowerTriangular(2ones(Int64,3,3))))
Expand All @@ -820,6 +828,11 @@ end
sprint(show, MIME"text/plain"(), UpperTriangular(2ones(Int64,3,3))))
@test occursin(r"3×3 (LinearAlgebra\.)?UnitUpperTriangular{Int64, Matrix{Int64}}:\n 1 2 2\n ⋅ 1 2\n ⋅ ⋅ 1",
sprint(show, MIME"text/plain"(), UnitUpperTriangular(2ones(Int64,3,3))))

# don't access non-structural elements while displaying
M = Matrix{BigFloat}(undef, 2, 2)
@test sprint(show, UpperTriangular(M)) == "BigFloat[#undef #undef; 0.0 #undef]"
@test sprint(show, LowerTriangular(M)) == "BigFloat[#undef 0.0; #undef #undef]"
end

@testset "adjoint/transpose triangular/vector multiplication" begin
Expand Down

0 comments on commit 1e45aba

Please sign in to comment.