update test cases and format code

This commit is contained in:
Ilya Grigorev
2024-10-09 14:09:48 +05:00
parent a41c15f3ee
commit 60bd99e570
8 changed files with 485 additions and 363 deletions
+47 -54
View File
@@ -1,9 +1,10 @@
#include "elimination.h"
#include "math.h"
FracturedMatrix::FracturedMatrix(const FracturedMatrix& other) : A(other.A), C(other.C), b(other.b), pivot_column_index(other.pivot_column_index), pivot_row_index(other.pivot_row_index) {}
FracturedMatrix::FracturedMatrix(const FracturedMatrix &other) : A(other.A), C(other.C), b(other.b), pivot_column_index(other.pivot_column_index), pivot_row_index(other.pivot_row_index) {}
FracturedMatrix::FracturedMatrix(Matrix A, Vector C, Vector b, int pivot_column_index, int pivot_row_index) : A(A), C(C), b(b), pivot_column_index(pivot_column_index), pivot_row_index(pivot_row_index) {}
FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) {
FracturedMatrix &FracturedMatrix::operator=(const FracturedMatrix &other)
{
A = other.A;
C = other.C;
b = other.b;
@@ -12,7 +13,8 @@ FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) {
return *this;
}
DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
DestroyMatrix disassembleGeneralMatrix(Matrix &generalMatrix)
{
int rows = generalMatrix.getRows();
int cols = generalMatrix.getColumns();
@@ -20,16 +22,20 @@ DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
Vector C(cols - 1);
Vector b(rows);
for (int j = 0; j < cols - 1; ++j) {
for (int j = 0; j < cols - 1; ++j)
{
C[j] = generalMatrix[0][j];
}
for (int i = 0; i < rows; ++i) {
for (int i = 0; i < rows; ++i)
{
b[i] = generalMatrix[i][cols - 1];
}
for (int i = 1; i < rows; ++i) {
for (int j = 0; j < cols - 1; ++j) {
for (int i = 1; i < rows; ++i)
{
for (int j = 0; j < cols - 1; ++j)
{
A[i - 1][j] = generalMatrix[i][j];
}
}
@@ -37,89 +43,76 @@ DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
return {A, C, b};
}
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b) {
Matrix createGeneralMatrix(Matrix &A, Vector &C, Vector &b)
{
int m = A.getRows() + 1;
int n = A.getColumns() + 1;
// States if equation has a slack variable
std::vector<bool> has_slack(A.getColumns(), false);
int number_of_slack = 0;
for (int j = 0; j < A.getColumns(); ++j) {
int basic_var_index = 0;
int ones = 0;
int zeros = 0;
for (int i = 0; i < A.getRows(); ++i) {
if (A[i][j] == 1) {
basic_var_index = i;
++ones;
} else if (A[i][j] == 0) {
++zeros;
}
}
if (ones + zeros == A.getRows()) {
has_slack[basic_var_index] = true;
number_of_slack += 1;
}
}
n = n + A.getRows() - number_of_slack;
int n = A.getColumns() + A.getRows() + 1;
Matrix generalMatrix(m, n);
for (int i = 0; i < n; i++) {
if (i < C.size()) {
for (int i = 0; i < n; i++)
{
if (i < C.size())
{
generalMatrix[0][i] = C[i];
} else {
}
else
{
generalMatrix[0][i] = 0;
}
}
// For objective function (j=0) the value is set to zero one step before
for (int j = 1; j < m; j++) {
generalMatrix[j][n-1] = b[j-1];
for (int j = 1; j < m; j++)
{
generalMatrix[j][n - 1] = b[j - 1];
}
int k = 0;
for (int i = 0; i < m - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (j < A.getColumns()) {
for (int i = 0; i < m - 1; i++)
{
for (int j = 0; j < n - 1; j++)
{
if (j < A.getColumns())
{
generalMatrix[i + 1][j] = A[i][j];
} else {
}
else
{
generalMatrix[i + 1][j] = 0;
}
}
if (!has_slack[i]) {
generalMatrix[i + 1][A.getColumns() + k] = 1;
++k;
}
generalMatrix[i + 1][A.getColumns() + k] = 1;
++k;
}
return generalMatrix;
}
void elimination(Matrix& generalMatrix, int pivot_row_index, int pivot_column_index) {
void elimination(Matrix &generalMatrix, int pivot_row_index, int pivot_column_index)
{
int rows = generalMatrix.getRows();
int cols = generalMatrix.getColumns();
double pivotElement = generalMatrix[pivot_row_index][pivot_column_index];
for (int j = 0; j < cols; ++j) {
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)
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) {
for (int j = 0; j < cols; ++j)
{
generalMatrix[i][j] -= pivotColumnCoefficient * generalMatrix[pivot_row_index][j];
}
}
}
+13 -13
View File
@@ -3,28 +3,28 @@
#include "matrix.h"
struct FracturedMatrix {
struct FracturedMatrix
{
Matrix A;
Vector C;
Vector b;
int pivot_column_index;
int pivot_row_index;
FracturedMatrix() = default;
FracturedMatrix(const FracturedMatrix& other);
FracturedMatrix(const FracturedMatrix &other);
FracturedMatrix(Matrix A, Vector C, Vector b, int pivot_column_index, int pivot_row_index);
FracturedMatrix& operator=(const FracturedMatrix& other);
FracturedMatrix &operator=(const FracturedMatrix &other);
};
struct DestroyMatrix {
Matrix A;
Vector C;
Vector b;
struct DestroyMatrix
{
Matrix A;
Vector C;
Vector b;
};
DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix);
void elimination(Matrix&, int pivot_row_index, int pivot_column_index);
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b);
DestroyMatrix disassembleGeneralMatrix(Matrix &generalMatrix);
void elimination(Matrix &, int pivot_row_index, int pivot_column_index);
Matrix createGeneralMatrix(Matrix &A, Vector &C, Vector &b);
#endif //ELIMINATION_H
#endif // ELIMINATION_H
+58 -30
View File
@@ -1,39 +1,50 @@
#include "math.h"
double min(Vector vector) {
double min(Vector vector)
{
double temp = vector[0];
for (int j = 0; j < vector.size(); j++) {
if (vector[j] < temp) {
for (int j = 0; j < vector.size(); j++)
{
if (vector[j] < temp)
{
temp = vector[j];
}
}
return temp;
}
double max(Vector vector) {
double max(Vector vector)
{
double temp = vector[0];
for (int j = 0; j < vector.size(); j++) {
if (vector[j] > temp) {
for (int j = 0; j < vector.size(); j++)
{
if (vector[j] > temp)
{
temp = vector[j];
}
}
return temp;
}
int min_index_positive(Vector vector) {
int min_index_positive(Vector vector)
{
int i = 0;
while (i < vector.size() && vector[i] <= 0) {
while (i < vector.size() && vector[i] <= 0)
{
++i;
}
if (i >= vector.size()) {
if (i >= vector.size())
{
// No positive value found -> iterations stop
return -1;
}
int temp_index = i;
double temp = vector[i];
for (int j = i + 1; j < vector.size(); j++) {
if (vector[j] < temp && vector[j] > 0) {
for (int j = i + 1; j < vector.size(); j++)
{
if (vector[j] < temp && vector[j] > 0)
{
temp_index = j;
temp = vector[j];
}
@@ -41,11 +52,14 @@ int min_index_positive(Vector vector) {
return temp_index;
}
int min_index(Vector vector) {
int min_index(Vector vector)
{
int temp_index = 0;
double temp = vector[0];
for (int j = 0; j < vector.size(); j++) {
if (vector[j] < temp) {
for (int j = 0; j < vector.size(); j++)
{
if (vector[j] < temp)
{
temp_index = j;
temp = vector[j];
}
@@ -53,11 +67,14 @@ int min_index(Vector vector) {
return temp_index;
}
int max_index(Vector vector) {
int max_index(Vector vector)
{
int temp_index = 0;
double temp = vector[0];
for (int j = 0; j < vector.size(); j++) {
if (vector[j] > temp) {
for (int j = 0; j < vector.size(); j++)
{
if (vector[j] > temp)
{
temp_index = j;
temp = vector[j];
}
@@ -65,31 +82,40 @@ int max_index(Vector vector) {
return temp_index;
}
double 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) {
for (size_t i = 1; i < array.size(); i++)
{
if (array[i] < temp)
{
temp = array[i];
}
}
return temp;
}
double 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) {
for (size_t i = 1; i < array.size(); i++)
{
if (array[i] > temp)
{
temp = array[i];
}
}
return temp;
}
int 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++) {
if (array[i] < temp) {
for (size_t i = 1; i < array.size(); i++)
{
if (array[i] < temp)
{
temp_index = i;
temp = array[i];
}
@@ -97,12 +123,14 @@ int min_index(std::vector<double> array) {
return temp_index;
}
int 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++) {
if (array[i] > temp) {
for (size_t i = 1; i < array.size(); i++)
{
if (array[i] > temp)
{
temp_index = i;
temp = array[i];
}
+131 -68
View File
@@ -1,142 +1,175 @@
#include "matrix.h"
Vector::Vector(int n) {
Vector::Vector(int n)
{
rows = n;
columns = 1;
vector.resize(n);
}
Vector::Vector(const Vector& other) {
Vector::Vector(const Vector &other)
{
rows = other.rows;
columns = other.columns;
vector = other.vector;
}
Vector::Vector(std::initializer_list<double> init) {
Vector::Vector(std::initializer_list<double> init)
{
rows = init.size();
columns = 1;
vector = std::vector<double>(rows);
auto it = init.begin();
for (size_t i = 0; i < init.size(); ++i) {
for (size_t i = 0; i < init.size(); ++i)
{
vector[i] = *it++;
}
}
int Vector::size() const {
int Vector::size() const
{
return rows;
}
double& Vector::operator[](int row) {
double &Vector::operator[](int row)
{
return vector[row];
}
Vector& Vector::operator=(const Vector& other) {
Vector &Vector::operator=(const Vector &other)
{
rows = other.rows;
columns = other.columns;
vector = other.vector;
return *this;
}
Vector Vector::operator+(Vector& other) {
if (rows != other.rows) {
Vector Vector::operator+(Vector &other)
{
if (rows != other.rows)
{
throw std::runtime_error("Error: the dimensional problem occurred");
}
Vector result(rows);
for (int i = 0; i < rows; ++i) {
for (int i = 0; i < rows; ++i)
{
result[i] = vector[i] + other[i];
}
return result;
}
Vector Vector::operator-(Vector& other) {
if (rows != other.rows) {
Vector Vector::operator-(Vector &other)
{
if (rows != other.rows)
{
throw std::runtime_error("Error: the dimensional problem occurred");
}
Vector result(rows);
for (int i = 0; i < rows; ++i) {
for (int i = 0; i < rows; ++i)
{
result[i] = vector[i] - other[i];
}
return result;
}
std::istream& operator>>(std::istream& cin, Vector& vectorObj) {
for (int i = 0; i < vectorObj.rows; ++i) {
std::istream &operator>>(std::istream &cin, Vector &vectorObj)
{
for (int i = 0; i < vectorObj.rows; ++i)
{
cin >> vectorObj[i];
}
return cin;
}
std::ostream& operator<<(std::ostream& cout, Vector& vectorObj) {
for (int i = 0; i < vectorObj.rows; ++i) {
if (i == vectorObj.rows - 1) {
std::ostream &operator<<(std::ostream &cout, Vector &vectorObj)
{
for (int i = 0; i < vectorObj.rows; ++i)
{
if (i == vectorObj.rows - 1)
{
cout << vectorObj[i] << std::endl;
}
else {
else
{
cout << vectorObj[i] << ' ';
}
}
return cout;
}
Matrix::Matrix(int n, int m) {
Matrix::Matrix(int n, int m)
{
rows = n;
columns = m;
matrix.resize(n, Vector(m));
for (auto& row : matrix) {
for (auto &row : matrix)
{
row = Vector(m);
}
}
Matrix::Matrix(const Matrix& other) {
Matrix::Matrix(const Matrix &other)
{
rows = other.rows;
columns = other.columns;
matrix = other.matrix;
}
Matrix::Matrix(std::initializer_list<std::vector<double>> init) {
Matrix::Matrix(std::initializer_list<std::vector<double>> init)
{
rows = init.size();
auto it = init.begin();
columns = it->size();
matrix = std::vector<Vector>(rows, Vector(columns));
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
matrix[i][j] = it->operator[](j);
}
++it;
}
}
int Matrix::getRows() const {
int Matrix::getRows() const
{
return rows;
}
int Matrix::getColumns() const {
int Matrix::getColumns() const
{
return columns;
}
Vector& Matrix::operator[](int row) {
Vector &Matrix::operator[](int row)
{
return matrix[row];
}
Matrix& Matrix::operator=(const Matrix& other) {
Matrix &Matrix::operator=(const Matrix &other)
{
rows = other.rows;
columns = other.columns;
matrix = other.matrix;
return *this;
}
Matrix Matrix::operator+(Matrix& other) const {
if (rows != other.rows || columns != other.columns) {
Matrix Matrix::operator+(Matrix &other) const
{
if (rows != other.rows || columns != other.columns)
{
throw std::runtime_error("Error: the dimensional problem occurred");
}
Matrix result(rows, columns);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
auto x = matrix[i];
auto y = other[i];
result[i][j] = x[j] + y[j];
@@ -146,13 +179,17 @@ Matrix Matrix::operator+(Matrix& other) const {
return result;
}
Matrix Matrix::operator-(Matrix& other) const {
if (rows != other.rows || columns != other.columns) {
Matrix Matrix::operator-(Matrix &other) const
{
if (rows != other.rows || columns != other.columns)
{
throw std::runtime_error("Error: the dimensional problem occurred");
}
Matrix result(rows, columns);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
auto x = matrix[i];
auto y = other[i];
result[i][j] = x[j] - y[j];
@@ -162,16 +199,21 @@ Matrix Matrix::operator-(Matrix& other) const {
return result;
}
Matrix Matrix::operator*(Matrix& other) const {
if (columns != other.rows) {
Matrix Matrix::operator*(Matrix &other) const
{
if (columns != other.rows)
{
throw std::runtime_error("Error: the dimensional problem occurred");
}
Matrix result(rows, other.columns);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < other.columns; ++j) {
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < other.columns; ++j)
{
result[i][j] = 0;
for (int k = 0; k < columns; ++k) {
for (int k = 0; k < columns; ++k)
{
auto x = matrix[i];
auto y = other[k];
result[i][j] += x[k] * y[j];
@@ -182,15 +224,19 @@ Matrix Matrix::operator*(Matrix& other) const {
return result;
}
Vector Matrix::operator*(Vector other) const {
if (columns != other.size()) {
Vector Matrix::operator*(Vector other) const
{
if (columns != other.size())
{
throw std::runtime_error("Error: the dimensional problem occurred");
}
Vector result(rows);
for (int i = 0; i < rows; ++i) {
for (int i = 0; i < rows; ++i)
{
result[i] = 0;
for (int k = 0; k < columns; ++k) {
for (int k = 0; k < columns; ++k)
{
auto x = matrix[i];
result[i] += x[k] * other[k];
}
@@ -199,10 +245,13 @@ Vector Matrix::operator*(Vector other) const {
return result;
}
Matrix Matrix::transpose() const {
Matrix Matrix::transpose() const
{
Matrix result(columns, rows);
for (int i = 0; i < rows; ++i) {
for (int j = 0; j < columns; ++j) {
for (int i = 0; i < rows; ++i)
{
for (int j = 0; j < columns; ++j)
{
auto x = matrix[i];
result[j][i] = x[j];
}
@@ -211,51 +260,65 @@ Matrix Matrix::transpose() const {
return result;
}
std::istream& operator>>(std::istream& cin, Matrix& matrixObj) {
for (int i = 0; i < matrixObj.rows; ++i) {
for (int j = 0; j < matrixObj.columns; ++j) {
std::istream &operator>>(std::istream &cin, Matrix &matrixObj)
{
for (int i = 0; i < matrixObj.rows; ++i)
{
for (int j = 0; j < matrixObj.columns; ++j)
{
cin >> matrixObj[i][j];
}
}
return cin;
}
std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) {
std::ostream &operator<<(std::ostream &cout, Matrix &matrixObj)
{
size_t maxNumberLength = 1;
for (int y = 0; y < matrixObj.getRows(); y++) {
for (int x = 0; x < matrixObj.getColumns(); x++) {
if (std::to_string(matrixObj[y][x]).length() > maxNumberLength) {
for (int y = 0; y < matrixObj.getRows(); y++)
{
for (int x = 0; x < matrixObj.getColumns(); x++)
{
if (std::to_string(matrixObj[y][x]).length() > maxNumberLength)
{
maxNumberLength = std::to_string(matrixObj[y][x]).length();
}
}
}
//std::cout << maxNumberLength << std::endl;
// std::cout << maxNumberLength << std::endl;
std::cout << std::endl;
for (int y = 0; y < matrixObj.getRows(); y++) {
for (int y = 0; y < matrixObj.getRows(); y++)
{
std::string row = "";
for (int x = 0; x < matrixObj.getColumns(); x++) {
for (int x = 0; x < matrixObj.getColumns(); x++)
{
std::string strNumber = std::to_string(matrixObj[y][x]);
bool spaceRight = true;
while (strNumber.length() < maxNumberLength) {
if (spaceRight) {
while (strNumber.length() < maxNumberLength)
{
if (spaceRight)
{
strNumber += " ";
}
else {
else
{
strNumber = " " + strNumber;
}
spaceRight = !spaceRight;
strNumber = "" + strNumber;
}
if (matrixObj[y][x] == 0) {
if (matrixObj[y][x] == 0)
{
row += "[\033[0m" + strNumber + "\033[0m] ";
}
else if(matrixObj[y][x] > 0) {
else if (matrixObj[y][x] > 0)
{
row += "[\033[32m" + strNumber + "\033[0m] ";
}else {
}
else
{
row += "[\033[31m" + strNumber + "\033[0m] ";
}
}
std::cout << row << std::endl;
}
+33 -30
View File
@@ -6,12 +6,12 @@
#include <vector>
/**
* Vector is a class to represent
* a column vector with n rows.
*/
* Vector is a class to represent
* a column vector with n rows.
*/
class Vector {
class Vector
{
protected:
// Number of rows in vector
int rows;
@@ -19,39 +19,41 @@ protected:
int columns;
// Matrix representation as vector of vectors of integers
std::vector<double> vector;
public:
Vector(int n);
Vector(const Vector& other);
Vector(const Vector &other);
Vector(std::initializer_list<double>);
/* Getter for the number of rows */
int size() const;
double& operator[](int row);
double &operator[](int row);
Vector& operator=(const Vector& other);
Vector &operator=(const Vector &other);
Vector operator+(Vector& other);
Vector operator+(Vector &other);
Vector operator-(Vector& other);
Vector operator-(Vector &other);
/* Input operator reads element of vector */
friend std::istream& operator>>(std::istream& cin, Vector& vectorObj);
friend std::istream &operator>>(std::istream &cin, Vector &vectorObj);
/* Output operator prints elements of the vector
* in a row separated with a space (no space at the end of the line)
*/
friend std::ostream& operator<<(std::ostream& cout, Vector& vectorObj);
* in a row separated with a space (no space at the end of the line)
*/
friend std::ostream &operator<<(std::ostream &cout, Vector &vectorObj);
};
/**
* Class Matrix represents
* a matrix of size n x m
* of type integer.
*/
class Matrix {
* Class Matrix represents
* a matrix of size n x m
* of type integer.
*/
class Matrix
{
protected:
// Number of rows in matrix
int rows;
@@ -59,10 +61,11 @@ protected:
int columns;
// Matrix representation as vector of vectors of integers
std::vector<Vector> matrix;
public:
Matrix(int n, int m);
Matrix(const Matrix& other);
Matrix(const Matrix &other);
Matrix(std::initializer_list<std::vector<double>>);
@@ -72,15 +75,15 @@ public:
/* Getter for the number of columns */
int getColumns() const;
Vector& operator[](int row);
Vector &operator[](int row);
Matrix& operator=(const Matrix& other);
Matrix &operator=(const Matrix &other);
Matrix operator+(Matrix& other) const;
Matrix operator+(Matrix &other) const;
Matrix operator-(Matrix& other) const;
Matrix operator-(Matrix &other) const;
Matrix operator*(Matrix& other) const;
Matrix operator*(Matrix &other) const;
/* Matrix-Vector multiplication */
Vector operator*(Vector other) const;
@@ -89,12 +92,12 @@ public:
Matrix transpose() const;
/* Input operator reads element of matrix row by row */
friend std::istream& operator>>(std::istream& cin, Matrix& matrixObj);
friend std::istream &operator>>(std::istream &cin, Matrix &matrixObj);
/* Output operator prints elements of the matrix
* row by row separated with a space (no space at the end of each line)
*/
friend std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj);
* row by row separated with a space (no space at the end of each line)
*/
friend std::ostream &operator<<(std::ostream &cout, Matrix &matrixObj);
};
#endif // TOOLS_MATRIX_H
#endif // TOOLS_MATRIX_H