1 #include <stdio.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "mtxutl.h"
6
MtxuntDouble(double ** mtx,int n)7 void MtxuntDouble( double **mtx, int n )
8 {
9 int i, j;
10 for( i=0; i<n; i++ ) for( j=0; j<n; j++ ) mtx[i][j] = 0.0;
11 for( i=0; i<n; i++ ) mtx[i][i] = 1.0;
12 }
13
MtxmltDouble(double ** mtx1,double ** mtx2,int n)14 void MtxmltDouble( double **mtx1, double **mtx2, int n )
15 {
16 int i, j, k;
17 double s, *tmp;
18
19 tmp = (double *)calloc( n, sizeof( double ) );
20 for( i=0; i<n; i++ )
21 {
22 for( k=0; k<n; k++ ) tmp[k] = mtx1[i][k];
23 for( j=0; j<n; j++ )
24 {
25 s = 0.0;
26 for( k=0; k<n; k++ ) s += tmp[k] * mtx2[k][j];
27 mtx1[i][j] = s;
28 }
29 }
30 free( tmp );
31 }
32
AllocateCharVec(int l1)33 char *AllocateCharVec( int l1 )
34 {
35 char *cvec;
36
37 cvec = (char *)calloc( l1, sizeof( char ) );
38 if( cvec == NULL )
39 {
40 fprintf( stderr, "Cannot allocate %d character vector.\n", l1 );
41 exit( 1 );
42 }
43 return( cvec );
44 }
45
46 #if 0
47 void ReallocateCharMtx( char **mtx, int l1, int l2 )
48 {
49 int i;
50 char *bk = (char *)malloc( l2+1 ); // hontou ha iranai
51 if( bk == NULL )
52 {
53 fprintf( stderr, "Cannot allocate bk in ReallocateCharMtx\n" );
54 exit( 1 );
55 }
56 for( i=0; i<l1; i++ )
57 {
58 #if 1
59 strcpy( bk, mtx[i] );
60 mtx[i] = (char *)realloc( mtx[i], (l2+1) * sizeof( char ) );
61 if( mtx[i] == NULL )
62 {
63 fprintf( stderr, "Cannot reallocate %d x %d character matrix.\n", l1, l2 );
64 }
65 if( strcmp( bk, mtx[i] ) ) // hontou ha iranai
66 {
67 fprintf( stderr, "changed!! \n%s\n \nto\n%s\n in realloc..\n", bk, mtx[i] );
68 strcpy( mtx[i], bk );
69 }
70 #else
71 strcpy( bk, mtx[i] );
72 free( mtx[i] );
73 mtx[i] = (char *)calloc( (l2+1), sizeof( char ) );
74 strcpy( mtx[i], bk );
75 #endif
76 }
77 free( bk ); // hontou ha iranai
78 }
79 #else
ReallocateCharMtx(char ** mtx,int l1,int l2)80 void ReallocateCharMtx( char **mtx, int l1, int l2 )
81 {
82 int i;
83 for( i=0; i<l1; i++ )
84 {
85 mtx[i] = (char *)realloc( mtx[i], (l2+1) * sizeof( char ) );
86 if( mtx[i] == NULL )
87 {
88 fprintf( stderr, "Cannot reallocate %d x %d character matrix.\n", l1, l2 );
89 }
90 }
91 }
92 #endif
93
AllocateCharMtx(int l1,int l2)94 char **AllocateCharMtx( int l1, int l2 )
95 {
96 int i;
97 char **cmtx;
98
99 cmtx = (char **)calloc( l1+1, sizeof( char * ) );
100 if( cmtx == NULL )
101 {
102 fprintf( stderr, "Cannot allocate %d x %d character matrix.\n", l1, l2 );
103 exit( 1 );
104 }
105 if( l2 )
106 {
107 for( i=0; i<l1; i++ )
108 {
109 cmtx[i] = AllocateCharVec( l2 );
110 }
111 }
112 cmtx[l1] = NULL;
113 return( cmtx );
114 }
115
FreeCharMtx(char ** mtx)116 void FreeCharMtx( char **mtx )
117 {
118 /*
119 char **x;
120 x = mtx;
121 while( *x != NULL ) free( *x++ );
122 free( mtx );
123 */
124 int i;
125 for( i=0; mtx[i]; i++ )
126 {
127 free( mtx[i] );
128 }
129 free( mtx );
130 }
131
AllocateFloatVec(int l1)132 double *AllocateFloatVec( int l1 )
133 {
134 double *vec;
135
136 vec = (double *)calloc( (unsigned int)l1, sizeof( double ) );
137 if( vec == NULL )
138 {
139 fprintf( stderr, "Allocation error ( %d fload vec )\n", l1 );
140 exit( 1 );
141 }
142 return( vec );
143 }
144
FreeFloatVec(double * vec)145 void FreeFloatVec( double *vec )
146 {
147 free( (char *)vec );
148 }
149
AllocateFloatHalfMtx(int ll1)150 double **AllocateFloatHalfMtx( int ll1 )
151 {
152 double **mtx;
153 int i;
154
155 mtx = (double **)calloc( (unsigned int)ll1+1, sizeof( double * ) );
156 if( mtx == NULL )
157 {
158 fprintf( stderr, "Allocation error ( %d fload halfmtx )\n", ll1 );
159 exit( 1 );
160 }
161 for( i=0; i<ll1; i++ )
162 {
163 mtx[i] = (double *)calloc( ll1-i, sizeof( double ) );
164 if( !mtx[i] )
165 {
166 fprintf( stderr, "Allocation error( %d doublehalfmtx )\n", ll1 );
167 exit( 1 );
168 }
169 }
170 mtx[ll1] = NULL;
171 return( mtx );
172 }
173
AllocateFloatMtx(int ll1,int ll2)174 double **AllocateFloatMtx( int ll1, int ll2 )
175 {
176 double **mtx;
177 int i;
178
179 mtx = (double **)calloc( (unsigned int)ll1+1, sizeof( double * ) );
180 if( mtx == NULL )
181 {
182 fprintf( stderr, "Allocation error ( %d x %d fload mtx )\n", ll1, ll2 );
183 exit( 1 );
184 }
185 if( ll2 )
186 {
187 for( i=0; i<ll1; i++ )
188 {
189 mtx[i] = (double *)calloc( ll2, sizeof( double ) );
190 if( !mtx[i] )
191 {
192 fprintf( stderr, "Allocation error( %d x %d doublemtx )\n", ll1, ll2 );
193 exit( 1 );
194 }
195 }
196 }
197 mtx[ll1] = NULL;
198 return( mtx );
199 }
200
FreeFloatHalfMtx(double ** mtx,int n)201 void FreeFloatHalfMtx( double **mtx, int n )
202 {
203 int i;
204
205 for( i=0; i<n; i++ )
206 {
207 if( mtx[i] ) FreeFloatVec( mtx[i] ); mtx[i] = NULL;
208 }
209 free( mtx );
210 }
FreeFloatMtx(double ** mtx)211 void FreeFloatMtx( double **mtx )
212 {
213 int i;
214
215 for( i=0; mtx[i]; i++ )
216 {
217 if( mtx[i] ) FreeFloatVec( mtx[i] ); mtx[i] = NULL;
218 }
219 free( mtx );
220 }
221
AllocateIntVec(int ll1)222 int *AllocateIntVec( int ll1 )
223 {
224 int *vec;
225
226 vec = (int *)calloc( ll1, sizeof( int ) );
227 if( vec == NULL )
228 {
229 fprintf( stderr, "Allocation error( %d int vec )\n", ll1 );
230 exit( 1 );
231 }
232 return( vec );
233 }
234
FreeIntVec(int * vec)235 void FreeIntVec( int *vec )
236 {
237 free( (char *)vec );
238 }
239
AllocateFloatTri(int ll1)240 double **AllocateFloatTri( int ll1 )
241 {
242 double **tri;
243 int i;
244
245 tri = (double **)calloc( (unsigned int)ll1+1, sizeof( double * ) );
246 if( !tri )
247 {
248 fprintf( stderr, "Allocation error ( double tri )\n" );
249 exit( 1 );
250 }
251 for( i=0; i<ll1; i++ )
252 {
253 tri[i] = AllocateFloatVec( i+3 );
254 }
255 tri[ll1] = NULL;
256
257 return( tri );
258 }
259
FreeFloatTri(double ** tri)260 void FreeFloatTri( double **tri )
261 {
262 /*
263 double **x;
264 x = tri;
265 while( *tri != NULL ) free( *tri++ );
266 free( x );
267 */
268 int i;
269 for( i=0; tri[i]; i++ )
270 free( tri[i] );
271 free( tri );
272 }
273
AllocateIntMtx(int ll1,int ll2)274 int **AllocateIntMtx( int ll1, int ll2 )
275 {
276 int i;
277 int **mtx;
278
279 mtx = (int **)calloc( ll1+1, sizeof( int * ) );
280 if( !mtx )
281 {
282 fprintf( stderr, "Allocation error( %d x %d int mtx )\n", ll1, ll2 );
283 exit( 1 );
284 }
285 if( ll2 )
286 {
287 for( i=0; i<ll1; i++ ) mtx[i] = AllocateIntVec( ll2 );
288 }
289 else
290 {
291 for( i=0; i<ll1; i++ ) mtx[i] = NULL;
292 }
293 mtx[ll1] = NULL;
294 return( mtx );
295 }
296
297 /*
298 void FreeIntMtx( int **mtx )
299 {
300 *
301 int **x;
302 x = mtx;
303 while( !*mtx ) free( *mtx++ );
304 free( x );
305 *
306 int i;
307 for( i=0; mtx[i] != NULL; i++ )
308 free( (char *)mtx[i] );
309 free( (char *)mtx );
310 }
311 */
312
AllocateCharCub(int ll1,int ll2,int ll3)313 char ***AllocateCharCub( int ll1, int ll2, int ll3 )
314 {
315 int i;
316 char ***cub;
317
318 cub = (char ***)calloc( ll1+1, sizeof( char ** ) );
319 if( !cub )
320 {
321 fprintf( stderr, "Allocation error( %d x %d x %d char cube\n", ll1, ll2, ll3 );
322 exit( 1 );
323 }
324 if( ll2 )
325 {
326 for( i=0; i<ll1; i++ )
327 {
328 cub[i] = AllocateCharMtx( ll2, ll3 );
329 }
330 }
331 cub[ll1] = NULL;
332 return( cub );
333 }
334
FreeCharCub(char *** cub)335 void FreeCharCub( char ***cub )
336 {
337 int i;
338
339 for( i=0; cub[i]; i++ )
340 {
341 FreeCharMtx( cub[i] );
342 }
343 free( cub );
344 }
345
freeintmtx(int ** mtx,int ll1,int ll2)346 void freeintmtx( int **mtx, int ll1, int ll2 )
347 {
348 int i;
349
350 for( i=0; i<ll1; i++ )
351 free( (char *)mtx[i] );
352 free( (char *)mtx );
353 }
354
FreeIntMtx(int ** mtx)355 void FreeIntMtx( int **mtx )
356 {
357 int i;
358
359 for( i=0; mtx[i]; i++ )
360 {
361 if( mtx[i] ) free( (char *)mtx[i] ); mtx[i] = NULL;
362 }
363 free( (char *)mtx );
364 }
365
AllocateCharHcu(int ll1,int ll2,int ll3,int ll4)366 char ****AllocateCharHcu( int ll1, int ll2, int ll3, int ll4 )
367 {
368 int i;
369 char ****hcu;
370
371 hcu = (char ****)calloc( ll1+1, sizeof( char *** ) );
372 if( hcu == NULL ) exit( 1 );
373 for( i=0; i<ll1; i++ )
374 hcu[i] = AllocateCharCub( ll2, ll3, ll4 );
375 hcu[ll1] = NULL;
376 return( hcu );
377 }
378
FreeCharHcu(char **** hcu)379 void FreeCharHcu( char ****hcu )
380 {
381 int i;
382 for( i=0; hcu[i]; i++ )
383 {
384 FreeCharCub( hcu[i] );
385 }
386 free ( (char *)hcu );
387 }
388
AllocateDoubleVec(int ll1)389 double *AllocateDoubleVec( int ll1 )
390 {
391 double *vec;
392
393 vec = (double *)calloc( ll1, sizeof( double ) ); // filled with 0.0
394 return( vec );
395 }
396
FreeDoubleVec(double * vec)397 void FreeDoubleVec( double *vec )
398 {
399 free( vec );
400 }
401
AllocateIntCub(int ll1,int ll2,int ll3)402 int ***AllocateIntCub( int ll1, int ll2, int ll3 )
403 {
404 int i;
405 int ***cub;
406
407 cub = (int ***)calloc( ll1+1, sizeof( int ** ) );
408 if( cub == NULL )
409 {
410 fprintf( stderr, "cannot allocate IntCub\n" );
411 exit( 1 );
412 }
413 for( i=0; i<ll1; i++ )
414 cub[i] = AllocateIntMtx( ll2, ll3 );
415 cub[ll1] = NULL;
416
417 return cub;
418 }
419
FreeIntCub(int *** cub)420 void FreeIntCub( int ***cub )
421 {
422 int i;
423 for( i=0; cub[i]; i++ )
424 {
425 if( cub[i] ) FreeIntMtx( cub[i] ); cub[i] = NULL;
426 }
427 free( cub );
428 }
429
AllocateDoubleMtx(int ll1,int ll2)430 double **AllocateDoubleMtx( int ll1, int ll2 )
431 {
432 int i;
433 double **mtx;
434 mtx = (double **)calloc( ll1+1, sizeof( double * ) );
435 if( !mtx )
436 {
437 fprintf( stderr, "cannot allocate DoubleMtx\n" );
438 exit( 1 );
439 }
440 if( ll2 )
441 {
442 for( i=0; i<ll1; i++ )
443 mtx[i] = AllocateDoubleVec( ll2 );
444 }
445 mtx[ll1] = NULL;
446
447 return mtx;
448 }
449
FreeDoubleMtx(double ** mtx)450 void FreeDoubleMtx( double **mtx )
451 {
452 int i;
453 for( i=0; mtx[i]; i++ )
454 FreeDoubleVec( mtx[i] );
455 free( mtx );
456 }
457
AllocateFloatCub(int ll1,int ll2,int ll3)458 double ***AllocateFloatCub( int ll1, int ll2, int ll3 )
459 {
460 int i;
461 double ***cub;
462
463 cub = (double ***)calloc( ll1+1, sizeof( double ** ) );
464 if( !cub )
465 {
466 fprintf( stderr, "cannot allocate double cube.\n" );
467 exit( 1 );
468 }
469 for( i=0; i<ll1; i++ )
470 {
471 cub[i] = AllocateFloatMtx( ll2, ll3 );
472 }
473 cub[ll1] = NULL;
474 return( cub );
475 }
476
FreeFloatCub(double *** cub)477 void FreeFloatCub( double ***cub )
478 {
479 int i;
480
481 for( i=0; cub[i]; i++ )
482 {
483 FreeFloatMtx( cub[i] );
484 }
485 free( cub );
486 }
487
AllocateDoubleCub(int ll1,int ll2,int ll3)488 double ***AllocateDoubleCub( int ll1, int ll2, int ll3 )
489 {
490 int i;
491 double ***cub;
492
493 cub = (double ***)calloc( ll1+1, sizeof( double ** ) );
494 if( !cub )
495 {
496 fprintf( stderr, "cannot allocate double cube.\n" );
497 exit( 1 );
498 }
499 for( i=0; i<ll1; i++ )
500 {
501 cub[i] = AllocateDoubleMtx( ll2, ll3 );
502 }
503 cub[ll1] = NULL;
504 return( cub );
505 }
506
FreeDoubleCub(double *** cub)507 void FreeDoubleCub( double ***cub )
508 {
509 int i;
510
511 for( i=0; cub[i]; i++ )
512 {
513 FreeDoubleMtx( cub[i] );
514 }
515 free( cub );
516 }
517
518
AllocateShortVec(int ll1)519 short *AllocateShortVec( int ll1 )
520 {
521 short *vec;
522
523 vec = (short *)calloc( ll1, sizeof( short ) );
524 if( vec == NULL )
525 {
526 fprintf( stderr, "Allocation error( %d short vec )\n", ll1 );
527 exit( 1 );
528 }
529 return( vec );
530 }
531
FreeShortVec(short * vec)532 void FreeShortVec( short *vec )
533 {
534 free( (char *)vec );
535 }
536
AllocateShortMtx(int ll1,int ll2)537 short **AllocateShortMtx( int ll1, int ll2 )
538 {
539 int i;
540 short **mtx;
541
542
543 mtx = (short **)calloc( ll1+1, sizeof( short * ) );
544 if( !mtx )
545 {
546 fprintf( stderr, "Allocation error( %d x %d short mtx ) \n", ll1, ll2 );
547 exit( 1 );
548 }
549 for( i=0; i<ll1; i++ )
550 {
551 mtx[i] = AllocateShortVec( ll2 );
552 }
553 mtx[ll1] = NULL;
554 return( mtx );
555 }
556
FreeShortMtx(short ** mtx)557 void FreeShortMtx( short **mtx )
558 {
559 int i;
560
561 for( i=0; mtx[i]; i++ )
562 free( (char *)mtx[i] );
563 free( (char *)mtx );
564 }
565
566