JuliaLang / LinearAlgebra.jl

Julia Linear Algebra standard library
Other
17 stars 4 forks source link

Type instability for eigen of a complex matrix #1039

Closed albertomercurio closed 1 year ago

albertomercurio commented 1 year ago

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 commented 1 year ago

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