-
Notifications
You must be signed in to change notification settings - Fork 0
/
nrutil.c
executable file
·161 lines (133 loc) · 4.41 KB
/
nrutil.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#include "schna_ar_p.h"
void nrerror(char error_text[])
/* SCHNAaP standard error handler */
{
fprintf(stderr, "SCHNAaP run-time error: <%s>\n", error_text);
exit(1);
}
long *lvector(long nl, long nh)
/* allocate an long vector with subscript range v[nl..nh] */
{
long *v;
v = (long *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof(long)));
if (!v)
nrerror("allocation failure in lvector()");
return v - nl + NR_END;
}
char *cvector(long nl, long nh)
/* allocate an char vector with subscript range v[nl..nh] */
{
char *v;
v = (char *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof(char)));
if (!v)
nrerror("allocation failure in cvector()");
return v - nl + NR_END;
}
double *dvector(long nl, long nh)
/* allocate a double vector with subscript range v[nl..nh] */
{
double *v;
v = (double *) malloc((size_t) ((nh - nl + 1 + NR_END) * sizeof(double)));
if (!v)
nrerror("allocation failure in dvector()");
return v - nl + NR_END;
}
double **dmatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a double matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
double **m;
/* allocate pointers to rows */
m = (double **) malloc((size_t) ((nrow + NR_END) * sizeof(double *)));
if (!m)
nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl] = (double *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof(double)));
if (!m[nrl])
nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for (i = nrl + 1; i <= nrh; i++)
m[i] = m[i - 1] + ncol;
/* return pointer to array of pointers to rows */
return m;
}
long **lmatrix(long nrl, long nrh, long ncl, long nch)
/* allocate a long matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
long **m;
/* allocate pointers to rows */
m = (long **) malloc((size_t) ((nrow + NR_END) * sizeof(long *)));
if (!m)
nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl] = (long *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof(long)));
if (!m[nrl])
nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for (i = nrl + 1; i <= nrh; i++)
m[i] = m[i - 1] + ncol;
/* return pointer to array of pointers to rows */
return m;
}
char **cmatrix(long nrl, long nrh, long ncl, long nch)
/* allocate an char matrix with subscript range m[nrl..nrh][ncl..nch] */
{
long i, nrow = nrh - nrl + 1, ncol = nch - ncl + 1;
char **m;
/* allocate pointers to rows */
m = (char **) malloc((size_t) ((nrow + NR_END) * sizeof(char *)));
if (!m)
nrerror("allocation failure 1 in matrix()");
m += NR_END;
m -= nrl;
/* allocate rows and set pointers to them */
m[nrl] = (char *) malloc((size_t) ((nrow * ncol + NR_END) * sizeof(char)));
if (!m[nrl])
nrerror("allocation failure 2 in matrix()");
m[nrl] += NR_END;
m[nrl] -= ncl;
for (i = nrl + 1; i <= nrh; i++)
m[i] = m[i - 1] + ncol;
/* return pointer to array of pointers to rows */
return m;
}
void free_lvector(long *v, long nl, long nh)
/* free an long vector allocated with lvector() */
{
free((FREE_ARG) (v + nl - NR_END));
}
void free_cvector(char *v, long nl, long nh)
/* free an char vector allocated with cvector() */
{
free((FREE_ARG) (v + nl - NR_END));
}
void free_dvector(double *v, long nl, long nh)
/* free a double vector allocated with dvector() */
{
free((FREE_ARG) (v + nl - NR_END));
}
void free_dmatrix(double **m, long nrl, long nrh, long ncl, long nch)
/* free a double matrix allocated by dmatrix() */
{
free((FREE_ARG) (m[nrl] + ncl - NR_END));
free((FREE_ARG) (m + nrl - NR_END));
}
void free_lmatrix(long **m, long nrl, long nrh, long ncl, long nch)
/* free an long matrix allocated by lmatrix() */
{
free((FREE_ARG) (m[nrl] + ncl - NR_END));
free((FREE_ARG) (m + nrl - NR_END));
}
void free_cmatrix(char **m, long nrl, long nrh, long ncl, long nch)
/* free an char matrix allocated by cmatrix() */
{
free((FREE_ARG) (m[nrl] + ncl - NR_END));
free((FREE_ARG) (m + nrl - NR_END));
}