HOPS
HOPS class reference
fit_gsl.h
Go to the documentation of this file.
1 /*
2  * Replacements for numerical recipe fitting routines that use GSL.
3  *
4  * fit_snr.c does the 3pt parabolic fit (as SSD coded it) but
5  * fit_msnr.c now calls it so that we have two fits.
6  *
7  * The GSL nonlinear fitting framework was revised for GSL 2.2
8  * Prior to that (at least as early as GSL 1.15) an older ...nlin.h
9  * framework existed. We have support here for both as old code
10  * may yet exist in newer versions of GSL for backwards compatibility.
11  */
12 #ifndef __fit_gsl_h__
13 #define __fit_gsl_h__
14 
15 #include "hops_config.h"
16 
17 #include <stdlib.h>
18 #include <math.h>
19 #include <gsl/gsl_blas.h>
20 #include <gsl/gsl_bspline.h>
21 #include <gsl/gsl_min.h>
22 #include <gsl/gsl_multifit.h>
23 
24 #ifndef HAVE_GSL_GSL_MULTIFIT_NLINEAR_H
25 #define HAVE_GSL_GSL_MULTIFIT_NLINEAR_H 0
26 #endif /* HAVE_GSL_GSL_MULTIFIT_NLINEAR_H */
27 #ifndef HAVE_GSL_GSL_MULTIFIT_NLIN_H
28 #define HAVE_GSL_GSL_MULTIFIT_NLIN_H 0
29 #endif /* HAVE_GSL_GSL_MULTIFIT_NLIN_H */
30 
31 #if HAVE_GSL_GSL_MULTIFIT_NLINEAR_H
32 #include <gsl/gsl_multifit_nlinear.h>
33 #endif /* HAVE_GSL_GSL_MULTIFIT_NLINEAR_H */
34 #if HAVE_GSL_GSL_MULTIFIT_NLIN_H
35 #include <gsl/gsl_multifit_nlin.h>
36 #endif /* HAVE_GSL_GSL_MULTIFIT_NLIN_H */
37 
38 /* which gsl/gsl_multifit library to use for amp fits */
39 #if HAVE_GSL_GSL_MULTIFIT_NLINEAR_H
40 #define GSLEGACY_DEFAULT 0
41 #elif HAVE_GSL_GSL_MULTIFIT_NLIN_H
42 #define GSLEGACY_DEFAULT 1
43 #else
44 #warning "missing gsl/gsl_multifit libraries"
45 #define GSLEGACY_DEFAULT -1
46 #endif
47 
48 #include <gsl/gsl_statistics.h>
49 #include "cohfit.h"
50 
51 /* data structure holding fit points; wgt and sigma are redundant,
52  * but the newer formulation uses wgt and the older one sigma,
53  * so we will allow for both in this structure. */
54 typedef struct data {
55  size_t n; /* number of data points */
56  double *t; /* the time values */
57  double *y; /* the data to fit */
58  double *wgt; /* 1/sigma^2 */
59  double *sigma; /* for the */
60 } Data;
61 
62 /* constants needed for gsl_multifit_nlinear_driver() */
63 extern const double xtol, gtol, ftol;
64 extern const size_t maxits;
65 
66 /* wrk and cover are the variables in ??_driver() where these are used */
67 #define FIT(ii) gsl_vector_get(wrk->x, ii)
68 #define ERR(ii) sqrt(gsl_matrix_get(covar,ii,ii))
69 
70 /* modern "fit" routines */
71 #if HAVE_GSL_GSL_MULTIFIT_NLINEAR_H
72 
73 /* machinery for the plateau-slope fitting (ps) */
74 extern int ps_f(const gsl_vector * x, void *data, gsl_vector * f);
75 extern int ps_df(const gsl_vector * x, void *data, gsl_matrix * J);
76 extern void ps_callback(const size_t iter, void *params,
77  const gsl_multifit_nlinear_workspace *w);
78 extern int ps_driver(Data *dp, double psi[3], double pse[3], double chisq[2],
79  int *dofp, int *status, int *info, int *niter, double *rchisq);
80 extern void populate_fitamp_ps(cosumary *codatum);
81 extern void plateau_slope_fit(cosumary *codatum, Data *dp);
82 
83 /* machinery for the plateau-only fitting (po) */
84 extern int po_f(const gsl_vector * x, void *data, gsl_vector * f);
85 extern int po_df(const gsl_vector * x, void *data, gsl_matrix * J);
86 extern void po_callback(const size_t iter, void *params,
87  const gsl_multifit_nlinear_workspace *w);
88 extern int po_driver(Data *dp, double poi[1], double poe[1], double chisq[2],
89  int *dofp, int *status, int *info, int *niter, double *rchisq);
90 extern void populate_fitamp_po(cosumary *codatum);
91 extern void plateau_only_fit(cosumary *codatum, Data *dp);
92 
93 /* machinery for the slope-only fitting (so) */
94 extern int so_f(const gsl_vector * x, void *data, gsl_vector * f);
95 extern int so_df(const gsl_vector * x, void *data, gsl_matrix * J);
96 extern void so_callback(const size_t iter, void *params,
97  const gsl_multifit_nlinear_workspace *w);
98 extern int so_driver(Data *dp, double soi[2], double soe[2], double chisq[2],
99  int *dofp, int *status, int *info, int *niter, double *rchisq);
100 extern void populate_fitamp_so(cosumary *codatum);
101 extern void slope_only_fit(cosumary *codatum, Data *dp);
102 
103 #endif /* HAVE_GSL_GSL_MULTIFIT_NLINEAR_H */
104 
105 /* legacy "gcy" routines */
106 #if HAVE_GSL_GSL_MULTIFIT_NLIN_H
107 
108 /* machinery for the plateau-slope fitting (ps) */
109 extern int gcy_ps_f(const gsl_vector * x, void *data, gsl_vector * f);
110 extern int gcy_ps_df(const gsl_vector * x, void *data, gsl_matrix * J);
111 extern int gcy_ps_fdf(
112  const gsl_vector * x, void *data, gsl_vector * f, gsl_matrix * J);
113 extern void gcy_ps_callback(
114  const size_t iter, gsl_multifit_fdfsolver *wrk, double *chisq);
115 extern void populate_gcyamp_ps(cosumary *codatum);
116 extern int gcy_ps_driver(
117  Data *dp, double psi[3], double pse[3], double chisq[2],
118  int *dofp, int *status, int *info, int *niter, double *rchisq);
119 extern void plateau_slope_gcy(cosumary *codatum, Data *dp);
120 
121 /* machinery for the plateau-only fitting (po) */
122 extern int gcy_po_f(const gsl_vector * x, void *data, gsl_vector * f);
123 extern int gcy_po_df(const gsl_vector * x, void *data, gsl_matrix * J);
124 extern int gcy_po_fdf(
125  const gsl_vector * x, void *data, gsl_vector * f, gsl_matrix * J);
126 extern void gcy_po_callback(
127  const size_t iter, gsl_multifit_fdfsolver *wrk, double *chisq);
128 extern void populate_gcyamp_po(cosumary *codatum);
129 extern int gcy_po_driver(
130  Data *dp, double poi[1], double poe[1], double chisq[2],
131  int *dofp, int *status, int *info, int *niter, double *rchisq);
132 extern void plateau_only_gcy(cosumary *codatum, Data *dp);
133 
134 /* machinery for the slope-only fitting (so) */
135 extern int gcy_so_f(const gsl_vector * x, void *data, gsl_vector * f);
136 extern int gcy_so_df(const gsl_vector * x, void *data, gsl_matrix * J);
137 extern int gcy_so_fdf(
138  const gsl_vector * x, void *data, gsl_vector * f, gsl_matrix * J);
139 extern void gcy_so_callback(
140  const size_t iter, gsl_multifit_fdfsolver *wrk, double *chisq);
141 extern void populate_gcyamp_so(cosumary *codatum);
142 extern int gcy_so_driver(
143  Data *dp, double soi[2], double soe[2], double chisq[2],
144  int *dofp, int *status, int *info, int *niter, double *rchisq);
145 extern void slope_only_gcy(cosumary *codatum, Data *dp);
146 
147 #endif /* HAVE_GSL_GSL_MULTIFIT_NLIN_H */
148 
149 /* top level entries are in cohfit.h: support for fit_ampl(): */
150 extern double cohereguess(cosumary *codatum);
151 extern int choose_best_amp_fit(cosumary *codatum);
152 
153 /* support for fit_msnr(); 3-pnt fit: */
154 extern double fit_snr(cosumary *, int);
155 
156 /* cubic spline fit and machinery */
157 extern double fit_cbs2p8(cosumary *, int);
158 extern double fit_cbs2p7(cosumary *, int);
159 void min_inv_snr_cbs_err2p8(int status, int msglvl);
160 void min_inv_snr_cbs_err2p7(int status, int msglvl);
161 double invsnr2p7(double x, void *params);
162 
163 /* these are slightly different */
164 int min_inv_snr_cbs2p8(cosumary *codatum, int npt, gsl_vector *x,
165  gsl_vector *control, gsl_bspline_workspace *work, double *peakslen);
166 int min_inv_snr_cbs2p7(cosumary *codatum, int npt, gsl_vector *x,
167  gsl_bspline_workspace *work,
168  gsl_vector *B, gsl_vector *c, gsl_matrix *cov, double *peakslen);
169 
170 #endif /* __fit_gsl_h__ */
171 /*
172  * eof vim:nospell
173  */
Definition: cohfit.h:58
double fit_cbs2p7(cosumary *, int)
double * y
Definition: fit_gsl.h:57
double * t
Definition: fit_gsl.h:56
void min_inv_snr_cbs_err2p8(int status, int msglvl)
double invsnr2p7(double x, void *params)
int choose_best_amp_fit(cosumary *codatum)
Definition: choose_best_amp_fit.c:11
double * sigma
Definition: fit_gsl.h:59
const double xtol
double fit_snr(cosumary *, int)
Definition: fit_snr.c:22
int min_inv_snr_cbs2p7(cosumary *codatum, int npt, gsl_vector *x, gsl_bspline_workspace *work, gsl_vector *B, gsl_vector *c, gsl_matrix *cov, double *peakslen)
size_t n
Definition: fit_gsl.h:55
void min_inv_snr_cbs_err2p7(int status, int msglvl)
struct data Data
double cohereguess(cosumary *codatum)
Definition: fit_ampl.c:75
const double gtol
Definition: fit_gsl.h:63
double fit_cbs2p8(cosumary *, int)
const double ftol
Definition: fit_gsl.h:63
const size_t maxits
int min_inv_snr_cbs2p8(cosumary *codatum, int npt, gsl_vector *x, gsl_vector *control, gsl_bspline_workspace *work, double *peakslen)
double * wgt
Definition: fit_gsl.h:58
Definition: fit_gsl.h:54
struct type_status status
Definition: fourfit3.c:53