source: CIVL/examples/compare/GaussianElimination/nrutil.c

main
Last change on this file was ea777aa, checked in by Alex Wilton <awilton@…>, 3 years ago

Moved examples, include, build_default.properties, common.xml, and README out from dev.civl.com into the root of the repo.

git-svn-id: svn://vsl.cis.udel.edu/civl/trunk@5704 fb995dde-84ed-4084-dfe6-e5aef3e2452c

  • Property mode set to 100644
File size: 15.8 KB
Line 
1#if defined(__STDC__) || defined(ANSI) || defined(NRANSI) /* ANSI */
2
3#include <stdio.h>
4#include <stddef.h>
5#include <stdlib.h>
6#define NR_END 1
7#define FREE_ARG char*
8
9void nrerror(char error_text[])
10/* Numerical Recipes standard error handler */
11{
12 fprintf(stderr,"Numerical Recipes run-time error...\n");
13 fprintf(stderr,"%s\n",error_text);
14 fprintf(stderr,"...now exiting to system...\n");
15 exit(1);
16}
17
18float *vector(long nl, long nh)
19/* allocate a float vector with subscript range v[nl..nh] */
20{
21 float *v;
22
23 v=(float *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(float)));
24 if (!v) nrerror("allocation failure in vector()");
25 return v-nl+NR_END;
26}
27
28int *ivector(long nl, long nh)
29/* allocate an int vector with subscript range v[nl..nh] */
30{
31 int *v;
32
33 v=(int *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(int)));
34 if (!v) nrerror("allocation failure in ivector()");
35 return v-nl+NR_END;
36}
37
38unsigned char *cvector(long nl, long nh)
39/* allocate an unsigned char vector with subscript range v[nl..nh] */
40{
41 unsigned char *v;
42
43 v=(unsigned char *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
44 if (!v) nrerror("allocation failure in cvector()");
45 return v-nl+NR_END;
46}
47
48unsigned long *lvector(long nl, long nh)
49/* allocate an unsigned long vector with subscript range v[nl..nh] */
50{
51 unsigned long *v;
52
53 v=(unsigned long *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(long)));
54 if (!v) nrerror("allocation failure in lvector()");
55 return v-nl+NR_END;
56}
57
58double *dvector(long nl, long nh)
59/* allocate a double vector with subscript range v[nl..nh] */
60{
61 double *v;
62
63 v=(double *)malloc((size_t) ((nh-nl+1+NR_END)*sizeof(double)));
64 if (!v) nrerror("allocation failure in dvector()");
65 return v-nl+NR_END;
66}
67
68float **matrix(long nrl, long nrh, long ncl, long nch)
69/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
70{
71 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
72 float **m;
73
74 /* allocate pointers to rows */
75 m=(float **) malloc((size_t)((nrow+NR_END)*sizeof(float*)));
76 if (!m) nrerror("allocation failure 1 in matrix()");
77 m += NR_END;
78 m -= nrl;
79
80 /* allocate rows and set pointers to them */
81 m[nrl]=(float *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float)));
82 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
83 m[nrl] += NR_END;
84 m[nrl] -= ncl;
85
86 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
87
88 /* return pointer to array of pointers to rows */
89 return m;
90}
91
92double **dmatrix(long nrl, long nrh, long ncl, long nch)
93/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
94{
95 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
96 double **m;
97
98 /* allocate pointers to rows */
99 m=(double **) malloc((size_t)((nrow+NR_END)*sizeof(double*)));
100 if (!m) nrerror("allocation failure 1 in matrix()");
101 m += NR_END;
102 m -= nrl;
103
104 /* allocate rows and set pointers to them */
105 m[nrl]=(double *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(double)));
106 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
107 m[nrl] += NR_END;
108 m[nrl] -= ncl;
109
110 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
111
112 /* return pointer to array of pointers to rows */
113 return m;
114}
115
116int **imatrix(long nrl, long nrh, long ncl, long nch)
117/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
118{
119 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
120 int **m;
121
122 /* allocate pointers to rows */
123 m=(int **) malloc((size_t)((nrow+NR_END)*sizeof(int*)));
124 if (!m) nrerror("allocation failure 1 in matrix()");
125 m += NR_END;
126 m -= nrl;
127
128
129 /* allocate rows and set pointers to them */
130 m[nrl]=(int *) malloc((size_t)((nrow*ncol+NR_END)*sizeof(int)));
131 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
132 m[nrl] += NR_END;
133 m[nrl] -= ncl;
134
135 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
136
137 /* return pointer to array of pointers to rows */
138 return m;
139}
140
141float **submatrix(float **a, long oldrl, long oldrh, long oldcl, long oldch,
142 long newrl, long newcl)
143/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
144{
145 long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
146 float **m;
147
148 /* allocate array of pointers to rows */
149 m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
150 if (!m) nrerror("allocation failure in submatrix()");
151 m += NR_END;
152 m -= newrl;
153
154 /* set pointers to rows */
155 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
156
157 /* return pointer to array of pointers to rows */
158 return m;
159}
160
161float **convert_matrix(float *a, long nrl, long nrh, long ncl, long nch)
162/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
163declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
164and ncol=nch-ncl+1. The routine should be called with the address
165&a[0][0] as the first argument. */
166{
167 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
168 float **m;
169
170 /* allocate pointers to rows */
171 m=(float **) malloc((size_t) ((nrow+NR_END)*sizeof(float*)));
172 if (!m) nrerror("allocation failure in convert_matrix()");
173 m += NR_END;
174 m -= nrl;
175
176 /* set pointers to rows */
177 m[nrl]=a-ncl;
178 for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
179 /* return pointer to array of pointers to rows */
180 return m;
181}
182
183float ***f3tensor(long nrl, long nrh, long ncl, long nch, long ndl, long ndh)
184/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
185{
186 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
187 float ***t;
188
189 /* allocate pointers to pointers to rows */
190 t=(float ***) malloc((size_t)((nrow+NR_END)*sizeof(float**)));
191 if (!t) nrerror("allocation failure 1 in f3tensor()");
192 t += NR_END;
193 t -= nrl;
194
195 /* allocate pointers to rows and set pointers to them */
196 t[nrl]=(float **) malloc((size_t)((nrow*ncol+NR_END)*sizeof(float*)));
197 if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
198 t[nrl] += NR_END;
199 t[nrl] -= ncl;
200
201 /* allocate rows and set pointers to them */
202 t[nrl][ncl]=(float *) malloc((size_t)((nrow*ncol*ndep+NR_END)*sizeof(float)));
203 if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
204 t[nrl][ncl] += NR_END;
205 t[nrl][ncl] -= ndl;
206
207 for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
208 for(i=nrl+1;i<=nrh;i++) {
209 t[i]=t[i-1]+ncol;
210 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
211 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
212 }
213
214 /* return pointer to array of pointers to rows */
215 return t;
216}
217
218void free_vector(float *v, long nl, long nh)
219/* free a float vector allocated with vector() */
220{
221 free((FREE_ARG) (v+nl-NR_END));
222}
223
224void free_ivector(int *v, long nl, long nh)
225/* free an int vector allocated with ivector() */
226{
227 free((FREE_ARG) (v+nl-NR_END));
228}
229
230void free_cvector(unsigned char *v, long nl, long nh)
231/* free an unsigned char vector allocated with cvector() */
232{
233 free((FREE_ARG) (v+nl-NR_END));
234}
235
236void free_lvector(unsigned long *v, long nl, long nh)
237/* free an unsigned long vector allocated with lvector() */
238{
239 free((FREE_ARG) (v+nl-NR_END));
240}
241
242void free_dvector(double *v, long nl, long nh)
243/* free a double vector allocated with dvector() */
244{
245 free((FREE_ARG) (v+nl-NR_END));
246}
247
248void free_matrix(float **m, long nrl, long nrh, long ncl, long nch)
249/* free a float matrix allocated by matrix() */
250{
251 free((FREE_ARG) (m[nrl]+ncl-NR_END));
252 free((FREE_ARG) (m+nrl-NR_END));
253}
254
255void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
256/* free a double matrix allocated by dmatrix() */
257{
258 free((FREE_ARG) (m[nrl]+ncl-NR_END));
259 free((FREE_ARG) (m+nrl-NR_END));
260}
261
262void free_imatrix(int **m, long nrl, long nrh, long ncl, long nch)
263/* free an int matrix allocated by imatrix() */
264{
265 free((FREE_ARG) (m[nrl]+ncl-NR_END));
266 free((FREE_ARG) (m+nrl-NR_END));
267}
268
269void free_submatrix(float **b, long nrl, long nrh, long ncl, long nch)
270/* free a submatrix allocated by submatrix() */
271{
272 free((FREE_ARG) (b+nrl-NR_END));
273}
274
275void free_convert_matrix(float **b, long nrl, long nrh, long ncl, long nch)
276/* free a matrix allocated by convert_matrix() */
277{
278 free((FREE_ARG) (b+nrl-NR_END));
279}
280
281void free_f3tensor(float ***t, long nrl, long nrh, long ncl, long nch,
282 long ndl, long ndh)
283/* free a float f3tensor allocated by f3tensor() */
284{
285 free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
286 free((FREE_ARG) (t[nrl]+ncl-NR_END));
287 free((FREE_ARG) (t+nrl-NR_END));
288}
289
290#else /* ANSI */
291/* traditional - K&R */
292
293#include <stdio.h>
294#define NR_END 1
295#define FREE_ARG char*
296
297void nrerror(error_text)
298char error_text[];
299/* Numerical Recipes standard error handler */
300{
301 void exit();
302
303 fprintf(stderr,"Numerical Recipes run-time error...\n");
304 fprintf(stderr,"%s\n",error_text);
305 fprintf(stderr,"...now exiting to system...\n");
306 exit(1);
307}
308
309float *vector(nl,nh)
310long nh,nl;
311/* allocate a float vector with subscript range v[nl..nh] */
312{
313 float *v;
314
315 v=(float *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(float)));
316 if (!v) nrerror("allocation failure in vector()");
317 return v-nl+NR_END;
318}
319
320int *ivector(nl,nh)
321long nh,nl;
322/* allocate an int vector with subscript range v[nl..nh] */
323{
324 int *v;
325
326 v=(int *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(int)));
327 if (!v) nrerror("allocation failure in ivector()");
328 return v-nl+NR_END;
329}
330
331unsigned char *cvector(nl,nh)
332long nh,nl;
333/* allocate an unsigned char vector with subscript range v[nl..nh] */
334{
335 unsigned char *v;
336
337 v=(unsigned char *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(unsigned char)));
338 if (!v) nrerror("allocation failure in cvector()");
339 return v-nl+NR_END;
340}
341
342unsigned long *lvector(nl,nh)
343long nh,nl;
344/* allocate an unsigned long vector with subscript range v[nl..nh] */
345{
346 unsigned long *v;
347
348 v=(unsigned long *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(long)));
349 if (!v) nrerror("allocation failure in lvector()");
350 return v-nl+NR_END;
351}
352
353double *dvector(nl,nh)
354long nh,nl;
355/* allocate a double vector with subscript range v[nl..nh] */
356{
357 double *v;
358
359 v=(double *)malloc((unsigned int) ((nh-nl+1+NR_END)*sizeof(double)));
360 if (!v) nrerror("allocation failure in dvector()");
361 return v-nl+NR_END;
362}
363
364float **matrix(nrl,nrh,ncl,nch)
365long nch,ncl,nrh,nrl;
366/* allocate a float matrix with subscript range m[nrl..nrh][ncl..nch] */
367{
368 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
369 float **m;
370
371 /* allocate pointers to rows */
372 m=(float **) malloc((unsigned int)((nrow+NR_END)*sizeof(float*)));
373 if (!m) nrerror("allocation failure 1 in matrix()");
374 m += NR_END;
375 m -= nrl;
376
377 /* allocate rows and set pointers to them */
378 m[nrl]=(float *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float)));
379 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
380 m[nrl] += NR_END;
381 m[nrl] -= ncl;
382
383 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
384
385 /* return pointer to array of pointers to rows */
386 return m;
387}
388
389double **dmatrix(nrl,nrh,ncl,nch)
390long nch,ncl,nrh,nrl;
391/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
392{
393 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
394 double **m;
395
396 /* allocate pointers to rows */
397 m=(double **) malloc((unsigned int)((nrow+NR_END)*sizeof(double*)));
398 if (!m) nrerror("allocation failure 1 in matrix()");
399 m += NR_END;
400 m -= nrl;
401
402 /* allocate rows and set pointers to them */
403 m[nrl]=(double *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(double)));
404 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
405 m[nrl] += NR_END;
406 m[nrl] -= ncl;
407
408 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
409
410 /* return pointer to array of pointers to rows */
411 return m;
412}
413
414int **imatrix(nrl,nrh,ncl,nch)
415long nch,ncl,nrh,nrl;
416/* allocate a int matrix with subscript range m[nrl..nrh][ncl..nch] */
417{
418 long i, nrow=nrh-nrl+1,ncol=nch-ncl+1;
419 int **m;
420
421 /* allocate pointers to rows */
422 m=(int **) malloc((unsigned int)((nrow+NR_END)*sizeof(int*)));
423 if (!m) nrerror("allocation failure 1 in matrix()");
424 m += NR_END;
425 m -= nrl;
426
427
428 /* allocate rows and set pointers to them */
429 m[nrl]=(int *) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(int)));
430 if (!m[nrl]) nrerror("allocation failure 2 in matrix()");
431 m[nrl] += NR_END;
432 m[nrl] -= ncl;
433
434 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
435
436 /* return pointer to array of pointers to rows */
437 return m;
438}
439
440float **submatrix(a,oldrl,oldrh,oldcl,oldch,newrl,newcl)
441float **a;
442long newcl,newrl,oldch,oldcl,oldrh,oldrl;
443/* point a submatrix [newrl..][newcl..] to a[oldrl..oldrh][oldcl..oldch] */
444{
445 long i,j,nrow=oldrh-oldrl+1,ncol=oldcl-newcl;
446 float **m;
447
448 /* allocate array of pointers to rows */
449 m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
450 if (!m) nrerror("allocation failure in submatrix()");
451 m += NR_END;
452 m -= newrl;
453
454 /* set pointers to rows */
455 for(i=oldrl,j=newrl;i<=oldrh;i++,j++) m[j]=a[i]+ncol;
456
457 /* return pointer to array of pointers to rows */
458 return m;
459}
460
461float **convert_matrix(a,nrl,nrh,ncl,nch)
462float *a;
463long nch,ncl,nrh,nrl;
464/* allocate a float matrix m[nrl..nrh][ncl..nch] that points to the matrix
465declared in the standard C manner as a[nrow][ncol], where nrow=nrh-nrl+1
466and ncol=nch-ncl+1. The routine should be called with the address
467&a[0][0] as the first argument. */
468{
469 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1;
470 float **m;
471
472 /* allocate pointers to rows */
473 m=(float **) malloc((unsigned int) ((nrow+NR_END)*sizeof(float*)));
474 if (!m) nrerror("allocation failure in convert_matrix()");
475 m += NR_END;
476 m -= nrl;
477
478 /* set pointers to rows */
479 m[nrl]=a-ncl;
480 for(i=1,j=nrl+1;i<nrow;i++,j++) m[j]=m[j-1]+ncol;
481 /* return pointer to array of pointers to rows */
482 return m;
483}
484
485float ***f3tensor(nrl,nrh,ncl,nch,ndl,ndh)
486long nch,ncl,ndh,ndl,nrh,nrl;
487/* allocate a float 3tensor with range t[nrl..nrh][ncl..nch][ndl..ndh] */
488{
489 long i,j,nrow=nrh-nrl+1,ncol=nch-ncl+1,ndep=ndh-ndl+1;
490 float ***t;
491
492 /* allocate pointers to pointers to rows */
493 t=(float ***) malloc((unsigned int)((nrow+NR_END)*sizeof(float**)));
494 if (!t) nrerror("allocation failure 1 in f3tensor()");
495 t += NR_END;
496 t -= nrl;
497
498 /* allocate pointers to rows and set pointers to them */
499 t[nrl]=(float **) malloc((unsigned int)((nrow*ncol+NR_END)*sizeof(float*)));
500 if (!t[nrl]) nrerror("allocation failure 2 in f3tensor()");
501 t[nrl] += NR_END;
502 t[nrl] -= ncl;
503
504 /* allocate rows and set pointers to them */
505 t[nrl][ncl]=(float *) malloc((unsigned int)((nrow*ncol*ndep+NR_END)*sizeof(float)));
506 if (!t[nrl][ncl]) nrerror("allocation failure 3 in f3tensor()");
507 t[nrl][ncl] += NR_END;
508 t[nrl][ncl] -= ndl;
509
510 for(j=ncl+1;j<=nch;j++) t[nrl][j]=t[nrl][j-1]+ndep;
511 for(i=nrl+1;i<=nrh;i++) {
512 t[i]=t[i-1]+ncol;
513 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
514 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
515 }
516
517 /* return pointer to array of pointers to rows */
518 return t;
519}
520
521void free_vector(v,nl,nh)
522float *v;
523long nh,nl;
524/* free a float vector allocated with vector() */
525{
526 free((FREE_ARG) (v+nl-NR_END));
527}
528
529void free_ivector(v,nl,nh)
530int *v;
531long nh,nl;
532/* free an int vector allocated with ivector() */
533{
534 free((FREE_ARG) (v+nl-NR_END));
535}
536
537void free_cvector(v,nl,nh)
538long nh,nl;
539unsigned char *v;
540/* free an unsigned char vector allocated with cvector() */
541{
542 free((FREE_ARG) (v+nl-NR_END));
543}
544
545void free_lvector(v,nl,nh)
546long nh,nl;
547unsigned long *v;
548/* free an unsigned long vector allocated with lvector() */
549{
550 free((FREE_ARG) (v+nl-NR_END));
551}
552
553void free_dvector(v,nl,nh)
554double *v;
555long nh,nl;
556/* free a double vector allocated with dvector() */
557{
558 free((FREE_ARG) (v+nl-NR_END));
559}
560
561void free_matrix(m,nrl,nrh,ncl,nch)
562float **m;
563long nch,ncl,nrh,nrl;
564/* free a float matrix allocated by matrix() */
565{
566 free((FREE_ARG) (m[nrl]+ncl-NR_END));
567 free((FREE_ARG) (m+nrl-NR_END));
568}
569
570void free_dmatrix(m,nrl,nrh,ncl,nch)
571double **m;
572long nch,ncl,nrh,nrl;
573/* free a double matrix allocated by dmatrix() */
574{
575 free((FREE_ARG) (m[nrl]+ncl-NR_END));
576 free((FREE_ARG) (m+nrl-NR_END));
577}
578
579void free_imatrix(m,nrl,nrh,ncl,nch)
580int **m;
581long nch,ncl,nrh,nrl;
582/* free an int matrix allocated by imatrix() */
583{
584 free((FREE_ARG) (m[nrl]+ncl-NR_END));
585 free((FREE_ARG) (m+nrl-NR_END));
586}
587
588void free_submatrix(b,nrl,nrh,ncl,nch)
589float **b;
590long nch,ncl,nrh,nrl;
591/* free a submatrix allocated by submatrix() */
592{
593 free((FREE_ARG) (b+nrl-NR_END));
594}
595
596void free_convert_matrix(b,nrl,nrh,ncl,nch)
597float **b;
598long nch,ncl,nrh,nrl;
599/* free a matrix allocated by convert_matrix() */
600{
601 free((FREE_ARG) (b+nrl-NR_END));
602}
603
604void free_f3tensor(t,nrl,nrh,ncl,nch,ndl,ndh)
605float ***t;
606long nch,ncl,ndh,ndl,nrh,nrl;
607/* free a float f3tensor allocated by f3tensor() */
608{
609 free((FREE_ARG) (t[nrl][ncl]+ndl-NR_END));
610 free((FREE_ARG) (t[nrl]+ncl-NR_END));
611 free((FREE_ARG) (t+nrl-NR_END));
612}
613
614#endif /* ANSI */
Note: See TracBrowser for help on using the repository browser.