ItsMods
[Release] Matrix Functions - Printable Version

+- ItsMods (https://www.itsmods.com)
+-- Forum: Archive (https://www.itsmods.com/Forum-Archive.html)
+--- Forum: Archive (https://www.itsmods.com/Forum-Archive--64.html)
+--- Thread: [Release] Matrix Functions (/Thread-Release-Matrix-Functions.html)



Matrix Functions - Yamato - 07-28-2013

Hello

I was recommended to release this functions, they are part of a math gsc stuff I made sometime ago, I havent made all the stuff I wanted about these, but I got tired and stopped the project, if anyone wants to work with these just go on or if you need help you can ask me Big Grin

Code:
CreateMatrix( matrix ) { files = StrTok( matrix, ";" ); m = []; wait ( 0.05 ); for( i = 0; i < files.size; i ++ ) { columns = StrTok( files[i], "," ); for( j = 0; j < columns.size; j ++ ) m[i][j] = int( columns[j] ); } return m; } getMatrixRows( matrix ) { rows = 0; for( j = 0; j <= 1000; j ++ ) { if( ! isDefined( matrix[j][0] ) ) { rows = ( j ); break; } } return rows; } getMatrixColumns( matrix ) { columns = 0; for( i = 0; i <= 1000; i ++ ) { if( ! isDefined( matrix[0][i] ) ) { columns = ( i ); break; } } return columns; } isSquaredMatrix( matrix ) { if( getMatrixRows( matrix ) == getMatrixColumns( matrix ) ) return true; return false; } getMatrixOrder( matrix ) { if( isSquaredMatrix( matrix ) ) return ( getMatrixRows( matrix ) ); } CreateOnes( m, n ) { ones = []; wait ( 0.05 ); for( i = 0; i < m; i ++ ) for( j = 0; j < n; j ++ ) ones[i][j] = 1; return ones; } TransposeMatrix( matrix ) { string = ""; rows = getMatrixRows( matrix ); columns = getMatrixColumns( matrix ); wait ( 0.1 ); for( u = 0; u < rows; u ++ ) { for( k = 0; k < columns; k ++ ) { string += matrix[k][u]; string += ","; } string = FixString( string ); } wait ( 0.05 ); newstring = ""; for( w = 0; w < ( string.size - 1 ); w ++ ) newstring += string[w]; return ( CreateMatrix( newstring ) ); } FixString( string ) { new = ""; for( i = 0; i < ( string.size - 1 ); i ++ ) new += string[i]; new += ";"; return new; } MultiplyMatrixes( matrix1, matrix2 ) { newm = []; sum = 0; m = getMatrixRows( matrix1 ); n = getMatrixColumns( matrix1 ); p = getMatrixRows( matrix2 ); q = getMatrixColumns( matrix2 ); if( n != p ) return; wait ( 0.1 ); for ( i = 0 ; i < m ; i ++ ) { for ( j = 0 ; j < q ; j ++ ) { for ( k = 0 ; k < p ; k++ ) sum += ( matrix1[i][k] * matrix2[k][j] ); newm[i][j] = sum; sum = 0; } } return newm; } AddMatrixes( matrix1, matrix2 ) { m = getMatrixRows( matrix1 ); n = getMatrixColumns( matrix1 ); p = getMatrixRows( matrix2 ); q = getMatrixColumns( matrix2 ); newm = []; if( m != p || n != q ) return; wait ( 0.05 ); for( i = 0; i < m; i ++ ) for( j = 0; j < n; j ++ ) newm[i][j] = ( matrix1[i][j] + matrix2[i][j] ); return newm; } Determinant( matrix ) { if( ! isSquaredMatrix( matrix ) ) return; n = getMatrixOrder( matrix ); m1 = []; m2 = []; det = 0; if( n == 1 ) return matrix[0][0]; wait ( 0.05 ); for( i = 0; i < n; i ++ ) { if( i % 2 == 0 ) { for( y = 0; y < n; y ++ ) for( x = 0; x < n; x ++ ) if(x<i) m1[x][y] = matrix[x][y+1]; else m1[x][y] = matrix[x+1][y+1]; det = det + matrix[i][0] * Determinant( m1, n - 1 ); } else { for( y = 0; y < n; y ++ ) for( x = 0; x < n; x ++ ) if(x<i) m2[x][y] = matrix[x][y+1]; else m2[x][y] = matrix[x+1][y+1]; det = det - matrix[i][0] * Determinant( m2, n - 1 ); } } return det; }

Examples of how to create a matrix and do some operations with them (I recommend to add a small wait after using each command):

Code:
matrixA = CreateMatrix( "2,3,1;1,4,0;2,2,3" ); matrixB = CreateMatrix( "1,1,2;1,1,1;2,2,1" ); detA = Determinant( matrixA ); TransA = TransposeMatrix( matrixA ); multiplication = MultiplyMatrixes( matrixA, matrixB ); add = AddMatrixes( matrixA, matrixB );

Thanks, @Yamato


RE: Matrix Functions - Rendflex - 07-29-2013

So much math!

Thanks, @Rendflex