diff --git a/README.md b/README.md index 5f10dfe0..47ef85f3 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,24 @@ # LinearAlgebra +--- +## 🚀 About this fork + +This is my fork of LinearAlgebra.jl for contributing to Julia as part of my GSoC 2026 preparation. + +**My contributions:** +- [PR #1445](https://github.com/JuliaLang/LinearAlgebra.jl/pull/1445) - Add hermitianpart method for Number types +- Active in issues: Coming + +**Original repository:** [JuliaLang/LinearAlgebra.jl](https://github.com/JuliaLang/LinearAlgebra.jl) + +--- + +# LinearAlgebra + +This package is part of the Julia standard library (stdlib). + +LinearAlgebra.jl provides functionality for performing linear algebra operations in Julia. + This package is part of the Julia standard library (stdlib). `LinearAlgebra.jl` provides functionality for performing linear algebra operations in Julia. diff --git a/src/symmetric.jl b/src/symmetric.jl index 50f34022..2a6751b9 100644 --- a/src/symmetric.jl +++ b/src/symmetric.jl @@ -1003,6 +1003,7 @@ end """ hermitianpart(A::AbstractMatrix, uplo::Symbol=:U) -> Hermitian + hermitianpart(x::Number) -> Number Return the Hermitian part of the square matrix `A`, defined as `(A + A') / 2`, as a [`Hermitian`](@ref) matrix. For real matrices `A`, this is also known as the symmetric part @@ -1010,12 +1011,18 @@ of `A`; it is also sometimes called the "operator real part". The optional argum [`Hermitian`](@ref) view. For real matrices, the latter is equivalent to a [`Symmetric`](@ref) view. +For scalar inputs `x`, the function returns the real part of `x`. Standard integer scalars +are promoted to `Float64` to align with the behavior of 1×1 Hermitian matrices, while other numeric types +(`Float32`, `BigInt`, `Rational`, `BigFloat`) are preserved. + See also [`hermitianpart!`](@ref) for the corresponding in-place operation. !!! compat "Julia 1.10" This function requires Julia 1.10 or later. """ + hermitianpart(A::AbstractMatrix, uplo::Symbol=:U) = Hermitian(_hermitianpart(A), uplo) +hermitianpart(x::Number) = float(real(x)) """ hermitianpart!(A::AbstractMatrix, uplo::Symbol=:U) -> Hermitian @@ -1052,4 +1059,4 @@ function Base.replace_in_print_matrix(A::HermOrSym,i::Integer,j::Integer,s::Abst ijminmax = minmax(i, j) inds = A.uplo == 'U' ? ijminmax : reverse(ijminmax) Base.replace_in_print_matrix(parent(A), inds..., s) -end +end \ No newline at end of file diff --git a/test/symmetric.jl b/test/symmetric.jl index 031dac27..c59e297d 100644 --- a/test/symmetric.jl +++ b/test/symmetric.jl @@ -1035,6 +1035,16 @@ end @test Aherm isa Hermitian @test Aherm.uplo == LinearAlgebra.char_uplo(uplo) end + @testset "hermitianpart for numbers" begin + @test hermitianpart(3 + 4im) == 3 + @test hermitianpart(5) == 5.0 + @test typeof(hermitianpart(5)) == Float64 + @test hermitianpart(2.5) == 2.5 + @test hermitianpart(-1 + 0im) == -1 + @test typeof(hermitianpart(3 + 4im)) == 3.0 + @test typeof(hermitianpart(3 + 4im)) == Float64 + @test typeof(hermitianpart(2.5 + 3im)) == Float64 +end end @testset "Structured display" begin