-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatriz_Inversa.cpp
More file actions
126 lines (107 loc) · 3.44 KB
/
Matriz_Inversa.cpp
File metadata and controls
126 lines (107 loc) · 3.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <vector>
#include <cmath> // Incluye esta cabecera para std::abs
using namespace std;
const double EPS = 1e-9;
double determinant(vector<vector<double> > a) {
int n = a.size();
double det = 1.0;
for (int i = 0; i < n; i++) {
int maxRow = i;
for (int k = i + 1; k < n; k++) {
if (std::abs(a[k][i]) > std::abs(a[maxRow][i])) { // Usa std::abs aquí
maxRow = k;
}
}
swap(a[maxRow], a[i]);
det *= a[i][i] * (i != maxRow ? -1 : 1);
for (int k = i + 1; k < n; k++) {
double factor = a[k][i] / a[i][i];
for (int j = i + 1; j < n; j++) {
a[k][j] -= factor * a[i][j];
}
}
}
return det;
}
vector<vector<double> > inverseMatrix(vector<vector<double> > a) {
int n = a.size();
vector<vector<double> > res(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
res[i][i] = 1.0;
}
for (int i = 0; i < n; i++) {
int maxRow = i;
for (int k = i + 1; k < n; k++) {
if (std::abs(a[k][i]) > std::abs(a[maxRow][i])) {
maxRow = k;
}
}
swap(a[maxRow], a[i]);
swap(res[maxRow], res[i]);
double div = a[i][i];
for (int j = 0; j < n; j++) {
a[i][j] /= div;
res[i][j] /= div;
}
for (int k = 0; k < n; k++) {
if (k != i) {
double factor = a[k][i];
for (int j = 0; j < n; j++) {
a[k][j] -= factor * a[i][j];
res[k][j] -= factor * res[i][j];
}
}
}
}
return res;
}
vector<vector<double> > multiplyMatrices(const vector<vector<double> >& A, const vector<vector<double> >& B) {
int n = A.size();
vector<vector<double> > result(n, vector<double>(n, 0));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
result[i][j] += A[i][k] * B[k][j];
}
}
}
return result;
}
int main() {
int n;
cout << "Ingrese el numero de filas y columnas de la matriz: ";
cin >> n;
vector<vector<double> > mat(n, vector<double>(n));
cout << "Ingrese los elementos de la matriz:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << "Elemento [" << i + 1 << "][" << j + 1 << "]: ";
cin >> mat[i][j];
}
}
double det = determinant(mat);
if (std::abs(det) < EPS) { // Usa std::abs aquí también
cout << "La matriz no tiene inversa." << endl;
cout << "No existe porque el determinante es = " << det << endl;
} else {
vector<vector<double> > inv = inverseMatrix(mat);
cout << "Matriz inversa:\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cout << inv[i][j] << " ";
}
cout << endl;
}
vector<vector<double> > identity = multiplyMatrices(mat, inv);
cout << "\nMultiplicacion de la matriz original con su inversa (debería ser la identidad):\n";
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if (std::abs(identity[i][j]) < EPS) identity[i][j] = 0; // Y aquí
cout << identity[i][j] << " ";
}
cout << endl;
}
}
return 0;
}