00001
00002
00003 #include <malloc.h>
00004 #include <stdio.h>
00005
00006 void nrerror(error_text)
00007 char error_text[];
00008 {
00009 void exit();
00010
00011 fprintf(stderr,"Numerical Recipes run-time error...\n");
00012 fprintf(stderr,"%s\n",error_text);
00013 fprintf(stderr,"...now exiting to system...\n");
00014 exit(1);
00015 }
00016
00017
00018
00019 float *vector(nl,nh)
00020 int nl,nh;
00021 {
00022 float *v;
00023
00024 v=(float *)malloc((unsigned) (nh-nl+1)*sizeof(float));
00025 if (!v) nrerror("allocation failure in vector()");
00026 return v-nl;
00027 }
00028
00029 int *ivector(nl,nh)
00030 int nl,nh;
00031 {
00032 int *v;
00033
00034 v=(int *)malloc((unsigned) (nh-nl+1)*sizeof(int));
00035 if (!v) nrerror("allocation failure in ivector()");
00036 return v-nl;
00037 }
00038
00039 double *dvector(nl,nh)
00040 int nl,nh;
00041 {
00042 double *v;
00043
00044 v=(double *)malloc((unsigned) (nh-nl+1)*sizeof(double));
00045 if (!v) nrerror("allocation failure in dvector()");
00046 return v-nl;
00047 }
00048
00049
00050
00051 float **matrix(nrl,nrh,ncl,nch)
00052 int nrl,nrh,ncl,nch;
00053 {
00054 int i;
00055 float **m;
00056
00057 m=(float **) malloc((unsigned) (nrh-nrl+1)*sizeof(float*));
00058 if (!m) nrerror("allocation failure 1 in matrix()");
00059 m -= nrl;
00060
00061 for(i=nrl;i<=nrh;i++) {
00062 m[i]=(float *) malloc((unsigned) (nch-ncl+1)*sizeof(float));
00063 if (!m[i]) nrerror("allocation failure 2 in matrix()");
00064 m[i] -= ncl;
00065 }
00066 return m;
00067 }
00068
00069 double **dmatrix(nrl,nrh,ncl,nch)
00070 int nrl,nrh,ncl,nch;
00071 {
00072 int i;
00073 double **m;
00074
00075 m=(double **) malloc((unsigned) (nrh-nrl+1)*sizeof(double*));
00076 if (!m) nrerror("allocation failure 1 in dmatrix()");
00077 m -= nrl;
00078
00079 for(i=nrl;i<=nrh;i++) {
00080 m[i]=(double *) malloc((unsigned) (nch-ncl+1)*sizeof(double));
00081 if (!m[i]) nrerror("allocation failure 2 in dmatrix()");
00082 m[i] -= ncl;
00083 }
00084 return m;
00085 }
00086
00087 int **imatrix(nrl,nrh,ncl,nch)
00088 int nrl,nrh,ncl,nch;
00089 {
00090 int i,**m;
00091
00092 m=(int **)malloc((unsigned) (nrh-nrl+1)*sizeof(int*));
00093 if (!m) nrerror("allocation failure 1 in imatrix()");
00094 m -= nrl;
00095
00096 for(i=nrl;i<=nrh;i++) {
00097 m[i]=(int *)malloc((unsigned) (nch-ncl+1)*sizeof(int));
00098 if (!m[i]) nrerror("allocation failure 2 in imatrix()");
00099 m[i] -= ncl;
00100 }
00101 return m;
00102 }
00103
00104
00105
00106 float **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
00107 float **a;
00108 int oldrl,oldrh,oldcl,oldch,newrl,newcl;
00109 {
00110 int i,j;
00111 float **m;
00112
00113 m=(float **) malloc((unsigned) (oldrh-oldrl+1)*sizeof(float*));
00114 if (!m) nrerror("allocation failure in submatrix()");
00115 m -= newrl;
00116
00117 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+oldcl-newcl;
00118
00119 return m;
00120 }
00121
00122
00123
00124 void free_vector(v,nl,nh)
00125 float *v;
00126 int nl,nh;
00127 {
00128 free((char*) (v+nl));
00129 }
00130
00131 void free_ivector(v,nl,nh)
00132 int *v,nl,nh;
00133 {
00134 free((char*) (v+nl));
00135 }
00136
00137 void free_dvector(v,nl,nh)
00138 double *v;
00139 int nl,nh;
00140 {
00141 free((char*) (v+nl));
00142 }
00143
00144
00145
00146 void free_matrix(m,nrl,nrh,ncl,nch)
00147 float **m;
00148 int nrl,nrh,ncl,nch;
00149 {
00150 int i;
00151
00152 for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
00153 free((char*) (m+nrl));
00154 }
00155
00156 void free_dmatrix(m,nrl,nrh,ncl,nch)
00157 double **m;
00158 int nrl,nrh,ncl,nch;
00159 {
00160 int i;
00161
00162 for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
00163 free((char*) (m+nrl));
00164 }
00165
00166 void free_imatrix(m,nrl,nrh,ncl,nch)
00167 int **m;
00168 int nrl,nrh,ncl,nch;
00169 {
00170 int i;
00171
00172 for(i=nrh;i>=nrl;i--) free((char*) (m[i]+ncl));
00173 free((char*) (m+nrl));
00174 }
00175
00176
00177
00178 void free_submatrix(b,nrl,nrh,ncl,nch)
00179 float **b;
00180 int nrl,nrh,ncl,nch;
00181 {
00182 free((char*) (b+nrl));
00183 }
00184
00185
00186
00187 float **convert_matrix(a,nrl,nrh,ncl,nch)
00188 float *a;
00189 int nrl,nrh,ncl,nch;
00190 {
00191 int i,j,nrow,ncol;
00192 float **m;
00193
00194 nrow=nrh-nrl+1;
00195 ncol=nch-ncl+1;
00196 m = (float **) malloc((unsigned) (nrow)*sizeof(float*));
00197 if (!m) nrerror("allocation failure in convert_matrix()");
00198 m -= nrl;
00199 for(i=0,j=nrl;i<=nrow-1;i++,j++) m[j]=a+ncol*i-ncl;
00200 return m;
00201 }
00202
00203
00204
00205 void free_convert_matrix(b,nrl,nrh,ncl,nch)
00206 float **b;
00207 int nrl,nrh,ncl,nch;
00208 {
00209 free((char*) (b+nrl));
00210 }