Skip to content

Out-of-place triu/tril for Symmetric in each branch #1318

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
24 changes: 12 additions & 12 deletions src/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -472,49 +472,49 @@ Base.conj!(A::HermOrSym) = typeof(A)(parentof_applytri(conj!, A), A.uplo)
# tril/triu
function tril(A::Hermitian, k::Integer=0)
if A.uplo == 'U' && k <= 0
return tril!(copy(A.data'),k)
return tril_maybe_inplace(copy(A.data'),k)
elseif A.uplo == 'U' && k > 0
return tril!(copy(A.data'),-1) + tril!(triu(A.data),k)
return tril_maybe_inplace(copy(A.data'),-1) + tril_maybe_inplace(triu(A.data),k)
elseif A.uplo == 'L' && k <= 0
return tril(A.data,k)
else
return tril(A.data,-1) + tril!(triu!(copy(A.data')),k)
return tril(A.data,-1) + tril_maybe_inplace(triu_maybe_inplace(copy(A.data')),k)
end
end

function tril(A::Symmetric, k::Integer=0)
if A.uplo == 'U' && k <= 0
return tril!(copy(transpose(A.data)),k)
return tril_maybe_inplace(copy(transpose(A.data)),k)
elseif A.uplo == 'U' && k > 0
return tril!(copy(transpose(A.data)),-1) + tril!(triu(A.data),k)
return tril_maybe_inplace(copy(transpose(A.data)),-1) + tril_maybe_inplace(triu(A.data),k)
elseif A.uplo == 'L' && k <= 0
return tril(A.data,k)
else
return tril(A.data,-1) + tril!(triu!(copy(transpose(A.data))),k)
return tril(A.data,-1) + tril_maybe_inplace(triu_maybe_inplace(copy(transpose(A.data))),k)
end
end

function triu(A::Hermitian, k::Integer=0)
if A.uplo == 'U' && k >= 0
return triu(A.data,k)
elseif A.uplo == 'U' && k < 0
return triu(A.data,1) + triu!(tril!(copy(A.data')),k)
return triu(A.data,1) + triu_maybe_inplace(tril_maybe_inplace(copy(A.data')),k)
elseif A.uplo == 'L' && k >= 0
return triu!(copy(A.data'),k)
return triu_maybe_inplace(copy(A.data'),k)
else
return triu!(copy(A.data'),1) + triu!(tril(A.data),k)
return triu_maybe_inplace(copy(A.data'),1) + triu_maybe_inplace(tril(A.data),k)
end
end

function triu(A::Symmetric, k::Integer=0)
if A.uplo == 'U' && k >= 0
return triu(A.data,k)
elseif A.uplo == 'U' && k < 0
return triu(A.data,1) + triu!(tril!(copy(transpose(A.data))),k)
return triu(A.data,1) + triu_maybe_inplace(tril_maybe_inplace(copy(transpose(A.data))),k)
elseif A.uplo == 'L' && k >= 0
return triu!(copy(transpose(A.data)),k)
return triu_maybe_inplace(copy(transpose(A.data)),k)
else
return triu!(copy(transpose(A.data)),1) + triu!(tril(A.data),k)
return triu_maybe_inplace(copy(transpose(A.data)),1) + triu_maybe_inplace(tril(A.data),k)
end
end

Expand Down
5 changes: 5 additions & 0 deletions src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,11 @@ function tril!(A::UnitLowerTriangular, k::Integer=0)
return tril!(LowerTriangular(A.data), k)
end

tril_maybe_inplace(A, k::Integer=0) = tril(A, k)
triu_maybe_inplace(A, k::Integer=0) = triu(A, k)
tril_maybe_inplace(A::StridedMatrix, k::Integer=0) = tril!(A, k)
triu_maybe_inplace(A::StridedMatrix, k::Integer=0) = triu!(A, k)

adjoint(A::LowerTriangular) = UpperTriangular(adjoint(A.data))
adjoint(A::UpperTriangular) = LowerTriangular(adjoint(A.data))
adjoint(A::UnitLowerTriangular) = UnitUpperTriangular(adjoint(A.data))
Expand Down
23 changes: 23 additions & 0 deletions test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1191,4 +1191,27 @@ end
@test_throws s_msg S[1,1] = v
end

@testset "triu/tril with immutable arrays" begin
struct ImmutableMatrix{T,A<:AbstractMatrix{T}} <: AbstractMatrix{T}
a :: A
end
Base.size(A::ImmutableMatrix) = size(A.a)
Base.getindex(A::ImmutableMatrix, i::Int, j::Int) = getindex(A.a, i, j)
Base.copy(A::ImmutableMatrix) = A
LinearAlgebra.adjoint(A::ImmutableMatrix) = ImmutableMatrix(adjoint(A.a))
LinearAlgebra.transpose(A::ImmutableMatrix) = ImmutableMatrix(transpose(A.a))

A = ImmutableMatrix([1 2; 3 4])
for T in (Symmetric, Hermitian), uplo in (:U, :L)
H = T(A, uplo)
MH = Matrix(H)
@test triu(H,-1) == triu(MH,-1)
@test triu(H) == triu(MH)
@test triu(H,1) == triu(MH,1)
@test tril(H,1) == tril(MH,1)
@test tril(H) == tril(MH)
@test tril(H,-1) == tril(MH,-1)
end
end

end # module TestSymmetric