#include
#include
#include
using namespace std;
int main() {
int i, j, k, n = 4, m = 5;
double x[4], s;
float mas[4][5] = {{4.4,-2.5,19.2,-10.8,4.3},{5.5,-9.3,-14.2,13.2,6.8},{7.1,-11.5,5.3,-6.7,-1.8},{14.2,23.4,-8.8,5.3,7.2}};
cout << "Метод Гаусса" << endl;
for (i = 0; i < n; i++) {
s = mas[i][i];
for (j = n; j >= i; j--) mas[i][j] /= s;
for (j = i + 1; j < n; j++) {
s = mas[j][i];
for (k = n; k >= i; k--) mas[j][k] -= s * mas[i][k];
}
}
for (i = 0; i < n; i++) {
for (j = 0; j < m; ++j) cout << mas[i][j] << " ";
cout << endl;
}
cout << endl;
x[n - 1] = mas[n - 1][m - 1];
for (i = n - 2; i >= 0; i--) {
x[i] = mas[i][m - 1];
for (j = i + 1; j < n; j++) x[i] -= mas[i][j] * x[j];
}
for (i = 0; i < n; ++i) cout << "x" << i + 1 << " = " << x[i] << endl;
for (i = 0; i < n; i++) {
for (j = 0; j < m-1; ++j) cout << x[i]*x[j] << " ";
cout << endl;
}
return 0;
}
int RowWithMax(double m[n][n], int j)
{
double max_el = m[j][j];
int max_i = j;
for (int i = j; i < n; i++)
{
if (abs(m[i][j]) > abs(max_el))
{
max_el = m[i][j];
max_i = i;
}
}
return max_i;
}
void RowChange(double m[n][n], double f[n], int i1, int i2)
{
for (int j = 0; j < n; j++)
{
/*m[i1][j] = m[i1][j] + m[i2][j];
m[i2][j] = m[i1][j] - m[i2][j];
m[i1][j] = m[i1][j] - m[i2][j];*/
swap(m[i1][j], m[i2][j]);
}
swap(f[i1], f[i2]);
}
double StraightRun(double m[n][n], double f[n], int i) //прямой метод
{
double el;
double det = 1;
int reverse = 0;
int max_i = RowWithMax(m, i);
if (i != max_i)
{
RowChange(m, f, i, max_i);
//reverse++;
det *= (-1);
}
el = m[i][i];
det *= el;
f[i] /= el;
for (int i1 = n - 1; i1 >= i; i1--)
{
m[i][i1] /= el;
}
for (int i2 = i + 1; i2 < n; i2++)
{
el = m[i2][i];
f[i2] -= f[i] * el;
for (int j = n - 1; j >= i; j--)
{
m[i2][j] -= el * m[i][j];
}
}
return det/**pow(-1, reverse)*/;
}