Isis 3 Developer Reference
NonLinearLSQ.h
Go to the documentation of this file.
1 #ifndef NonLinearLSQ_h
2 #define NonLinearLSQ_h
3 
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 #include <algorithm>
32 
33 #include "tnt_array1d.h"
34 #include "tnt_array1d_utils.h"
35 #include "tnt_array2d.h"
36 #include "tnt_array2d_utils.h"
37 
38 #include <gsl/gsl_vector.h>
39 #include <gsl/gsl_matrix.h>
40 #include <gsl/gsl_blas.h>
41 #include <gsl/gsl_multifit_nlin.h>
42 
43 #include "IException.h"
44 
45 namespace Isis {
46 
57  class NonLinearLSQ {
58  public:
59  typedef TNT::Array1D<double> NLVector;
60  typedef TNT::Array2D<double> NLMatrix;
61 
62  // Constructors and Destructor
63  NonLinearLSQ() : _fitParms(), _uncert(), _nIters(0), _maxIters(50),
64  _status(0), _userTerminated(false), _userMessage() {}
65 
67  virtual ~NonLinearLSQ() { }
68 
69  virtual int nSize() const = 0;
70  virtual int nParms() const = 0;
71 
77  void setMaxIters(int m) { _maxIters = m; }
78 
85  int maxIters() const { return (_maxIters); }
86 
87  virtual NLVector guess() = 0;
88  virtual NLVector f_x(const NLVector &x) = 0;
89  virtual NLMatrix df_x(const NLVector &x) = 0;
90 
91  virtual double absErr() const { return (1.0E-4); }
92  virtual double relErr() const { return (1.0E-4); }
93 
94  int curvefit();
96  inline int status() const { return (_status); }
98  inline bool success() const { return (_status == GSL_SUCCESS); }
100  inline bool success(int status) const { return (status == GSL_SUCCESS); }
102  inline std::string statusstr() const {
103  return (std::string(gsl_strerror(_status)));
104  }
106  inline std::string statusstr(int status) const {
107  return (std::string(gsl_strerror(status)));
108  }
109 
111  virtual int checkIteration(const int Iter, const NLVector &fitcoefs,
112  const NLVector &uncerts, double cplxconj,
113  int Istatus) {
114  return (Istatus);
115  }
116 
118  inline NLVector coefs() const { return (_fitParms); }
120  inline NLVector uncert() const { return (_uncert); }
122  inline int nIterations() const { return (_nIters); }
123 
124  protected:
125  void Terminate(const std::string &message = "");
126  void Abort(const std::string &reason = "");
127 
128  bool doContinue() const { return (!_userTerminated); }
129 
130 
131  private:
132  NLVector _fitParms;
133  NLVector _uncert;
134  int _nIters;
135  int _maxIters;
136  int _status;
137  bool _userTerminated;
138  std::string _userMessage;
139 
140  struct _nlsqPointer {
141  NonLinearLSQ *nlsq;
142  };
143  static int f(const gsl_vector *x, void *params, gsl_vector *fx);
144  static int df(const gsl_vector *x, void *params, gsl_matrix *J);
145  static int fdf(const gsl_vector *x, void *params, gsl_vector *fx,
146  gsl_matrix *J);
147 
148  NLVector gslToNlsq(const gsl_vector *v) const;
149  NLMatrix gslToNlsq(const gsl_matrix *m) const;
150  gsl_vector *NlsqTogsl(const NLVector &v, gsl_vector *gv = 0) const;
151  gsl_matrix *NlsqTogsl(const NLMatrix &m, gsl_matrix *gm = 0) const;
152  NLVector getUncertainty(const gsl_matrix *m) const;
153  };
154 
155 } // namespace Isis
156 #endif
157 
virtual ~NonLinearLSQ()
Destructor.
Definition: NonLinearLSQ.h:67
void Abort(const std::string &reason="")
Definition: NonLinearLSQ.cpp:106
std::string statusstr(int status) const
Return error message given status condition.
Definition: NonLinearLSQ.h:106
bool doContinue() const
Definition: NonLinearLSQ.h:128
void Terminate(const std::string &message="")
Definition: NonLinearLSQ.cpp:99
virtual int nParms() const =0
int maxIters() const
Maximum number iterations for valid solution.
Definition: NonLinearLSQ.h:85
int nIterations() const
Return number of iterations from last fit processing.
Definition: NonLinearLSQ.h:122
virtual NLVector f_x(const NLVector &x)=0
NonLinearLSQ Computes a fit using a Levenberg-Marquardt algorithm.
Definition: NonLinearLSQ.h:57
std::string statusstr() const
Return error message pertaining to last fit procesing.
Definition: NonLinearLSQ.h:102
NLVector uncert() const
Return uncertainties from last fit processing.
Definition: NonLinearLSQ.h:120
TNT::Array1D< double > NLVector
Definition: NonLinearLSQ.h:59
NonLinearLSQ()
Definition: NonLinearLSQ.h:63
virtual double relErr() const
Definition: NonLinearLSQ.h:92
virtual int nSize() const =0
virtual NLMatrix df_x(const NLVector &x)=0
int status() const
Return status of last fit processing.
Definition: NonLinearLSQ.h:96
bool success(int status) const
Check status for success of the given condition.
Definition: NonLinearLSQ.h:100
const double E
Sets some basic constants for use in ISIS programming.
Definition: Constants.h:55
TNT::Array2D< double > NLMatrix
Definition: NonLinearLSQ.h:60
virtual double absErr() const
Definition: NonLinearLSQ.h:91
Namespace for ISIS/Bullet specific routines.
Definition: Apollo.h:31
bool success() const
Determine success from last fit processing.
Definition: NonLinearLSQ.h:98
virtual NLVector guess()=0
int curvefit()
Definition: NonLinearLSQ.cpp:43
NLVector coefs() const
Return coefficients from last fit processing.
Definition: NonLinearLSQ.h:118
virtual int checkIteration(const int Iter, const NLVector &fitcoefs, const NLVector &uncerts, double cplxconj, int Istatus)
Default interation test simply returns input status.
Definition: NonLinearLSQ.h:111
void setMaxIters(int m)
Sets the maximum number of iterations.
Definition: NonLinearLSQ.h:77