30 #include <gsl/gsl_errno.h> 31 #include <gsl/gsl_blas.h> 32 #include <gsl/gsl_multifit_nlin.h> 43 int NonLinearLSQ::curvefit() {
49 _nlsqPointer d = {
this };
50 gsl_multifit_function_fdf mf;
58 const gsl_multifit_fdfsolver_type *T = gsl_multifit_fdfsolver_lmsder;
59 gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc(T, n, p);
62 gsl_vector *x = NlsqTogsl(_fitParms);
63 gsl_matrix *covar = gsl_matrix_alloc(p, p);
64 gsl_multifit_fdfsolver_set(s, &mf, x);
67 checkIteration(_nIters, gslToNlsq(s->x), NLVector(p,999.0),
68 gsl_blas_dnrm2(s->f), GSL_CONTINUE);
71 gsl_matrix *J = gsl_matrix_alloc(n, p);
75 _status = gsl_multifit_fdfsolver_iterate(s);
76 _fitParms = gslToNlsq(s->x);
78 gsl_multifit_fdfsolver_jac(s, J);
79 gsl_multifit_covar(J, 0.0, covar);
80 _uncert = getUncertainty(covar);
82 _status = checkIteration(_nIters, _fitParms, _uncert, gsl_blas_dnrm2(s->f),
84 if ( _status ) {
break; }
85 if(!doContinue()) {
break; }
87 _status = gsl_multifit_test_delta(s->dx, s->x, absErr(), relErr());
88 }
while ((_status == GSL_CONTINUE) && (_nIters < _maxIters));
91 gsl_multifit_fdfsolver_free(s);
92 gsl_matrix_free(covar);
99 void NonLinearLSQ::Terminate(
const std::string &message) {
100 _userMessage = message;
101 _userTerminated =
true;
102 _status = GSL_SUCCESS;
106 void NonLinearLSQ::Abort(
const std::string &reason) {
107 _userMessage = reason;
108 _userTerminated =
true;
109 _status = GSL_FAILURE;
114 int NonLinearLSQ::f(
const gsl_vector *x,
void *params, gsl_vector *fx) {
115 NonLinearLSQ *nlsq = (
static_cast<_nlsqPointer *
> (params))->nlsq;
116 int n = nlsq->nSize();
117 NLVector fxv = nlsq->f_x(nlsq->gslToNlsq(x));
118 for (
int i = 0 ; i < n ; i++ ) {
119 gsl_vector_set(fx, i, fxv[i]);
121 return (GSL_SUCCESS);
124 int NonLinearLSQ::df(
const gsl_vector *x,
void *params, gsl_matrix *J) {
125 NonLinearLSQ *nlsq = (
static_cast<_nlsqPointer *
> (params))->nlsq;
126 int n = nlsq->nSize();
127 int p = nlsq->nParms();
129 NLMatrix m = nlsq->df_x(nlsq->gslToNlsq(x));
131 for (
int i = 0 ; i < n ; i++ ) {
132 for (
int j = 0 ; j < p ; j++ ) {
133 gsl_matrix_set(J, i, j, m[i][j]);
136 return (GSL_SUCCESS);
139 int NonLinearLSQ::fdf(
const gsl_vector *x,
void *params, gsl_vector *fx,
143 return (GSL_SUCCESS);
147 NonLinearLSQ::NLVector NonLinearLSQ::getUncertainty(
const gsl_matrix *covar)
149 NLVector unc(covar->size1);
150 for (
size_t i = 0 ; i < covar->size1 ; i++ ) {
151 unc[i] = sqrt(gsl_matrix_get(covar, i, i));
156 NonLinearLSQ::NLVector NonLinearLSQ::gslToNlsq(
const gsl_vector *v)
const {
159 for (
size_t i = 0 ; i < n ; i++) {
160 Nv[i] = gsl_vector_get(v, i);
165 NonLinearLSQ::NLMatrix NonLinearLSQ::gslToNlsq(
const gsl_matrix *m)
const {
166 size_t nrows = m->size1;
167 size_t ncols = m->size2;
168 NLMatrix Nm(nrows, ncols);
169 for (
size_t i = 0 ; i < nrows ; i++) {
170 for (
size_t j = 0 ; j < ncols ; j++) {
171 Nm[i][j] = gsl_matrix_get(m,i,j);
177 gsl_vector *NonLinearLSQ::NlsqTogsl(
const NonLinearLSQ::NLVector &v,
178 gsl_vector *gv)
const {
180 gv = gsl_vector_alloc(v.dim());
182 else if (gv->size != (
size_t) v.dim()) {
184 mess <<
"Size of NL vector (" << v.dim() <<
") not same as GSL vector (" 186 throw IException(IException::Programmer, mess.str(),
_FILEINFO_);
189 for (
int i = 0 ; i < v.dim() ; i++) {
190 gsl_vector_set(gv, i, v[i]);
195 gsl_matrix *NonLinearLSQ::NlsqTogsl(
const NonLinearLSQ::NLMatrix &m,
196 gsl_matrix *gm)
const {
198 gm = gsl_matrix_alloc(m.dim1(), m.dim2());
200 else if ((gm->size1 != (
size_t) m.dim1()) &&
201 (gm->size2 != (
size_t) m.dim2()) ) {
203 mess <<
"Size of NL matrix (" << m.dim1() <<
"," << m.dim2()
204 <<
") not same as GSL matrix (" << gm->size1 <<
"," << gm->size2
206 throw IException(IException::Programmer, mess.str(),
_FILEINFO_);
209 for (
int i = 0 ; i < m.dim1() ; i++) {
210 for (
int j = 0 ; j < m.dim2() ; j++) {
211 gsl_matrix_set(gm, i, j, m[i][j]);
Namespace for the standard library.
#define _FILEINFO_
Macro for the filename and line number.
Namespace for ISIS/Bullet specific routines.