Examples - pi.science.math.PIMatrix
1. How to multiplicate two matrices ?
Suppose this two matrices (Ax=B):
matrices
/* create two matrices */ PIMatrix matrixMultiplicationA = new PIMatrix( 4, 3 ); PIMatrix matrixMultiplicationB = new PIMatrix( 3, 2 ); /* fill data (by rows) */ matrixMultiplicationA.AddValues( new int[] { 2, 0, 1, 3, 4, 2, 1, 1, 0, -1, -2, -3 } ); matrixMultiplicationB.AddValues( new int[] { 1, -1, 2, 1, 3, -2 } ); /* perform A*B */ PIMatrix matrixMultiplication = matrixMultiplicationA.Multiply( matrixMultiplicationB ); /* show result matrix */ Console.WriteLine( matrixMultiplication.AsString( 0 ) );
Output:
5 | -4 | 17 | -3 | 3 | 0 | -14 | 5 |
Multiplication result:
result
2. How to perform matrix inversion ?
Suppose this matrix:
matrix
/* create matrix */ PIMatrix matrixInverseA = new PIMatrix( 2, 2 ); /* fill data (by rows) */ matrixInverseA.AddValues( new int[] { 1, -2, -3, 8 } ); /* inverse matrix */ PIMatrix matrixInverse = matrixInverseA.Inverse(); /* show result matrix */ Console.WriteLine( matrixInverse.AsString( 1 ) );
Output:
4.0 | 1.0 | 1.5 | 0.5 |
Inversion result:
result
3. How to calc matrix determinant ?
Suppose this matrix:
matrix
/* create matrix */ PIMatrix matrixDeterminant44 = new PIMatrix( 4, 4 ); /* fill data (by rows) */ matrixDeterminant44.AddValues( new int[] { 1, 2, 1, 1, 2, 5, 1, 2, 3, 2, 2, 1, 1, 3, 2, 2 } ); /* compute matrix determinant */ double valueDeterminant44 = matrixDeterminant44.Determinant(); /* show result */ Console.WriteLine( valueDeterminant44 );
Output:
-1.0
4. How to get matrix rank ?
Suppose this matrix:
matrix
/* create matrix */ PIMatrix matrixRank = new PIMatrix( 3, 6 ); /* fill data (by rows) */ matrixRank.AddValues( new int[] { 3, 1, -8, 2, 1, 0, 2 , -2, -3, -7, 2, 0, 1, -5, 2, -16, 3, 0 } ); /* get rank */ int valueRank = matrixRank.Rank(); /* show result */ Console.WriteLine( valueRank );
Output:
2