[Kst] extragear/graphics/kst/plugins/fits_nonlinear/general_levenberg_marquardt
Nicolas Brisset
nicolas.brisset at eurocopter.com
Mon Nov 21 11:26:03 CET 2005
SVN commit 481981 by brisset:
Last fixes: it WORKS! Some details can probably still be improved, but this works quite well. I will commit a small test file for interested people to play with. I believe this general-purpose plugin has a lot of potential, provided that performance is OK (still to be thoroughly tested) as it could be used for all sorts of fits, and if we have at some point in 2006 a new plugin system where the GUI is more flexible, we could reduce drastically the number of plugins that need to be installed. To be discussed further...
M +10 -6 kstfit_general_levenberg_marquardt.cpp
--- trunk/extragear/graphics/kst/plugins/fits_nonlinear/general_levenberg_marquardt/kstfit_general_levenberg_marquardt.cpp #481980:481981
@@ -46,14 +46,15 @@
double tolerance = inScalars[0];
int maxIterations = (int) inScalars[1];
size_t n = inArrayLens[0];
+
// Extract parameter names
-QStringList params = QStringList::split(QRegExp("\\s*{,|;|:}\\s*"), inStrings[1]);
-size_t paramsNumber = (size_t) params.count();
+QStringList params = QStringList::split(QRegExp("\\s*(,|;|:)\\s*"), inStrings[1]);
+size_t paramsNumber = params.count();
// TODO: replace X with x to make sure :-)
// Set parameter initial guesses
double pInit[paramsNumber];
-QStringList paramsInit = QStringList::split(QRegExp("\\s*{,|;|:}\\s*"), inStrings[2]);
+QStringList paramsInit = QStringList::split(QRegExp("\\s*(,|;|:)\\s*"), inStrings[2]);
bool ok = true;
for (i=0; i<(int)paramsNumber; i++) {
pInit[i]=paramsInit[i].toDouble(&ok);
@@ -63,8 +64,11 @@
// Allocate storage for output vectors
outArrays[0] = (double *) realloc(outArrays[0], (int)n * sizeof(double));
+outArrayLens[0] = (int)n;
outArrays[1] = (double *) realloc(outArrays[1], (int)paramsNumber * sizeof(double));
+outArrayLens[1] = (int)paramsNumber;
outArrays[2] = (double *) realloc(outArrays[2], (int)paramsNumber * sizeof(double));
+outArrayLens[2] = (int)paramsNumber;
// Store information in our struct
struct fit d = { n, inArrays[0], inArrays[1], outArrays[0], inStrings[0], params };
@@ -74,9 +78,7 @@
gsl_multifit_fdfsolver *s = gsl_multifit_fdfsolver_alloc (T, n, paramsNumber);
gsl_multifit_function_fdf f;
gsl_matrix *covar = gsl_matrix_alloc (paramsNumber, paramsNumber);
-gsl_multifit_fdfsolver_set (s, &f, &x.vector);
-
f.f = &function_f;
f.df = &function_df;
f.fdf = &function_fdf;
@@ -84,6 +86,7 @@
f.p = paramsNumber;
f.params = &d;
+gsl_multifit_fdfsolver_set (s, &f, &x.vector);
int status;
int iteration = 0;
@@ -97,6 +100,7 @@
gsl_multifit_covar (s->J, 0.0, covar);
+// Store results
for (i=0; i<(int)paramsNumber; i++) {
outArrays[1][i]=gsl_vector_get(s->x,i); // Store fitted parameter values
outArrays[2][i]=sqrt(gsl_matrix_get(covar,i,i)); // Store parameter precision
@@ -105,7 +109,7 @@
gsl_multifit_fdfsolver_free (s);
gsl_matrix_free (covar);
-return 1;
+return 0;
}
int function_f(const gsl_vector * x, void *params, gsl_vector * f)
More information about the Kst
mailing list