Skip to content

Commit 8bf4fdc

Browse files
Apply suggestion to return Float64 for scalars; update docstring and tests accordingly
1 parent 9335758 commit 8bf4fdc

File tree

2 files changed

+12
-5
lines changed

2 files changed

+12
-5
lines changed

src/symmetric.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,21 +1003,26 @@ end
10031003

10041004
"""
10051005
hermitianpart(A::AbstractMatrix, uplo::Symbol=:U) -> Hermitian
1006-
hermitianpart(x::Number) -> Real
1006+
hermitianpart(x::Number) -> Number
10071007
10081008
Return the Hermitian part of the square matrix `A`, defined as `(A + A') / 2`, as a
10091009
[`Hermitian`](@ref) matrix. For real matrices `A`, this is also known as the symmetric part
10101010
of `A`; it is also sometimes called the "operator real part". The optional argument `uplo` controls the corresponding argument of the
10111011
[`Hermitian`](@ref) view. For real matrices, the latter is equivalent to a
10121012
[`Symmetric`](@ref) view.
10131013
1014+
For scalar inputs `x`, the function returns the real part of `x`. Standard integer scalars
1015+
are promoted to `Float64` to align with the behavior of 1×1 Hermitian matrices, while other numeric types
1016+
(`Float32`, `BigInt`, `Rational`, `BigFloat`) are preserved.
1017+
10141018
See also [`hermitianpart!`](@ref) for the corresponding in-place operation.
10151019
10161020
!!! compat "Julia 1.10"
10171021
This function requires Julia 1.10 or later.
10181022
"""
1023+
10191024
hermitianpart(A::AbstractMatrix, uplo::Symbol=:U) = Hermitian(_hermitianpart(A), uplo)
1020-
hermitianpart(x::Number) = real(x)
1025+
hermitianpart(x::Number) = float(real(x))
10211026

10221027
"""
10231028
hermitianpart!(A::AbstractMatrix, uplo::Symbol=:U) -> Hermitian
@@ -1054,4 +1059,4 @@ function Base.replace_in_print_matrix(A::HermOrSym,i::Integer,j::Integer,s::Abst
10541059
ijminmax = minmax(i, j)
10551060
inds = A.uplo == 'U' ? ijminmax : reverse(ijminmax)
10561061
Base.replace_in_print_matrix(parent(A), inds..., s)
1057-
end
1062+
end

test/symmetric.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1037,10 +1037,12 @@ end
10371037
end
10381038
@testset "hermitianpart for numbers" begin
10391039
@test hermitianpart(3 + 4im) == 3
1040-
@test hermitianpart(5) == 5
1040+
@test hermitianpart(5) == 5.0
1041+
@test typeof(hermitianpart(5)) == Float64
10411042
@test hermitianpart(2.5) == 2.5
10421043
@test hermitianpart(-1 + 0im) == -1
1043-
@test typeof(hermitianpart(3 + 4im)) == Int
1044+
@test typeof(hermitianpart(3 + 4im)) == 3.0
1045+
@test typeof(hermitianpart(3 + 4im)) == Float64
10441046
@test typeof(hermitianpart(2.5 + 3im)) == Float64
10451047
end
10461048
end

0 commit comments

Comments
 (0)