Skip to content
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

Inplace transpose for unit Triangular may skip diagonal #53101

Merged
merged 1 commit into from
Jan 31, 2024
Merged
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
8 changes: 4 additions & 4 deletions stdlib/LinearAlgebra/src/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -465,13 +465,13 @@ transpose(A::UnitLowerTriangular) = UnitUpperTriangular(transpose(A.data))
transpose(A::UnitUpperTriangular) = UnitLowerTriangular(transpose(A.data))

transpose!(A::LowerTriangular) = UpperTriangular(copytri!(A.data, 'L', false, true))
transpose!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L', false, true))
transpose!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L', false, false))
transpose!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U', false, true))
transpose!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U', false, true))
transpose!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U', false, false))
adjoint!(A::LowerTriangular) = UpperTriangular(copytri!(A.data, 'L' , true, true))
adjoint!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L' , true, true))
adjoint!(A::UnitLowerTriangular) = UnitUpperTriangular(copytri!(A.data, 'L' , true, false))
adjoint!(A::UpperTriangular) = LowerTriangular(copytri!(A.data, 'U' , true, true))
adjoint!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U' , true, true))
adjoint!(A::UnitUpperTriangular) = UnitLowerTriangular(copytri!(A.data, 'U' , true, false))

diag(A::LowerTriangular) = diag(A.data)
diag(A::UnitLowerTriangular) = fill(oneunit(eltype(A)), size(A,1))
Expand Down
26 changes: 23 additions & 3 deletions stdlib/LinearAlgebra/test/triangular.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ debug && println("Test basic type functionality")
@test LowerTriangular(randn(3, 3)) |> t -> [size(t, i) for i = 1:3] == [size(Matrix(t), i) for i = 1:3]

# The following test block tries to call all methods in base/linalg/triangular.jl in order for a combination of input element types. Keep the ordering when adding code.
for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFloat}, Int)
@testset for elty1 in (Float32, Float64, BigFloat, ComplexF32, ComplexF64, Complex{BigFloat}, Int)
# Begin loop for first Triangular matrix
for (t1, uplo1) in ((UpperTriangular, :U),
(UnitUpperTriangular, :U),
Expand Down Expand Up @@ -998,9 +998,12 @@ end

@testset "arithmetic with partly uninitialized matrices" begin
@testset "$(typeof(A))" for A in (Matrix{BigFloat}(undef,2,2), Matrix{Complex{BigFloat}}(undef,2,2)')
A[1,1] = A[2,2] = A[2,1] = 4
A[2,1] = eltype(A) <: Complex ? 4 + 3im : 4
B = Matrix{eltype(A)}(undef, size(A))
for MT in (LowerTriangular, UnitLowerTriangular)
if MT == LowerTriangular
A[1,1] = A[2,2] = eltype(A) <: Complex ? 4 + 3im : 4
end
L = MT(A)
B .= 0
copyto!(B, L)
Expand All @@ -1009,13 +1012,23 @@ end
@test 2\L == 2\B
@test real(L) == real(B)
@test imag(L) == imag(B)
if A isa Matrix
@test transpose!(MT(copy(A))) == transpose(L)
@test adjoint!(MT(copy(A))) == adjoint(L)
else
@test_broken transpose!(MT(copy(A))) == transpose(L)
@test_broken adjoint!(MT(copy(A))) == adjoint(L)
end
end
end

@testset "$(typeof(A))" for A in (Matrix{BigFloat}(undef,2,2), Matrix{Complex{BigFloat}}(undef,2,2)')
A[1,1] = A[2,2] = A[1,2] = 4
A[1,2] = eltype(A) <: Complex ? 4 + 3im : 4
B = Matrix{eltype(A)}(undef, size(A))
for MT in (UpperTriangular, UnitUpperTriangular)
if MT == UpperTriangular
A[1,1] = A[2,2] = eltype(A) <: Complex ? 4 + 3im : 4
end
U = MT(A)
B .= 0
copyto!(B, U)
Expand All @@ -1024,6 +1037,13 @@ end
@test 2\U == 2\B
@test real(U) == real(B)
@test imag(U) == imag(B)
if A isa Matrix
@test transpose!(MT(copy(A))) == transpose(U)
@test adjoint!(MT(copy(A))) == adjoint(U)
else
@test_broken transpose!(MT(copy(A))) == transpose(U)
@test_broken adjoint!(MT(copy(A))) == adjoint(U)
end
end
end
end
Expand Down