Xmipp  v3.23.11-Nereus
numerical_recipes.h
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Authors: Carlos Oscar S. Sorzano (coss@cnb.csic.es)
4  *
5  * Unidad de Bioinformatica of Centro Nacional de Biotecnologia , CSIC
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20  * 02111-1307 USA
21  *
22  * All comments concerning this program package may be sent to the
23  * e-mail address 'xmipp@cnb.csic.es'
24  ***************************************************************************/
25 /*****************************************************************************/
26 /* Variable and prototype definitions for the Numerical Core */
27 /*****************************************************************************/
28 #ifndef CORE__NUMERICAL_HH
29 #define CORE__NUMERICAL_HH
30 
31 #include <math.h>
32 #include "xmipp_memory.h"
33 #include "xmipp_macros.h"
34 #include <algorithm>
35 #include <vector>
36 
37 //@defgroup NumericalRecipes Functions from the Numerical Recipes
38 //@ingroup DataLibrary
40 
41 // Utilities --------------------------------------------------------------
42 void nrerror(const char error_text[]);
43 
44 // Random numbers ---------------------------------------------------------
45 double ran1(int *idum); // Uniform random
46 double gasdev(int *idum); // Gaussian random
47 double tdev(double nu, int *idum); // t-student random
48 
49 // Cumulative distribution functions and Kolmogorov-Smirnov test
50 void ksone(double data[], int n, double(*func)(double), double * d, double * prob); // Chapter 13.5
51 double probks(double alam); // Chapter 13.5
52 
53 // FFT ---------------------------------------------------------------------
54 void four1(double data[], int nn, int isign); // Complex FFT 1D
55 void realft(double data[], int n, int isign); // Real FFT 1D
56 void fourn(double data[], int nn[], int ndim, int isign); // Complex FFT 2D,3D,...
57 
58 // Sorting -----------------------------------------------------------------
59 void indexx(int n, double arrin[], int indx[]); // Sorting indexes
60 
61 // Bessel functions --------------------------------------------------------
62 double bessj0(double x);
63 double bessj3_5(double x);
64 double bessj1_5(double x);
65 
66 double bessi0(double x);
67 double bessi1(double x);
68 double bessi0_5(double x);
69 double bessi1_5(double x);
70 double bessi2(double x);
71 double bessi3(double x);
72 double bessi2_5(double x);
73 double bessi3_5(double x);
74 double bessi4(double x);
75 
76 // Special functions -------------------------------------------------------
77 double gammln(double xx);
78 double gammp(double a, double x);
79 double betacf(double a, double b, double x);
80 double betai(double a, double b, double x);
81 inline double sinc(double x)
82 {
83  if (fabs(x)<0.0001)
84  return 1;
85  else
86  {
87  double arg=PI*x;
88  return sin(arg)/arg;
89  }
90 }
91 inline size_t fact(int num)
92 {
93  size_t value = 1;
94  if (num !=1 && num != 0)
95  {
96  for (int i = num; i > 0; --i)
97  value *= i;
98  }
99  return value;
100 
101 }
102 
103 inline size_t binom(int n, int k)
104 {
105  size_t factor=1;
106  for (int i = n; i > (n-k); --i)
107  factor *= i;
108  return factor/fact(k);
109 }
110 
111 // Singular value descomposition of matrix a (numerical recipes, chapter 2-6 for details)
112 void svdcmp(double *a, int m, int n, double *w, double *v);
113 void svbksb(double *u, double *w, double *v, int m, int n, double *b, double *x);
114 
115 // -------------------------------------------------------------------------
116 void convlv(double *data, int n, double *respns, int m, int isign, double *ans);
117 void realft(double *data, int n, int isign);
118 void twofft(double *data1, double *data2, double *fft1, double *fft2, int n);
119 void four1(double *data, int nn, int isign);
120 
121 // Optimization ------------------------------------------------------------
122 void powell(double *p, double *xi, int n, double ftol, int &iter,
123  double &fret, double(*func)(double *, void *), void *prm,
124  bool show);
125 
126 // These two routines have been taken from
127 // http://users.utu.fi/vesoik/userdocs/programs/libpet
128 // and they implement an algorithm of Lawson-Hanson of
129 // nonnegative least squares
130 int nnls(double *a, int m, int n, double *b, double *x,
131  double *rnorm, double *w, double *zz, int *index);
132 int nnlsWght(int N, int M, double *A, double *b, double *weight);
133 
134 // CFSQP -------------------------------------------------------------------
135 // These routines are from
136 // http://www.aemtechnology.com/aemdesign/downloadfsqp.htm
137 // They implement the CFSQP algorithm
138 /* Declare and initialize user-accessible flag indicating */
139 /* whether x sent to user functions has been changed within */
140 /* CFSQP. */
141 // Gradients - Finite Difference
142 void grobfd(int, int, double *, double *, void(*)(int, int,
143  double *, double *, void *), void *);
144 void grcnfd(int, int, double *, double *, void(*)(int, int,
145  double *, double *, void *), void *);
146 
147 // CFSQP
148 void cfsqp(int, int, int, int, int, int, int, int, int, int *, int, int,
149  int, int *, double, double, double, double, double *,
150  double *, double *, double *, double *, double *,
151  void(*)(int, int, double *, double *, void *),
152  void(*)(int, int, double *, double *, void *),
153  void(*)(int, int, double *, double *,
154  void(*)(int, int, double *, double *, void *), void *),
155  void(*)(int, int, double *, double *,
156  void(*)(int, int, double *, double *, void *), void *),
157  void *);
158 
159 // Wavelets ----------------------------------------------------------------
160 void wtn(double a[], unsigned long nn[], int ndim, int isign,
161  void(*wtstep)(double [], unsigned long, int));
162 void pwtset(int n);
163 void pwt(double a[], unsigned long n, int isign);
164 
165 // Working with matrices ---------------------------------------------------
166 // LU decomposition
167 #define TINY 1.0e-20;
168 /* Chapter 2 Section 3: LU DECOMPOSITION */
169 template <class T>
170 void ludcmp(T *a, int n, int *indx, T *d)
171 {
172  int i, imax=0, j, k;
173  T big, dum, sum, temp;
174 
175  std::vector<T> buffer(n);
176  auto *vv= buffer.data()-1;
177  *d = (T)1.0;
178  for (i = 1;i <= n;i++)
179  {
180  big = (T)0.0;
181  for (j = 1;j <= n;j++)
182  if ((temp = (T)fabs((double)a[i*n+j])) > big)
183  big = temp;
184  if (big == (T)0.0)
185  nrerror("Singular matrix in routine LUDCMP");
186  vv[i] = (T)1.0 / big;
187  }
188  for (j = 1;j <= n;j++)
189  {
190  for (i = 1;i < j;i++)
191  {
192  sum = a[i*n+j];
193  for (k = 1;k < i;k++)
194  sum -= a[i*n+k] * a[k*n+j];
195  a[i*n+j] = sum;
196  }
197  big = (T)0.0;
198  for (i = j;i <= n;i++)
199  {
200  sum = a[i*n+j];
201  for (k = 1;k < j;k++)
202  sum -= a[i*n+k] * a[k*n+j];
203  a[i*n+j] = sum;
204  if ((dum = vv[i] * (T)fabs((double)sum)) >= big)
205  {
206  big = dum;
207  imax = i;
208  }
209  }
210  if (j != imax)
211  {
212  for (k = 1;k <= n;k++)
213  {
214  dum = a[imax*n+k];
215  a[imax*n+k] = a[j*n+k];
216  a[j*n+k] = dum;
217  }
218  *d = -(*d);
219  vv[imax] = vv[j];
220  }
221  indx[j] = imax;
222  if (a[j*n+j] == 0.0)
223  a[j*n+j] = (T) TINY;
224  if (j != n)
225  {
226  dum = (T)1.0 / (a[j*n+j]);
227  for (i = j + 1;i <= n;i++)
228  a[i*n+j] *= dum;
229  }
230  }
231 }
232 #undef TINY
233 
234 // Solve Ax=b
235 /* Chapter 2 Section 3: LU BACKWARD-FORWARD SUBSTITUTION */
236 template <class T>
237 void lubksb(T *a, int n, int *indx, T b[])
238 {
239  int i, ii = 0, ip, j;
240  T sum;
241 
242  for (i = 1;i <= n;i++)
243  {
244  ip = indx[i];
245  sum = b[ip];
246  b[ip] = b[i];
247  if (ii)
248  for (j = ii;j <= i - 1;j++)
249  sum -= a[i*n+j] * b[j];
250  else if (sum)
251  ii = i;
252  b[i] = sum;
253  }
254  for (i = n;i >= 1;i--)
255  {
256  sum = b[i];
257  for (j = i + 1;j <= n;j++)
258  sum -= a[i*n+j] * b[j];
259  b[i] = sum / a[i*n+i];
260  }
261 }
262 
263 /* Chapter 2, Section 1. Gauss-Jordan equation system resolution ----------- */
264 // Solve Ax=b (b=matrix)
265 template <class T>
266 void gaussj(T *a, int n, T *b, int m)
267 {
268  T temp;
269  int i, icol=0, irow=0, j, k, l, ll;
270  T big, dum;
271  double pivinv;
272 
273  std::vector<int> buffer(3*n);
274  auto *indxc= buffer.data()-1;
275  auto *indxr= indxc + n;
276  auto *ipiv= indxr + n;
277  for (j = 1;j <= n;j++)
278  ipiv[j] = 0;
279  for (i = 1;i <= n;i++)
280  {
281  big = (T)0;
282  for (j = 1;j <= n;j++)
283  if (ipiv[j] != 1)
284  for (k = 1;k <= n;k++)
285  {
286  if (ipiv[k] == 0)
287  {
288  if (fabs((double)a[j*n+k]) >= (double) big)
289  {
290  big = ABS(a[j*n+k]);
291  irow = j;
292  icol = k;
293  }
294  }
295  else if (ipiv[k] > 1)
296  nrerror("GAUSSJ: Singular Matrix-1");
297  }
298  ++(ipiv[icol]);
299  if (irow != icol)
300  {
301  for (l = 1;l <= n;l++)
302  SWAP(a[irow*n+l], a[icol*n+l], temp)
303  for (l = 1;l <= m;l++)
304  SWAP(b[irow*n+l], b[icol*n+l], temp)
305  }
306  indxr[i] = irow;
307  indxc[i] = icol;
308  if (a[icol*n+icol] == 0.0)
309  nrerror("GAUSSJ: Singular Matrix-2");
310  pivinv = 1.0f / a[icol*n+icol];
311  a[icol*n+icol] = (T)1;
312  for (l = 1;l <= n;l++)
313  a[icol*n+l] = (T)(pivinv * a[icol*n+l]);
314  for (l = 1;l <= m;l++)
315  b[icol*n+l] = (T)(pivinv * b[icol*n+l]);
316  for (ll = 1;ll <= n;ll++)
317  if (ll != icol)
318  {
319  dum = a[ll*n+icol];
320  a[ll*n+icol] = (T)0;
321  for (l = 1;l <= n;l++)
322  a[ll*n+l] -= a[icol*n+l] * dum;
323  for (l = 1;l <= m;l++)
324  b[ll*n+l] -= b[icol*n+l] * dum;
325  }
326  }
327  for (l = n;l >= 1;l--)
328  {
329  if (indxr[l] != indxc[l])
330  for (k = 1;k <= n;k++)
331  SWAP(a[k*n+indxr[l]], a[k*n+indxc[l]], temp);
332  }
333 }
334 
335 // Cholesky factorization
336 void choldc(double *a, int n, double *p);
337 // Cholesky backsubstitution
338 void cholsl(double *a, int n, double *p, double *b, double *x);
339 // Polynomial interpolation
340 void polint(double *xa, double *ya, int n, double x, double &y, double &dy);
342 
343 #endif
void svdcmp(double *a, int m, int n, double *w, double *v)
double xi
void realft(double data[], int n, int isign)
void nrerror(const char error_text[])
double bessi1(double x)
double bessi2_5(double x)
void cfsqp(int, int, int, int, int, int, int, int, int, int *, int, int, int, int *, double, double, double, double, double *, double *, double *, double *, double *, double *, void(*)(int, int, double *, double *, void *), void(*)(int, int, double *, double *, void *), void(*)(int, int, double *, double *, void(*)(int, int, double *, double *, void *), void *), void(*)(int, int, double *, double *, void(*)(int, int, double *, double *, void *), void *), void *)
double gasdev(int *idum)
void wtn(double a[], unsigned long nn[], int ndim, int isign, void(*wtstep)(double [], unsigned long, int))
double bessi2(double x)
double sinc(double x)
HBITMAP buffer
Definition: svm-toy.cpp:37
static double * y
void lubksb(T *a, int n, int *indx, T b[])
void twofft(double *data1, double *data2, double *fft1, double *fft2, int n)
doublereal * w
void convlv(double *data, int n, double *respns, int m, int isign, double *ans)
glob_prnt iter
doublereal * x
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
void gaussj(T *a, int n, T *b, int m)
double ran1(int *idum)
doublereal * d
void ludcmp(T *a, int n, int *indx, T *d)
double tdev(double nu, int *idum)
void grobfd(int, int, double *, double *, void(*)(int, int, double *, double *, void *), void *)
double vv
void cholsl(double *a, int n, double *p, double *b, double *x)
double probks(double alam)
void indexx(int n, double arrin[], int indx[])
double bessi4(double x)
doublereal * b
double bessi3_5(double x)
double bessj1_5(double x)
viol index
double bessj3_5(double x)
void svbksb(double *u, double *w, double *v, int m, int n, double *b, double *x)
#define ABS(x)
Definition: xmipp_macros.h:142
void polint(double *xa, double *ya, int n, double x, double &y, double &dy)
double gammln(double xx)
double betai(double a, double b, double x)
double bessi1_5(double x)
#define j
double gammp(double a, double x)
int m
#define TINY
int nnlsWght(int N, int M, double *A, double *b, double *weight)
void fourn(double data[], int nn[], int ndim, int isign)
double betacf(double a, double b, double x)
void pwtset(int n)
void four1(double data[], int nn, int isign)
doublereal * u
#define SWAP(a, b, tmp)
Definition: xmipp_macros.h:428
ProgClassifyCL2D * prm
int idum
double bessi3(double x)
void powell(double *p, double *xi, int n, double ftol, int &iter, double &fret, double(*func)(double *, void *), void *prm, bool show)
void ksone(double data[], int n, double(*func)(double), double *d, double *prob)
#define PI
Definition: tools.h:43
size_t fact(int num)
void pwt(double a[], unsigned long n, int isign)
int * n
double bessi0_5(double x)
doublereal * a
double bessj0(double x)
void choldc(double *a, int n, double *p)
size_t binom(int n, int k)
double bessi0(double x)
void grcnfd(int, int, double *, double *, void(*)(int, int, double *, double *, void *), void *)
int nnls(double *a, int m, int n, double *b, double *x, double *rnorm, double *w, double *zz, int *index)