Skip to content

Commit

Permalink
Forward istriu/istril for triangular to parent (#55663)
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub authored and KristofferC committed Sep 12, 2024
1 parent c8df3e8 commit 722657d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 4 additions & 0 deletions stdlib/LinearAlgebra/src/special.jl
Original file line number Diff line number Diff line change
Expand Up @@ -586,3 +586,7 @@ function cholesky(S::RealHermSymComplexHerm{<:Real,<:SymTridiagonal}, ::NoPivot
B = Bidiagonal{T}(diag(S, 0), diag(S, S.uplo == 'U' ? 1 : -1), sym_uplo(S.uplo))
cholesky!(Hermitian(B, sym_uplo(S.uplo)), NoPivot(); check = check)
end

# istriu/istril for triangular wrappers of structured matrices
_istril(A::LowerTriangular{<:Any, <:BandedMatrix}, k) = istril(parent(A), k)
_istriu(A::UpperTriangular{<:Any, <:BandedMatrix}, k) = istriu(parent(A), k)
22 changes: 20 additions & 2 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,32 @@ function Base.replace_in_print_matrix(A::Union{LowerTriangular,UnitLowerTriangul
return i >= j ? s : Base.replace_with_centered_mark(s)
end

Base.@constprop :aggressive function istril(A::Union{LowerTriangular,UnitLowerTriangular}, k::Integer=0)
istril(A::UnitLowerTriangular, k::Integer=0) = k >= 0
istriu(A::UnitUpperTriangular, k::Integer=0) = k <= 0
Base.@constprop :aggressive function istril(A::LowerTriangular, k::Integer=0)
k >= 0 && return true
return _istril(A, k)
end
Base.@constprop :aggressive function istriu(A::Union{UpperTriangular,UnitUpperTriangular}, k::Integer=0)
@inline function _istril(A::LowerTriangular, k)
P = parent(A)
m = size(A, 1)
for j in max(1, k + 2):m
all(iszero, view(P, j:min(j - k - 1, m), j)) || return false
end
return true
end
Base.@constprop :aggressive function istriu(A::UpperTriangular, k::Integer=0)
k <= 0 && return true
return _istriu(A, k)
end
@inline function _istriu(A::UpperTriangular, k)
P = parent(A)
m = size(A, 1)
for j in 1:min(m, m + k - 1)
all(iszero, view(P, max(1, j - k + 1):j, j)) || return false
end
return true
end
istril(A::Adjoint, k::Integer=0) = istriu(A.parent, -k)
istril(A::Transpose, k::Integer=0) = istriu(A.parent, -k)
istriu(A::Adjoint, k::Integer=0) = istril(A.parent, -k)
Expand Down
28 changes: 28 additions & 0 deletions stdlib/LinearAlgebra/test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1226,4 +1226,32 @@ end
end
end

@testset "istriu/istril forwards to parent" begin
@testset "$(nameof(typeof(M)))" for M in [Tridiagonal(rand(n-1), rand(n), rand(n-1)),
Tridiagonal(zeros(n-1), zeros(n), zeros(n-1)),
Diagonal(randn(n)),
Diagonal(zeros(n)),
]
@testset for TriT in (UpperTriangular, UnitUpperTriangular, LowerTriangular, UnitLowerTriangular)
U = TriT(M)
A = Array(U)
for k in -n:n
@test istriu(U, k) == istriu(A, k)
@test istril(U, k) == istril(A, k)
end
end
end
z = zeros(n,n)
@testset for TriT in (UpperTriangular, UnitUpperTriangular, LowerTriangular, UnitLowerTriangular)
P = Matrix{BigFloat}(undef, n, n)
copytrito!(P, z, TriT <: Union{UpperTriangular, UnitUpperTriangular} ? 'U' : 'L')
U = TriT(P)
A = Array(U)
@testset for k in -n:n
@test istriu(U, k) == istriu(A, k)
@test istril(U, k) == istril(A, k)
end
end
end

end # module TestTriangular

0 comments on commit 722657d

Please sign in to comment.