1 | /* |
---|
2 | * matrixut.c - numerical matrix utility |
---|
3 | * Ver. 1.2 Aug 26, 1993 Adachi, J. |
---|
4 | * |
---|
5 | * Copyright (C) 1992, 1993 J. Adachi & M. Hasegawa, All rights reserved. |
---|
6 | */ |
---|
7 | |
---|
8 | #ifndef MATRIX_UTILITY |
---|
9 | #define MATRIX_UTILITY |
---|
10 | |
---|
11 | #include <stdio.h> |
---|
12 | #include <stddef.h> |
---|
13 | |
---|
14 | #include "matrixut.h" |
---|
15 | |
---|
16 | void |
---|
17 | maerror(message) |
---|
18 | char *message; |
---|
19 | /* memory allocation error handler */ |
---|
20 | { |
---|
21 | fprintf(stderr, "\nmemory allocation failure %s\n", message); |
---|
22 | exit(1); |
---|
23 | } |
---|
24 | |
---|
25 | /* |
---|
26 | * float matrix utility |
---|
27 | */ |
---|
28 | |
---|
29 | fvector |
---|
30 | new_fvector(n) |
---|
31 | int n; |
---|
32 | /* memory allocate a float vector */ |
---|
33 | { |
---|
34 | fvector v; |
---|
35 | |
---|
36 | v = (fvector) malloc((unsigned) (n * sizeof(float))); |
---|
37 | if (v == NULL) maerror("in fvector()."); |
---|
38 | return v; |
---|
39 | } |
---|
40 | |
---|
41 | fmatrix |
---|
42 | new_fmatrix(nrow, ncol) |
---|
43 | int nrow; |
---|
44 | int ncol; |
---|
45 | /* memory allocate a float matrix */ |
---|
46 | { |
---|
47 | int i; |
---|
48 | fmatrix m; |
---|
49 | |
---|
50 | m = (fmatrix) malloc((unsigned) (nrow * sizeof(fvector))); |
---|
51 | if (m == NULL) maerror("1 in fmatrix()."); |
---|
52 | *m = (fvector) malloc((unsigned) (nrow * ncol * sizeof(float))); |
---|
53 | if (*m == NULL) maerror("2 in fmatrix()."); |
---|
54 | for (i = 1; i < nrow; i++) m[i] = m[i-1] + ncol; |
---|
55 | return m; |
---|
56 | } |
---|
57 | |
---|
58 | fcube |
---|
59 | new_fcube(ntri, nrow, ncol) |
---|
60 | int ntri; |
---|
61 | int nrow; |
---|
62 | int ncol; |
---|
63 | /* memory allocate a float cube */ |
---|
64 | { |
---|
65 | int i, j; |
---|
66 | fcube c; |
---|
67 | |
---|
68 | c = (fcube) malloc((unsigned) (ntri * sizeof(fmatrix))); |
---|
69 | if (c == NULL) maerror("1 in fcube()."); |
---|
70 | *c = (fmatrix) malloc((unsigned) (ntri * nrow * sizeof(fvector))); |
---|
71 | if (*c == NULL) maerror("2 in fcube()."); |
---|
72 | **c = (fvector) malloc((unsigned) (ntri * nrow * ncol * sizeof(float))); |
---|
73 | if (**c == NULL) maerror("3 in fcube()."); |
---|
74 | for (j = 1; j < nrow; j++) c[0][j] = c[0][j-1] + ncol; |
---|
75 | for (i = 1; i < ntri; i++) { |
---|
76 | c[i] = c[i-1] + nrow; |
---|
77 | c[i][0] = c[i-1][0] + nrow * ncol; |
---|
78 | for (j = 1; j < nrow; j++) c[i][j] = c[i][j-1] + ncol; |
---|
79 | } |
---|
80 | return c; |
---|
81 | } |
---|
82 | |
---|
83 | void |
---|
84 | free_fvector(v) |
---|
85 | fvector v; |
---|
86 | { |
---|
87 | free((char *) v); |
---|
88 | } |
---|
89 | |
---|
90 | void |
---|
91 | free_fmatrix(m) |
---|
92 | fmatrix m; |
---|
93 | { |
---|
94 | free((char *) *m); |
---|
95 | free((char *) m); |
---|
96 | } |
---|
97 | |
---|
98 | void |
---|
99 | free_fcube(c) |
---|
100 | fcube c; |
---|
101 | { |
---|
102 | free((char *) **c); |
---|
103 | free((char *) *c); |
---|
104 | free((char *) c); |
---|
105 | } |
---|
106 | |
---|
107 | |
---|
108 | /* |
---|
109 | * double matrix utility |
---|
110 | */ |
---|
111 | |
---|
112 | dvector |
---|
113 | new_dvector(n) |
---|
114 | int n; |
---|
115 | /* memory allocate a double vector */ |
---|
116 | { |
---|
117 | dvector v; |
---|
118 | |
---|
119 | v = (dvector) malloc((unsigned) (n * sizeof(double))); |
---|
120 | if (v == NULL) maerror("in dvector()."); |
---|
121 | return v; |
---|
122 | } |
---|
123 | |
---|
124 | dmatrix |
---|
125 | new_dmatrix(nrow, ncol) |
---|
126 | int nrow; |
---|
127 | int ncol; |
---|
128 | /* memory allocate a double matrix */ |
---|
129 | { |
---|
130 | int i; |
---|
131 | dmatrix m; |
---|
132 | |
---|
133 | m = (dmatrix) malloc((unsigned) (nrow * sizeof(dvector))); |
---|
134 | if (m == NULL) maerror("1 in dmatrix()."); |
---|
135 | *m = (dvector) malloc((unsigned) (nrow * ncol * sizeof(double))); |
---|
136 | if (*m == NULL) maerror("2 in dmatrix()."); |
---|
137 | for (i = 1; i < nrow; i++) m[i] = m[i-1] + ncol; |
---|
138 | return m; |
---|
139 | } |
---|
140 | |
---|
141 | dcube |
---|
142 | new_dcube(ntri, nrow, ncol) |
---|
143 | int ntri; |
---|
144 | int nrow; |
---|
145 | int ncol; |
---|
146 | /* memory allocate a double cube */ |
---|
147 | { |
---|
148 | int i, j; |
---|
149 | dcube c; |
---|
150 | |
---|
151 | c = (dcube) malloc((unsigned) (ntri * sizeof(dmatrix))); |
---|
152 | if (c == NULL) maerror("1 in dcube()."); |
---|
153 | *c = (dmatrix) malloc((unsigned) (ntri * nrow * sizeof(dvector))); |
---|
154 | if (*c == NULL) maerror("2 in dcube()."); |
---|
155 | **c = (dvector) malloc((unsigned) (ntri * nrow * ncol * sizeof(double))); |
---|
156 | if (**c == NULL) maerror("3 in dcube()."); |
---|
157 | for (j = 1; j < nrow; j++) c[0][j] = c[0][j-1] + ncol; |
---|
158 | for (i = 1; i < ntri; i++) { |
---|
159 | c[i] = c[i-1] + nrow; |
---|
160 | c[i][0] = c[i-1][0] + nrow * ncol; |
---|
161 | for (j = 1; j < nrow; j++) c[i][j] = c[i][j-1] + ncol; |
---|
162 | } |
---|
163 | return c; |
---|
164 | } |
---|
165 | |
---|
166 | void |
---|
167 | free_dvector(v) |
---|
168 | dvector v; |
---|
169 | { |
---|
170 | free((char *) v); |
---|
171 | } |
---|
172 | |
---|
173 | void |
---|
174 | free_dmatrix(m) |
---|
175 | dmatrix m; |
---|
176 | { |
---|
177 | free((char *) *m); |
---|
178 | free((char *) m); |
---|
179 | } |
---|
180 | |
---|
181 | void |
---|
182 | free_dcube(c) |
---|
183 | dcube c; |
---|
184 | { |
---|
185 | free((char *) **c); |
---|
186 | free((char *) *c); |
---|
187 | free((char *) c); |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | /* |
---|
192 | * char matrix utility |
---|
193 | */ |
---|
194 | |
---|
195 | cvector |
---|
196 | new_cvector(n) |
---|
197 | int n; |
---|
198 | /* memory allocate a char vector */ |
---|
199 | { |
---|
200 | cvector v; |
---|
201 | |
---|
202 | v = (cvector) malloc((unsigned)n * sizeof(char)); |
---|
203 | if (v == NULL) maerror("in cvector()."); |
---|
204 | return v; |
---|
205 | } |
---|
206 | |
---|
207 | cmatrix |
---|
208 | new_cmatrix(nrow, ncol) |
---|
209 | int nrow; |
---|
210 | int ncol; |
---|
211 | /* memory allocate a char matrix */ |
---|
212 | { |
---|
213 | int i; |
---|
214 | cmatrix m; |
---|
215 | |
---|
216 | m = (cmatrix) malloc((unsigned) (nrow * sizeof(cvector))); |
---|
217 | if (m == NULL) maerror("1 in cmatrix()."); |
---|
218 | *m = (cvector) malloc((unsigned) (nrow * ncol * sizeof(char))); |
---|
219 | if (*m == NULL) maerror("2 in cmatrix()."); |
---|
220 | for (i = 1; i < nrow; i++) m[i] = m[i-1] + ncol; |
---|
221 | return m; |
---|
222 | } |
---|
223 | |
---|
224 | ccube |
---|
225 | new_ccube(ntri, nrow, ncol) |
---|
226 | int ntri; |
---|
227 | int nrow; |
---|
228 | int ncol; |
---|
229 | /* memory allocate a char cube */ |
---|
230 | { |
---|
231 | int i, j; |
---|
232 | ccube c; |
---|
233 | |
---|
234 | c = (ccube) malloc((unsigned) (ntri * sizeof(cmatrix))); |
---|
235 | if (c == NULL) maerror("1 in ccube()."); |
---|
236 | *c = (cmatrix) malloc((unsigned) (ntri * nrow * sizeof(cvector))); |
---|
237 | if (*c == NULL) maerror("2 in ccube()."); |
---|
238 | **c = (cvector) malloc((unsigned) (ntri * nrow * ncol * sizeof(char))); |
---|
239 | if (**c == NULL) maerror("3 in ccube()."); |
---|
240 | for (j = 1; j < nrow; j++) c[0][j] = c[0][j-1] + ncol; |
---|
241 | for (i = 1; i < ntri; i++) { |
---|
242 | c[i] = c[i-1] + nrow; |
---|
243 | c[i][0] = c[i-1][0] + nrow * ncol; |
---|
244 | for (j = 1; j < nrow; j++) c[i][j] = c[i][j-1] + ncol; |
---|
245 | } |
---|
246 | return c; |
---|
247 | } |
---|
248 | |
---|
249 | void |
---|
250 | free_cvector(v) |
---|
251 | cvector v; |
---|
252 | { |
---|
253 | free((char *) v); |
---|
254 | } |
---|
255 | |
---|
256 | void |
---|
257 | free_cmatrix(m) |
---|
258 | cmatrix m; |
---|
259 | { |
---|
260 | free((char *) *m); |
---|
261 | free((char *) m); |
---|
262 | } |
---|
263 | |
---|
264 | void |
---|
265 | free_ccube(c) |
---|
266 | ccube c; |
---|
267 | { |
---|
268 | free((char *) **c); |
---|
269 | free((char *) *c); |
---|
270 | free((char *) c); |
---|
271 | } |
---|
272 | |
---|
273 | |
---|
274 | /* |
---|
275 | * int matrix utility |
---|
276 | */ |
---|
277 | |
---|
278 | ivector |
---|
279 | new_ivector(n) |
---|
280 | int n; |
---|
281 | /* memory allocate a int vector */ |
---|
282 | { |
---|
283 | ivector v; |
---|
284 | |
---|
285 | v = (ivector) malloc((unsigned) (n * sizeof(int))); |
---|
286 | if (v == NULL) maerror("in ivector()."); |
---|
287 | return v; |
---|
288 | } |
---|
289 | |
---|
290 | imatrix |
---|
291 | new_imatrix(nrow, ncol) |
---|
292 | int nrow; |
---|
293 | int ncol; |
---|
294 | /* memory allocate a int matrix */ |
---|
295 | { |
---|
296 | int i; |
---|
297 | imatrix m; |
---|
298 | |
---|
299 | m = (imatrix) malloc((unsigned) (nrow * sizeof(ivector))); |
---|
300 | if (m == NULL) maerror("1 in imatrix()."); |
---|
301 | *m = (ivector) malloc((unsigned) (nrow * ncol * sizeof(int))); |
---|
302 | if (*m == NULL) maerror("2 in imatrix()."); |
---|
303 | for (i = 1; i < nrow; i++) m[i] = m[i-1] + ncol; |
---|
304 | return m; |
---|
305 | } |
---|
306 | |
---|
307 | icube |
---|
308 | new_icube(ntri, nrow, ncol) |
---|
309 | int ntri; |
---|
310 | int nrow; |
---|
311 | int ncol; |
---|
312 | /* memory allocate a int cube */ |
---|
313 | { |
---|
314 | int i, j; |
---|
315 | icube c; |
---|
316 | |
---|
317 | c = (icube) malloc((unsigned) (ntri * sizeof(imatrix))); |
---|
318 | if (c == NULL) maerror("1 in icube()."); |
---|
319 | *c = (imatrix) malloc((unsigned) (ntri * nrow * sizeof(ivector))); |
---|
320 | if (*c == NULL) maerror("2 in icube()."); |
---|
321 | **c = (ivector) malloc((unsigned) (ntri * nrow * ncol * sizeof(int))); |
---|
322 | if (**c == NULL) maerror("3 in icube()."); |
---|
323 | for (j = 1; j < nrow; j++) c[0][j] = c[0][j-1] + ncol; |
---|
324 | for (i = 1; i < ntri; i++) { |
---|
325 | c[i] = c[i-1] + nrow; |
---|
326 | c[i][0] = c[i-1][0] + nrow * ncol; |
---|
327 | for (j = 1; j < nrow; j++) c[i][j] = c[i][j-1] + ncol; |
---|
328 | } |
---|
329 | return c; |
---|
330 | } |
---|
331 | |
---|
332 | void |
---|
333 | free_ivector(v) |
---|
334 | ivector v; |
---|
335 | { |
---|
336 | free((char *) v); |
---|
337 | } |
---|
338 | |
---|
339 | void |
---|
340 | free_imatrix(m) |
---|
341 | imatrix m; |
---|
342 | { |
---|
343 | free((char *) *m); |
---|
344 | free((char *) m); |
---|
345 | } |
---|
346 | |
---|
347 | void |
---|
348 | free_icube(c) |
---|
349 | icube c; |
---|
350 | { |
---|
351 | free((char *) **c); |
---|
352 | free((char *) *c); |
---|
353 | free((char *) c); |
---|
354 | } |
---|
355 | |
---|
356 | |
---|
357 | #endif /* MATRIX_UTILITY */ |
---|