# Eigen() warntype

**URL:** <https://discourse.julialang.org/t/eigen-warntype/21239>\
**Category:** New to Julia\
**Created:** [February 27, 2019, 12:28am UTC](https://discourse.julialang.org/t/eigen-warntype/21239 "2019-02-27T00:28:35Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 27, 2019, 12:28am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/1 "2019-02-27T00:28:35Z")

</div>

calling `LinearAlgebra.eigen()` gives warntype. Is it a bug?

```julia
import Statistics
import LinearAlgebra
function test(x::Matrix{Float64})
	sigma = Statistics.cov(x)
	eig = LinearAlgebra.eigen(sigma)
	return eig
end

julia> @code_warntype test(randn(20, 5) )
Body::Union{Eigen{Complex{Float64},Complex{Float64},Array{Complex{Float64},2},Array{Complex{Float64},1}}, Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}}
1 ─ %1 = Statistics.cov::Core.Compiler.Const(Statistics.cov, false)
│ %2 = invoke Statistics.:(#cov#37)(1::Int64, true::Bool, %1::Function, _2::Array{Float64,2})::Array{Float64,2}
│ %3 = LinearAlgebra.eigen::Core.Compiler.Const(LinearAlgebra.eigen, false)
│ %4 = invoke LinearAlgebra.:(#eigen#58)(true::Bool, true::Bool, %3::Function, %2::Array{Float64,2})::Union{Eigen{Complex{Float64},Complex{Float64},Array{Complex{Float64},2},Array{Complex{Float64},1}}, Eigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}}
└── return %4

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2019, 12:39am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/2 "2019-02-27T00:39:28Z")

</div>

The eigenvalues and eigenvectors can be real or complex. This is deliberate behaviour (not a bug).

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 27, 2019, 2:17am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/3 "2019-02-27T02:17:16Z")

</div>

okay, even it’s not a bug, it’s a design issue: it gives a warntype, which would slows down runtime.

or, is it a special case that has **no** runtime impact?

moreover, as far as I know, for positive-semifinite matrix like the covariance, the Eigen values and vectors would always be real. Could we do better?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2019, 2:26am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/4 "2019-02-27T02:26:38Z")

</div>

The amount of time spent on the warning will be tiny compared to the time taken to calculate the eigen-decomposition.

There is no type instability if the matrix is e.g. of type `Symmetric`.

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 27, 2019, 2:38am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/5 "2019-02-27T02:38:35Z")

</div>

thanks! so, the function should better be written as:

```julia
function test2(x::Matrix{Float64})
	sigma = Statistics.cov(x)
	eig = LinearAlgebra.eigen(LinearAlgebra.Symmetric(sigma) )
	return eig
end

x = randn(20, 5)

julia> test(x) == test2(x)
true

@code_warntype test2(x) # fine

```

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2019, 2:40am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/6 "2019-02-27T02:40:06Z")

</div>

Maybe `Statistics.cov` should automatically return a `Symmetric` matrix?

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2019, 2:40am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/7 "2019-02-27T02:40:37Z")

</div>

But probably that would lead to type instability somewhere down the line.

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 27, 2019, 2:41am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/8 "2019-02-27T02:41:35Z")

</div>

Note that you can test that the result type of the calculation is the same as the type that Julia infers using

```julia
using Test
@inferred test2(x)

```

---

<div class="post-metadata">

**Author:** ![tomtom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomtom/32/5106_2.png) [@tomtom](https://discourse.julialang.org/u/tomtom)\
**Post date:** [February 27, 2019, 2:42am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/9 "2019-02-27T02:42:58Z")

</div>

covariance is not only symmetric, it’s also positive-semi-finite (I think). It may be a big change to `LinearAlgebra` and `Statistics` to incorporate a special matrix type for it.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [February 27, 2019, 4:10am UTC](https://discourse.julialang.org/t/eigen-warntype/21239/10 "2019-02-27T04:10:01Z")

</div>

Yes, a covariance matrix is at least positive semi-definite.  
If the number of linearly independent samples exceeds the dimensionality, it will be positive definite.

If you’re taking the eigendecomposition anyway, you can easily check whether all the eigenvalues are positive.
