Isis Developer Reference
Isis::LeastSquares Class Reference

Generic least square fitting class. More...

#include <LeastSquares.h>

Collaboration diagram for Isis::LeastSquares:
Collaboration graph

Public Types

enum  SolveMethod { SVD, QRD, SPARSE }
 

Public Member Functions

 LeastSquares (Isis::BasisFunction &basis, bool sparse=false, int sparseRows=0, int sparseCols=0, bool jigsaw=false)
 Creates a LeastSquares Object. More...
 
 ~LeastSquares ()
 Destroys the LeastSquares object. More...
 
void AddKnown (const std::vector< double > &input, double expected, double weight=1.0)
 Invoke this method for each set of knowns. More...
 
std::vector< double > GetInput (int row) const
 This method returns the data at the given row. More...
 
double GetExpected (int row) const
 This method returns the expected value at the given row. More...
 
int Rows () const
 This methods returns the number of rows in the matrix. More...
 
int Solve (Isis::LeastSquares::SolveMethod method=SVD)
 After all the data has been registered through AddKnown, invoke this method to solve the system of equations. More...
 
double Evaluate (const std::vector< double > &input)
 Invokes the BasisFunction Evaluate method. More...
 
std::vector< double > Residuals () const
 Returns a vector of residuals (errors). More...
 
double Residual (int i) const
 Returns the ith residual. More...
 
void Weight (int index, double weight)
 Reset the weight for the ith known. More...
 
int Knowns () const
 The number of knowns (or times AddKnown was invoked) linear combination of the variables. More...
 
double GetSigma0 ()
 
int GetDegreesOfFreedom ()
 
void Reset ()
 
void ResetSparse ()
 
std::vector< double > GetEpsilons () const
 
void SetParameterWeights (const std::vector< double > weights)
 
void SetNumberOfConstrainedParameters (int n)
 

Detailed Description

Generic least square fitting class.

This class can be used to solved systems of linear equations through least squares fitting. The solution is derived through singular value decomposition or QR decomposition. For example:

\[ x + y = 3 \]

\[ -2x + 3y = 1 \]

\[ 2x - y = 2 \]

Is a simple system of equations that can be solved using this class.

Isis::BasisFunction b("Linear",2,2);
std::vector<double> one;
one.push_back(1.0);
one.push_back(1.0);
lsq.AddKnown(one,3.0);
std::vector<double> two;
two.push_back(-2.0);
two.push_back(3.0);
lsq.AddKnown(two,1.0);
std::vector<double> tre;
tre.push_back(2.0);
tre.push_back(-1.0);
lsq.AddKnown(tre,2.0);
lsq.Solve();
double out1 = lsq.Evaluate(one);
double out2 = lsq.Evaluate(two);
double out3 = lsq.Evaluate(tre);
See also
PolynomialUnivariate
PolynomialBiVariate
BasisFunction
Author
2004-06-24 Jeff Anderson

Member Enumeration Documentation

◆ SolveMethod

Enumerator
SVD 

Singular Value Decomposition.

QRD 

QR Decomposition.

SPARSE 

Sparse.

Constructor & Destructor Documentation

◆ LeastSquares()

Isis::LeastSquares::LeastSquares ( Isis::BasisFunction basis,
bool  sparse = false,
int  sparseRows = 0,
int  sparseCols = 0,
bool  jigsaw = false 
)

Creates a LeastSquares Object.

Parameters
basisA BasisFunction. This parameter allows for the least squares fitting to be applied to arbitrary equations.

References _FILEINFO_, and Isis::IException::Programmer.

◆ ~LeastSquares()

Isis::LeastSquares::~LeastSquares ( )

Destroys the LeastSquares object.

Member Function Documentation

◆ AddKnown()

void Isis::LeastSquares::AddKnown ( const std::vector< double > &  data,
double  result,
double  weight = 1.0 
)

Invoke this method for each set of knowns.

Given our example in the description, we have three knowns and expecteds. They are

\[ (1,1) = 3 \]

\[ (-2,3) = 1 \]

\[ (2,-1) = 2 \]

Parameters
dataA vector of knowns.
resultThe expected value for the knowns.
weight(Default = 1.0) How strongly to weight this known. Weight less than 1 increases residual for this known, while weight greater than 1 decreases the residual for this known.
Exceptions
Isis::IException::Programmer- Number of elements in data does not match basis requirements

References _FILEINFO_, Isis::BasisFunction::Name(), Isis::IException::Programmer, and Isis::BasisFunction::Variables().

Referenced by Isis::SurfaceModel::AddTriplet(), Isis::ReseauDistortionMap::SetFocalPlane(), Isis::VimsGroundMap::SetGround(), Isis::SpicePosition::SetPolynomial(), Isis::SpiceRotation::SetPolynomial(), Isis::VimsSkyMap::SetSky(), Isis::ReseauDistortionMap::SetUndistortedFocalPlane(), and Isis::Affine::Solve().

◆ Evaluate()

double Isis::LeastSquares::Evaluate ( const std::vector< double > &  data)

Invokes the BasisFunction Evaluate method.

Parameters
dataThe input variables to evaluate.
Returns
The evaluation for the input variable.
Exceptions
Isis::IException::Programmer- Unable to evaluate until a solution has been computed

References _FILEINFO_, Isis::BasisFunction::Evaluate(), and Isis::IException::Programmer.

Referenced by Isis::SurfaceModel::Evaluate(), Isis::ReseauDistortionMap::SetFocalPlane(), Isis::VimsGroundMap::SetGround(), Isis::VimsSkyMap::SetSky(), and Isis::ReseauDistortionMap::SetUndistortedFocalPlane().

◆ GetDegreesOfFreedom()

int Isis::LeastSquares::GetDegreesOfFreedom ( )
inline

◆ GetEpsilons()

std::vector<double> Isis::LeastSquares::GetEpsilons ( ) const
inline

◆ GetExpected()

double Isis::LeastSquares::GetExpected ( int  row) const

This method returns the expected value at the given row.

Parameters
row
Returns
double

References _FILEINFO_, Isis::IException::Programmer, Rows(), and Isis::toString().

◆ GetInput()

std::vector< double > Isis::LeastSquares::GetInput ( int  row) const

This method returns the data at the given row.

Parameters
row
Returns
std::vector<double>

References _FILEINFO_, Isis::IException::Programmer, Rows(), and Isis::toString().

◆ GetSigma0()

double Isis::LeastSquares::GetSigma0 ( )
inline

◆ Knowns()

int Isis::LeastSquares::Knowns ( ) const
inline

The number of knowns (or times AddKnown was invoked) linear combination of the variables.

Returns
int The number of knowns

Referenced by Isis::VimsGroundMap::SetGround(), and Isis::VimsSkyMap::SetSky().

◆ Reset()

void Isis::LeastSquares::Reset ( )

Referenced by ResetSparse().

◆ ResetSparse()

void Isis::LeastSquares::ResetSparse ( )
inline

References Reset().

◆ Residual()

double Isis::LeastSquares::Residual ( int  i) const

Returns the ith residual.

That is, the difference between the evaluation of a known with the solution against the expected value. There is one residual for each time AddKnown was invoked.

Parameters
iThe number of times AddKnown was invoked to be evaluated.
Returns
The output value of the residual.
Exceptions
Isis::IException::Programmer- Unable to return residuals until a solution has been computed

References _FILEINFO_, and Isis::IException::Programmer.

◆ Residuals()

std::vector< double > Isis::LeastSquares::Residuals ( ) const

Returns a vector of residuals (errors).

That is, the difference between the evaluation of a known with the solution against the expected value.

Returns
The vector of residuals.
Exceptions
Isis::IException::Programmer- Unable to return residuals until a solution has been computed

References _FILEINFO_, and Isis::IException::Programmer.

◆ Rows()

int Isis::LeastSquares::Rows ( ) const

This methods returns the number of rows in the matrix.

Returns
int

Referenced by GetExpected(), GetInput(), and Solve().

◆ SetNumberOfConstrainedParameters()

void Isis::LeastSquares::SetNumberOfConstrainedParameters ( int  n)
inline

◆ SetParameterWeights()

void Isis::LeastSquares::SetParameterWeights ( const std::vector< double >  weights)
inline

◆ Solve()

int Isis::LeastSquares::Solve ( Isis::LeastSquares::SolveMethod  method = SVD)

After all the data has been registered through AddKnown, invoke this method to solve the system of equations.

You can then use the Evaluate and Residual methods freely.

References _FILEINFO_, QRD, Rows(), SPARSE, SVD, and Isis::IException::Unknown.

Referenced by Isis::ReseauDistortionMap::SetFocalPlane(), Isis::VimsGroundMap::SetGround(), Isis::SpicePosition::SetPolynomial(), Isis::SpiceRotation::SetPolynomial(), Isis::VimsSkyMap::SetSky(), Isis::ReseauDistortionMap::SetUndistortedFocalPlane(), Isis::SurfaceModel::Solve(), and Isis::Affine::Solve().

◆ Weight()

void Isis::LeastSquares::Weight ( int  index,
double  weight 
)

Reset the weight for the ith known.

This weight will not be used unless the system is resolved using the Solve method.

Parameters
indexThe position in the array to assign the given weight value
weightA weight factor to apply to the ith known. A weight less than one increase the residual for this known while a weight greater than one decrease the residual for this known.

The documentation for this class was generated from the following files:
Isis::BasisFunction
Generic linear equation class.
Definition: BasisFunction.h:48
Isis::LeastSquares
Generic least square fitting class.
Definition: LeastSquares.h:99