inversa de una matriz(operaciones con matrices)c++
Necesito el codigo fuente de la inversa de una matriz, asi como codigo de operaciones con matrices:suma,multiplicacion, division
te envio un programa que realiza la suma y multiplicacion de matrizes.
Para la division : a / b, tienes que encontrar la inversa de b, c = 1/b y realizar la multiplicacion a*c.
Una de las formas de invertir una matriz, es por el metodo de reduccion de Gauss.
Suponiendo que tienes una matriz
1 2 3
1 4 9
1 8 27
escribes a su lado la matriz unitaria
1 2 3 1 0 0
1 4 9 0 1 0
1 8 27 0 0 1
vas rezlizando simultaneamente transformaciones elementales hasta transformar las dos matrizes asi
1 0 0 3 -5/2 1/2
0 1 0 -3/2 2 -1/2
0 0 1 1/3 -1/2 1/6
si no conoces el metodo de Gauss, buscalo en algun libro de algebra lineal.
Exito ! ! !
// program k5a3.CPP - page 77
// for the given matrix of integers : a(5x5), b(5x5), c(1x5), d(5x1)
// calculate : 1 - a + b
// 2 - a * b
// 3 - a * d
// 4 - c * d
// c++ exercices book - dr. gershon kagan (first edition : 2001)
/// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#define MAX 5
enum direction {horizontal,vertical};
void Init(int a[][MAX],int M, int N)
{
for(int i = 0; i < M; i++)
for(int j = 0; j < N; j++)
a[i][j] = random(20) + 1;
} // INIT MATRIX
void Init(int a[],int M)
{
for(int i = 0; i < M; i++)
a[i] = random(20) + 1;
} // INIT ARRAY
void Show(int *a,int M, int N)
{
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
cout << setw(6) << *(a+M*i+j);
cout << endl;
}
cout << endl;
} // SHOW MATRIX
void Show(int *a,int M,direction x)
{
for(int i = 0; i < M; i++)
{
cout << setw(6) << a[i];
if(x == vertical)
cout << endl;
}
cout << endl << endl;
} // SHOW ARRAY
void ProcessOne(int a[][MAX],int b[][MAX])
{
clrscr();
cout << "sum = a + b" << endl << endl;
int sum[MAX][MAX];
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
sum[i][j] = a[i][j] + b[i][j];
Show(&a[0][0],MAX,MAX);
Show(&b[0][0],MAX,MAX);
Show(&sum[0][0],MAX,MAX);
getch();
} // PROCESS ONE
void ProcessTwo(int a[][MAX],int b[][MAX])
{
clrscr();
cout << "mult = a * b" << endl << endl;
int mult[MAX][MAX];
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
{
mult[i][j] = 0;
for(int k = 0; k < MAX; k++)
mult[i][j] += a[i][k]*b[k][j];
}
Show(&a[0][0],MAX,MAX);
Show(&b[0][0],MAX,MAX);
Show(&mult[0][0],MAX,MAX);
getch();
} // PROCESS TWO
void ProcessThree(int a[MAX][MAX],int d[MAX])
{
clrscr();
cout << "mult = a * d" << endl << endl;
int mult[MAX];
for(int i = 0; i < MAX; i++)
{
mult[i] = 0;
for(int j = 0; j < MAX; j++)
mult[i] += a[i][j]*d[j];
}
Show(&a[0][0],MAX,MAX);
Show(d,MAX,vertical);
Show(mult,MAX,vertical);
getch();
} // PROCESS THREE
void ProcessFour(int c[MAX],int d[MAX])
{
clrscr();
cout << "mult = c * d" << endl << endl;
int mult = 0;
for(int i = 0; i < MAX; i++)
mult += c[i]*d[i];
Show(c,MAX,horizontal);
Show(d,MAX,vertical);
cout << setw(6) << mult << endl << endl;
} // PROCESS FOUR
void main()
{
randomize();
int a[MAX][MAX],b[MAX][MAX],c[MAX],d[MAX];
Init(a,MAX,MAX);
Init(b,MAX,MAX);
Init(c,MAX);
Init(d,MAX);
ProcessOne(a,b);
ProcessTwo(a,b);
ProcessThree(a,d);
ProcessFour(c,d);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
/*
sum = a + b
12 5 12 4 13
5 15 14 9 16
3 13 10 8 19
4 3 10 20 5
5 8 5 5 17
14 7 8 2 1
17 18 12 4 5
10 10 13 20 15
9 4 8 14 12
13 10 19 4 17
26 12 20 6 14
22 33 26 13 21
13 23 23 28 34
13 7 18 34 17
18 18 24 9 34
mult = a * b
12 5 12 4 13
5 15 14 9 16
3 13 10 8 19
4 3 10 20 5
5 8 5 5 17
14 7 8 2 1
17 18 12 4 5
10 10 13 20 15
9 4 8 14 12
13 10 19 4 17
578 440 591 392 486
754 641 778 540 670
682 577 735 446 637
452 312 453 520 494
522 419 564 280 469
mult = a * d
12 5 12 4 13
5 15 14 9 16
3 13 10 8 19
4 3 10 20 5
5 8 5 5 17
16
14
12
17
6
552
707
600
596
439
mult = c * d
18 13 4 13 4
16
14
12
17
6
763
end of program - good bye ! ! !
*/
Para la division : a / b, tienes que encontrar la inversa de b, c = 1/b y realizar la multiplicacion a*c.
Una de las formas de invertir una matriz, es por el metodo de reduccion de Gauss.
Suponiendo que tienes una matriz
1 2 3
1 4 9
1 8 27
escribes a su lado la matriz unitaria
1 2 3 1 0 0
1 4 9 0 1 0
1 8 27 0 0 1
vas rezlizando simultaneamente transformaciones elementales hasta transformar las dos matrizes asi
1 0 0 3 -5/2 1/2
0 1 0 -3/2 2 -1/2
0 0 1 1/3 -1/2 1/6
si no conoces el metodo de Gauss, buscalo en algun libro de algebra lineal.
Exito ! ! !
// program k5a3.CPP - page 77
// for the given matrix of integers : a(5x5), b(5x5), c(1x5), d(5x1)
// calculate : 1 - a + b
// 2 - a * b
// 3 - a * d
// 4 - c * d
// c++ exercices book - dr. gershon kagan (first edition : 2001)
/// written in Borland CPP ver 3.1
#include <conio.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#define MAX 5
enum direction {horizontal,vertical};
void Init(int a[][MAX],int M, int N)
{
for(int i = 0; i < M; i++)
for(int j = 0; j < N; j++)
a[i][j] = random(20) + 1;
} // INIT MATRIX
void Init(int a[],int M)
{
for(int i = 0; i < M; i++)
a[i] = random(20) + 1;
} // INIT ARRAY
void Show(int *a,int M, int N)
{
for(int i = 0; i < M; i++)
{
for(int j = 0; j < N; j++)
cout << setw(6) << *(a+M*i+j);
cout << endl;
}
cout << endl;
} // SHOW MATRIX
void Show(int *a,int M,direction x)
{
for(int i = 0; i < M; i++)
{
cout << setw(6) << a[i];
if(x == vertical)
cout << endl;
}
cout << endl << endl;
} // SHOW ARRAY
void ProcessOne(int a[][MAX],int b[][MAX])
{
clrscr();
cout << "sum = a + b" << endl << endl;
int sum[MAX][MAX];
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
sum[i][j] = a[i][j] + b[i][j];
Show(&a[0][0],MAX,MAX);
Show(&b[0][0],MAX,MAX);
Show(&sum[0][0],MAX,MAX);
getch();
} // PROCESS ONE
void ProcessTwo(int a[][MAX],int b[][MAX])
{
clrscr();
cout << "mult = a * b" << endl << endl;
int mult[MAX][MAX];
for(int i = 0; i < MAX; i++)
for(int j = 0; j < MAX; j++)
{
mult[i][j] = 0;
for(int k = 0; k < MAX; k++)
mult[i][j] += a[i][k]*b[k][j];
}
Show(&a[0][0],MAX,MAX);
Show(&b[0][0],MAX,MAX);
Show(&mult[0][0],MAX,MAX);
getch();
} // PROCESS TWO
void ProcessThree(int a[MAX][MAX],int d[MAX])
{
clrscr();
cout << "mult = a * d" << endl << endl;
int mult[MAX];
for(int i = 0; i < MAX; i++)
{
mult[i] = 0;
for(int j = 0; j < MAX; j++)
mult[i] += a[i][j]*d[j];
}
Show(&a[0][0],MAX,MAX);
Show(d,MAX,vertical);
Show(mult,MAX,vertical);
getch();
} // PROCESS THREE
void ProcessFour(int c[MAX],int d[MAX])
{
clrscr();
cout << "mult = c * d" << endl << endl;
int mult = 0;
for(int i = 0; i < MAX; i++)
mult += c[i]*d[i];
Show(c,MAX,horizontal);
Show(d,MAX,vertical);
cout << setw(6) << mult << endl << endl;
} // PROCESS FOUR
void main()
{
randomize();
int a[MAX][MAX],b[MAX][MAX],c[MAX],d[MAX];
Init(a,MAX,MAX);
Init(b,MAX,MAX);
Init(c,MAX);
Init(d,MAX);
ProcessOne(a,b);
ProcessTwo(a,b);
ProcessThree(a,d);
ProcessFour(c,d);
cout << "end of program - good bye ! ! !n";
getch();
} // MAIN
/*
sum = a + b
12 5 12 4 13
5 15 14 9 16
3 13 10 8 19
4 3 10 20 5
5 8 5 5 17
14 7 8 2 1
17 18 12 4 5
10 10 13 20 15
9 4 8 14 12
13 10 19 4 17
26 12 20 6 14
22 33 26 13 21
13 23 23 28 34
13 7 18 34 17
18 18 24 9 34
mult = a * b
12 5 12 4 13
5 15 14 9 16
3 13 10 8 19
4 3 10 20 5
5 8 5 5 17
14 7 8 2 1
17 18 12 4 5
10 10 13 20 15
9 4 8 14 12
13 10 19 4 17
578 440 591 392 486
754 641 778 540 670
682 577 735 446 637
452 312 453 520 494
522 419 564 280 469
mult = a * d
12 5 12 4 13
5 15 14 9 16
3 13 10 8 19
4 3 10 20 5
5 8 5 5 17
16
14
12
17
6
552
707
600
596
439
mult = c * d
18 13 4 13 4
16
14
12
17
6
763
end of program - good bye ! ! !
*/
