[R] Solving tridiagonal matrix in R

S Ellison S.Ellison at LGCGroup.com
Wed Apr 10 20:23:38 CEST 2013

> I am trying to solve a tridiagonal matrix in R. I am 
> wondering if there is an inbuilt R function or package to 
> solve that. I tried looking on google but couldn't find 
> something that would help directly. Any help is highly appreciated.

You must be searching a different Google. I found  Solve.tridiag in limSolve, and solve.tridiag in the fame package just by looking for 'R tridiag'.Both sound relevant, though you'd have to install FAME itself for the latter. I should say, though, that I got very puzzling results from Solve.tridiag at first try (it does not like integer B in A*x=B at all) so you may need to be careful there

But you may not have to look so far: In base R, if it's a small matrix, does creating an ordinary matrix and using solve() not do what you want?

And if it's big, the Matrix package (also included in the standard distribution) includes support for sparse matrices and should (and does) handle tridiagonals, though I don't think there is specific support for constructing them. It's not hard, though: one direct indexing method could be:

(caveat: be careful with applying lu() and the like in Matrix: lu itself does not return the simple form you might expect from the usual literature LU decomposition for a tridiagonal matrix)

S Ellison

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list