10 void numericalRecipes::nrerror(
const char error_text[])
13 fprintf(stderr,
"Numerical Recipes run-time error...\n");
14 fprintf(stderr,
"%s\n",error_text);
15 fprintf(stderr,
"...now exiting to system...\n");
19 float *numericalRecipes::vector(
long nl,
long nh)
24 v=(
float *)malloc((
size_t) ((nh-nl+1+NR_END)*
sizeof(
float)));
25 if (!v) nrerror(
"allocation failure in nrvector()");
29 int *numericalRecipes::ivector(
long nl,
long nh)
34 v=(
int *)malloc((
size_t) ((nh-nl+1+NR_END)*
sizeof(
int)));
35 if (!v) nrerror(
"allocation failure in inrvector()");
39 double *numericalRecipes::dvector(
long nl,
long nh)
44 v=(
double *)malloc((
size_t) ((nh-nl+1+NR_END)*
sizeof(
double)));
45 if (!v) nrerror(
"allocation failure in dnrvector()");
49 float **numericalRecipes::matrix(
long nrl,
long nrh,
long ncl,
long nch)
52 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
56 m=(
float **) malloc((
size_t)((nrow+NR_END)*
sizeof(
float*)));
57 if (!m) nrerror(
"allocation failure 1 in matrix()");
62 m[nrl]=(
float *) malloc((
size_t)((nrow*ncol+NR_END)*
sizeof(
float)));
63 if (!m[nrl]) nrerror(
"allocation failure 2 in matrix()");
67 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
73 double **numericalRecipes::dmatrix(
long nrl,
long nrh,
long ncl,
long nch)
76 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
80 m=(
double **) malloc((
size_t)((nrow+NR_END)*
sizeof(
double*)));
81 if (!m) nrerror(
"allocation failure 1 in matrix()");
86 m[nrl]=(
double *) malloc((
size_t)((nrow*ncol+NR_END)*
sizeof(
double)));
87 if (!m[nrl]) nrerror(
"allocation failure 2 in matrix()");
91 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
97 int **numericalRecipes::imatrix(
long nrl,
long nrh,
long ncl,
long nch)
100 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
104 m=(
int **) malloc((
size_t)((nrow+NR_END)*
sizeof(
int*)));
105 if (!m) nrerror(
"allocation failure 1 in matrix()");
111 m[nrl]=(
int *) malloc((
size_t)((nrow*ncol+NR_END)*
sizeof(
int)));
112 if (!m[nrl]) nrerror(
"allocation failure 2 in matrix()");
116 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
122 float **numericalRecipes::submatrix(
float **a,
long oldrl,
long oldrh,
long oldcl,
long oldch,
123 long newrl,
long newcl)
126 long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
130 m=(
float **) malloc((
size_t) ((nrow+NR_END)*
sizeof(
float*)));
131 if (!m) nrerror(
"allocation failure in submatrix()");
136 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
142 float **numericalRecipes::convert_matrix(
float *a,
long nrl,
long nrh,
long ncl,
long nch)
148 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
152 m=(
float **) malloc((
size_t) ((nrow+NR_END)*
sizeof(
float*)));
153 if (!m) nrerror(
"allocation failure in convert_matrix()");
159 for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
164 int numericalRecipes::ludcmp(
double **a,
int n,
int *indx,
double *d) {
166 double big,dum,sum,temp;
175 if ((temp=fabs(a[i][j])) > big) big=temp;
177 printf(
"Singular matrix in routine ludcmp");
185 for (k=1;k<i;k++) sum -= a[i][k]*a[k][j];
192 sum -= a[i][k]*a[k][j];
194 if ( (dum=vv[i]*fabs(sum)) >= big) {
209 if (a[j][j] == 0.0) a[j][j]=TINY;
212 for (i=j+1;i<=n;i++) a[i][j] *= dum;
215 free_dvector(vv, 1, n);
218 void numericalRecipes::lubksb(
double **a,
int n,
int *indx,
double b[]) {
227 for (j=ii;j<=i-1;j++) sum -= a[i][j]*b[j];
233 for (j=i+1;j<=n;j++) sum -= a[i][j]*b[j];
237 void numericalRecipes::invertMatrix(
int n,
double **matrix,
double **inverse ) {
242 indx = ivector( 1, n );
243 a = dmatrix( 1, n, 1, n );
244 col = dvector( 1, n );
248 a[i][j] = matrix[i][j];
251 if (ludcmp(a,n,indx,&d) < 0) {
252 printf(
"Error in ludcmp. Ending inversion.");
259 lubksb(a,n,indx,col);
261 inverse[j][i] = col[j];
264 free_dvector( col, 1, n);
265 free_dmatrix( a, 1, n, 1, n);
266 free_ivector( indx, 1, n);
268 #define SWAP(a,b) {swap=(a);(a)=(b);(b)=swap;}
269 void numericalRecipes::covsrt(
double **covar,
int ma,
int ia[],
int mfit)
274 for (i=mfit+1;i<=ma;i++)
275 for (j=1;j<=i;j++) covar[i][j]=covar[j][i]=0.0;
277 for (j=ma;j>=1;j--) {
279 for (i=1;i<=ma;i++) SWAP(covar[i][k],covar[i][j])
280 for (i=1;i<=ma;i++) SWAP(covar[k][i],covar[j][i])
286 #define SWAP(a,b) {temp=(a);(a)=(b);(b)=temp;}
288 void numericalRecipes::gaussj(
double **a,
int n,
double **b,
int m)
290 int *indxc,*indxr,*ipiv;
291 int i,icol,irow,j,k,l,ll;
292 double big,dum,pivinv,temp;
299 for (j=1;j<=n;j++) ipiv[j]=0;
306 if (fabs(a[j][k]) >= big) {
311 }
else if (ipiv[k] > 1) {
312 printf(
"gaussj: Singular Matrix-1\n");
318 for (l=1;l<=n;l++) SWAP(a[irow][l],a[icol][l])
319 for (l=1;l<=m;l++) SWAP(b[irow][l],b[icol][l])
323 if (a[icol][icol] == 0.0) {
324 printf(
"gaussj: Singular Matrix-2\n");
327 pivinv=1.0/a[icol][icol];
329 for (l=1;l<=n;l++) a[icol][l] *= pivinv;
330 for (l=1;l<=m;l++) b[icol][l] *= pivinv;
331 for (ll=1;ll<=n;ll++)
335 for (l=1;l<=n;l++) a[ll][l] -= a[icol][l]*dum;
336 for (l=1;l<=m;l++) b[ll][l] -= b[icol][l]*dum;
340 if (indxr[l] != indxc[l])
342 SWAP(a[k][indxr[l]],a[k][indxc[l]]);
344 free_ivector(ipiv,1,n);
345 free_ivector(indxr,1,n);
346 free_ivector(indxc,1,n);
352 void numericalRecipes::free_vector(
float *v,
long nl,
long nh)
355 free((FREE_ARG) (v+nl-NR_END));
358 void numericalRecipes::free_ivector(
int *v,
long nl,
long nh)
361 free((FREE_ARG) (v+nl-NR_END));
364 void numericalRecipes::free_dvector(
double *v,
long nl,
long nh)
367 free((FREE_ARG) (v+nl-NR_END));
370 void numericalRecipes::free_matrix(
float **m,
long nrl,
long nrh,
long ncl,
long nch)
373 free((FREE_ARG) (m[nrl]+ncl-NR_END));
374 free((FREE_ARG) (m+nrl-NR_END));
377 void numericalRecipes::free_dmatrix(
double **m,
long nrl,
long nrh,
long ncl,
long nch)
380 free((FREE_ARG) (m[nrl]+ncl-NR_END));
381 free((FREE_ARG) (m+nrl-NR_END));
384 void numericalRecipes::free_imatrix(
int **m,
long nrl,
long nrh,
long ncl,
long nch)
387 free((FREE_ARG) (m[nrl]+ncl-NR_END));
388 free((FREE_ARG) (m+nrl-NR_END));
391 void numericalRecipes::free_submatrix(
float **b,
long nrl,
long nrh,
long ncl,
long nch)
394 free((FREE_ARG) (b+nrl-NR_END));
397 void numericalRecipes::free_convert_matrix(
float **b,
long nrl,
long nrh,
long ncl,
long nch)
400 free((FREE_ARG) (b+nrl-NR_END));