Trabalho inversa de uma matriz
Página 1 de 1
Trabalho inversa de uma matriz
Fazer um programa que calcule a inversa de uma matriz e depois calcular a matriz pela inversa para obter a matriz identidade.
Renancr- Mensagens : 118
Data de inscrição : 08/03/2010
Programa matriz inversa >> identidade
Programa matriz inversa >> identidade
Versão Final long Float melhores resultados
- Código:
#include <iostream>
#include <cstdlib>
#include <iomanip>
using namespace std;
const int lin=3, col=3;
float determinantOfMinor( int theRowHeightY, int theColumnWidthX, const long float theMatrix [/*Y=*/lin] [/*X=*/col] );
float determinant( const long float theMatrix [/*Y=*/lin] [/*X=*/col] );
bool inverse( const long float theMatrix [/*Y=*/lin] [/*X=*/col], long float theOutput [/*Y=*/lin] [/*X=*/col] );
void matrixMultiply( const long float theMatrixA [/*Y=*/lin] [/*X=*/col], const long float theMatrixB [/*Y=*/lin] [/*X=*/col],long float theOutput [/*Y=*/lin] [/*X=*/col]);
void printMatrix( const long float theMatrix [/*Y=*/lin] [/*X=*/col], int indicate);
void main()
{
long float matriz[lin][col], inversa[lin][col], identidade[lin][col], determinante;
int l, c, op, chk=0, chk2=0, chk3=0;
for(l=0; l<lin; l++)
for(c=0; c<col; c++)
{
matriz[l][c]=0;
inversa[l][c]=0;
identidade[l][c]=0;
}
do{
do{
system("cls");
cout<< "1- Inserir valor na matriz\n";
cout<< "2- Mostrar a matriz\n";
if(chk!=0)
{
cout<< "3- Calcular a inversa\n";
if(chk2!=0)
{
cout<< "4- Mostrar a inversa\n";
cout<< "5- Verificar se a matriz * inversa = identidade\n";
}
}
cout<< "6- Sair\n";
cout<< "\n\n\tDigite a opcao desejada\n\t";
cin>> op;
if(op < 1 || op > 6)
{
system("cls");
cout<< "Opcao invalida tente novamente\n\n";
system("pause");
}
}while(op < 1 || op > 6);
if(op == 1)
{
system("cls");
for(l=0; l<3; l++)
{
system("cls");
for(c=0; c<3; c++)
{
cout<< "Digite o valor da matriz linha " << l+1 << " coluna " << c+1 << ".\n";
cin>> matriz[l][c];
}
}
chk=1;
}
else
if(op == 2)
{
system("cls");
printMatrix(matriz, 1);
system("pause");
}
else
if(op == 3)
{
if(chk==0)
{
system("cls");
cout<< "A matriz nao foi preenchida\n\n";
system("pause");
}
else
{
determinante = determinant(matriz);
if(determinante != 0)
{
inverse( matriz, inversa);
chk3=1;
}
else
{
system("cls");
cout<< "\n\n\tEsta matriz nao possui inversa\n\n";
system("pause");
}
chk2=1;
}
}
else
if(op == 4)
{
if(chk==0 || chk2 == 0)
{
system("cls");
cout<< "\n\n\nA matriz nao foi preenchida ou a inversa nao foi calculada\n\n";
system("pause");
}
else
{
system("cls");
printMatrix(inversa, 1);
system("pause");
}
}
else
if(op == 5)
{
if(chk3 == 0)
{
system("cls");
cout<< "\n\n\n\tA inversa ainda nao foi calculada\n\n\n";
system("pause");
}
else
{
system("cls");
matrixMultiply(matriz, inversa, identidade);
printMatrix(matriz, 1);
printMatrix(inversa, 1);
printMatrix(identidade, 2);
system("pause");
}
}
}while(op != 6);
}
float determinantOfMinor( int theRowHeightY, int theColumnWidthX, const long float theMatrix [/*Y=*/lin] [/*X=*/col] )
{
int x1 = theColumnWidthX == 0 ? 1 : 0; /* always either 0 or 1 */
int x2 = theColumnWidthX == 2 ? 1 : 2; /* always either 1 or 2 */
int y1 = theRowHeightY == 0 ? 1 : 0; /* always either 0 or 1 */
int y2 = theRowHeightY == 2 ? 1 : 2; /* always either 1 or 2 */
return ( theMatrix [y1] [x1] * theMatrix [y2] [x2] )
- ( theMatrix [y1] [x2] * theMatrix [y2] [x1] );
}
float determinant( const long float theMatrix [/*Y=*/lin] [/*X=*/col] )
{
return ( theMatrix [0] [0] * determinantOfMinor( 0, 0, theMatrix ) )
- ( theMatrix [0] [1] * determinantOfMinor( 0, 1, theMatrix ) )
+ ( theMatrix [0] [2] * determinantOfMinor( 0, 2, theMatrix ) );
}
bool inverse( const long float theMatrix [/*Y=*/lin] [/*X=*/col], long float theOutput [/*Y=*/lin] [/*X=*/col] )
{
float det = determinant( theMatrix ); /* Arbitrary for now. This should be something nicer... */
if ( abs(det) < 1e-2 )
{
memset( theOutput, 0, sizeof theOutput );
return false;
}
float oneOverDeterminant = 1.0 / det;
for ( int y = 0; y < 3; y ++ )
for ( int x = 0; x < 3; x ++ )
{
/* Rule is inverse = 1/det * minor of the TRANSPOSE matrix. * * Note (y,x) becomes (x,y) INTENTIONALLY here! */
theOutput [y] [x] = determinantOfMinor( x, y, theMatrix ) * oneOverDeterminant;
/* (y0,x1) (y1,x0) (y1,x2) and (y2,x1) all need to be negated. */
if( 1 == ((x + y) % 2) ) theOutput [y] [x] = - theOutput [y] [x];
}
return true;
}
void matrixMultiply( const long float theMatrixA [/*Y=*/lin] [/*X=*/col], const long float theMatrixB [/*Y=*/lin] [/*X=*/col], long float theOutput [/*Y=*/lin] [/*X=*/col])
{
for ( int y = 0; y < 3; y ++ )
for ( int x = 0; x < 3; x ++ )
{
theOutput [y] [x] = 0;
for ( int i = 0; i < 3; i ++ )
theOutput [y] [x] += theMatrixA [y] [i] * theMatrixB [i] [x];
}
}
void printMatrix( const long float theMatrix [/*Y=*/lin] [/*X=*/col], int indicate)
{
if(indicate == 1)
{
for ( int y = 0; y < 3; y ++ )
{
cout << "[ ";
for ( int x = 0; x < 3; x ++ )
cout << setprecision(4) << theMatrix [y] [x] << " ";
cout << "]" << endl;
}
cout << endl;
}
else
{
for ( int y = 0; y < 3; y ++ )
{
cout << "[ ";
for ( int x = 0; x < 3; x ++ )
cout << setprecision(1) << theMatrix [y] [x] << " ";
cout << "]" << endl;
}
cout << endl;
}
}
Versão final usando double
- Código:
#include <iostream>
#include <cstdlib>
using namespace std;
const int lin=3, col=3;
double determinantOfMinor( int theRowHeightY, int theColumnWidthX, const double theMatrix [/*Y=*/lin] [/*X=*/col] );
double determinant( const double theMatrix [/*Y=*/lin] [/*X=*/col] );
bool inverse( const double theMatrix [/*Y=*/lin] [/*X=*/col], double theOutput [/*Y=*/lin] [/*X=*/col] );
void matrixMultiply( const double theMatrixA [/*Y=*/lin] [/*X=*/col], const double theMatrixB [/*Y=*/lin] [/*X=*/col], double theOutput [/*Y=*/lin] [/*X=*/col]);
void printMatrix( const double theMatrix [/*Y=*/lin] [/*X=*/col] );
void main()
{
double matriz[lin][col], inversa[lin][col], identidade[lin][col], determinante;
int l, c, op, chk=0, chk2=0, chk3=0;
for(l=0; l<lin; l++)
for(c=0; c<col; c++)
{
matriz[l][c]=0;
inversa[l][c]=0;
identidade[l][c]=0;
}
do{
do{
system("cls");
cout<< "1- Inserir valor na matriz\n";
cout<< "2- Mostrar a matriz\n";
if(chk!=0)
{
cout<< "3- Calcular a inversa\n";
if(chk2!=0)
{
cout<< "4- Mostrar a inversa\n";
cout<< "5- Verificar se a matriz * inversa = identidade\n";
}
}
cout<< "6- Sair\n";
cout<< "\n\n\tDigite a opcao desejada\n\t";
cin>> op;
if(op < 1 || op > 6)
{
system("cls");
cout<< "Opcao invalida tente novamente\n\n";
system("pause");
}
}while(op < 1 || op > 6);
if(op == 1)
{
system("cls");
for(l=0; l<3; l++)
{
system("cls");
for(c=0; c<3; c++)
{
cout<< "Digite o valor da matriz linha " << l+1 << " coluna " << c+1 << ".\n";
cin>> matriz[l][c];
}
}
chk=1;
}
else
if(op == 2)
{
system("cls");
printMatrix(matriz);
system("pause");
}
else
if(op == 3)
{
if(chk==0)
{
system("cls");
cout<< "A matriz nao foi preenchida\n\n";
system("pause");
}
else
{
determinante = determinant(matriz);
if(determinante != 0)
{
inverse( matriz, inversa);
chk3=1;
}
else
{
system("cls");
cout<< "\n\n\tEsta matriz nao possui inversa\n\n";
system("pause");
}
chk2=1;
}
}
else
if(op == 4)
{
if(chk==0 || chk2 == 0)
{
system("cls");
cout<< "\n\n\nA matriz nao foi preenchida ou a inversa nao foi calculada\n\n";
system("pause");
}
else
{
system("cls");
printMatrix(inversa);
system("pause");
}
}
else
if(op == 5)
{
if(chk3 == 0)
{
system("cls");
cout<< "\n\n\n\tA inversa ainda nao foi calculada\n\n\n";
system("pause");
}
else
{
system("cls");
matrixMultiply(matriz, inversa, identidade);
printMatrix(matriz);
printMatrix(inversa);
printMatrix(identidade);
system("pause");
}
}
}while(op != 6);
}
double determinantOfMinor( int theRowHeightY, int theColumnWidthX, const double theMatrix [/*Y=*/lin] [/*X=*/col] )
{
int x1 = theColumnWidthX == 0 ? 1 : 0; /* always either 0 or 1 */
int x2 = theColumnWidthX == 2 ? 1 : 2; /* always either 1 or 2 */
int y1 = theRowHeightY == 0 ? 1 : 0; /* always either 0 or 1 */
int y2 = theRowHeightY == 2 ? 1 : 2; /* always either 1 or 2 */
return ( theMatrix [y1] [x1] * theMatrix [y2] [x2] )
- ( theMatrix [y1] [x2] * theMatrix [y2] [x1] );
}
double determinant( const double theMatrix [/*Y=*/lin] [/*X=*/col] )
{
return ( theMatrix [0] [0] * determinantOfMinor( 0, 0, theMatrix ) )
- ( theMatrix [0] [1] * determinantOfMinor( 0, 1, theMatrix ) )
+ ( theMatrix [0] [2] * determinantOfMinor( 0, 2, theMatrix ) );
}
bool inverse( const double theMatrix [/*Y=*/lin] [/*X=*/col], double theOutput [/*Y=*/lin] [/*X=*/col] )
{
double det = determinant( theMatrix ); /* Arbitrary for now. This should be something nicer... */
if ( abs(det) < 1e-2 )
{
memset( theOutput, 0, sizeof theOutput );
return false;
}
double oneOverDeterminant = 1.0 / det;
for ( int y = 0; y < 3; y ++ )
for ( int x = 0; x < 3; x ++ )
{
/* Rule is inverse = 1/det * minor of the TRANSPOSE matrix. * * Note (y,x) becomes (x,y) INTENTIONALLY here! */
theOutput [y] [x] = determinantOfMinor( x, y, theMatrix ) * oneOverDeterminant;
/* (y0,x1) (y1,x0) (y1,x2) and (y2,x1) all need to be negated. */
if( 1 == ((x + y) % 2) ) theOutput [y] [x] = - theOutput [y] [x];
}
return true;
}
void matrixMultiply( const double theMatrixA [/*Y=*/lin] [/*X=*/col], const double theMatrixB [/*Y=*/lin] [/*X=*/col], double theOutput [/*Y=*/lin] [/*X=*/col])
{
for ( int y = 0; y < 3; y ++ )
for ( int x = 0; x < 3; x ++ )
{
theOutput [y] [x] = 0;
for ( int i = 0; i < 3; i ++ )
theOutput [y] [x] += theMatrixA [y] [i] * theMatrixB [i] [x];
}
}
void printMatrix( const double theMatrix [/*Y=*/lin] [/*X=*/col] )
{
for ( int y = 0; y < 3; y ++ )
{
cout << "[ ";
for ( int x = 0; x < 3; x ++ )
cout << theMatrix [y] [x] << " ";
cout << "]" << endl;
}
cout << endl;
}
Ver fonte
Renancr- Mensagens : 118
Data de inscrição : 08/03/2010
Página 1 de 1
Permissões neste sub-fórum
Não podes responder a tópicos