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

Fix dispatch on alg in Float16 Hermitian eigen #55928

Merged
merged 1 commit into from
Oct 1, 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
19 changes: 12 additions & 7 deletions stdlib/LinearAlgebra/src/symmetriceigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,6 @@ function eigen!(A::RealHermSymComplexHerm{<:BlasReal,<:StridedMatrix}, alg::Algo
throw(ArgumentError("Unsupported value for `alg` keyword."))
end
end
function eigen(A::RealHermSymComplexHerm{Float16}; sortby::Union{Function,Nothing}=nothing)
S = eigtype(eltype(A))
E = eigen!(eigencopy_oftype(A, S), sortby=sortby)
values = convert(AbstractVector{Float16}, E.values)
vectors = convert(AbstractMatrix{isreal(E.vectors) ? Float16 : Complex{Float16}}, E.vectors)
return Eigen(values, vectors)
end

"""
eigen(A::Union{Hermitian, Symmetric}, alg::Algorithm = default_eigen_alg(A)) -> Eigen
Expand All @@ -53,10 +46,22 @@ The default `alg` used may change in the future.
The following functions are available for `Eigen` objects: [`inv`](@ref), [`det`](@ref), and [`isposdef`](@ref).
"""
function eigen(A::RealHermSymComplexHerm, alg::Algorithm = default_eigen_alg(A); sortby::Union{Function,Nothing}=nothing)
_eigen(A, alg; sortby)
end

# we dispatch on the eltype in an internal method to avoid ambiguities
function _eigen(A::RealHermSymComplexHerm, alg::Algorithm; sortby)
S = eigtype(eltype(A))
eigen!(eigencopy_oftype(A, S), alg; sortby)
end

function _eigen(A::RealHermSymComplexHerm{Float16}, alg::Algorithm; sortby::Union{Function,Nothing}=nothing)
S = eigtype(eltype(A))
E = eigen!(eigencopy_oftype(A, S), alg, sortby=sortby)
values = convert(AbstractVector{Float16}, E.values)
vectors = convert(AbstractMatrix{isreal(E.vectors) ? Float16 : Complex{Float16}}, E.vectors)
return Eigen(values, vectors)
end

eigen!(A::RealHermSymComplexHerm{<:BlasReal,<:StridedMatrix}, irange::UnitRange) =
Eigen(LAPACK.syevr!('V', 'I', A.uplo, A.data, 0.0, 0.0, irange.start, irange.stop, -1.0)...)
Expand Down
5 changes: 5 additions & 0 deletions stdlib/LinearAlgebra/test/symmetriceigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,11 @@ end
@test D isa Eigen{ComplexF16, Float16, Matrix{ComplexF16}, Vector{Float16}}
@test D.values ≈ D32.values
@test D.vectors ≈ D32.vectors

# ensure that different algorithms dispatch correctly
λ, V = eigen(C, LinearAlgebra.QRIteration())
@test λ isa Vector{Float16}
@test C * V ≈ V * Diagonal(λ)
end

@testset "complex Symmetric" begin
Expand Down