tags:

views:

310

answers:

4

If I have this:

A * f = g;
A: upper triangular matrix (n x n)
f: (n x 1)
g: (n x 1)

Need to solve for f using back substitution algorithm. I would say that it not really that hard to write one myself, but oh well, if there is a library out there, then why not.

+3  A: 

Use the LAPACK. It's already installed on many systems, and there are many implementations available for systems that don't have it.

Specifically, the routine you want is dtrtrs or strtrs, depending on whether your data is in double- or single-precision.

Stephen Canon
From a quick glance at their site: "LAPACK is written in Fortran90..." Not that you can't get C++ to call Fortran, but it can be somewhat inconvenient.
Travis Gockel
The language that a library is written in doesn't matter; there's a standardized C interface, and the library is available on most every platform so you don't generally need to build it yourself. If you do need to build it for some reason, there's a C version of the library (CLAPACK) as well. It's the industry standard, and the most portable solution by far.
Stephen Canon
A: 

Is it one of these? I haven't heard of solving systems of linear equations with "back substitution" before. Why does it have to be back substitution?

http://eigen.tuxfamily.org/dox/TutorialAdvancedLinearAlgebra.html

Chris H
Triangular matrices are special in that no factorization is necessary to solve the system, and the standard method (which is also the grade-school algebra method) of solving them is called "back substitution" if the matrix is upper triangular, because you first solve for x[n], then x[n-1], then x[n-2], etc...
Stephen Canon
Oh. I haven't heard of it being called that before.Did a quick check with eigen. It has it...but it's not "official yet" whatever that means.http://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html#a89efaba57cb2952b65868b4d679ce954
Chris H
+2  A: 

Boost uBlas should work. At least if I understand your question correctly, you probably want to start by looking at lu_substitute() and inplace_solve().

Jerry Coffin
Harry Pham
A: 

For simplicity & lack of dependencies, I'd go for JAMA+TNT and use the LU class for factorization and its solve() method. There doesn't appear to be a way you can initialize LU with a pre-existing upper triangular matrix (LU's constructor makes no assumptions and just starts factoring) but I would think you could either use it as is, and live with the performance hit of the redundant factoring, or just take solve method and adapt it to your needs.

Jason S