class Matrix
{
public :
vector< vector< double >> data;
int rows;
int columns;
Matrix ( int rowNum, int columnNum)
{
rows = rowNum;
columns = columnNum;
data. resize ( rows) ;
for ( int i = 0 ; i < rows; i++ )
{
data[ i] . resize ( columns, 0 ) ;
}
}
Matrix ( )
{
rows = 0 ;
columns = 0 ;
}
vector< double > & operator [ ] ( int i)
{
return data[ i] ;
}
Matrix operator * ( Matrix& B)
{
int BColumns = B. columns;
Matrix C ( rows, BColumns) ;
for ( int i = 0 ; i < rows; i++ )
{
for ( int j = 0 ; j < BColumns; j++ )
{
for ( int k = 0 ; k < columns; k++ )
{
C[ i] [ j] += data[ i] [ k] * B[ k] [ j] ;
}
}
}
return C;
}
Matrix sub ( int i, int j)
{
Matrix C ( rows- 1 , columns- 1 ) ;
int r = - 1 ;
int c = - 1 ;
for ( int x = 0 ; x < rows; x++ )
{
if ( x != i)
{
++ r;
for ( int y = 0 ; y < columns; y++ )
{
if ( y != j)
{
++ c;
C[ r] [ c] = data[ x] [ y] ;
}
}
}
}
return C;
}
double det ( )
{
if ( rows == 1 && columns == 1 )
{
return data[ 0 ] [ 0 ] ;
}
else
{
double value = 0 ;
for ( int j = 0 ; j < columns; j++ )
{
value += pow ( - 1 , j + 2 ) * data[ 0 ] [ j] * ( * this ) . sub ( 0 , j) . det ( ) ;
}
return value;
}
}
Matrix inverse ( )
{
Matrix C ( rows, columns) ;
double d = this -> det ( ) ;
for ( int i = 0 ; i < rows; i++ )
{
for ( int j = 0 ; j < columns; j++ )
{
C[ i] [ j] = pow ( - 1 , i + j + 2 ) * ( * this ) . sub ( j, i) . det ( ) ;
C[ i] [ j] /= d;
}
}
return C;
}
void Print ( )
{
for ( int i = 0 ; i < rows; i++ )
{
for ( int j = 0 ; j < columns; j++ )
{
printf ( "%.2f " , data[ i] [ j] ) ;
}
printf ( "\n" ) ;
}
}
void GetData ( )
{
for ( int i = 0 ; i < rows; i++ )
{
for ( int j = 0 ; j < columns; j++ )
{
cin >> data[ i] [ j] ;
}
}
}
} ;