/** * Copyright (c): Uwe Schmidt, FH Wedel * * You may study, modify and distribute this source code * FOR NON-COMMERCIAL PURPOSES ONLY. * This copyright message has to remain unchanged. * * Note that this document is provided 'as is', * WITHOUT WARRANTY of any kind either expressed or implied. */ #include "Matrix2.h" #include /*--------------------*/ Matrix newMatrix( int h, int w ) { Matrix res = malloc( h * sizeof( Row ) ); if ( res ) { Row rows = malloc( h * w * sizeof( Element ) ); if ( rows ) { Matrix m = res; Row r = rows; while ( h-- ) { *m++ = r; r += w; } return res; } } /* heap overflow */ exit( 1 ); } /*--------------------*/ void freeMatrix( Matrix m ) { free( m[0] ); free( m ); } /*--------------------*/ Matrix zeroMatrix( int h, int w ) { Matrix res = newMatrix( h, w ); int len = w * h; Row p = res[0]; while ( len-- ) { *p++ = 0.0; } return res; } /*--------------------*/ Matrix unitMatrix( int h, int w ) { Matrix res = zeroMatrix( h, w ); int i; for ( i = 0; i < w && i < h; ++i ) { res[i][i] = 1.0; } return res; } /*--------------------*/ Matrix addMatrix( Matrix m1, Matrix m2, int h, int w ) { Matrix res = newMatrix( h, w ); int i, j; for ( i = 0; i < h; ++i ) { for ( j = 0; j < w; ++j ) { res[i][j] = m1[i][j] + m2[i][j]; } } return res; } /*--------------------*/ Matrix transposeMatrix( Matrix m, int h, int w ) { Matrix res = newMatrix( w, h ); int i, j; for ( i = 0; i < h; ++i ) { for ( j = 0; j < w; ++j ) { res[j][i] = m[i][j]; } } return res; }