Elimination func created
This commit is contained in:
@@ -2,15 +2,45 @@
|
||||
#include "tools/matrix.h"
|
||||
#include "tools/math.h"
|
||||
|
||||
struct DestroyMatrix {
|
||||
Matrix A;
|
||||
ColumnVector C;
|
||||
ColumnVector b;
|
||||
};
|
||||
|
||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
||||
int rows = generalMatrix.getRows();
|
||||
int cols = generalMatrix.getColumns();
|
||||
|
||||
Matrix A(rows - 1, cols - 1);
|
||||
ColumnVector C(cols - 1);
|
||||
ColumnVector b(rows - 1);
|
||||
|
||||
for (int j = 0; j < cols - 1; ++j) {
|
||||
C[j] = generalMatrix[0][j];
|
||||
}
|
||||
|
||||
for (int i = 1; i < rows; ++i) {
|
||||
b[i - 1] = generalMatrix[i - 1][cols - 1];
|
||||
}
|
||||
|
||||
for (int i = 1; i < rows; ++i) {
|
||||
for (int j = 0; j < cols - 1; ++j) {
|
||||
A[i - 1][j] = generalMatrix[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return {A, C, b};
|
||||
}
|
||||
|
||||
Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
||||
Matrix generalMatrix(A.getRows() + 1, A.getColumns() + 1);
|
||||
for (int i = 0; i < A.getColumns(); i++) {
|
||||
generalMatrix[0][i] = C[i];
|
||||
}
|
||||
|
||||
generalMatrix[0][A.getColumns()] = 0;
|
||||
for (int j = 0; j < A.getRows(); j++) {
|
||||
generalMatrix[j + 1][A.getColumns()] = b[j];
|
||||
generalMatrix[j][A.getColumns()] = b[j];
|
||||
}
|
||||
|
||||
for (int i = 0; i < A.getRows(); i++) {
|
||||
@@ -28,13 +58,13 @@ int main() {
|
||||
std::cin >> m;
|
||||
|
||||
ColumnVector C(n);
|
||||
Matrix A(n, m);
|
||||
ColumnVector b(n);
|
||||
Matrix A(m - 1, n);
|
||||
ColumnVector b(m);
|
||||
double eps;
|
||||
double eps_default;
|
||||
std::cin >> C;
|
||||
std::cin >> A;
|
||||
std::cin >> b;
|
||||
std::cin >> A;
|
||||
|
||||
std::cout << "Вектор C: " << C << std::endl;
|
||||
std::cout << "Вектор b: " << b << std::endl;
|
||||
@@ -42,8 +72,14 @@ int main() {
|
||||
std::cout << "Матрица A: " << A << std::endl;
|
||||
|
||||
Matrix E = createGeneralMatrix(A, C, b);
|
||||
std::cout << "Матрица E: " << E << std::endl;
|
||||
|
||||
DestroyMatrix destroyed = destroyGeneralMatrix(E);
|
||||
std::cout << "Вектор C: " << destroyed.C << std::endl;
|
||||
std::cout << "Вектор b: " << destroyed.b << std::endl;
|
||||
|
||||
std::cout << "Матрица A: " << destroyed.A << std::endl;
|
||||
|
||||
std::cout << "Новая матрица:" << E << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+75
-5
@@ -2,15 +2,53 @@
|
||||
#include "matrix.h"
|
||||
#include "math.h"
|
||||
|
||||
struct FracturedMatrix {
|
||||
Matrix A;
|
||||
ColumnVector C;
|
||||
ColumnVector b;
|
||||
int pivot_column_index;
|
||||
int pivot_row_index;
|
||||
};
|
||||
|
||||
struct DestroyMatrix {
|
||||
Matrix A;
|
||||
ColumnVector C;
|
||||
ColumnVector b;
|
||||
};
|
||||
|
||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
||||
int rows = generalMatrix.getRows();
|
||||
int cols = generalMatrix.getColumns();
|
||||
|
||||
Matrix A(rows - 1, cols - 1);
|
||||
ColumnVector C(cols - 1);
|
||||
ColumnVector b(rows - 1);
|
||||
|
||||
for (int j = 0; j < cols - 1; ++j) {
|
||||
C[j] = generalMatrix[0][j];
|
||||
}
|
||||
|
||||
for (int i = 1; i < rows; ++i) {
|
||||
b[i - 1] = generalMatrix[i - 1][cols - 1];
|
||||
}
|
||||
|
||||
for (int i = 1; i < rows; ++i) {
|
||||
for (int j = 0; j < cols - 1; ++j) {
|
||||
A[i - 1][j] = generalMatrix[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
return {A, C, b};
|
||||
}
|
||||
|
||||
Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
||||
Matrix generalMatrix(A.getRows() + 1, A.getColumns() + 1);
|
||||
for (int i = 0; i < A.getColumns(); i++) {
|
||||
generalMatrix[0][i] = C[i];
|
||||
}
|
||||
|
||||
generalMatrix[0][A.getColumns()] = 0;
|
||||
for (int j = 0; j < A.getRows(); j++) {
|
||||
generalMatrix[j + 1][A.getColumns()] = b[j];
|
||||
generalMatrix[j][A.getColumns()] = b[j];
|
||||
}
|
||||
|
||||
for (int i = 0; i < A.getRows(); i++) {
|
||||
@@ -22,10 +60,42 @@ Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
||||
return generalMatrix;
|
||||
}
|
||||
|
||||
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index, double eps) {
|
||||
FracturedMatrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) {
|
||||
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
||||
|
||||
int rows = generalMatrix.getRows();
|
||||
int cols = generalMatrix.getColumns();
|
||||
|
||||
double pivotElement = generalMatrix[pivot_row_index][pivot_column_index];
|
||||
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
generalMatrix[pivot_row_index][j] /= pivotElement;
|
||||
}
|
||||
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
if (i == pivot_row_index)
|
||||
continue;
|
||||
|
||||
double pivotColumnCoefficient = generalMatrix[i][pivot_column_index];
|
||||
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
generalMatrix[i][j] -= pivotColumnCoefficient * generalMatrix[pivot_row_index][j];
|
||||
}
|
||||
}
|
||||
|
||||
DestroyMatrix destroyedMatrix = destroyGeneralMatrix(generalMatrix);
|
||||
|
||||
pivot_column_index = Math::max_index(destroyedMatrix.C);
|
||||
|
||||
|
||||
return generalMatrix;
|
||||
ColumnVector ratio_vector(A.getRows());
|
||||
|
||||
for (int i = 0; i < A.getRows(); i++) {
|
||||
ratio_vector[i] = b[i] / A[i][pivot_column_index];
|
||||
}
|
||||
|
||||
int pivot_row_index = Math::min_index(ratio_vector);
|
||||
|
||||
return {A, C, b, pivot_column_index, pivot_row_index};
|
||||
}
|
||||
|
||||
|
||||
|
||||
+1
-1
@@ -5,7 +5,7 @@
|
||||
|
||||
class Elimination {
|
||||
public:
|
||||
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index, double eps);
|
||||
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, double eps);
|
||||
};
|
||||
|
||||
|
||||
|
||||
+52
-4
@@ -1,7 +1,7 @@
|
||||
#include "math.h"
|
||||
#include "matrix.h"
|
||||
|
||||
double Math::minVector(ColumnVector columnVector) {
|
||||
double Math::min(ColumnVector columnVector) {
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] < temp) {
|
||||
@@ -11,7 +11,7 @@ double Math::minVector(ColumnVector columnVector) {
|
||||
return temp;
|
||||
}
|
||||
|
||||
double Math::maxVector(ColumnVector columnVector) {
|
||||
double Math::max(ColumnVector columnVector) {
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] > temp) {
|
||||
@@ -21,7 +21,31 @@ double Math::maxVector(ColumnVector columnVector) {
|
||||
return temp;
|
||||
}
|
||||
|
||||
double Math::minArray(std::vector<double> array) {
|
||||
int Math::min_index(ColumnVector columnVector) {
|
||||
int temp_index = 0;
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] < temp) {
|
||||
temp_index = j;
|
||||
temp = columnVector[j];
|
||||
}
|
||||
}
|
||||
return temp_index;
|
||||
}
|
||||
|
||||
int Math::max_index(ColumnVector columnVector) {
|
||||
int temp_index = 0;
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] > temp) {
|
||||
temp_index = j;
|
||||
temp = columnVector[j];
|
||||
}
|
||||
}
|
||||
return temp_index;
|
||||
}
|
||||
|
||||
double Math::min(std::vector<double> array) {
|
||||
double temp = array[0];
|
||||
for (size_t i = 1; i < array.size(); i++) {
|
||||
if (array[i] < temp) {
|
||||
@@ -31,7 +55,7 @@ double Math::minArray(std::vector<double> array) {
|
||||
return temp;
|
||||
}
|
||||
|
||||
double Math::maxArray(std::vector<double> array) {
|
||||
double Math::max(std::vector<double> array) {
|
||||
double temp = array[0];
|
||||
for (size_t i = 1; i < array.size(); i++) {
|
||||
if (array[i] > temp) {
|
||||
@@ -40,3 +64,27 @@ double Math::maxArray(std::vector<double> array) {
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
double Math::min(std::vector<double> array) {
|
||||
int temp_index = 0;
|
||||
double temp = array[0];
|
||||
for (size_t i = 1; i < array.size(); i++) {
|
||||
if (array[i] < temp) {
|
||||
temp_index = i;
|
||||
temp = array[i];
|
||||
}
|
||||
}
|
||||
return temp_index;
|
||||
}
|
||||
|
||||
double Math::max(std::vector<double> array) {
|
||||
int temp_index = 0;
|
||||
double temp = array[0];
|
||||
for (size_t i = 1; i < array.size(); i++) {
|
||||
if (array[i] > temp) {
|
||||
temp_index = i;
|
||||
temp = array[i];
|
||||
}
|
||||
}
|
||||
return temp_index;
|
||||
}
|
||||
+8
-5
@@ -4,14 +4,17 @@
|
||||
#include <vector>
|
||||
|
||||
class ColumnVector;
|
||||
class Matrix;
|
||||
|
||||
class Math {
|
||||
public:
|
||||
double minVector(ColumnVector columnVector);
|
||||
double maxVector(ColumnVector columnVector);
|
||||
double minArray(std::vector<double> array);
|
||||
double maxArray(std::vector<double> array);
|
||||
double min(ColumnVector columnVector);
|
||||
double max(ColumnVector columnVector);
|
||||
static int min_index(ColumnVector columnVector);
|
||||
static int max_index(ColumnVector columnVector);
|
||||
double min(std::vector<double> array);
|
||||
double max(std::vector<double> array);
|
||||
int min_index(std::vector<double> array);
|
||||
int max_index(std::vector<double> array);
|
||||
};
|
||||
|
||||
#endif // MATH_H
|
||||
Reference in New Issue
Block a user