# Generalized eigenvalues with Diagonal B

**URL:** https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372
**Category:** Numerics
**Created:** [March 1, 2020, 8:17pm UTC](https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372 "2020-03-01T20:17:15Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)
#### Post date: [March 1, 2020, 8:17pm UTC](https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372/1 "2020-03-01T20:17:15Z")

</div>

Looking to solve a generalized eigenproblem. All is well here:

```julia
julia> A = rand(4,4);
julia> B = rand(4,4);
julia> eigen(A,B)
GeneralizedEigen{Complex{Float64},Complex{Float64},Array{Complex{Float64},2},Array{Complex{Float64},1}}
eigenvalues:
4-element Array{Complex{Float64},1}:
  -1.7837186962342586 + 0.0im               
 -0.24734999477221975 - 0.3007940273651078im
...

```

But then I ran into this:

```julia
julia> B = Diagonal(1:4);
julia> eigen(A,B)
ERROR: MethodError: no method matching eigen!(::Array{Float64,2}, ::Diagonal{Float64,Array{Float64,1}})
Closest candidates are:
...

```

An explicit cast to a matrix makes it all better:

```julia
julia> eigen(A,Matrix(B))
GeneralizedEigen{Float64,Float64,Array{Float64,2},Array{Float64,1}}
eigenvalues:
4-element Array{Float64,1}:
 -0.009056339130226663
  0.14327387286591    
...

```

But I expected the original to work, given that

```julia
julia> B isa AbstractMatrix
true

```

and `eigen(B)` is fine, too. So is this the intended behavior?

---

<div class="post-metadata">

### Author: ![andreasnoack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andreasnoack/32/27_2.png) [@andreasnoack](https://discourse.julialang.org/u/andreasnoack)
#### Post date: [March 1, 2020, 8:37pm UTC](https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372/2 "2020-03-01T20:37:17Z")

</div>

Our generalized eigensolvers are currently just calling LAPACK so the supported signatures correspond to what LAPACK offers. It wouldn’t be hard to add a specialized method for diagonal right hand side but I think you are the first one to ask for it. It would be great if you could make a PR that adds support for diagonal right hand side.

---

<div class="post-metadata">

### Author: ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)
#### Post date: [March 1, 2020, 8:57pm UTC](https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372/3 "2020-03-01T20:57:16Z")

</div>

You can also call `C = sqrt.(B); eigen(inv(C) * A * inv(C))`, and then multiply the eigenvectors by `inv(C)` to get the generalized eigenvectors. The eigenvalues are the same.

---

<div class="post-metadata">

### Author: ![tobydriscoll](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tobydriscoll/32/1843_2.png) [@tobydriscoll](https://discourse.julialang.org/u/tobydriscoll)
#### Post date: [March 6, 2020, 2:34pm UTC](https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372/4 "2020-03-06T14:34:17Z")

</div>

I’m willing to look into that, but what surprised me is that there is no dispatch on an `AbstractMatrix` for B that would supply a default fallback behavior. I’m sure there are aspects to doing that I don’t yet appreciate.

---

<div class="post-metadata">

### Author: ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)
#### Post date: [March 6, 2020, 5:45pm UTC](https://discourse.julialang.org/t/generalized-eigenvalues-with-diagonal-b/35372/5 "2020-03-06T17:45:13Z")

</div>

> [@tobydriscoll](#):
>
> what surprised me is that there is no dispatch on an `AbstractMatrix` for B that would supply a default fallback behavior.

Basically it is because generic dense eigensolver algorithms are very complicated to implement well. There is one in the [GenericLinearAlgebra package](https://github.com/JuliaLinearAlgebra/GenericLinearAlgebra.jl), but it will be much slower than the LAPACK version for `Matrix{Float64}` and (AFAIK) doesn’t take advantage of special sparsity structures like diagonal `B`.
