Next version of JABA
[jabaws.git] / binaries / src / mafft / core / mtxutl.c
1 #include <stdio.h>
2 #include <stddef.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include "mtxutl.h"
6
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
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
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
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
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
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
132 float *AllocateFloatVec( int l1 )
133 {
134         float *vec;
135
136         vec = (float *)calloc( (unsigned int)l1, sizeof( float ) );
137         if( vec == NULL )
138         {
139                 fprintf( stderr, "Allocation error ( %d fload vec )\n", l1 );
140                 exit( 1 );
141         }
142         return( vec );
143 }
144
145 void FreeFloatVec( float *vec )
146 {
147         free( (char *)vec );
148 }
149
150 float **AllocateFloatHalfMtx( int ll1 )
151 {
152         float **mtx;
153         int i;
154
155         mtx = (float **)calloc( (unsigned int)ll1+1, sizeof( float * ) );
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] = (float *)calloc( ll1-i, sizeof( float ) );
164                 if( !mtx[i] )
165                 {
166                         fprintf( stderr, "Allocation error( %d floathalfmtx )\n", ll1 );
167                         exit( 1 );
168                 }
169         }
170         mtx[ll1] = NULL;
171         return( mtx );
172 }
173
174 float **AllocateFloatMtx( int ll1, int ll2 )
175 {
176         float **mtx;
177         int i;
178
179         mtx = (float **)calloc( (unsigned int)ll1+1, sizeof( float * ) );
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] = (float *)calloc( ll2, sizeof( float ) );
190                         if( !mtx[i] )
191                         {
192                                 fprintf( stderr, "Allocation error( %d x %d floatmtx )\n", ll1, ll2 );
193                                 exit( 1 );
194                         }
195                 }
196         }
197         mtx[ll1] = NULL;
198         return( mtx );
199 }
200
201 void FreeFloatHalfMtx( float **mtx, int n )
202 {
203         int i;
204
205         for( i=0; i<n; i++ ) 
206         {
207                 if( mtx[i] ) FreeFloatVec( mtx[i] );
208         }
209         free( mtx );
210 }
211 void FreeFloatMtx( float **mtx )
212 {
213         int i;
214
215         for( i=0; mtx[i]; i++ ) 
216         {
217                 FreeFloatVec( mtx[i] );
218         }
219         free( mtx );
220 }
221
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
235 void FreeIntVec( int *vec )
236 {
237         free( (char *)vec );
238 }
239
240 float **AllocateFloatTri( int ll1 )
241 {
242         float **tri;
243         int i;
244
245         tri = (float **)calloc( (unsigned int)ll1+1, sizeof( float * ) );
246         if( !tri )
247         {
248                 fprintf( stderr, "Allocation error ( float 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
260 void FreeFloatTri( float **tri )
261 {
262 /*
263         float **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                 
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++ ) 
288                 {
289                         mtx[i] = AllocateIntVec( ll2 );
290                 }
291         }
292         mtx[ll1] = NULL;
293         return( mtx );
294 }
295
296 /*
297 void FreeIntMtx( int **mtx )
298 {
299 *
300         int **x;
301         x = mtx;
302         while( !*mtx ) free( *mtx++ );
303         free( x );
304 *
305         int i;
306         for( i=0; mtx[i] != NULL; i++ ) 
307                 free( (char *)mtx[i] );
308         free( (char *)mtx );
309 }
310 */
311
312 char ***AllocateCharCub( int ll1, int ll2, int  ll3 )
313 {
314         int i;
315         char ***cub;
316
317         cub = (char ***)calloc( ll1+1, sizeof( char ** ) );
318         if( !cub ) 
319         {
320                 fprintf( stderr, "Allocation error( %d x %d x %d char cube\n", ll1, ll2, ll3 );
321                 exit( 1 );
322         }
323         if( ll2 )
324         {
325                 for( i=0; i<ll1; i++ ) 
326                 {
327                         cub[i] = AllocateCharMtx( ll2, ll3 );
328                 }
329         }
330         cub[ll1] = NULL;
331         return( cub );
332 }
333
334 void FreeCharCub( char ***cub )
335 {
336         int i;
337
338         for( i=0; cub[i]; i++ ) 
339         {
340                 FreeCharMtx( cub[i] );
341         }
342         free( cub );
343 }
344
345 void freeintmtx( int **mtx, int ll1, int ll2 )
346 {
347     int i;
348
349     for( i=0; i<ll1; i++ ) 
350         free( (char *)mtx[i] );
351     free( (char *)mtx );
352 }
353       
354 void FreeIntMtx( int **mtx )
355 {
356         int i;
357
358         for( i=0; mtx[i]; i++ ) 
359                 free( (char *)mtx[i] );
360         free( (char *)mtx );
361 }
362
363 char ****AllocateCharHcu( int ll1, int ll2, int ll3, int ll4 )
364 {
365         int i;
366         char ****hcu;
367
368         hcu = (char ****)calloc( ll1+1, sizeof( char *** ) );
369         if( hcu == NULL ) exit( 1 );
370         for( i=0; i<ll1; i++ ) 
371                 hcu[i] = AllocateCharCub( ll2, ll3, ll4 );
372         hcu[ll1] = NULL;
373         return( hcu );
374 }
375
376 void FreeCharHcu( char ****hcu )
377 {
378         int i;
379         for( i=0; hcu[i]; i++ )
380         {
381                 FreeCharCub( hcu[i] );
382         }
383         free ( (char *)hcu );
384 }
385
386 double *AllocateDoubleVec( int ll1 )
387 {
388         double *vec;
389
390         vec = (double *)calloc( ll1, sizeof( double ) );
391         return( vec );
392 }
393
394 void FreeDoubleVec( double *vec )
395 {
396         free( vec );
397 }
398
399 int ***AllocateIntCub( int ll1, int ll2, int ll3 )
400 {
401         int i;
402         int ***cub;
403
404         cub = (int ***)calloc( ll1+1, sizeof( int ** ) );
405         if( cub == NULL )
406         {
407                 fprintf( stderr, "cannot allocate IntCub\n" );
408                 exit( 1 );
409         }
410         for( i=0; i<ll1; i++ ) 
411                 cub[i] = AllocateIntMtx( ll2, ll3 );
412         cub[ll1] = NULL;
413
414         return cub;
415 }
416
417 void FreeIntCub( int ***cub )
418 {
419         int i;
420         for( i=0; cub[i]; i++ ) 
421         {
422                 FreeIntMtx( cub[i] );
423         }
424         free( cub );
425 }
426
427 double **AllocateDoubleMtx( int ll1, int ll2 )
428 {
429         int i;
430         double **mtx;
431         mtx = (double **)calloc( ll1+1, sizeof( double * ) );
432         if( !mtx )
433         {
434                 fprintf( stderr, "cannot allocate DoubleMtx\n" );
435                 exit( 1 );
436         }
437         if( ll2 )
438         {
439                 for( i=0; i<ll1; i++ ) 
440                         mtx[i] = AllocateDoubleVec( ll2 );
441         }
442         mtx[ll1] = NULL;
443
444         return mtx;
445 }
446
447 void FreeDoubleMtx( double **mtx )
448 {
449         int i;
450         for( i=0; mtx[i]; i++ )
451                 FreeDoubleVec( mtx[i] );
452         free( mtx );
453 }
454
455 float ***AllocateFloatCub( int ll1, int ll2, int  ll3 )
456 {
457         int i;
458         float ***cub;
459
460         cub = (float ***)calloc( ll1+1, sizeof( float ** ) );
461         if( !cub ) 
462         {
463                 fprintf( stderr, "cannot allocate float cube.\n" );
464                 exit( 1 );
465         }
466         for( i=0; i<ll1; i++ ) 
467         {
468                 cub[i] = AllocateFloatMtx( ll2, ll3 );
469         }
470         cub[ll1] = NULL;
471         return( cub );
472 }
473
474 void FreeFloatCub( float ***cub )
475 {
476         int i;
477
478         for( i=0; cub[i]; i++ ) 
479         {
480                 FreeFloatMtx( cub[i] );
481         }
482         free( cub );
483 }
484
485 double ***AllocateDoubleCub( int ll1, int ll2, int  ll3 )
486 {
487         int i;
488         double ***cub;
489
490         cub = (double ***)calloc( ll1+1, sizeof( double ** ) );
491         if( !cub ) 
492         {
493                 fprintf( stderr, "cannot allocate double cube.\n" );
494                 exit( 1 );
495         }
496         for( i=0; i<ll1; i++ ) 
497         {
498                 cub[i] = AllocateDoubleMtx( ll2, ll3 );
499         }
500         cub[ll1] = NULL;
501         return( cub );
502 }
503
504 void FreeDoubleCub( double ***cub )
505 {
506         int i;
507
508         for( i=0; cub[i]; i++ ) 
509         {
510                 FreeDoubleMtx( cub[i] );
511         }
512         free( cub );
513 }
514
515
516 short *AllocateShortVec( int ll1 )
517 {
518         short *vec;
519
520         vec = (short *)calloc( ll1, sizeof( short ) );
521         if( vec == NULL )
522         {       
523                 fprintf( stderr, "Allocation error( %d short vec )\n", ll1 );
524                 exit( 1 );
525         }
526         return( vec );
527 }       
528
529 void FreeShortVec( short *vec )
530 {
531         free( (char *)vec );
532 }
533
534 short **AllocateShortMtx( int ll1, int ll2 )
535 {
536         int i;
537         short **mtx;
538
539
540         mtx = (short **)calloc( ll1+1, sizeof( short * ) );
541         if( !mtx )
542         {
543                 fprintf( stderr, "Allocation error( %d x %d short mtx ) \n", ll1, ll2 );
544                 exit( 1 );
545         }
546         for( i=0; i<ll1; i++ ) 
547         {
548                 mtx[i] = AllocateShortVec( ll2 );
549         }
550         mtx[ll1] = NULL;
551         return( mtx );
552 }
553
554 void FreeShortMtx( short **mtx )
555 {
556         int i;
557
558         for( i=0; mtx[i]; i++ ) 
559                 free( (char *)mtx[i] );
560         free( (char *)mtx );
561 }
562