LinearSolver Class Template Reference
[Fixed-size classesSolving systems of equations]

Solver for systems of linear equations (fixed-size). More...

#include <linearsolver.h>

Inheritance diagram for LinearSolver:

LinearSolverBase

List of all members.

Public Member Functions

int dimSolutions ()
const VectorType & genericSolution (const T *parameter)
 LinearSolver (const Matrix< T, Size > &leftHandSide, const Vector< T, Size > &rightHandSide)
const VectorType & someSolution ()
bool thereExistSolutions ()

Protected Member Functions

void init (const MatrixType &leftHandSide, const VectorType &rightHandSide)

Protected Attributes

MatrixType m_basisKer
bool m_computedBasisKer
VectorType m_genericSolution
LUDecompositionType m_luDecomposition
VectorType m_rightHandSide
VectorType m_someSolution
bool m_thereExistSolutions


Detailed Description

template<typename T, int Size>
class Eigen::LinearSolver< T, Size >

Solver for systems of linear equations (fixed-size).

This class provides an easy way to solve systems of linear equations.

The template parameter T is the type of the numbers over which the equations are to be solved. It can be any type representing either real or complex numbers. The template parameter Size is the number of equations, or equivalently the number of indeterminates, as Eigen only allows square matrices. The following typedefs are provided to cover the usual cases:

    typedef LinearSolver<double, 2>               LinearSolver2d;
    typedef LinearSolver<double, 3>               LinearSolver3d;
    typedef LinearSolver<double, 4>               LinearSolver4d;
    typedef LinearSolver<float,  2>               LinearSolver2f;
    typedef LinearSolver<float,  3>               LinearSolver3f;
    typedef LinearSolver<float,  4>               LinearSolver4f;
    typedef LinearSolver<std::complex<double>, 2> LinearSolver2cd;
    typedef LinearSolver<std::complex<double>, 3> LinearSolver3cd;
    typedef LinearSolver<std::complex<double>, 4> LinearSolver4cd;
    typedef LinearSolver<std::complex<float>,  2> LinearSolver2cf;
    typedef LinearSolver<std::complex<float>,  3> LinearSolver3cf;
    typedef LinearSolver<std::complex<float>,  4> LinearSolver4cf;

For instance, suppose that you want to solve the system of equations:

        2x + 3y -  z  = 1
        4x -  y + 7z  = -6
        -x + 2y + 5z  = 2
First, store the left hand side of this system in a matrix:
        double lhs_rows[3][3] = { { 2,  3, -1 },
                                  { 4, -1,  7 },
                                  { -1, 2,  5 } };
        Matrix3d lhs;
        lhs.readRows( &lhs_rows[0][0] );
Next, store the right hand side of this system in a vector:
        double rhs_coords[3] = { 1, -6, 2 };
        Vector3d rhs( rhs_coords );
Next, construct a LinearSolver:
        LinearSolver3d solver( lhs, rhs );
Now, you can use the methods of LinearSolver to compute the solutions. If all you need is to know whether there exist solutions and to compute a solution if any exists, then just do:
        if( solver.thereExistSolutions() )
        {
            cout << "There are solutions! Here's one:" << endl;
            cout << solver.someSolution() << endl;
        }
        else cout << "There are no solutions." << endl;

If you want a complete analysis of the space of solutions, you need to use the methods genericSolution() and dimSolutions(). The following example shows how to use them:

        if( solver.thereExistSolutions() )
        {
            cout << "There are solutions! Here's one:" << endl;
            cout << solver.someSolution() << endl;

            if( solver.dimSolutions() == 0 )
                cout << "And this is the only solution." << endl;
            else
            {
                cout << "The space of solutions is of dimension "
                     << solver.dimSolutions() << endl;
                cout << "Here's another solution:" << endl;

                // let's construct a random parameter vector. This vector needs
                // to have size equal to dimSolutions(), but as this can't be
                // larger than 3, it's simpler to allocate an array of size 3.
                double param [3];
                for( int i = 0; i < solver.dimSolutions(); i++ )
                    param[i] = -10.0 + 20.0 * rand() / RAND_MAX;

                cout << solver.genericSolution( param ) << endl;
            }
        }
        else cout << "There are no solutions." << endl;

NOTE: As Eigen only handles square matrices, the number of equations must be equal to the number of variables. If you need to solve systems with more variables than equations, you can simply add trivial equations like 0x+0y+0z=0 to the system, in order to have as many equations as you have variables.


Constructor & Destructor Documentation

LinearSolver ( const Matrix< T, Size > &  leftHandSide,
const Vector< T, Size > &  rightHandSide 
) [inline]

Constructor recommended for convenient usage of this class: directly sets both sides of the system of equations.


Member Function Documentation

int dimSolutions (  )  [inline, inherited]

Assuming that there exist solutions (which can be checked with thereExistSolutions()), this function returns the dimension of the space of solutions. Thus, 0 means that there exists exactly one solution, and a positive value means that there exist infinitely many solutions, forming a space of the given dimension.

const VectorType & genericSolution ( const T *  parameter  )  [inline, inherited]

In short, this method returns a reference to a solution vector (kept alive by this class) of the system, corresponding to a given parameter. The parameter must be an array of size equal to dimSolutions(). Any such parameter array will give a solution, and each solution corresponds to a unique parameter. Thus this function gives a complete description of the space of solutions.

Parameters:
parameter An array with size equal to dimSolutions().
Returns:
A pointer to the solution associated to this parameter vector. If no such solution exists (that is, if dimSolutions()==0), the return value is undefined.

void init ( const MatrixType &  leftHandSide,
const VectorType &  rightHandSide 
) [inline, protected, inherited]

helper for the constructors

const VectorType& someSolution (  )  [inline, inherited]

Returns a reference to a vector (kept alive by this class) that is a solution of the system of equations, if any solution exists. If there exist no solutions, the return value is undefined.

bool thereExistSolutions (  )  [inline, inherited]

This function returns true if there exists at least one solution. It returns false if there are no solutions.


Member Data Documentation

MatrixType m_basisKer [protected, inherited]

A matrix whose column vectors form a basis of the kernel of the left hand side.

bool m_computedBasisKer [protected, inherited]

Equals true if m_basisKer has already been computed

VectorType m_genericSolution [protected, inherited]

Stores the vector that was last returned by genericSolution().

LUDecompositionType m_luDecomposition [protected, inherited]

Stores the LU decomposition that is used for computations.

VectorType m_rightHandSide [protected, inherited]

The right hand side vector.

VectorType m_someSolution [protected, inherited]

Stores some solution of the system, or 0 if there are no solutions. Used as a base point of the space of solutions. Returned by someSolution().

bool m_thereExistSolutions [protected, inherited]

Equals true if at least one solution exists


The documentation for this class was generated from the following file:

Generated on Fri Aug 22 11:18:26 2008 for Eigen by  doxygen 1.5.6