Skip to content

Commit

Permalink
Default uplo in symmetric/hermitian (#52605)
Browse files Browse the repository at this point in the history
This makes the function signatures match the respective docstrings, as
well as that of `Symmetric/Hermitian`.
  • Loading branch information
jishnub committed Dec 27, 2023
1 parent 713560b commit b4eefd0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
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

0 comments on commit b4eefd0

Please sign in to comment.