Skip to content

Commit f3fe7d2

Browse files
authored
Improve type-inference in complex eigen (#52290)
Indexing using integers instead of a `Vector` uses constant-propagation to improve the inferred return types. After this, the return type of `eigen(::Matrix{ComplexF64})` is inferred as a small union. ```julia julia> @code_typed eigen(rand(ComplexF64,2,2)) CodeInfo( 1 ─ %1 = invoke LinearAlgebra.:(var"#eigen#94")(true::Bool, true::Bool, LinearAlgebra.eigsortby::typeof(LinearAlgebra.eigsortby), #self#::typeof(eigen), A::Matrix{ComplexF64})::Union{Eigen{ComplexF64, ComplexF64, Matrix{ComplexF64}, Vector{ComplexF64}}, Eigen{ComplexF64, Float64, Matrix{ComplexF64}, Vector{Float64}}} └── return %1 ) => Union{Eigen{ComplexF64, ComplexF64, Matrix{ComplexF64}, Vector{ComplexF64}}, Eigen{ComplexF64, Float64, Matrix{ComplexF64}, Vector{Float64}}} ``` Close #52289
1 parent 3a33188 commit f3fe7d2

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/eigen.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,8 @@ function eigen!(A::StridedMatrix{T}; permute::Bool=true, scale::Bool=true, sortb
173173
n = size(A, 2)
174174
n == 0 && return Eigen(zeros(T, 0), zeros(T, 0, 0))
175175
ishermitian(A) && return eigen!(Hermitian(A), sortby=sortby)
176-
eval, evec = LAPACK.geevx!(permute ? (scale ? 'B' : 'P') : (scale ? 'S' : 'N'), 'N', 'V', 'N', A)[[2,4]]
176+
E = LAPACK.geevx!(permute ? (scale ? 'B' : 'P') : (scale ? 'S' : 'N'), 'N', 'V', 'N', A)
177+
eval, evec = E[2], E[4]
177178
return Eigen(sorteig!(eval, evec, sortby)...)
178179
end
179180

test/eigen.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,12 @@ end
243243
@test F.vectors F32.vectors
244244
end
245245

246+
@testset "complex eigen inference (#52289)" begin
247+
A = ComplexF64[1.0 0.0; 0.0 8.0]
248+
TC = Eigen{ComplexF64, ComplexF64, Matrix{ComplexF64}, Vector{ComplexF64}}
249+
TR = Eigen{ComplexF64, Float64, Matrix{ComplexF64}, Vector{Float64}}
250+
λ, v = @inferred Union{TR,TC} eigen(A)
251+
@test λ == [1.0, 8.0]
252+
end
253+
246254
end # module TestEigen

0 commit comments

Comments
 (0)