#include <linearsolver.h>
Public Member Functions | |
int | dimSolutions () |
const VectorType & | genericSolution (const T *parameter) |
LinearSolverX (const MatrixX< T > &leftHandSide, const VectorX< T > &rightHandSide) | |
LinearSolverX () | |
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 |
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 following typedefs are provided to cover the usual cases:
typedef LinearSolverX<double> LinearSolverXd; typedef LinearSolverX<float> LinearSolverXf; typedef LinearSolverX< std::complex<double> > LinearSolverXcd; typedef LinearSolverX< std::complex<float> > LinearSolverXcf;
For instance, suppose that you want to solve the system of equations:
2x + 3y - z = 1 4x - y + 7z = -6 -x + 2y + 5z = 2
double lhs_rows[3][3] = { { 2, 3, -1 },
{ 4, -1, 7 },
{ -1, 2, 5 } };
MatrixXd lhs( 3 );
lhs.readRows( &lhs_rows[0][0] );
double rhs_coords[3] = { 1, -6, 2 }; VectorXd rhs( 3, rhs_coords );
LinearSolverXd solver( lhs, rhs );
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.
LinearSolverX | ( | ) | [inline] |
Default constructor.
LinearSolverX | ( | const MatrixX< T > & | leftHandSide, | |
const VectorX< T > & | rightHandSide | |||
) | [inline] |
Constructor recommended for convenient usage of this class: directly sets both sides of the system of equations.
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.
parameter | An array with size equal to dimSolutions(). |
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.
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