Merge branch 'main' into first_half

This commit is contained in:
emil28092005
2024-09-22 18:06:23 +03:00
committed by GitHub
7 changed files with 131 additions and 36 deletions
-2
View File
@@ -15,7 +15,5 @@ int main() {
b = {24, 6, 1, 2};
std::cout << A << std::endl;
std::cout << C << std::endl;
Simplex(C, A, b, 0.01, true);
return 0;
}
+101
View File
@@ -0,0 +1,101 @@
#include "elimination.h"
#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];
}
for (int j = 0; j < A.getRows(); j++) {
generalMatrix[j][A.getColumns()] = b[j];
}
for (int i = 0; i < A.getRows(); i++) {
for (int j = 0; j < A.getColumns(); j++) {
generalMatrix[i + 1][j] = A[i][j];
}
}
return generalMatrix;
}
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);
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};
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef ELIMINATION_H
#define ELIMINATION_H
class Elimination {
public:
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, double eps);
};
#endif //ELIMINATION_H
-1
View File
@@ -1 +0,0 @@
#include "gauss_jordan.h"
-12
View File
@@ -1,12 +0,0 @@
#ifndef GAUSS_JORDAN_H
#define GAUSS_JORDAN_H
class gauss_jordan {
};
#endif //GAUSS_JORDAN_H
+9 -8
View File
@@ -1,7 +1,7 @@
#include "math.h"
#include "matrix.h"
double Math::min(ColumnVector columnVector) {
double 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::min(ColumnVector columnVector) {
return temp;
}
double Math::max(ColumnVector columnVector) {
double max(ColumnVector columnVector) {
double temp = columnVector[0];
for (int j = 0; j < columnVector.getColumns(); j++) {
if (columnVector[j] > temp) {
@@ -21,7 +21,7 @@ double Math::max(ColumnVector columnVector) {
return temp;
}
int Math::min_index(ColumnVector columnVector) {
int min_index(ColumnVector columnVector) {
int temp_index = 0;
double temp = columnVector[0];
for (int j = 0; j < columnVector.getColumns(); j++) {
@@ -33,7 +33,7 @@ int Math::min_index(ColumnVector columnVector) {
return temp_index;
}
int Math::max_index(ColumnVector columnVector) {
int max_index(ColumnVector columnVector) {
int temp_index = 0;
double temp = columnVector[0];
for (int j = 0; j < columnVector.getColumns(); j++) {
@@ -45,7 +45,7 @@ int Math::max_index(ColumnVector columnVector) {
return temp_index;
}
double Math::min(std::vector<double> array) {
double min(std::vector<double> array) {
double temp = array[0];
for (size_t i = 1; i < array.size(); i++) {
if (array[i] < temp) {
@@ -55,7 +55,7 @@ double Math::min(std::vector<double> array) {
return temp;
}
double Math::max(std::vector<double> array) {
double max(std::vector<double> array) {
double temp = array[0];
for (size_t i = 1; i < array.size(); i++) {
if (array[i] > temp) {
@@ -65,7 +65,7 @@ double Math::max(std::vector<double> array) {
return temp;
}
int Math::min_index(std::vector<double> array) {
int min_index(std::vector<double> array) {
int temp_index = 0;
double temp = array[0];
for (size_t i = 1; i < array.size(); i++) {
@@ -77,7 +77,8 @@ int Math::min_index(std::vector<double> array) {
return temp_index;
}
int Math::max_index(std::vector<double> array) {
int max_index(std::vector<double> array) {
int temp_index = 0;
double temp = array[0];
for (size_t i = 1; i < array.size(); i++) {
+8 -13
View File
@@ -3,18 +3,13 @@
#include <vector>
class ColumnVector;
class Math {
public:
double min(ColumnVector columnVector);
double max(ColumnVector columnVector);
int min_index(ColumnVector columnVector);
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);
};
double min(ColumnVector columnVector);
double max(ColumnVector columnVector);
int min_index(ColumnVector columnVector);
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