Re: [eigen] Solving a tridiagonal linear system |
[ Thread Index |
Date Index
| More lists.tuxfamily.org/eigen Archives
]
- To: eigen@xxxxxxxxxxxxxxxxxxx
- Subject: Re: [eigen] Solving a tridiagonal linear system
- From: Gael Guennebaud <gael.guennebaud@xxxxxxxxx>
- Date: Fri, 26 Nov 2010 10:28:56 +0100
- Dkim-signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:content-type; bh=s/qnr0KwzDRUC/mVRqHDagJTQYjl7H+r8yDOcLiiKyk=; b=DDgFioommCC9ctA/z4OcJK1paY9EsJrhX1bHqxwXEN26uxl/Owdy1ubAXplRu3ZXHJ pGL9cQ73eV5uD1SQL+uiKoW9joeLw4hiL46zL1rPTID9xykVCT2jFhAblX7EnRfaKaOU 3tgF1gDSBbMJGmqUqBTPRsQKvzARfMmBnaRTc=
- Domainkey-signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; b=EKeH3ugfDFNHMpDffM2obWG1WZf87XFXGvP20y6UEKtPsa8HdJnspF9pJKegZ7TM8i tdPJUYZey7ZjX2pLi8hNNLYY1s9vXmkzoPRv2z8PSE7UiDvIS/uQBuazljsJFVXGQWzT GcITvSh1QU2ErtOcA8Pvp2gg7RDnbpUKXruNE=
Indeed, currently we only provide a storage class for band and
tridiagonal matrices and the operations like products, triangular
solving and more general solver are missing.
However writing an efficient dedicated solver is quite straightforward
since there is no need to consider special optimization such as
blocking and/or vectorization.
In the meantime, I've attached an example of a Cholesky solver for
selfadjoint tridiagonal matrices which uses the subdiagonal only.
Adapting it for a tridiagonal LU factorization should be easy.
gael
On Fri, Nov 26, 2010 at 2:03 AM, Andrea Arteaga <yo.eres@xxxxxxxxx> wrote:
> Hi.
> First of all thank you for this fantastic library!
> I have a TridiagonalMatrix resulting from a cubic natural spline
> interpolation problem. Now I have to solve a system with this matrix and a
> rhs vector, but I don't find any method to do this in an efficient way.
> Actually the only method which seems to exist is doing A.toDenseMatrix()....,
> but in this way the optimization due to the tridiagonal pattern gets lost (I
> suppose). So, how can I do that?
> Thanks.
> Andrea Arteaga
#include <iostream>
#include <Eigen/Dense>
namespace Eigen {
namespace internal {
template<typename Scalar,int Size, int Options> bool tridiag_llt_inplace(TridiagonalMatrix<Scalar,Size,Options>& mat) {
typedef typename NumTraits<Scalar>::Real RealScalar;
typedef DenseIndex Index;
const Index size = mat.cols();
for(Index k = 0; k < size; ++k)
{
RealScalar x = real(mat.diagonal().coeff(k));
if (k>0) x -= abs2(mat.sub().coeff(k-1));
if (x<=RealScalar(0)) return false;
mat.diagonal().coeffRef(k) = x = sqrt(x);
if (k+1<size) mat.sub().coeffRef(k) /= x;
}
return true;
}
template<typename Scalar,int Size, int Options, typename Vector> void tridiag_llt_solve_inplace(TridiagonalMatrix<Scalar,Size,Options>& mat, Vector& vec) {
typedef typename NumTraits<Scalar>::Real RealScalar;
typedef DenseIndex Index;
const Index size = mat.cols();
for(Index k = 0; k < size; ++k)
{
if(k>0) vec.coeffRef(k) -= vec.coeff(k-1) * mat.sub().coeff(k-1);
vec.coeffRef(k) /= mat.diagonal().coeff(k);
}
for(Index k = size-1; k >= 0; --k)
{
if(k<size-1) vec.coeffRef(k) -= vec.coeff(k+1) * conj(mat.sub().coeff(k));
vec.coeffRef(k) /= mat.diagonal().coeff(k);
}
}
}
}
using namespace Eigen;
int main()
{
int n = 10;
TridiagonalMatrix<double,Dynamic,SelfAdjoint> t(n);
t.diagonal().setRandom();
t.sub().setRandom();
MatrixXd d = t.toDenseMatrix();
VectorXd x0(n), x1;
x0.setRandom();
x1 = x0;
internal::tridiag_llt_inplace(t);
tridiag_llt_solve_inplace(t,x0);
x1 = d.selfadjointView<Lower>().llt().solve(x1);
std::cerr << x0.transpose() << "\n" << x1.transpose() << "\n";
return 0;
}