23 #include "jama/jama_svd.h" 24 #include "jama/jama_qr.h" 40 int sparseRows,
int sparseCols,
bool jigsaw) {
47 p_sparseRows = sparseRows;
48 p_sparseCols = sparseCols;
54 if (sparseRows == 0 || sparseCols == 0) {
55 QString msg =
"If solving using sparse matrices, you must enter the " 56 "number of rows/columns";
61 p_sparseA.set_size(sparseRows, sparseCols);
62 p_normals.set_size(sparseCols, sparseCols);
63 p_ATb.resize(sparseCols, 1);
74 p_currentFillRow = -1;
115 QString msg =
"Number of elements in data does not match basis [" +
159 int ncolumns = (int)data.size();
161 for(
int c = 0; c < ncolumns; c++) {
175 if((row >=
Rows()) || (row < 0)) {
176 QString msg =
"Index out of bounds [Given = " +
toString(row) +
"]";
190 if((row >=
Rows()) || (row < 0)) {
191 QString msg =
"Index out of bounds [Given = " +
toString(row) +
"]";
223 if((method ==
SPARSE && p_sparseRows == 0) ||
226 QString msg =
"No solution available because no input data was provided";
233 else if(method ==
QRD) {
236 else if(method ==
SPARSE) {
256 for(
int r = 0; r < A.dim1(); r++) {
258 for(
int c = 0; c < A.dim2(); c++) {
269 JAMA::SVD<double> svd(A);
271 TNT::Array2D<double> V;
275 TNT::Array2D<double> invS;
278 for(
int i = 0; i < invS.dim1(); i++) {
279 if(invS[i][i] != 0.0) invS[i][i] = 1.0 / invS[i][i];
283 TNT::Array2D<double> U;
285 TNT::Array2D<double> transU(U.dim2(), U.dim1());
287 for(
int r = 0; r < U.dim1(); r++) {
288 for(
int c = 0; c < U.dim2(); c++) {
289 transU[c][r] = U[r][c];
294 TNT::Array2D<double> VinvS = TNT::matmult(V, invS);
295 TNT::Array2D<double> Aplus = TNT::matmult(VinvS, transU);
300 for(
int r = 0; r < (int)
p_expected.size(); r++) {
304 TNT::Array2D<double> coefs = TNT::matmult(Aplus, b);
309 QString msg =
"Unable to solve least-squares using SVD method. No " 310 "solution available. Not enough knowns or knowns are " 311 "co-linear ... [Unknowns = " 318 std::vector<double> bcoefs;
319 for (
int i = 0; i < coefs.dim1(); i++) bcoefs.push_back(coefs[i][0]);
324 for(
int i = 0; i < (int)
p_input.size(); i++) {
359 for(
int r = 0; r < A.dim1(); r++) {
361 for(
int c = 0; c < A.dim2(); c++) {
371 JAMA::QR<double> qr(A);
375 for(
int r = 0; r < (int)
p_expected.size(); r++) {
382 int full = qr.isFullRank();
384 QString msg =
"Unable to solve-least squares using QR Decomposition. " 385 "The upper triangular R matrix is not full rank";
389 TNT::Array1D<double> coefs = qr.solve(b);
392 std::vector<double> bcoefs;
393 for(
int i = 0; i < coefs.dim1(); i++) {
394 bcoefs.push_back(coefs[i]);
399 for(
int i = 0; i < (int)
p_input.size(); i++) {
450 arma::mat b(p_sparseRows, 1);
453 for (
int r = 0; r < p_sparseRows; r++ )
461 for(
int i = 0; i < p_sparseCols; i++) {
474 if (status ==
false) {
475 QString msg =
"Could not solve sparse least squares problem.";
485 for(
int i = 0; i < p_sparseCols; i++ )
498 for (
int i = 0; i < p_sparseRows; i++ ) {
507 double constrained_vTPv = 0.0;
509 for (
int i = 0; i < p_sparseCols; i++ ) {
539 void LeastSquares::Reset ()
545 p_currentFillRow = -1;
571 QString msg =
"Unable to evaluate until a solution has been computed";
588 QString msg =
"Unable to return residuals until a solution has been computed";
608 QString msg =
"Unable to return residuals until a solution has been computed";
arma::mat p_xSparse
sparse solution matrix
void Weight(int index, double weight)
Reset the weight for the ith known.
void SetCoefficients(const std::vector< double > &coefs)
Set the coefficients for the equation.
QString Name() const
Returns the name of the equation.
std::vector< double > Residuals() const
Returns a vector of residuals (errors).
void SolveSVD()
After all the data has been registered through AddKnown, invoke this method to solve the system of eq...
LeastSquares(Isis::BasisFunction &basis, bool sparse=false, int sparseRows=0, int sparseCols=0, bool jigsaw=false)
Creates a LeastSquares Object.
std::vector< double > p_epsilonsSparse
sparse vector of total parameter corrections
double Residual(int i) const
Returns the ith residual.
int Rows() const
This methods returns the number of rows in the matrix.
std::vector< double > GetInput(int row) const
This method returns the data at the given row.
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
double Term(int c) const
Returns the cth term.
This error is for when a programmer made an API call that was illegal.
std::vector< double > p_expected
A vector of the expected values when solved.
~LeastSquares()
Destroys the LeastSquares object.
std::vector< std::vector< double > > p_input
A vector of the input variables to evaluate.
void FillSparseA(const std::vector< double > &data)
Invoke this method for each set of knowns for sparse solutions.
std::vector< double > p_parameterWeights
vector of parameter weights
Singular Value Decomposition.
double Evaluate(const std::vector< double > &input)
Invokes the BasisFunction Evaluate method.
virtual void Expand(const std::vector< double > &vars)
This is the function you should replace depending on your needs.
std::vector< double > p_residuals
A vector of the residuals (or difference between expected and solved values).
#define _FILEINFO_
Macro for the filename and line number.
A type of error that cannot be classified as any of the other error types.
double GetExpected(int row) const
This method returns the expected value at the given row.
arma::SpMat< double > p_sparseA
design matrix 'A'
arma::SpMat< double > p_normals
normal equations matrix 'N'
int SolveSparse()
Solve using sparse class.
double p_sigma0
sigma nought - reference variance
void AddKnown(const std::vector< double > &input, double expected, double weight=1.0)
Invoke this method for each set of knowns.
int p_degreesOfFreedom
degrees of freedom (redundancy)
arma::mat p_ATb
right-hand side vector
double Evaluate(const std::vector< double > &vars)
Compute the equation using the input variables.
Generic linear equation class.
Isis::BasisFunction * p_basis
Pointer to the BasisFunction object.
Namespace for ISIS/Bullet specific routines.
std::vector< double > p_sqrtWeight
A vector of the square roots of the weights for each known value.
int Variables() const
Returns the number of variables in the equation.
bool p_solved
Boolean value indicating solution is complete.
void SolveQRD()
After all the data has been registered through AddKnown, invoke this method to solve the system of eq...
int Solve(Isis::LeastSquares::SolveMethod method=SVD)
After all the data has been registered through AddKnown, invoke this method to solve the system of eq...
int Coefficients() const
Returns the number of coefficients for the equation.
int p_constrainedParameters
constrained parameters