JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.77k stars 5.49k forks source link

eigen is type-unstable #41243

Open oxinabox opened 3 years ago

oxinabox commented 3 years ago

This was mentioned as an aside in https://github.com/JuliaLang/julia/issues/35667 but it deserves it's own issue I think.

Consider for a input of real zeros

julia> eigen(zeros(3,3))
Eigen{Float64, Float64, Matrix{Float64}, Vector{Float64}, Vector{Float64}}
values:
3-element Vector{Float64}:
 0.0
 0.0
 0.0
vectors:
3×3 Matrix{Float64}:
 1.0  0.0  0.0
 0.0  1.0  0.0
 0.0  0.0  1.0

but for a different input the result is Complex

julia> eigen([0.9 0.1 0.4; 0.8 0.6 0.4; 0.2 0.8 0.8])
Eigen{ComplexF64, ComplexF64, Matrix{ComplexF64}, Vector{ComplexF64}, Vector{Float64}}
values:
3-element Vector{ComplexF64}:
 0.3286783740011112 - 0.26864083867299626im
 0.3286783740011112 + 0.26864083867299626im
 1.6426432519977765 + 0.0im
vectors:
3×3 Matrix{ComplexF64}:
 -0.366344+0.227207im  -0.366344-0.227207im  0.441497+0.0im
 -0.359545-0.313935im  -0.359545+0.313935im   0.59605+0.0im
  0.765729-0.0im        0.765729+0.0im       0.670675+0.0im

This is in-contrast with the behavior of log

julia> log(-0.2)
ERROR: DomainError with -0.2:
log will only return a complex result if called with a complex argument. Try log(Complex(x)).

was a problem for ChainRules requiring: https://github.com/JuliaDiff/ChainRules.jl/pull/445#pullrequestreview-685218673

andreasnoack commented 3 years ago

See https://groups.google.com/g/julia-users/c/hNyrSOvKP1s/m/Fy59kBQPTCUJ and https://github.com/JuliaLang/julia/issues/12304