23 #include "jama/jama_svd.h"
24 #include "jama/jama_qr.h"
27 #include "gmm/gmm_superlu_interface.h"
42 int sparseRows,
int sparseCols,
bool jigsaw) {
56 if (sparseRows == 0 || sparseCols == 0) {
57 QString msg =
"If solving using sparse matrices, you must enter the "
58 "number of rows/columns";
63 gmm::resize(
p_sparseA, sparseRows, sparseCols);
64 gmm::resize(
p_normals, sparseCols, sparseCols);
65 gmm::resize(
p_ATb, sparseCols, 1);
76 p_sparseRows = sparseRows;
77 p_sparseCols = sparseCols;
79 p_currentFillRow = -1;
120 QString msg =
"Number of elements in data does not match basis [" +
168 int ncolumns = (int)data.size();
170 for(
int c = 0; c < ncolumns; c++) {
185 if((row >=
Rows()) || (row < 0)) {
186 QString msg =
"Index out of bounds [Given = " +
toString(row) +
"]";
200 if((row >=
Rows()) || (row < 0)) {
201 QString msg =
"Index out of bounds [Given = " +
toString(row) +
"]";
237 if((method ==
SPARSE && p_sparseRows == 0) ||
240 QString msg =
"No solution available because no input data was provided";
247 else if(method ==
QRD) {
250 else if(method ==
SPARSE) {
272 for(
int r = 0; r < A.dim1(); r++) {
274 for(
int c = 0; c < A.dim2(); c++) {
285 JAMA::SVD<double> svd(A);
287 TNT::Array2D<double> V;
291 TNT::Array2D<double> invS;
294 for(
int i = 0; i < invS.dim1(); i++) {
295 if(invS[i][i] != 0.0) invS[i][i] = 1.0 / invS[i][i];
299 TNT::Array2D<double> U;
301 TNT::Array2D<double> transU(U.dim2(), U.dim1());
303 for(
int r = 0; r < U.dim1(); r++) {
304 for(
int c = 0; c < U.dim2(); c++) {
305 transU[c][r] = U[r][c];
310 TNT::Array2D<double> VinvS = TNT::matmult(V, invS);
311 TNT::Array2D<double> Aplus = TNT::matmult(VinvS, transU);
316 for(
int r = 0; r < (int)
p_expected.size(); r++) {
320 TNT::Array2D<double> coefs = TNT::matmult(Aplus, b);
325 QString msg =
"Unable to solve least-squares using SVD method. No "
326 "solution available. Not enough knowns or knowns are "
327 "co-linear ... [Unknowns = "
334 std::vector<double> bcoefs;
335 for (
int i = 0; i < coefs.dim1(); i++) bcoefs.push_back(coefs[i][0]);
340 for(
int i = 0; i < (int)
p_input.size(); i++) {
375 for(
int r = 0; r < A.dim1(); r++) {
377 for(
int c = 0; c < A.dim2(); c++) {
387 JAMA::QR<double> qr(A);
391 for(
int r = 0; r < (int)
p_expected.size(); r++) {
398 int full = qr.isFullRank();
400 QString msg =
"Unable to solve-least squares using QR Decomposition. "
401 "The upper triangular R matrix is not full rank";
405 TNT::Array1D<double> coefs = qr.solve(b);
408 std::vector<double> bcoefs;
409 for(
int i = 0; i < coefs.dim1(); i++) {
410 bcoefs.push_back(coefs[i]);
415 for(
int i = 0; i < (int)
p_input.size(); i++) {
469 for(
int c = 0; c < p_sparseCols; c++) {
470 numNonZeros = gmm::nnz(gmm::sub_matrix(
p_normals,
471 gmm::sub_interval(0,p_sparseCols),
472 gmm::sub_interval(c,1)));
474 if(numNonZeros == 0)
return c + 1;
478 gmm::dense_matrix<double> b(p_sparseRows, 1);
481 for (
int r = 0; r < p_sparseRows; r++ )
489 for(
int i = 0; i < p_sparseCols; i++) {
518 for(
int i = 0; i < p_sparseCols; i++ )
548 for (
int i = 0; i < p_sparseRows; i++ ) {
557 double constrained_vTPv = 0.0;
559 for (
int i = 0; i < p_sparseCols; i++ ) {
574 printf(
"Observations: %d\nConstrained: %d\nParameters: %d\nDOF: %d\n",
658 for (
int i = 0; i < p_sparseCols; i++ )
701 void LeastSquares::Reset ()
707 p_currentFillRow = -1;
734 QString msg =
"Unable to evaluate until a solution has been computed";
751 QString msg =
"Unable to return residuals until a solution has been computed";
771 QString msg =
"Unable to return residuals until a solution has been computed";
void Weight(int index, double weight)
Reset the weight for the ith known.
int Coefficients() const
Returns the number of coefficients for the equation.
void SetCoefficients(const std::vector< double > &coefs)
Set the coefficients for the equation.
gmm::row_matrix< gmm::rsvector< double > > p_sparseA
design matrix 'A'
QString Name() const
Returns the name of the equation.
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.
double Term(int c) const
Returns the cth term.
std::vector< double > p_epsilonsSparse
sparse vector of total parameter corrections
QString toString(bool boolToConvert)
Global function to convert a boolean to a string.
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.
std::vector< double > Residuals() const
Returns a vector of residuals (errors).
~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.
bool SparseErrorPropagation()
Error propagation for sparse least-squares solution.
std::vector< double > p_xSparse
sparse solution vector
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.
int Variables() const
Returns the number of variables in the equation.
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.
gmm::row_matrix< gmm::rsvector< double > > p_normals
normal equations matrix 'N'
gmm::dense_matrix< double > p_ATb
right-hand side vector
gmm::SuperLU_factor< double > p_SLU_Factor
decomposed normal equations matrix
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.
double Residual(int i) const
Returns the ith residual.
int p_degreesOfFreedom
degrees of freedom (redundancy)
std::vector< double > GetInput(int row) const
This method returns the data at the given row.
double Evaluate(const std::vector< double > &vars)
Compute the equation using the input variables.
Generic linear equation class.
double GetExpected(int row) const
This method returns the expected value at the given row.
Isis::BasisFunction * p_basis
Pointer to the BasisFunction object.
std::vector< double > p_sqrtWeight
A vector of the square roots of the weights for each known value.
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 Rows() const
This methods returns the number of rows in the matrix.
int p_constrainedParameters
constrained parameters