# easy way to solve problems with tridiagonal matrix

**URL:** <https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715>\
**Category:** General Usage\
**Tags:** question\
**Created:** [October 1, 2018, 2:25am UTC](https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715 "2018-10-01T02:25:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sahaskn1](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sahaskn1/32/5568_2.png) [@sahaskn1](https://discourse.julialang.org/u/sahaskn1)\
**Post date:** [October 1, 2018, 2:25am UTC](https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715/1 "2018-10-01T02:25:42Z")

</div>

I’m learning JuliaLang as an alternative to python for its speed. I would like to know if there exist functions in julia for tridiagonal matrix problem typically encountered in quantum kinetic energy operator in coordinate grid, like eigen value and vectors of the tridiagonal matrix and solution: Tridg\*x = b where b’s are known without full form of Tridg 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:** [October 1, 2018, 5:22am UTC](https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715/2 "2018-10-01T05:22:20Z")

</div>

Julia has `Tridiagonal` and `SymTriDiagonal` types in the standard library package `LinearAlgebra`.

For example (Julia 1.0):

```julia
julia> using LinearAlgebra

julia> n = 10000

julia> S = SymTridiagonal(randn(n), randn(n-1));

julia> @time evals = eigvals(S);
  2.216645 seconds (13 allocations: 313.047 KiB)

```

---

<div class="post-metadata">

**Author:** ![ccmejia](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ccmejia/32/18680_2.png) [@ccmejia](https://discourse.julialang.org/u/ccmejia)\
**Post date:** [June 7, 2020, 1:23am UTC](https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715/3 "2020-06-07T01:23:54Z")

</div>

Dear @dpsanders, I would like to ask you something. We can write a symmetric tridiagonal matrix by using the Tridiagonal function:

```julia
S = Tridiagonal( randn(n-1),randn(n), randn(n-1));

```

However when I pass this S matrix to eigvals function I obtain:

> MethodError: no method matching eigvals!(::Tridiagonal{Float64,Array{Float64,1}})  
> Closest candidates are: eigvals!(!Matched::SymTridiagonal{#s662,V}

So, what is going on here?  
Thanks in advance,  
Crist.

---

<div class="post-metadata">

**Author:** ![jamblejoe](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@jamblejoe](https://discourse.julialang.org/u/jamblejoe)\
**Post date:** [June 7, 2020, 2:07am UTC](https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715/4 "2020-06-07T02:07:20Z")

</div>

@ccmejia That is because your `S` is in general not symmetric, hence its eigenvalues are in general complex. I do not know exactly why, but Julia forbids using `eigvals` with non-symmetric real matrices, but I guess it uses specialized fast algorithms, which make use of the real entries and the symmetry of the matrix. You have at least 2 options:

1. Make the matrix symmetric
2. Do not specialize the tridimensional structure and cast it to an array

```julia
S = Array( Tridiagonal( randn(n-1),randn(n), randn(n-1)) )

```

if it comes in that type or avoid it at all.

---

<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:** [June 7, 2020, 2:18am UTC](https://discourse.julialang.org/t/easy-way-to-solve-problems-with-tridiagonal-matrix/15715/5 "2020-06-07T02:18:23Z")

</div>

Not every specialized matrix type has solvers for every factorization (eigensolvers, SVD, `inv`, etcetera), so you might have to convert it to `Matrix` first.

The `Tridiagonal` type was initially designed mainly for solving tridiagonal systems (e.g. with `\`), so it doesn’t have a specialized eigensolver. It would be reasonable to implement one, however: [No method `eigen(::Tridiagonal)` · Issue #29396 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/29396)
