Toast++  1.0.2 (r.539)
Forward and inverse modelling in optical tomography
nr.h
1 // -*-C++-*-
2 // ============================================================================
3 // TOAST V.14 (1999) M. Schweiger and S.R. Arridge
4 // Routines derived (but not necessarily identical) from NR
5 // ============================================================================
6 
7 // ============================================================================
8 // Contents of nrutil.h
9 // ============================================================================
10 
11 #ifndef _NR_UTILS_H_
12 #define _NR_UTILS_H_
13 
14 #include "mathdef.h"
15 
16 #ifdef UNDEF
17 static double sqrarg;
18 #define SQR(a) ((sqrarg=(a)) == 0.0 ? 0.0 : sqrarg*sqrarg)
19 // replaced by sqr (mathdef.h)
20 
21 static double dsqrarg;
22 #define DSQR(a) ((dsqrarg=(a)) == 0.0 ? 0.0 : dsqrarg*dsqrarg)
23 // replaced by sqr (mathdef.h)
24 
25 static double dmaxarg1,dmaxarg2;
26 #define DMAX(a,b) (dmaxarg1=(a),dmaxarg2=(b),(dmaxarg1) > (dmaxarg2) ?\
27  (dmaxarg1) : (dmaxarg2))
28 // replaced by max (mathdef.h)
29 
30 static double dminarg1,dminarg2;
31 #define DMIN(a,b) (dminarg1=(a),dminarg2=(b),(dminarg1) < (dminarg2) ?\
32  (dminarg1) : (dminarg2))
33 // replaced by min (mathdef.h)
34 
35 static double maxarg1,maxarg2;
36 #define FMAX(a,b) (maxarg1=(a),maxarg2=(b),(maxarg1) > (maxarg2) ?\
37  (maxarg1) : (maxarg2))
38 // replaced by max (mathdef.h)
39 
40 static double minarg1,minarg2;
41 #define FMIN(a,b) (minarg1=(a),minarg2=(b),(minarg1) < (minarg2) ?\
42  (minarg1) : (minarg2))
43 // replaced by min (mathdef.h)
44 
45 static long lmaxarg1,lmaxarg2;
46 #define LMAX(a,b) (lmaxarg1=(a),lmaxarg2=(b),(lmaxarg1) > (lmaxarg2) ?\
47  (lmaxarg1) : (lmaxarg2))
48 // replaced by max (mathdef.h)
49 
50 static long lminarg1,lminarg2;
51 #define LMIN(a,b) (lminarg1=(a),lminarg2=(b),(lminarg1) < (lminarg2) ?\
52  (lminarg1) : (lminarg2))
53 // replaced by min (mathdef.h)
54 
55 static int imaxarg1,imaxarg2;
56 #define IMAX(a,b) (imaxarg1=(a),imaxarg2=(b),(imaxarg1) > (imaxarg2) ?\
57  (imaxarg1) : (imaxarg2))
58 // replaced by max (mathdef.h)
59 
60 static int iminarg1,iminarg2;
61 #define IMIN(a,b) (iminarg1=(a),iminarg2=(b),(iminarg1) < (iminarg2) ?\
62  (iminarg1) : (iminarg2))
63 // replaced by min (mathdef.h)
64 
65 #define SIGN(a,b) ((b) >= 0.0 ? fabs(a) : -fabs(a))
66 // replaced by sign (mathdef.h)
67 
68 #endif // UNDEF
69 
70 #if defined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
71 
72 void nrerror(const char error_text[]);
73 double *vector(long nl, long nh);
74 int *ivector(long nl, long nh);
75 unsigned char *cvector(long nl, long nh);
76 unsigned long *lvector(long nl, long nh);
77 double *dvector(long nl, long nh);
78 double **matrix(long nrl, long nrh, long ncl, long nch);
79 double **dmatrix(long nrl, long nrh, long ncl, long nch);
80 int **imatrix(long nrl, long nrh, long ncl, long nch);
81 double **submatrix(double **a, long oldrl, long oldrh, long oldcl, long oldch,
82  long newrl, long newcl);
83 double **convert_matrix(double *a, long nrl, long nrh, long ncl, long nch);
84 double ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh);
85 void free_vector(double *v, long nl, long nh);
86 void free_ivector(int *v, long nl, long nh);
87 void free_cvector(unsigned char *v, long nl, long nh);
88 void free_lvector(unsigned long *v, long nl, long nh);
89 void free_dvector(double *v, long nl, long nh);
90 void free_matrix(double **m, long nrl, long nrh, long ncl, long nch);
91 void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch);
92 void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch);
93 void free_submatrix(double **b, long nrl, long nrh, long ncl, long nch);
94 void free_convert_matrix(double **b, long nrl, long nrh, long ncl, long nch);
95 void free_f3tensor(double ***t, long nrl, long nrh, long ncl, long nch,
96  long ndl, long ndh);
97 
98 #else /* ANSI */
99 /* traditional - K&R */
100 
101 void nrerror();
102 double *vector();
103 double **matrix();
104 double **submatrix();
105 double **convert_matrix();
106 double ***f3tensor();
107 double *dvector();
108 double **dmatrix();
109 int *ivector();
110 int **imatrix();
111 unsigned char *cvector();
112 unsigned long *lvector();
113 void free_vector();
114 void free_dvector();
115 void free_ivector();
116 void free_cvector();
117 void free_lvector();
118 void free_matrix();
119 void free_submatrix();
120 void free_convert_matrix();
121 void free_dmatrix();
122 void free_imatrix();
123 void free_f3tensor();
124 
125 #endif /* ANSI */
126 
127 #endif /* _NR_UTILS_H_ */
128 
129 // ============================================================================
130 // End of nrutil.h
131 // ============================================================================
132 
133 double gammln (double xx);
134 // Returns the logarithm of the gamma function, ln (Gamma (xx))
135 
136 double poidev (double xm);
137 // Returns as a floating point number an integer value that is a random
138 // deviate drawn from a Poisson distribution of mean xm, using drand48
139 // as a source of uniform random deviates
140 // NR p. 294
141 
142 double bnldev (double pp, int n);
143 // Returns as a floating point number an integer value that is a random
144 // deviate drawn from a binomial distribution on n trials each of
145 // probability pp, using drand48 as a source of uniform random deviates
146 // NR p. 295
147 
148 double gasdev (double sigma);
149 // Returns as a floating point number an integer value that is a random
150 // derivate drawn from a Gaussian distribution ...
151 
152 double bessi0 (double x);
153 // Returns the modified Bessel function I_0(x) for any real x
154 // NR p. 237
155 
156 MATHLIB double bessi (int n, double x);
157 // Returns the modified Bessel function I_n(x) for any real x and n >= 2
158 // NR p. 239
159 
160 double erff (double x);
161 // Returns the error function erf(x)
162 // NR p. 220
163 
164 void fit (double x[], double y[], int ndata, double *a, double *b,
165  double *siga, double *sigb, double *chi2);
166 
167 void four1(double data[], int nn, int isign);
168 // Replaces data[] by its discrete Fourier transform, if isign is input as 1,
169 // or replaces data[] by nn times its inverse Fourier transform, if isign is
170 // input as -1. data is a complex array o length nn, or equivalently a real
171 // array of length 2*nn. nn must be an integer power of 2
172 // NR p. 507
173 
174 double gammp (double a, double x);
175 // Returns the incomplete gamma function P(a,x)
176 // NR p. 218
177 
178 void gcf (double *gammcf, double a, double x, double *gln);
179 // Returns the incomplete gamma function Q(a,x) evaluated by its continued
180 // fraction representation as gammcf. Also returns ln Gamma(a) as gln
181 // NR p. 219
182 
183 void gser (double *gamser, double a, double x, double *gln);
184 // Returns the incomplete gamma function P(a,x) evaluated by its series
185 // representation as gamser. Also returns ln Gamma(a) as gln
186 // NR p. 218
187 
188 void lnsrch (int n, double xold[], double fold, double g[], double p[],
189  double x[], double *f, double stpmax, int *check,
190  double (*func)(double[]));
191 // Given an n-dimensional point xold[1..n], the value of the function and
192 // gradient there, fold, and g[1..n], and a direction p[1..n], finds a new
193 // point x[1..n] along the direction p from xold where the function func
194 // has decreased "sufficiently". The new function value is returned in f.
195 // stpmax is an input quantity that limits the length of the steps so that
196 // you do not try to evaluate the function in regions where it is undefined
197 // or subject to overflow. p is usually the Newton direction. The output
198 // quantity check is false (0) on a normal exit. It is true (1) when x is
199 // too close to xold. In a minimization algorithm, this usually signals
200 // convergence and can be ignored. However, in a zero-finding algorithm the
201 // calling program should check whether the convergence is spurious. Some
202 // "difficult" problems may require double precision in this routine.
203 // NR p. 385
204 
205 void dfpmin (double p[], int n, double gtol, int *iter, double *fret,
206  double(*func)(double[]), void (*dfunc)(double[], double[]));
207 // Given a starting point p[1..n] that is a vector of length n, the Broyden-
208 // Fletcher-Goldfarb-Shanno variant of Davidon-Fletcher-Powell minimization
209 // is performed on a function func, using its gradient as calculated by a
210 // routine dfunc. The convergence requirement on zeroing the gradient is
211 // input as gtol. Returned quantities are p[1..n] (the location of the
212 // minimum), iter (the number of iterations that were performed), and fret
213 // (the minimum value of the function). The routine lnsrch is called to
214 // perform approximate line minimizations.
215 
216 MATHLIB void fourn (double data[], unsigned long nn[], int ndim, int isign);
217 // Replaces data by its ndim-dimensional discrete Fourier transform, if
218 // isign is input as 1. nn[0..ndim-1] is an integer array containing the
219 // lenghts of each dimension (number of complex values), which must be
220 // powers of 2. data is a real array of length twice the product of these
221 // lenghts, in which the data are stored as in a multidimensional complex
222 // array: real and imaginary parts of each element are in consecutive
223 // locations, and the rightmost index of the array increases most rapidly
224 // as one proceeds along data. For a two-dimensional array, this is
225 // equivalent to storing the array by rows. If isisgn is input as -1, data
226 // is replaced by its inverse transform times the product of the lenghts
227 // of all dimensions.
228 
229 void spline (double x[], double y[], int n, double yp1, double ypn,
230  double y2[]);
231 // Given arrays x[0..n-1] and y[0..n-1] containing a tabulated function,
232 // i.e. y_i = f(x_i), with x_0 < x_1 < ... < x_{N-1}, and given values
233 // yp1 and ypn for the first derivative of the interpolating function at
234 // points 0 and n-1, respectively, this routine returns an array y2[0..n-1]
235 // that contains the second derivatives of the interpolating function at
236 // the tabulated points x_i. If yp1 and/or ypn are equal to 1e30 or larger,
237 // the routine is signaled to set the corresponding boundary condition for
238 // a natural spline, with zero second derivative on that boundary.
239 
240 void splint (double xa[], double ya[], double y2a[], int n, double x,
241  double *y);
242 // Given the arrays xa[0..n-1] and ya[0..n-1], which tabulate a function
243 // (with the xa_i's in order), and given the array y2a[0..n-1], which is
244 // the output from spline above, and given a value of x, this routine
245 // returns a cubic-spline interpolated value y.