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

Type instability for eigen of a complex matrix #52289

Closed
albertomercurio opened this issue Nov 23, 2023 · 1 comment · Fixed by #52290
Closed

Type instability for eigen of a complex matrix #52289

albertomercurio opened this issue Nov 23, 2023 · 1 comment · Fixed by #52290
Labels

Comments

@albertomercurio
Copy link
Contributor

Hello, this is a simple example.

First, I test the code_warntype with a Float matrix.

using LinearAlgebra

A = rand(Float64, 10, 10)

@code_warntype eigen(A)
MethodInstance for LinearAlgebra.eigen(::Matrix{Float64})
  from eigen(A::AbstractMatrix{T}; permute, scale, sortby) where T @ LinearAlgebra ~/.julia/juliaup/julia-1.9.3+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/eigen.jl:235
Static Parameters
  T = Float64
Arguments
  #self#::Core.Const(LinearAlgebra.eigen)
  A::Matrix{Float64}
Body::Union{Eigen{ComplexF64, ComplexF64, Matrix{ComplexF64}, Vector{ComplexF64}}, Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}}}
1%1 = LinearAlgebra.:(var"#eigen#96")(true, true, LinearAlgebra.eigsortby, #self#, A)::Union{Eigen{ComplexF64, ComplexF64, Matrix{ComplexF64}, Vector{ComplexF64}}, Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}}}
└──      return %1

I get a Unition type, but I think that it is because I can have complex or real eigenvalues depending on A, if it is Hermitian or not. (Should it be fixed by introducing a function barrier, perhaps with a Val argument depending on the hermiticity?)

Here, instead, there is the code in the case of a complex matrix.

A = rand(ComplexF64, 10, 10)

@code_warntype eigen(A)
MethodInstance for LinearAlgebra.eigen(::Matrix{ComplexF64})
  from eigen(A::AbstractMatrix{T}; permute, scale, sortby) where T @ LinearAlgebra ~/.julia/juliaup/julia-1.9.3+0.x64.linux.gnu/share/julia/stdlib/v1.9/LinearAlgebra/src/eigen.jl:235
Static Parameters
  T = ComplexF64
Arguments
  #self#::Core.Const(LinearAlgebra.eigen)
  A::Matrix{ComplexF64}
Body::Eigen
1%1 = LinearAlgebra.:(var"#eigen#96")(true, true, LinearAlgebra.eigsortby, #self#, A)::Eigen
└──      return %1

Here there is my versioninfo

Julia Version 1.9.3
Commit bed2cd540a1 (2023-08-24 14:43 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 12 × Intel(R) Core(TM) i7-10750H CPU @ 2.60GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-14.0.6 (ORCJIT, skylake)
  Threads: 6 on 12 virtual cores
Environment:
  JULIA_NUM_THREADS = 6
  JULIA_EDITOR = code
@albertomercurio
Copy link
Contributor Author

Thank you. When will it be available after the merging?

aviatesk pushed a commit that referenced this issue Nov 25, 2023
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
mkitti pushed a commit to mkitti/julia that referenced this issue Dec 9, 2023
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 JuliaLang#52289
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants