[3] | 1 | #include <stdio.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <math.h> |
---|
| 5 | |
---|
| 6 | /* dm1d: memory allocation for a vector of double */ |
---|
| 7 | /* ---------------------------------------------- */ |
---|
| 8 | |
---|
| 9 | double *dm1d( int nl, int nh ) |
---|
| 10 | { |
---|
| 11 | double *m; |
---|
| 12 | FILE *out; |
---|
| 13 | |
---|
| 14 | m = (double *)calloc( (unsigned)(nh-nl+1), sizeof(double) ); |
---|
| 15 | if(!m) |
---|
| 16 | { |
---|
| 17 | out = fopen( "err.log", "a" ); |
---|
| 18 | fprintf( out, "Memory allocation error in dm1d" ); |
---|
| 19 | fclose( out ); |
---|
| 20 | exit(0); |
---|
| 21 | } |
---|
| 22 | return m-nl; |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | /* dm2d: memory allocation for a matrix of double */ |
---|
| 26 | /* ---------------------------------------------- */ |
---|
| 27 | |
---|
| 28 | double **dm2d( int nrl, int nrh, int ncl, int nch ) |
---|
| 29 | { |
---|
| 30 | int i; |
---|
| 31 | double **m; |
---|
| 32 | FILE *out; |
---|
| 33 | |
---|
| 34 | m = (double **)calloc( (unsigned)(nrh-nrl+1), sizeof(double *) ); |
---|
| 35 | if(!m) |
---|
| 36 | { |
---|
| 37 | out = fopen( "err.log", "a" ); |
---|
| 38 | fprintf( out, "Memory allocation error in dm2d" ); |
---|
| 39 | fclose( out ); |
---|
| 40 | exit(0); |
---|
| 41 | } |
---|
| 42 | m -= nrl; |
---|
| 43 | for( i = nrl; i <= nrh; i++ ) |
---|
| 44 | { |
---|
| 45 | m[i] = (double *)calloc( (unsigned)(nch-ncl+1), sizeof(double) ); |
---|
| 46 | if(!m[i]) |
---|
| 47 | { |
---|
| 48 | out = fopen( "err.log", "a" ); |
---|
| 49 | fprintf( out, "Memory allocation error in dm2d" ); |
---|
| 50 | fclose( out ); |
---|
| 51 | exit(0); |
---|
| 52 | } |
---|
| 53 | m[i] -= ncl; |
---|
| 54 | } |
---|
| 55 | return m; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | /* dm3d: memory allocation for a 3D vector of double */ |
---|
| 59 | /* ------------------------------------------------- */ |
---|
| 60 | |
---|
| 61 | double ***dm3d( int nrl, int nrh, int ncl, int nch, int nal, int nah ) |
---|
| 62 | { |
---|
| 63 | int i; |
---|
| 64 | double ***m; |
---|
| 65 | FILE *out; |
---|
| 66 | |
---|
| 67 | m = (double ***)calloc( (unsigned)(nrh-nrl+1), sizeof(double **) ); |
---|
| 68 | if(!m) |
---|
| 69 | { |
---|
| 70 | out = fopen( "err.log", "a" ); |
---|
| 71 | fprintf( out, "Memory allocation error in dm3d" ); |
---|
| 72 | fclose( out ); |
---|
| 73 | exit(0); |
---|
| 74 | } |
---|
| 75 | m -= nrl; |
---|
| 76 | for( i = nrl; i <= nrh; i++ ) |
---|
| 77 | m[i] = dm2d( ncl, nch, nal, nah ); |
---|
| 78 | return m; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | /* dm4d: memory allocation for a 4D vector of double */ |
---|
| 82 | /* ------------------------------------------------- */ |
---|
| 83 | |
---|
| 84 | double ****dm4d( int nrl, int nrh, int ncl, int nch, int nal, int nah, int nll, int nlh ) |
---|
| 85 | { |
---|
| 86 | int i; |
---|
| 87 | double ****m; |
---|
| 88 | FILE *out; |
---|
| 89 | |
---|
| 90 | m = (double ****)calloc( (unsigned)(nrh-nrl+1), sizeof(double ***) ); |
---|
| 91 | if(!m) |
---|
| 92 | { |
---|
| 93 | out = fopen( "err.log", "a" ); |
---|
| 94 | fprintf( out, "Memory allocation error in dm4d" ); |
---|
| 95 | fclose( out ); |
---|
| 96 | exit(0); |
---|
| 97 | } |
---|
| 98 | m -= nrl; |
---|
| 99 | for( i = nrl; i <= nrh; i++ ) |
---|
| 100 | m[i] = dm3d( ncl, nch, nal, nah, nll, nlh ); |
---|
| 101 | return m; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | /* fdm1d: release a vector of double */ |
---|
| 105 | /* --------------------------------- */ |
---|
| 106 | |
---|
| 107 | void fdm1d(v,nl) |
---|
| 108 | double *v; |
---|
| 109 | int nl; |
---|
| 110 | { |
---|
| 111 | free((char *)(v+nl)); |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | /* fdm2d: release a matrix of double */ |
---|
| 115 | /* --------------------------------- */ |
---|
| 116 | |
---|
| 117 | void fdm2d(m,nrl,nrh,ncl) |
---|
| 118 | double **m; |
---|
| 119 | int ncl,nrh,nrl; |
---|
| 120 | { |
---|
| 121 | int i; |
---|
| 122 | |
---|
| 123 | for( i = nrh; i >= nrl; i-- ) free((char *)(m[i]+ncl)); |
---|
| 124 | free((char *)(m+nrl)); |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | /* fdm3d: release a 3D vector of double */ |
---|
| 128 | /* ------------------------------------ */ |
---|
| 129 | |
---|
| 130 | void fdm3d(m,nrl,nrh,ncl,nch,nal) |
---|
| 131 | double ***m; |
---|
| 132 | int nch,ncl,nrh,nrl,nal; |
---|
| 133 | { |
---|
| 134 | int i; |
---|
| 135 | |
---|
| 136 | for( i = nrh; i >= nrl; i-- ) fdm2d(m[i],ncl,nch,nal); |
---|
| 137 | free((char *)(m+nrl)); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | /* frm1d: release a vector of float */ |
---|
| 141 | /* -------------------------------- */ |
---|
| 142 | |
---|
| 143 | void frm1d(v,nl) |
---|
| 144 | float *v; |
---|
| 145 | int nl; |
---|
| 146 | { |
---|
| 147 | free((char *)(v+nl)); |
---|
| 148 | } |
---|
| 149 | |
---|
| 150 | /* frm2d: release a matrix of float */ |
---|
| 151 | /* -------------------------------- */ |
---|
| 152 | |
---|
| 153 | void frm2d(m,nrl,nrh,ncl) |
---|
| 154 | float **m; |
---|
| 155 | int ncl,nrh,nrl; |
---|
| 156 | { |
---|
| 157 | int i; |
---|
| 158 | |
---|
| 159 | for( i = nrh; i >= nrl; i-- ) free((char *)(m[i]+ncl)); |
---|
| 160 | free((char *)(m+nrl)); |
---|
| 161 | } |
---|
| 162 | |
---|
| 163 | /* frm3d: release a 3D vector of float */ |
---|
| 164 | /* ----------------------------------- */ |
---|
| 165 | |
---|
| 166 | void frm3d(m,nrl,nrh,ncl,nch,nal) |
---|
| 167 | float ***m; |
---|
| 168 | int nch,ncl,nrh,nrl,nal; |
---|
| 169 | { |
---|
| 170 | int i; |
---|
| 171 | |
---|
| 172 | for( i = nrh; i >= nrl; i-- ) frm2d(m[i],ncl,nch,nal); |
---|
| 173 | free((char *)(m+nrl)); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /* rm1d: memory allocation for a vector of float */ |
---|
| 177 | /* --------------------------------------------- */ |
---|
| 178 | |
---|
| 179 | float *rm1d( int nl, int nh ) |
---|
| 180 | { |
---|
| 181 | float *m; |
---|
| 182 | FILE *out; |
---|
| 183 | |
---|
| 184 | m = (float *)calloc( (unsigned)(nh-nl+1), sizeof(float) ); |
---|
| 185 | if(!m) |
---|
| 186 | { |
---|
| 187 | out = fopen( "err.log", "a" ); |
---|
| 188 | fprintf( out, "Memory allocation error in rm1d" ); |
---|
| 189 | fclose( out ); |
---|
| 190 | exit(0); |
---|
| 191 | } |
---|
| 192 | return m-nl; |
---|
| 193 | } |
---|
| 194 | |
---|
| 195 | /* rm2d: memory allocation for a matrix of float */ |
---|
| 196 | /* --------------------------------------------- */ |
---|
| 197 | |
---|
| 198 | float **rm2d( int nrl, int nrh, int ncl, int nch ) |
---|
| 199 | { |
---|
| 200 | int i; |
---|
| 201 | float **m; |
---|
| 202 | FILE *out; |
---|
| 203 | |
---|
| 204 | m = (float **)calloc( (unsigned)(nrh-nrl+1), sizeof(float *) ); |
---|
| 205 | if(!m) |
---|
| 206 | { |
---|
| 207 | out = fopen( "err.log", "a" ); |
---|
| 208 | fprintf( out, "Memory allocation error in rm2d" ); |
---|
| 209 | fclose( out ); |
---|
| 210 | exit(0); |
---|
| 211 | } |
---|
| 212 | m -= nrl; |
---|
| 213 | for( i = nrl; i <= nrh; i++ ) |
---|
| 214 | { |
---|
| 215 | m[i] = (float *)calloc( (unsigned)(nch-ncl+1), sizeof(float) ); |
---|
| 216 | if(!m[i]) |
---|
| 217 | { |
---|
| 218 | out = fopen( "err.log", "a" ); |
---|
| 219 | fprintf( out, "Memory allocation error in rm2d" ); |
---|
| 220 | fclose( out ); |
---|
| 221 | exit(0); |
---|
| 222 | } |
---|
| 223 | m[i] -= ncl; |
---|
| 224 | } |
---|
| 225 | return m; |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | /* rm3d: memory allocation for a 3D vector of float */ |
---|
| 229 | /* ------------------------------------------------ */ |
---|
| 230 | |
---|
| 231 | float ***rm3d( int nrl, int nrh, int ncl, int nch, int nal, int nah ) |
---|
| 232 | { |
---|
| 233 | int i; |
---|
| 234 | float ***m; |
---|
| 235 | FILE *out; |
---|
| 236 | |
---|
| 237 | m = (float ***)calloc( (unsigned)(nrh-nrl+1), sizeof(float **) ); |
---|
| 238 | if(!m) |
---|
| 239 | { |
---|
| 240 | out = fopen( "err.log", "a" ); |
---|
| 241 | fprintf( out, "Memory allocation error in rm3d" ); |
---|
| 242 | fclose( out ); |
---|
| 243 | exit(0); |
---|
| 244 | } |
---|
| 245 | m -= nrl; |
---|
| 246 | for( i = nrl; i <= nrh; i++ ) |
---|
| 247 | m[i] = rm2d( ncl, nch, nal, nah ); |
---|
| 248 | return m; |
---|
| 249 | } |
---|
| 250 | |
---|
| 251 | /* rm4d: memory allocation for a 4D vector of float */ |
---|
| 252 | /* ------------------------------------------------ */ |
---|
| 253 | |
---|
| 254 | float ****rm4d( int nrl, int nrh, int ncl, int nch, int nal, int nah, int nll, int nlh ) |
---|
| 255 | { |
---|
| 256 | int i; |
---|
| 257 | float ****m; |
---|
| 258 | FILE *out; |
---|
| 259 | |
---|
| 260 | m = (float ****)calloc( (unsigned)(nrh-nrl+1), sizeof(float ***) ); |
---|
| 261 | if(!m) |
---|
| 262 | { |
---|
| 263 | out = fopen( "err.log", "a" ); |
---|
| 264 | fprintf( out, "Memory allocation error in rm4d" ); |
---|
| 265 | fclose( out ); |
---|
| 266 | exit(0); |
---|
| 267 | } |
---|
| 268 | m -= nrl; |
---|
| 269 | for( i = nrl; i <= nrh; i++ ) |
---|
| 270 | m[i] = rm3d( ncl, nch, nal, nah, nll, nlh ); |
---|
| 271 | return m; |
---|
| 272 | } |
---|
| 273 | |
---|
| 274 | /* im1d: memory allocation for a vector of integer */ |
---|
| 275 | /* ----------------------------------------------- */ |
---|
| 276 | |
---|
| 277 | int *im1d( int nl, int nh ) |
---|
| 278 | { |
---|
| 279 | int *m; |
---|
| 280 | FILE *out; |
---|
| 281 | |
---|
| 282 | m = (int *)calloc( (unsigned)(nh-nl+1), sizeof(int ) ); |
---|
| 283 | if(!m) |
---|
| 284 | { |
---|
| 285 | out = fopen( "err.log", "a" ); |
---|
| 286 | fprintf( out, "Memory allocation error in im1d" ); |
---|
| 287 | fclose( out ); |
---|
| 288 | exit(0); |
---|
| 289 | } |
---|
| 290 | return m-nl; |
---|
| 291 | } |
---|
| 292 | |
---|
| 293 | /* im2d: memory allocation for a matrix of integer */ |
---|
| 294 | /* ----------------------------------------------- */ |
---|
| 295 | |
---|
| 296 | int **im2d( int nrl, int nrh, int ncl, int nch ) |
---|
| 297 | { |
---|
| 298 | int i,**m; |
---|
| 299 | FILE *out; |
---|
| 300 | |
---|
| 301 | m = (int **)calloc( (unsigned)(nrh-nrl+1), sizeof(int *) ); |
---|
| 302 | if( !m ) |
---|
| 303 | { |
---|
| 304 | out = fopen( "err.log", "a" ); |
---|
| 305 | fprintf( out, "Memory allocation error in im2d" ); |
---|
| 306 | fclose( out ); |
---|
| 307 | exit(0); |
---|
| 308 | } |
---|
| 309 | m -= nrl; |
---|
| 310 | for( i = nrl; i <= nrh; i++ ) |
---|
| 311 | { |
---|
| 312 | m[i] = (int *)calloc( (unsigned)(nch-ncl+1), sizeof(int) ); |
---|
| 313 | if(!m[i]) |
---|
| 314 | { |
---|
| 315 | out = fopen( "err.log", "a" ); |
---|
| 316 | fprintf( out, "Memory allocation error in im2d" ); |
---|
| 317 | fclose( out ); |
---|
| 318 | exit(0); |
---|
| 319 | } |
---|
| 320 | m[i] -= ncl; |
---|
| 321 | } |
---|
| 322 | return m; |
---|
| 323 | } |
---|
| 324 | |
---|
| 325 | /* im3d: memory allocation for a 3D vector of integer */ |
---|
| 326 | /* -------------------------------------------------- */ |
---|
| 327 | |
---|
| 328 | int ***im3d( int nrl, int nrh, int ncl, int nch, int nal, int nah ) |
---|
| 329 | { |
---|
| 330 | int i,***m; |
---|
| 331 | FILE *out; |
---|
| 332 | |
---|
| 333 | m = (int ***)calloc( (unsigned)(nrh-nrl+1), sizeof(int **) ); |
---|
| 334 | if(!m) |
---|
| 335 | { |
---|
| 336 | out = fopen( "err.log", "a" ); |
---|
| 337 | fprintf( out, "Memory allocation error in im3d" ); |
---|
| 338 | fclose( out ); |
---|
| 339 | exit(0); |
---|
| 340 | } |
---|
| 341 | m -= nrl; |
---|
| 342 | for( i = nrl; i <= nrh; i++ ) |
---|
| 343 | m[i] = im2d( ncl, nch, nal, nah ); |
---|
| 344 | return m; |
---|
| 345 | } |
---|