1 /* corematx.c
2 * ===========================================================================
3 *
4 * PUBLIC DOMAIN NOTICE
5 * National Center for Biotechnology Information
6 *
7 * This software/database is a "United States Government Work" under the
8 * terms of the United States Copyright Act. It was written as part of
9 * the author's official duties as a United States Government employee and
10 * thus cannot be copyrighted. This software/database is freely available
11 * to the public for use. The National Library of Medicine and the U.S.
12 * Government have not placed any restriction on its use or reproduction.
13 *
14 * Although all reasonable efforts have been taken to ensure the accuracy
15 * and reliability of the software and data, the NLM and the U.S.
16 * Government do not and cannot warrant the performance or results that
17 * may be obtained by using this software or data. The NLM and the U.S.
18 * Government disclaim all warranties, express or implied, including
19 * warranties of performance, merchantability or fitness for any particular
20 * purpose.
21 *
22 * Please cite the author in any work or product based on this material.
23 *
24 * ===========================================================================
25 *
26 * File Name: corematx.c
27 *
28 * Author: Hogue
29 *
30 * Version Creation Date: 25 JULY 95
31 *
32 * $Revision: 6.0 $
33 *
34 * File Description:
35 * Numerical Recipies in C toolkit-ized
36 * matrix and vector allocators and free-ers
37 * see corematx.c for implementation docs.
38 *
39 * Modifications:
40 * --------------------------------------------------------------------------
41 * Date Name Description of modification
42 * ------- ---------- -----------------------------------------------------
43 * 16 Aug Hogue Added a vector of pointers.
44 *
45 * $Log: corematx.c,v $
46 * Revision 6.0 1997/08/25 18:10:30 madden
47 * Revision changed to 6.0
48 *
49 * Revision 5.0 1996/05/28 14:02:09 ostell
50 * Set to revision 5.0
51 *
52 * Revision 1.3 1995/08/28 19:31:53 kans
53 * version 4.1
54 *
55 * Revision 1.2 1995/08/03 21:44:33 kans
56 * changes
57 *
58 * Revision 1.1 1995/08/02 17:07:15 kans
59 * Initial revision
60 *
61 *
62 * ==========================================================================
63 */
64 /*****************************************************************************
65 *
66 * corematx.c
67 *
68 * vector and matrix allocation and free-ing.
69 * address a vector like a Fortran [1:n] or like C [0:n-1]
70 *
71 * CoreLib -ized matrix routines from NUMERICAL RECIPIES IN C
72 * 1st Edition, Appendix D.
73 * with the 2nd Edition, the NR_END value is used to ANSI-ize this...
74 *
75 * Use this to replace the files "nrutil.h" and "nrutil.c" if you want
76 * NCBI-TOOLKIT compatible Numerical Recipies code.
77 * You still have to replace NR code calls to C function with
78 * TOOLKIT macros, however, but these are not too critical for
79 * most compilers supported by toolkit.
80 *
81 * There is STILL no check for out-of bounds with these routines.
82 * as with any C matrix. The advantage of these routines is that
83 * you can allocate 2-D matricies with -any size- and with
84 * - any indices, e.g I2Vector(1, 5) gives a
85 * pointer to an array starting with index 1 ending with index 5, 5 elements
86 * mymatrix = FHMatrix(1, 10, 1, 3) gets a matrix of double
87 * you can address from
88 * [1:10][1:3] with 30 elements.
89 * to free, call FHMatrixFree(mymatrix, 1, 1); args are the starting
90 * indicies of each dimension.
91 *
92 *
93 * a Matrix is allocated as a CONTIGUOUS BLOCK OF MEMORY!
94 * Vector allocators and free-ers for:
95 * xx =I2 Int2
96 * =I4 Int4
97 * =FL FloatLo -
98 * =FH FloatHi
99 * =UC Uchar
100 * =UI4 Uint4
101 *
102 * Matrix allocators and free-ers for:
103 * xx = I2, I4, FL, FH,
104 * FL3Matrix()
105 * allocates a 3-way matrix with range with FloatLo data.
106 * t[nrl:nrh][ncl:nch][ndl:ndh]
107 *
108 * The following are untested:
109 *
110 * xxConvtMatrix()
111 * Allocates a matrix m[nrl..nrh][ncl..nch] that points to the standard C matrix
112 * a[nrow][ncol], where nrow=nrh-nrl+1 and ncol=nch-ncl+1. The routine
113 * should be called with the address &a[0][0] as the first argument
114 * There are I2, I4, FL and FH ConvtMatrix() routines.
115 *
116 * FLSubmatrix() - returns a FL submatrix with range
117 * [newrl..newrl+(oldrh-oldrl][newcl..newcl+(oldch-oldcl)] which
118 * points to the EXISTING matrix range [oldrl..oldrh][oldcl..oldch]
119 * free with FLSubmatrixFree()
120 * no other submatrix calls presently other than for FL
121 *
122 * Note at the bottom are "wrappers" declared for using unmodified
123 * Numerical Recipies
124 * code calls, just define NRTONCBI and compile.
125 *
126 * Standard NCBI disclaimer for US Government Software applies.
127 * programmer C.W.V. Hogue
128 * created: 14 Mar 95
129 * last mod: 20 Mar 95
130 * TESTED: 20 Mar 95 - all but xxConvtMatrix() FLSubmatrix() tested.
131 *
132 **********/
133
134
135 #include <ncbi.h>
136 #include <corematx.h>
137 #define NR_END 1
138
139
140
141
FLVector(Int4 nl,Int4 nh)142 FloatLoPtr LIBCALL FLVector(Int4 nl, Int4 nh)
143 {
144 FloatLoPtr v = NULL;
145 v=(FloatLoPtr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(FloatLo)));
146 if (!v)
147 {
148 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in FLVector");
149 ErrShow();
150 return NULL;
151 }
152 return (FloatLoPtr)(v-nl+NR_END);
153 }
154
155
I2Vector(Int4 nl,Int4 nh)156 Int2Ptr LIBCALL I2Vector(Int4 nl, Int4 nh)
157 {
158 Int2Ptr v = NULL;
159 v=(Int2Ptr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(Int2)));
160 if (!v)
161 {
162 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in I2Vector");
163 ErrShow();
164 return NULL;
165 }
166 return (Int2Ptr)(v-nl+NR_END);
167 }
168
169
I4Vector(Int4 nl,Int4 nh)170 Int4Ptr LIBCALL I4Vector(Int4 nl, Int4 nh)
171 {
172 Int4Ptr v = NULL;
173 v=(Int4Ptr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(Int4)));
174 if (!v)
175 {
176 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in I4Vector");
177 ErrShow();
178 return NULL;
179 }
180 return (Int4Ptr)(v-nl+NR_END);
181 }
182
FHVector(Int4 nl,Int4 nh)183 FloatHiPtr LIBCALL FHVector(Int4 nl, Int4 nh)
184 {
185 FloatHiPtr v = NULL;
186 v=(FloatHiPtr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(FloatHi)));
187 if (!v)
188 {
189 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in FHVector");
190 ErrShow();
191 return NULL;
192 }
193 return (FloatHiPtr)(v-nl+NR_END);
194 }
195
UCVector(Int4 nl,Int4 nh)196 UcharPtr LIBCALL UCVector(Int4 nl, Int4 nh)
197 {
198 UcharPtr v = NULL;
199 v=(UcharPtr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(Uchar)));
200 if (!v)
201 {
202 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in UCVector");
203 ErrShow();
204 return NULL;
205 }
206 return (UcharPtr)(v-nl+NR_END);
207 }
208
209
UI4Vector(Int4 nl,Int4 nh)210 Uint4Ptr LIBCALL UI4Vector(Int4 nl, Int4 nh)
211 {
212 Uint4Ptr v = NULL;
213 v=(Uint4Ptr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(Uint4)));
214 if (!v)
215 {
216 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in UI4Vector");
217 ErrShow();
218 return NULL;
219 }
220 return (Uint4Ptr)(v-nl+NR_END);
221 }
222
223
PTRVector(Int4 nl,Int4 nh)224 PointerPtr LIBCALL PTRVector(Int4 nl, Int4 nh)
225 {
226 PointerPtr v = NULL;
227 v=(PointerPtr)MemNew((size_t)((nh-nl+1+NR_END)*sizeof(Pointer)));
228 if (!v)
229 {
230 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in PTRVector");
231 ErrShow();
232 return NULL;
233 }
234 return (PointerPtr)(v-nl+NR_END);
235 }
236
237
238
239
FLMatrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)240 FloatLoPtrPtr LIBCALL FLMatrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
241 {
242 Int4 i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
243 FloatLoPtr *m = NULL;
244
245 m=(FloatLoPtr *)MemNew((size_t)(nrow+NR_END)*sizeof(FloatLoPtr));
246 if (!m)
247 {
248 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 1 in FLMatrix");
249 ErrShow();
250 return NULL;
251 }
252 m += NR_END;
253 m -= nrl;
254 m[nrl] = (FloatLoPtr)MemNew((size_t)((nrow*ncol+NR_END)*sizeof(FloatLo)));
255 if (!m[nrl])
256 {
257 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 2 in FLMatrix");
258 ErrShow();
259 return NULL;
260 }
261 m[nrl] += NR_END;
262 m[nrl] -= ncl;
263 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
264 return (FloatLoPtrPtr) m;
265 }
266
267
268
FHMatrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)269 FloatHiPtrPtr LIBCALL FHMatrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
270 {
271 Int4 i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
272 FloatHiPtr *m = NULL;
273
274 m=(FloatHiPtr *)MemNew((size_t)(nrow+NR_END)*sizeof(FloatHiPtr));
275 if (!m)
276 {
277 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 1 in FHMatrix");
278 ErrShow();
279 return NULL;
280 }
281 m += NR_END;
282 m -= nrl;
283 m[nrl] = (FloatHiPtr)MemNew((size_t)((nrow*ncol+NR_END)*sizeof(FloatHi)));
284 if (!m[nrl])
285 {
286 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 2 in FHMatrix");
287 ErrShow();
288 return NULL;
289 }
290 m[nrl] += NR_END;
291 m[nrl] -= ncl;
292 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
293 return (FloatHiPtrPtr) m;
294 }
295
296
297
I2Matrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)298 Int2PtrPtr LIBCALL I2Matrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
299 {
300 Int2 i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
301 Int2Ptr *m = NULL;
302
303 m=(Int2Ptr *)MemNew((size_t)(nrow+NR_END)*sizeof(Int2Ptr));
304 if (!m)
305 {
306 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 1 in I2Matrix");
307 ErrShow();
308 return NULL;
309 }
310 m += NR_END;
311 m -= nrl;
312 m[nrl] = (Int2Ptr)MemNew((size_t)((nrow*ncol+NR_END)*sizeof(Int2)));
313 if (!m[nrl])
314 {
315 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 2 in I2Matrix");
316 ErrShow();
317 return NULL;
318 }
319 m[nrl] += NR_END;
320 m[nrl] -= ncl;
321 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
322 return (Int2PtrPtr) m;
323 }
324
325
I4Matrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)326 Int4PtrPtr LIBCALL I4Matrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
327 {
328 Int4 i, nrow=nrh-nrl+1, ncol=nch-ncl+1;
329 Int4Ptr *m = NULL;
330
331 m=(Int4Ptr *)MemNew((size_t)(nrow+NR_END)*sizeof(Int4Ptr));
332 if (!m)
333 {
334 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 1 in I4Matrix");
335 ErrShow();
336 return NULL;
337 }
338 m += NR_END;
339 m -= nrl;
340 m[nrl] = (Int4Ptr)MemNew((size_t)((nrow*ncol+NR_END)*sizeof(Int4)));
341 if (!m[nrl])
342 {
343 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 2 in I4Matrix");
344 ErrShow();
345 return NULL;
346 }
347 m[nrl] += NR_END;
348 m[nrl] -= ncl;
349 for(i=nrl+1;i<=nrh;i++) m[i]=m[i-1]+ncol;
350 return (Int4PtrPtr) m;
351 }
352
FLSubmatrix(FloatLoPtr * a,Int4 oldrl,Int4 oldrh,Int4 oldcl,Int4 oldch,Int4 newrl,Int4 newcl)353 FloatLoPtrPtr LIBCALL FLSubmatrix(FloatLoPtr *a, Int4 oldrl, Int4 oldrh,
354 Int4 oldcl, Int4 oldch, Int4 newrl, Int4 newcl)
355 {
356 Int4 i, j, nrow=oldrh-oldrl+1, ncol=oldcl-newcl;
357 FloatLoPtr *m = NULL;
358 m=(FloatLoPtr *)MemNew((size_t)((nrow+NR_END)*sizeof(FloatLoPtr)));
359 if (!m)
360 {
361 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory in FLSubmatrix");
362 ErrShow();
363 return NULL;
364 }
365 m += NR_END;
366 m -= newrl;
367 for(i=oldrl, j=newrl; i<=oldrh; i++, j++) m[j]=a[i]+ncol;
368 return (FloatLoPtrPtr) m;
369 }
370
371
FLSubmatrixFree(FloatLoPtr * b,Int4 nrl)372 void LIBCALL FLSubmatrixFree(FloatLoPtr *b, Int4 nrl)
373 {
374 MemFree((CharPtr)(b+nrl-NR_END));
375 }
376
FHVectorFree(FloatHiPtr v,Int4 nl)377 void LIBCALL FHVectorFree(FloatHiPtr v, Int4 nl)
378 {
379 MemFree((CharPtr) (v+nl-NR_END));
380 }
381
FLVectorFree(FloatLoPtr v,Int4 nl)382 void LIBCALL FLVectorFree(FloatLoPtr v, Int4 nl)
383 {
384 MemFree((CharPtr)(v+nl-NR_END));
385 }
386
I4VectorFree(Int4Ptr v,Int4 nl)387 void LIBCALL I4VectorFree(Int4Ptr v, Int4 nl)
388 {
389 MemFree((CharPtr)(v+nl-NR_END));
390 }
391
I2VectorFree(Int2Ptr v,Int4 nl)392 void LIBCALL I2VectorFree(Int2Ptr v, Int4 nl)
393 {
394 MemFree((Int2Ptr)(v+nl-NR_END));
395 }
396
UCVectorFree(UcharPtr v,Int4 nl)397 void LIBCALL UCVectorFree(UcharPtr v, Int4 nl)
398 {
399 MemFree((CharPtr)(v+nl-NR_END));
400 }
401
UI4VectorFree(Uint4Ptr v,Int4 nl)402 void LIBCALL UI4VectorFree(Uint4Ptr v, Int4 nl)
403 {
404 MemFree((CharPtr)(v+nl-NR_END));
405 }
406
PTRVectorFree(PointerPtr v,Int4 nl)407 void LIBCALL PTRVectorFree(PointerPtr v, Int4 nl)
408 {
409 MemFree((CharPtr)(v+nl-NR_END));
410 }
411
I2MatrixFree(Int2Ptr * m,Int4 nrl,Int4 ncl)412 void LIBCALL I2MatrixFree(Int2Ptr *m, Int4 nrl, Int4 ncl)
413 {
414 MemFree((CharPtr)(m[nrl]+ncl-NR_END));
415 MemFree((CharPtr)(m+nrl-NR_END));
416 }
417
418
I4MatrixFree(Int4Ptr * m,Int4 nrl,Int4 ncl)419 void LIBCALL I4MatrixFree(Int4Ptr *m, Int4 nrl, Int4 ncl)
420 {
421 MemFree((CharPtr)(m[nrl]+ncl-NR_END));
422 MemFree((CharPtr)(m+nrl-NR_END));
423 }
424
425
426
FLMatrixFree(FloatLoPtr * m,Int4 nrl,Int4 ncl)427 void LIBCALL FLMatrixFree(FloatLoPtr *m, Int4 nrl, Int4 ncl)
428 {
429 MemFree((CharPtr)(m[nrl]+ncl-NR_END));
430 MemFree((CharPtr)(m+nrl-NR_END));
431 }
432
433
FHMatrixFree(FloatHiPtr * m,Int4 nrl,Int4 ncl)434 void LIBCALL FHMatrixFree(FloatHiPtr *m, Int4 nrl, Int4 ncl)
435 {
436 MemFree((CharPtr)(m[nrl]+ncl-NR_END));
437 MemFree((CharPtr)(m+nrl-NR_END));
438 }
439
440
FHConvtMatrix(FloatHiPtr a,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)441 FloatHiPtrPtr LIBCALL FHConvtMatrix(FloatHiPtr a, Int4 nrl, Int4 nrh, Int4 ncl,
442 Int4 nch)
443 {
444 Int4 i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1;
445 FloatHiPtr *m = NULL;
446 m=(FloatHiPtr *)MemNew((size_t)((nrow+NR_END)*sizeof(FloatHiPtr)));
447 if (!m)
448 {
449 ErrPostEx(SEV_FATAL, 0, 0, "Out of memory in FHConvtMatrix");
450 ErrShow();
451 return NULL;
452 }
453 m += NR_END;
454 m -= nrl;
455 m[nrl]=a-ncl;
456 for(i=1, j=nrl+1;i<nrow;i++,j++)
457 m[j] = m[j-1]+ncol;
458 return (FloatHiPtrPtr) m;
459 }
460
461
FLConvtMatrix(FloatLoPtr a,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)462 FloatLoPtrPtr LIBCALL FLConvtMatrix(FloatLoPtr a, Int4 nrl, Int4 nrh, Int4 ncl,
463 Int4 nch)
464 {
465 Int4 i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1;
466 FloatLoPtr *m = NULL;
467 m=(FloatLoPtr *)MemNew((size_t)((nrow+NR_END)*sizeof(FloatLoPtr)));
468 if (!m)
469 {
470 ErrPostEx(SEV_FATAL, 0, 0, "Out of memory in FLConvtMatrix");
471 ErrShow();
472 return NULL;
473 }
474 m += NR_END;
475 m -= nrl;
476 m[nrl]=a-ncl;
477 for(i=1, j=nrl+1;i<nrow;i++,j++)
478 m[j] = m[j-1]+ncol;
479 return (FloatLoPtrPtr) m;
480 }
481
I2ConvtMatrix(Int2Ptr a,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)482 Int2PtrPtr LIBCALL I2ConvtMatrix(Int2Ptr a, Int4 nrl, Int4 nrh, Int4 ncl,
483 Int4 nch)
484 {
485 Int4 i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1;
486 Int2Ptr *m = NULL;
487 m=(Int2Ptr *)MemNew((size_t)((nrow+NR_END)*sizeof(Int2Ptr)));
488 if (!m)
489 {
490 ErrPostEx(SEV_FATAL, 0, 0, "Out of memory in I2ConvtMatrix");
491 ErrShow();
492 return NULL;
493 }
494 m += NR_END;
495 m -= nrl;
496 m[nrl]=a-ncl;
497 for(i=1, j=nrl+1;i<nrow;i++,j++)
498 m[j] = m[j-1]+ncol;
499 return (Int2PtrPtr) m;
500 }
501
502
I4ConvtMatrix(Int4Ptr a,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)503 Int4PtrPtr LIBCALL I4ConvtMatrix(Int4Ptr a, Int4 nrl, Int4 nrh, Int4 ncl,
504 Int4 nch)
505 {
506 Int4 i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1;
507 Int4Ptr *m = NULL;
508 m=(Int4Ptr *)MemNew((size_t)((nrow+NR_END)*sizeof(Int4Ptr)));
509 if (!m)
510 {
511 ErrPostEx(SEV_FATAL, 0, 0, "Out of memory in I4ConvtMatrix");
512 ErrShow();
513 return NULL;
514 }
515 m += NR_END;
516 m -= nrl;
517 m[nrl]=a-ncl;
518 for(i=1, j=nrl+1;i<nrow;i++,j++)
519 m[j] = m[j-1]+ncol;
520 return (Int4PtrPtr) m;
521 }
522
I2ConvtFree(Int2Ptr * b,Int4 nrl)523 void LIBCALL I2ConvtFree(Int2Ptr *b, Int4 nrl)
524 {
525 /* frees a matrix allocated by xxConvtMatrix */
526 MemFree((CharPtr)(b+nrl-NR_END));
527 }
528
529
I4ConvtFree(Int4Ptr * b,Int4 nrl)530 void LIBCALL I4ConvtFree(Int4Ptr *b, Int4 nrl)
531 {
532 /* frees a matrix allocated by xxConvtMatrix */
533 MemFree((CharPtr)(b+nrl-NR_END));
534 }
535
536
537
FLConvtFree(FloatLoPtr * b,Int4 nrl)538 void LIBCALL FLConvtFree(FloatLoPtr *b, Int4 nrl)
539 {
540 /* frees a matrix allocated by xxConvtMatrix */
541 MemFree((CharPtr)(b+nrl-NR_END));
542 }
543
544
545
FHConvtFree(FloatHiPtr * b,Int4 nrl)546 void LIBCALL FHConvtFree(FloatHiPtr *b, Int4 nrl)
547 {
548 /* frees a matrix allocated by xxConvtMatrix */
549 MemFree((CharPtr)(b+nrl-NR_END));
550 }
551
552
FL3Matrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch,Int4 ndl,Int4 ndh)553 FloatLoPtrPtrPtr LIBCALL FL3Matrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch, Int4 ndl,
554 Int4 ndh)
555 {
556 Int4 i, j, nrow=nrh-nrl+1, ncol=nch-ncl+1, ndep=ndh-ndl+1;
557 FloatLoPtr **t = NULL;
558
559 t=(FloatLoPtr **)MemNew((size_t)((nrow+NR_END)*sizeof(FloatLoPtr *)));
560 if (!t)
561 {
562 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 1 in FL3Matrix");
563 ErrShow();
564 return NULL;
565 }
566 t += NR_END;
567 t -= nrl;
568
569 t[nrl]=(FloatLoPtr *)MemNew((size_t)((nrow*ncol+NR_END)*sizeof(FloatLoPtr)));
570 if (!t[nrl])
571 {
572 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 3 in FL3Matrix");
573 ErrShow();
574 return NULL;
575 }
576 t[nrl] += NR_END;
577 t[nrl] -= ncl;
578
579 t[nrl][ncl]=(FloatLoPtr)MemNew((size_t)((nrow*ncol*ndep+NR_END)*sizeof(FloatLo)));
580 if (!t[nrl][ncl])
581 {
582 ErrPostEx(SEV_FATAL, 0, 0, "Out of Memory 3 in FL3Matrix");
583 ErrShow();
584 return NULL;
585 }
586 t[nrl][ncl] += NR_END;
587 t[nrl][ncl] -= ncl;
588
589 for(j=ncl+1;j<=nch;j++) t[nrl][j] = t[nrl][j-1]+ndep;
590 for (i=nrl+1;i<=nrh;i++)
591 {
592 t[i]=t[i-1]+ncol;
593 t[i][ncl]=t[i-1][ncl]+ncol*ndep;
594 for(j=ncl+1;j<=nch;j++) t[i][j]=t[i][j-1]+ndep;
595 }
596 return (FloatLoPtrPtrPtr) t;
597 }
598
FL3MatrixFree(FloatLoPtr ** b,Int4 nrl,Int4 ncl,Int4 ndl)599 void LIBCALL FL3MatrixFree(FloatLoPtr **b, Int4 nrl, Int4 ncl, Int4 ndl)
600 {
601 MemFree((CharPtr)(b[nrl][ncl]+ndl-NR_END));
602 MemFree((CharPtr)(b[nrl]+ncl-NR_END));
603 MemFree((CharPtr)(b+nrl-NR_END));
604 }
605
606
607
608 /* WRAPPERS FOR NUMERICAL RECIPIES IN C 2nd Ed'n CODE */
609 /* define NRTONCBI and include this instead of nrutil.h nrutil.c */
610 /* These are not tested but are here anyhow if they are ever needed... */
611 #ifdef NRTONCBI
nrerror(char error_text[])612 void nrerror(char error_text[])
613 {
614 Char buf[200];
615 StrCpy(buf, "Numerical Recipies run-time error...\n");
616 StrCat(buf, error_text);
617 StrCat(buf, "Fatal, must exit to system\n");
618 ErrPostEx(SEV_FATAL, 0, 0, buf);
619 ErrShow();
620 exit(1);
621 }
622
vector(Int4 nl,Int4 nh)623 FloatLoPtr vector(Int4 nl, Int4 nh)
624 {
625 return FLVector(nl, nh);
626 }
627
628
ivector(Int4 nl,Int4 nh)629 Int4Ptr ivector(Int4 nl, Int4 nh)
630 {
631 return I4Vector(nl, nh);
632 } /* Note this removes the "int" platform dependence of the NR call */
633
dvector(Int4 nl,Int4 nh)634 FloatHiPtr dvector(Int4 nl, Int4 nh)
635 {
636 return FHVector(nl, nh);
637 }
638
cvector(Int4 nl,Int4 nh)639 UcharPtr cvector(Int4 nl, Int4 nh)
640 {
641 return UCVector(nl, nh);
642 }
643
lvector(Int4 nl,Int4 nh)644 Uint4Ptr lvector(Int4 nl, Int4 nh)
645 {
646 return UI4Vector(nl, nh);
647 }
648
matrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)649 FloatLoPtr *matrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
650 {
651 return FLMatrix(nrl, nrh, ncl, nch);
652
653 }
dmatrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)654 FloatHiPtr *dmatrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
655 {
656 return FHMatrix(nrl, nrh, ncl, nch);
657 }
658
imatrix(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)659 Int4Ptr *imatrix(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
660 {
661 return I4Matrix(nrl, nrh, ncl, nch);
662 }
663
free_dvector(FloaHiPtr v,Int4 nl,Int4 nh)664 void free_dvector(FloaHiPtr v, Int4 nl, Int4 nh)
665 {
666 FHVectorFree(v, nl);
667 }
668
free_vector(FloatLoPtr v,Int4 nl,Int4 nh)669 void free_vector(FloatLoPtr v, Int4 nl, Int4 nh)
670 {
671 FLVectorFree(v, nl);
672 }
673
free_cvector(UcharPtr v,Int4 nl,Int4 nh)674 void free_cvector(UcharPtr v, Int4 nl, Int4 nh)
675 {
676 UCVectorFree(v, nl);
677 }
678
free_lvector(Uint4Ptr v,Int4 nl,Int4 nh)679 void free_lvector(Uint4Ptr v, Int4 nl, Int4 nh)
680 {
681 UI4VectorFree(v, nl);
682 }
683
free_ivector(Int4Ptr v,Int4 nl,Int4 nh)684 void free_ivector(Int4Ptr v, Int4 nl, Int4 nh)
685 {
686 I4VectorFree(v, nl);
687 }
688
free_imatrix(Int4Ptr * m,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)689 void free_imatrix(Int4Ptr *m, Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
690 {
691 I4MatrixFree(m, nrl, ncl);
692 }
693
free_matrix(FloatLoPtr * m,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)694 void free_matrix(FloatLoPtr *m, Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
695 {
696 FLMatrixFree(m, nrl, ncl);
697 }
698
free_dmatrix(FloatHiPtr * m,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)699 void free_dmatrix(FloatHiPtr *m, Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
700 {
701 FHMatrixFree(m, nrl, ncl);
702 }
703
convert_matrix(FloatLoPtr a,Int4 nrl,Int4 nrh,Int4 ncl,Int2 nch)704 FloatLoPtr convert_matrix(FloatLoPtr a, Int4 nrl, Int4 nrh, Int4 ncl,
705 Int2 nch)
706 {
707 return FLConvtMatrix(a, nrl, nrh, ncl, nch);
708 }
709
free_convert_matrix(FloatLoPtr * b,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)710 void free_convert_matrix(FloatLoPtr *b, Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch)
711 {
712 FLConvtFree(b, nrl)
713 }
714
submatrix(FloatLoPtr * a,Int4 oldrl,Int4 oldrh,Int4 oldcl,Int4 oldch,Int4 newrl,Int4 newcl)715 FloatLoPtr *submatrix(FloatLoPtr *a, Int4 oldrl, Int4 oldrh,
716 Int4 oldcl, Int4 oldch, Int4 newrl, Int4 newcl)
717 {
718 return FLSubmatrix(a, oldrl, oldrh, oldcl, oldch, newrl, newcl);
719 }
720
free_submatrix(FloatLoPtr * b,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch)721 void free_submatrix(FloatLoPtr *b, Int4 nrl, Int4 nrh,
722 Int4 ncl, Int4 nch)
723 {
724 FLSubmatrixFree(b, nrl);
725 }
726
f3tensor(Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch,Int4 ndl,Int4 ndh)727 FloatLoPtr **f3tensor(Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch, Int4 ndl, Int4 ndh)
728 {
729 return FL3Matrix(nrl, nrh, ncl, nch, ndl, ndh);
730 }
731
free_f3tensor(FloatLoPtr ** t,Int4 nrl,Int4 nrh,Int4 ncl,Int4 nch,Int4 ndl,Int4 ndh)732 void free_f3tensor(FloatLoPtr **t, Int4 nrl, Int4 nrh, Int4 ncl, Int4 nch,
733 Int4 ndl, Int4 ndh)
734 {
735 FL3MatrixFree(t, nrl, ncl, ndl);
736 }
737
738 #endif
739
740
741 /* test code follows */
742 /*
743 #define NUMARGS 1
744 Args myargs[NUMARGS] = {
745 {"Dummy Arg","stdout", NULL,NULL, TRUE,'f',ARG_FILE_OUT,0.0,0,NULL}
746 };
747
748
749 Int2 Main(void)
750 {
751 Int2Ptr iv;
752 Int2Ptr *im;
753 Int4Ptr i4v;
754 Int4Ptr *i4m;
755 FloatLoPtr flv;
756 FloatLoPtr *flm;
757 FloatLoPtr *flmsub;
758 FloatLoPtr **fltd;
759 FloatHiPtr fhv;
760 FloatHiPtr *fhm;
761 UcharPtr ucv;
762 Uint4Ptr uiv;
763 Int4 i, j, k;
764 FloatLo I, J, K;
765 CharPtr mys;
766
767
768 if (! GetArgs("CoreMatx 1.0",NUMARGS, myargs))
769 return 1;
770
771 printf("starting\n");
772 printf("Allocating integers\n");
773 iv = I2Vector(1, 100);
774
775
776 im = I2Matrix(1, 100, 1, 100);
777 i4v = I4Vector(1, 100);
778 i4m = I4Matrix(1, 100, 1, 100);
779 printf("Allocating reals \n");
780 flv = FLVector(1, 100);
781 flm = FLMatrix(1, 100, 1, 100);
782 fhv = FHVector(1, 100);
783 fhm = FHMatrix(1, 100, 1, 100);
784 printf("Allocating others\n");
785 ucv = UCVector(1, 100);
786 uiv = UI4Vector(1, 100);
787 printf("Allocating 3D float matrix\n");
788 fltd = FL3Matrix(1, 100, 1, 10, 1, 10);
789 printf("Assigning\n");
790 for (i = 1; i<= 100; i++)
791 {
792 I = (FloatHi) i;
793 iv[i] = (Int2)i;
794 i4v[i] = i * (-1000);
795 flv[i] = (FloatLo)((I+1)/I);
796 fhv[i] = (FloatHi) ((I+1/I) + I*10000000000000);
797 ucv[i] = (Uchar)(i+64);
798 uiv[i] = (Uint4)(i * 100000);
799 for (j = 1; j<=100; j++)
800 {
801 J = (FloatLo) j;
802 im[i][j] = (Int2)i+j;
803 i4m[i][j] = (Int4) i+j * 100000;
804 flm[i][j] = (FloatLo)((J+1)/(I+J));
805 fhm[i][j] = (FloatHi)(I/(I+J) + I*J*1000000000000);
806 }
807 }
808 for (i =1;i<=100;i++)
809 for (j=1;j<=10;j++)
810 for (k=1;k<=10;k++)
811 {
812 I = (FloatLo) i;
813 J = (FloatLo) j;
814 K = (FloatLo) k;
815 fltd[i][j][k] = I+J+K + 1/(I*J*K);
816 }
817 printf("Vectors \n");
818 printf("Int2\n");
819 for (i = 1;i<=91;i+=10)
820 {
821 for (j = 0; j<=9; j++)
822 {
823 printf("%d ", iv[i+j]);
824 }
825 printf("\n");
826 }
827 printf("Int4\n");
828 for (i = 1;i<=91;i+=10)
829 {
830 for (j = 0; j<=9; j++)
831 {
832 printf("%ld ", i4v[i+j]);
833 }
834 printf("\n");
835 }
836 printf("FloatLo\n");
837 for (i = 1;i<=91;i+=10)
838 {
839 for (j = 0; j<=9; j++)
840 {
841 printf("%f ", flv[i+j]);
842 }
843 printf("\n");
844 }
845 printf("FloatHi\n");
846 for (i = 1;i<=91;i+=10)
847 {
848 for (j = 0; j<=9; j++)
849 {
850 printf("%ef ", fhv[i+j]);
851 }
852 printf("\n");
853 }
854 printf("UChar\n");
855 for (i = 1;i<=91;i+=10)
856 {
857 for (j = 0; j<=9; j++)
858 {
859 printf("%d ", ucv[i+j]);
860 }
861 printf("\n");
862 }
863 printf("UInt4\n");
864 for (i = 1;i<=91;i+=10)
865 {
866 for (j = 0; j<=9; j++)
867 {
868 printf("%ld ", uiv[i+j]);
869 }
870 printf("\n");
871 }
872 printf("Matricies\n");
873 printf("FloatHi\n");
874 for (k=1;k<=100;k++)
875 {
876 printf("Row [%d]\n", k);
877 for (i = 1;i<=91;i+=10)
878 {
879 for (j = 0; j<=9; j++)
880 {
881 printf("%ef ", fhm[k][i+j]);
882 }
883 printf("\n");
884 }
885 }
886 printf("FloatLo\n");
887 for (k=1;k<=100;k++)
888 {
889 printf("Row [%d]\n", k);
890 for (i = 1;i<=91;i+=10)
891 {
892 for (j = 0; j<=9; j++)
893 {
894 printf("%f ", flm[k][i+j]);
895 }
896 printf("\n");
897 }
898 }
899 printf("Int2\n");
900 for (k=1;k<=100;k++)
901 {
902 printf("Row [%d]\n", k);
903 for (i = 1;i<=91;i+=10)
904 {
905 for (j = 0; j<=9; j++)
906 {
907 printf("%d ", im[k][i+j]);
908 }
909 printf("\n");
910 }
911 }
912 printf("Int4\n");
913 for (k=1;k<=100;k++)
914 {
915 printf("Row [%d]\n", k);
916 for (i = 1;i<=91;i+=10)
917 {
918 for (j = 0; j<=9; j++)
919 {
920 printf("%ld ", i4m[k][i+j]);
921 }
922 printf("\n");
923 }
924 }
925 printf("3DFloat");
926 for (k=1;k<=100;k++)
927 {
928 printf("Row [%d]\n", k);
929 for (i = 1;i<=10;i++)
930 {
931 printf("Col [%d]\n", i);
932 for (j = 1; j<=10; j++)
933 {
934 printf("%ef ", fltd[k][i][j]);
935 }
936 printf("\n");
937 }
938 }
939 printf("freeing floats\n");
940 FL3MatrixFree(fltd, 1, 1, 1);
941 FLMatrixFree(flm, 1, 1);
942 FLVectorFree(flv, 1);
943 FHMatrixFree(fhm, 1, 1);
944 FHVectorFree(fhv, 1);
945 printf("freeing ints\n");
946 I4MatrixFree(i4m, 1, 1);
947 I4VectorFree(i4v, 1);
948 I2MatrixFree(im, 1, 1);
949 I2VectorFree(iv, 1);
950 UCVectorFree(ucv, 1);
951 UI4VectorFree(uiv, 1);
952 printf("done\n");
953 mys = (CharPtr) MemNew((size_t)(100*sizeof(Char)));
954 strcpy(mys, "adios amiogs, MemNew still functions..\n");
955 printf(mys);
956 MemFree(mys);
957 printf("and MemFree is OK\n");
958 return 0;
959 }
960
961 */
962