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

Default uplo in symmetric/hermitian #52605

Merged
merged 2 commits into from
Dec 27, 2023
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/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ If a symmetric view of a matrix is to be constructed of which the elements are n
matrices nor numbers, an appropriate method of `symmetric` has to be implemented. In that
case, `symmetric_type` has to be implemented, too.
"""
symmetric(A::AbstractMatrix, uplo::Symbol) = Symmetric(A, uplo)
symmetric(A::Number, ::Symbol) = A
symmetric(A::AbstractMatrix, uplo::Symbol=:U) = Symmetric(A, uplo)
symmetric(A::Number, ::Symbol=:U) = A

"""
symmetric_type(T::Type)
Expand Down Expand Up @@ -164,8 +164,8 @@ If a hermitian view of a matrix is to be constructed of which the elements are n
matrices nor numbers, an appropriate method of `hermitian` has to be implemented. In that
case, `hermitian_type` has to be implemented, too.
"""
hermitian(A::AbstractMatrix, uplo::Symbol) = Hermitian(A, uplo)
hermitian(A::Number, ::Symbol) = convert(typeof(A), real(A))
hermitian(A::AbstractMatrix, uplo::Symbol=:U) = Hermitian(A, uplo)
hermitian(A::Number, ::Symbol=:U) = convert(typeof(A), real(A))

"""
hermitian_type(T::Type)
Expand Down
12 changes: 10 additions & 2 deletions stdlib/LinearAlgebra/test/symmetric.jl
Original file line number Diff line number Diff line change
Expand Up @@ -729,9 +729,9 @@ end
end

@testset "symmetric()/hermitian() for Numbers" begin
@test LinearAlgebra.symmetric(1, :U) == 1
@test LinearAlgebra.symmetric(1) == LinearAlgebra.symmetric(1, :U) == 1
@test LinearAlgebra.symmetric_type(Int) == Int
@test LinearAlgebra.hermitian(1, :U) == 1
@test LinearAlgebra.hermitian(1) == LinearAlgebra.hermitian(1, :U) == 1
@test LinearAlgebra.hermitian_type(Int) == Int
end

Expand Down Expand Up @@ -902,6 +902,14 @@ end
end
end

@testset "symmetric/hermitian for matrices" begin
A = [1 2; 3 4]
@test LinearAlgebra.symmetric(A) === Symmetric(A)
@test LinearAlgebra.symmetric(A, :L) === Symmetric(A, :L)
@test LinearAlgebra.hermitian(A) === Hermitian(A)
@test LinearAlgebra.hermitian(A, :L) === Hermitian(A, :L)
end

@testset "custom axes" begin
SZA = SizedArrays.SizedArray{(2,2)}([1 2; 3 4])
for T in (Symmetric, Hermitian)
Expand Down