1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
//
// A rust binding for the GSL library by Guillaume Gomez (guillaume1.gomez@gmail.com)
//
/*!
# Nonlinear Least-Squares Fitting
This chapter describes functions for multidimensional nonlinear least-squares fitting. The library
provides low level components for a variety of iterative solvers and convergence tests. These can
be combined by the user to achieve the desired solution, with full access to the intermediate steps
of the iteration. Each class of methods uses the same framework, so that you can switch between
solvers at runtime without needing to recompile your program. Each instance of a solver keeps track
of its own state, allowing the solvers to be used in multi-threaded programs.
## Overview
The problem of multidimensional nonlinear least-squares fitting requires the minimization of the
squared residuals of n functions, f_i, in p parameters, x_i,
\Phi(x) = (1/2) || F(x) ||^2
= (1/2) \sum_{i=1}^{n} f_i(x_1, ..., x_p)^2
All algorithms proceed from an initial guess using the linearization,
\psi(p) = || F(x+p) || ~=~ || F(x) + J p ||
where x is the initial point, p is the proposed step and J is the Jacobian matrix J_{ij} = d f_i /
d x_j. Additional strategies are used to enlarge the region of convergence. These include requiring
a decrease in the norm ||F|| on each step or using a trust region to avoid steps which fall outside
the linear regime.
To perform a weighted least-squares fit of a nonlinear model Y(x,t) to data (t_i, y_i) with
independent Gaussian errors \sigma_i, use function components of the following form,
f_i = (Y(x, t_i) - y_i) / \sigma_i
Note that the model parameters are denoted by x in this chapter since the non-linear least-squares
algorithms are described geometrically (i.e. finding the minimum of a surface). The independent
variable of any data to be fitted is denoted by t.
With the definition above the Jacobian is J_{ij} =(1 / \sigma_i) d Y_i / d x_j, where Y_i =
Y(x,t_i).
## High Level Driver
These routines provide a high level wrapper that combine the iteration and convergence testing for
easy use.
*/
/*
C Equivalent code:
```
#include <stdlib.h>
#include <stdio.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>
#include <gsl/gsl_multifit_nlin.h>
struct data {
size_t n;
double * y;
double * sigma;
};
void print_state(size_t iter, gsl_multifit_fdfsolver * s) {
printf("iter: %3u x = % 15.8f % 15.8f % 15.8f |f(x)| = %g\n",
iter,
gsl_vector_get (s->x, 0),
gsl_vector_get (s->x, 1),
gsl_vector_get (s->x, 2),
gsl_blas_dnrm2 (s->f));
}
int expb_f(const gsl_vector * x, void *params,
gsl_vector * f) {
size_t n = ((struct data *)params)->n;
double *y = ((struct data *)params)->y;
double *sigma = ((struct data *) params)->sigma;
double A = gsl_vector_get (x, 0);
double lambda = gsl_vector_get (x, 1);
double b = gsl_vector_get (x, 2);
size_t i;
for (i = 0; i < n; i++) {
/* Model Yi = A * exp(-lambda * i) + b */
double t = i;
double Yi = A * exp (-lambda * t) + b;
gsl_vector_set (f, i, (Yi - y[i])/sigma[i]);
}
return GSL_SUCCESS;
}
int expb_df(const gsl_vector * x, void *params,
gsl_matrix * J) {
size_t n = ((struct data *)params)->n;
double *sigma = ((struct data *) params)->sigma;
double A = gsl_vector_get (x, 0);
double lambda = gsl_vector_get (x, 1);
size_t i;
for (i = 0; i < n; i++) {
/* Jacobian matrix J(i,j) = dfi / dxj, */
/* where fi = (Yi - yi)/sigma[i], */
/* Yi = A * exp(-lambda * i) + b */
/* and the xj are the parameters (A,lambda,b) */
double t = i;
double s = sigma[i];
double e = exp(-lambda * t);
gsl_matrix_set (J, i, 0, e/s);
gsl_matrix_set (J, i, 1, -t * A * e/s);
gsl_matrix_set (J, i, 2, 1/s);
}
return GSL_SUCCESS;
}
int expb_fdf(const gsl_vector * x, void *params,
gsl_vector * f, gsl_matrix * J) {
expb_f (x, params, f);
expb_df (x, params, J);
return GSL_SUCCESS;
}
int main(void) {
const gsl_multifit_fdfsolver_type *T;
gsl_multifit_fdfsolver *s;
int status;
size_t i, iter = 0;
const size_t n = 40;
const size_t p = 3;
gsl_matrix *covar = gsl_matrix_alloc (p, p);
double y[n], sigma[n];
struct data d = { n, y, sigma};
gsl_multifit_function_fdf f;
double x_init[3] = { 1.0, 0.0, 0.0 };
gsl_vector_view x = gsl_vector_view_array (x_init, p);
const gsl_rng_type * type;
gsl_rng * r;
gsl_rng_env_setup();
type = gsl_rng_default;
r = gsl_rng_alloc (type);
f.f = &expb_f;
f.df = &expb_df;
f.fdf = &expb_fdf;
f.n = n;
f.p = p;
f.params = &d;
/* This is the data to be fitted */
for (i = 0; i < n; i++) {
double t = i;
y[i] = 1.0 + 5 * exp (-0.1 * t) + gsl_ran_gaussian(r, 0.1);
sigma[i] = 0.1;
printf("data: %d %g %g\n", i, y[i], sigma[i]);
}
T = gsl_multifit_fdfsolver_lmsder;
s = gsl_multifit_fdfsolver_alloc (T, n, p);
gsl_multifit_fdfsolver_set (s, &f, &x.vector);
print_state (iter, s);
do {
iter++;
status = gsl_multifit_fdfsolver_iterate (s);
printf ("status = %s\n", gsl_strerror (status));
print_state (iter, s);
if (status)
break;
status = gsl_multifit_test_delta (s->dx, s->x,
1e-4, 1e-4);
} while (status == GSL_CONTINUE && iter < 500);
gsl_multifit_covar (s->J, 0.0, covar);
gsl_matrix_fprintf (stdout, covar, "%g");
#define FIT(i) gsl_vector_get(s->x, i)
#define ERR(i) sqrt(gsl_matrix_get(covar,i,i))
printf("A = %.5f +/- %.5f\n", FIT(0), ERR(0));
printf("lambda = %.5f +/- %.5f\n", FIT(1), ERR(1));
printf("b = %.5f +/- %.5f\n", FIT(2), ERR(2));
printf ("status = %s\n", gsl_strerror (status));
gsl_multifit_fdfsolver_free (s);
return 0;
}
```
*/
use ffi::{self, FFI};
use std::os::raw::{c_int, c_void};
use VectorF64;
ffi_wrapper!(MultiFitFSolverType, *mut sys::gsl_multifit_fsolver_type);
// pub struct MultiFitFunction<F: Fn(x: &::VectorF64, f: &mut ::VectorF64)> {
// pub f: Box<F>,
// /// Number of functions.
// pub n: usize,
// /// Number of independent variables.
// pub p,
// }
pub struct MultiFitFunction(pub sys::gsl_multifit_function);
ffi_wrapper!(
MultiFitFSolver,
*mut sys::gsl_multifit_fsolver,
gsl_multifit_fsolver_free
);
impl MultiFitFSolver {
/// This function returns a pointer to a newly allocated instance of a solver of type T for n
/// observations and p parameters. The number of observations n must be greater than or equal to
/// parameters p.
///
/// If there is insufficient memory to create the solver then the function returns a null
/// pointer and the error handler is invoked with an error code of `Value::NoMemory`.
#[doc(alias = "gsl_multifit_fsolver_alloc")]
pub fn new(t: &MultiFitFSolverType, n: usize, p: usize) -> Option<MultiFitFSolver> {
let tmp = unsafe { sys::gsl_multifit_fsolver_alloc(t.unwrap_shared(), n, p) };
if tmp.is_null() {
None
} else {
Some(MultiFitFSolver::wrap(tmp))
}
}
#[doc(alias = "gsl_multifit_fsolver_set")]
pub fn set(&mut self, f: &mut MultiFitFunction, x: &mut VectorF64) -> ::Value {
// unsafe {
// let func = (*self.0).function;
// if !func.is_null() {
// Box::from_raw((*func).params);
// }
// }
::Value::from(unsafe {
sys::gsl_multifit_fsolver_set(self.unwrap_unique(), &mut f.0, x.unwrap_shared())
})
}
#[doc(alias = "gsl_multifit_fsolver_iterate")]
pub fn iterate(&mut self) -> ::Value {
::Value::from(unsafe { sys::gsl_multifit_fsolver_iterate(self.unwrap_unique()) })
}
#[doc(alias = "gsl_multifit_fsolver_name")]
pub fn name(&self) -> String {
unsafe {
let tmp = sys::gsl_multifit_fsolver_name(self.unwrap_shared());
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string()
}
}
#[doc(alias = "gsl_multifit_fsolver_position")]
pub fn position(&self) -> VectorF64 {
unsafe { ffi::FFI::wrap(sys::gsl_multifit_fsolver_position(self.unwrap_shared())) }
}
}
ffi_wrapper!(
MultiFitFdfSolver,
*mut sys::gsl_multifit_fdfsolver,
gsl_multifit_fdfsolver_free
);
impl MultiFitFdfSolver {
/// This function returns a pointer to a newly allocated instance of a solver of type T for n
/// observations and p parameters. The number of observations n must be greater than or equal
/// to parameters p.
#[doc(alias = "gsl_multifit_fdfsolver_alloc")]
pub fn new(_type: &MultiFitFdfSolverType, n: usize, p: usize) -> Option<MultiFitFdfSolver> {
let s = unsafe { sys::gsl_multifit_fdfsolver_alloc(_type.unwrap_shared(), n, p) };
if s.is_null() {
None
} else {
Some(MultiFitFdfSolver::wrap(s))
}
}
/// This function initializes, or reinitializes, an existing solver s to use the function f and
/// the initial guess x.
#[doc(alias = "gsl_multifit_fdfsolver_set")]
pub fn set(&mut self, f: &mut MultiFitFunctionFdf, x: &::VectorF64) -> ::Value {
::Value::from(unsafe {
sys::gsl_multifit_fdfsolver_set(self.unwrap_unique(), f.to_raw(), x.unwrap_shared())
})
}
pub fn x(&self) -> ::VectorF64 {
unsafe { ffi::FFI::soft_wrap((*self.unwrap_shared()).x) }
}
pub fn f(&self) -> ::VectorF64 {
unsafe { ffi::FFI::soft_wrap((*self.unwrap_shared()).f) }
}
pub fn dx(&self) -> ::VectorF64 {
unsafe { ffi::FFI::soft_wrap((*self.unwrap_shared()).dx) }
}
pub fn g(&self) -> ::VectorF64 {
unsafe { ffi::FFI::soft_wrap((*self.unwrap_shared()).g) }
}
pub fn sqrt_wts(&self) -> ::VectorF64 {
unsafe { ffi::FFI::soft_wrap((*self.unwrap_shared()).sqrt_wts) }
}
#[doc(alias = "gsl_multifit_fdfsolver_name")]
pub fn name(&self) -> String {
unsafe {
let tmp = sys::gsl_multifit_fdfsolver_name(self.unwrap_shared());
String::from_utf8_lossy(::std::ffi::CStr::from_ptr(tmp).to_bytes()).to_string()
}
}
/// This function performs a single iteration of the solver s. If the iteration encounters an
/// unexpected problem then an error code will be returned. The solver maintains a current
/// estimate of the best-fit parameters at all times.
#[doc(alias = "gsl_multifit_fdfsolver_iterate")]
pub fn iterate(&mut self) -> ::Value {
::Value::from(unsafe { sys::gsl_multifit_fdfsolver_iterate(self.unwrap_unique()) })
}
/// This function returns the current position (i.e. best-fit parameters) s->x of the solver s.
#[doc(alias = "gsl_multifit_fdfsolver_position")]
pub fn position(&self) -> ::VectorF64 {
unsafe { ffi::FFI::wrap(sys::gsl_multifit_fdfsolver_position(self.unwrap_shared())) }
}
/// These functions iterate the solver s for a maximum of maxiter iterations. After each
/// iteration, the system is tested for convergence using gsl_multifit_test_delta with the
/// error tolerances epsabs and epsrel.
// checker:ignore
#[allow(unused_assignments)]
#[doc(alias = "gsl_multifit_test_delta")]
pub fn driver(&mut self, max_iter: usize, epsabs: f64, epsrel: f64) -> ::Value {
let mut status = ::Value::Failure;
let ptr = self.unwrap_shared();
if !ptr.is_null() {
let mut iter = 0usize;
loop {
status = self.iterate();
if status != ::Value::Success {
break;
}
/* test for convergence */
status = ::Value::from(unsafe {
sys::gsl_multifit_test_delta((*ptr).dx, (*ptr).x, epsabs, epsrel)
});
iter += 1;
if status != ::Value::Continue || iter >= max_iter {
break;
}
}
}
status
}
}
ffi_wrapper!(
MultiFitFdfSolverType,
*const sys::gsl_multifit_fdfsolver_type
);
impl MultiFitFdfSolverType {
#[doc(alias = "gsl_multifit_fdfsolver_lmder")]
pub fn lmder() -> MultiFitFdfSolverType {
ffi_wrap!(gsl_multifit_fdfsolver_lmder)
}
#[doc(alias = "gsl_multifit_fdfsolver_lmsder")]
pub fn lmsder() -> MultiFitFdfSolverType {
ffi_wrap!(gsl_multifit_fdfsolver_lmsder)
}
}
pub struct MultiFitFunctionFdf {
pub f: Option<Box<dyn Fn(::VectorF64, ::VectorF64) -> ::Value>>,
pub df: Option<Box<dyn Fn(::VectorF64, ::MatrixF64) -> ::Value>>,
pub fdf: Option<Box<dyn Fn(::VectorF64, ::VectorF64, ::MatrixF64) -> ::Value>>,
pub n: usize,
pub p: usize,
intern: sys::gsl_multifit_function_fdf,
}
impl MultiFitFunctionFdf {
#[doc(alias = "gsl_multifit_function_fdf")]
pub fn new(n: usize, p: usize, nevalf: usize, nevaldf: usize) -> MultiFitFunctionFdf {
MultiFitFunctionFdf {
f: None,
df: None,
fdf: None,
n,
p,
intern: sys::gsl_multifit_function_fdf {
f: Some(f),
df: Some(df),
fdf: Some(fdf),
n,
p,
params: ::std::ptr::null_mut(),
nevalf,
nevaldf,
},
}
}
#[allow(clippy::wrong_self_convention)]
fn to_raw(&mut self) -> *mut sys::gsl_multifit_function_fdf {
self.intern.n = self.n;
self.intern.p = self.p;
self.intern.params = self as *mut MultiFitFunctionFdf as *mut c_void;
&mut self.intern
}
}
unsafe extern "C" fn f(
x: *const sys::gsl_vector,
params: *mut c_void,
pf: *mut sys::gsl_vector,
) -> c_int {
let t = params as *mut MultiFitFunctionFdf;
if let Some(ref i_f) = (*t).f {
i_f(
ffi::FFI::soft_wrap(x as usize as *mut _),
ffi::FFI::soft_wrap(pf),
)
.into()
} else {
::Value::Success.into()
}
}
unsafe extern "C" fn df(
x: *const sys::gsl_vector,
params: *mut c_void,
pdf: *mut sys::gsl_matrix,
) -> c_int {
let t = params as *mut MultiFitFunctionFdf;
if let Some(ref i_df) = (*t).df {
i_df(
ffi::FFI::soft_wrap(x as usize as *mut _),
ffi::FFI::soft_wrap(pdf),
)
.into()
} else {
::Value::Success.into()
}
}
unsafe extern "C" fn fdf(
x: *const sys::gsl_vector,
params: *mut c_void,
pf: *mut sys::gsl_vector,
pdf: *mut sys::gsl_matrix,
) -> c_int {
let t = params as *mut MultiFitFunctionFdf;
if let Some(ref i_fdf) = (*t).fdf {
i_fdf(
ffi::FFI::soft_wrap(x as usize as *mut _),
ffi::FFI::soft_wrap(pf),
ffi::FFI::soft_wrap(pdf),
)
.into()
} else {
::Value::Success.into()
}
}