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
+125 -115
View File
@@ -4,94 +4,91 @@
#include "tools/math.h" #include "tools/math.h"
#include "simplex.h" #include "simplex.h"
void _printInitialInputs(Vector& C, Matrix& A, Vector& b, double eps, bool maximize) { void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maximize)
std::cout << "Running for the following inputs:" << std::endl << std::endl; {
std::cout << "Running for the following inputs:" << std::endl
<< std::endl;
std::cout << "epsilon: " << eps << std::endl; std::cout << "epsilon: " << eps << std::endl;
if (maximize) if (maximize)
{ {
std::cout << "Maximize" << std::endl; std::cout << "Maximize" << std::endl;
} }
else { else
{
std::cout << "Minimize" << std::endl; std::cout << "Minimize" << std::endl;
} }
std::cout << "z = "; std::cout << "z = ";
bool previousIsNegative = false; for (int i = 0; i < C.size(); i++)
for (size_t i = 0; i < C.size(); i++)
{ {
bool isNegative = false; bool isNegative = false;
if (C[i] != 0){ if (i != 0)
if (i != 0 && !previousIsNegative){ {
std::cout << " + "; std::cout << " + ";
}
if (C[i] != 1)
{
if (C[i] < 0)
{
isNegative = true;
std::cout << "(";
} }
if (C[i] != 1) { std::cout << C[i] << " * ";
if (C[i] < 0){
isNegative = true;
std::cout << "(";
}
std::cout << C[i] << " * ";
}
std::cout << "x" << i + 1;
if (isNegative){
std::cout << ")";
}
}else {
previousIsNegative = true;
} }
std::cout << "x" << i + 1;
if (isNegative)
{
std::cout << ")";
}
} }
std::cout << std::endl << "subject to the constrains:" << std::endl; std::cout << std::endl
std::cout<< std::endl; << "subject to the constrains:" << std::endl;
for (size_t i = 0; i < b.size(); i++) std::cout << std::endl;
for (int i = 0; i < b.size(); i++)
{ {
bool previousIsZero = false; for (int j = 0; j < A.getColumns(); j++)
for (size_t j = 0; j < A.getColumns(); j++)
{ {
bool isNegative = false; bool isNegative = false;
if (A[i][j] != 0) { if (j != 0)
if (j != 0 && !previousIsZero){ {
std::cout << " + "; std::cout << " + ";
previousIsZero = false; }
if (A[i][j] != 1)
{
if (A[i][j] < 0)
{
isNegative = true;
std::cout << "(";
} }
if (A[i][j] != 1) { std::cout << A[i][j] << " * ";
if (A[i][j] < 0) { }
isNegative = true; std::cout << "x" << j + 1;
std::cout << "("; if (isNegative)
} {
std::cout << A[i][j] << " * "; std::cout << ")";
}
std::cout << "x" << j + 1;
if (isNegative) {
std::cout << ")";
}
}else {
previousIsZero = true;
} }
} }
std::cout << " <= " << b[i] << std::endl; std::cout << " <= " << b[i] << std::endl;
} }
} }
int printResult(Result result)
{
int printResult(Result result) { if (result.state == unsolvable)
{
if (result.state == unsolvable) {
std::cout << "The method is not applicable!" << std::endl; std::cout << "The method is not applicable!" << std::endl;
}else { }
else
{
std::cout << "SOLVED!" << std::endl; std::cout << "SOLVED!" << std::endl;
std::cout << "Decision variables: ["; std::cout << "Decision variables: [";
for (size_t i = 0; i < result.solution.size(); i++) for (int i = 0; i < result.solution.size(); i++)
{ {
std::cout << result.solution[i]; std::cout << result.solution[i];
if (i != result.solution.size()-1){ if (i != result.solution.size() - 1)
{
std::cout << ", "; std::cout << ", ";
} }
} }
@@ -100,7 +97,9 @@ int printResult(Result result) {
if (result.maximize) if (result.maximize)
{ {
std::cout << "Maximum "; std::cout << "Maximum ";
}else{ }
else
{
std::cout << "Minimum "; std::cout << "Minimum ";
} }
std::cout << "objective function value: " << result.objective_function_value << std::endl; std::cout << "objective function value: " << result.objective_function_value << std::endl;
@@ -109,7 +108,8 @@ int printResult(Result result) {
return 0; return 0;
} }
bool check_eq(double a, double b, double relativeEpsilon = 0.0001) { bool check_eq(double a, double b, double relativeEpsilon = 0.0001)
{
double diff = std::abs(a - b); double diff = std::abs(a - b);
a = std::abs(a); a = std::abs(a);
b = std::abs(b); b = std::abs(b);
@@ -118,7 +118,8 @@ bool check_eq(double a, double b, double relativeEpsilon = 0.0001) {
return diff <= largest * relativeEpsilon; return diff <= largest * relativeEpsilon;
} }
int TEST_GENERAL_CASE() { int TEST_GENERAL_CASE()
{
std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl;
Vector C = {5, 4}; Vector C = {5, 4};
@@ -126,16 +127,17 @@ int TEST_GENERAL_CASE() {
{6, 4}, {6, 4},
{1, 2}, {1, 2},
{-1, 1}, {-1, 1},
{0, 1} {0, 1}};
};
Vector b = {24, 6, 1, 2}; Vector b = {24, 6, 1, 2};
_printInitialInputs(C, A, b, 0.01, true); _printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b); auto result = simplex(C, A, b);
if (!(result.state == bounded)) { if (!(result.state == bounded))
{
std::string state_name; std::string state_name;
switch (result.state) { switch (result.state)
{
case unsolvable: case unsolvable:
state_name = "unsolvable"; state_name = "unsolvable";
break; break;
@@ -150,14 +152,16 @@ int TEST_GENERAL_CASE() {
return 0; return 0;
} }
if (!check_eq(result.objective_function_value, 21)) { if (!check_eq(result.objective_function_value, 21))
std::cout << "Incorrect objective function value. Expected 21. Got " //ЭТО УДАЛИТЬ? {
std::cout << "Incorrect objective function value. Expected 21. Got " // ЭТО УДАЛИТЬ?
<< result.objective_function_value << std::endl; << result.objective_function_value << std::endl;
return 0; return 0;
} }
if (!( check_eq(result.solution[0],3) && check_eq(result.solution[1], 1.5) )) { if (!(check_eq(result.solution[0], 3) && check_eq(result.solution[1], 1.5)))
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got " //ЭТО УДАЛИТЬ? {
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got " // ЭТО УДАЛИТЬ?
<< result.solution; << result.solution;
return 0; return 0;
} }
@@ -167,23 +171,25 @@ int TEST_GENERAL_CASE() {
return 1; return 1;
} }
int TEST_MINIMIZE_CASE() { int TEST_MINIMIZE_CASE()
{
std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl;
Vector C = {-2, 2, -6}; Vector C = {-2, 2, -6};
Matrix A = { Matrix A = {
{2, 1, -2}, {2, 1, -2},
{1, 2, 4}, {1, 2, 4},
{1, -1, 2} {1, -1, 2}};
};
Vector b = {24, 23, 10}; Vector b = {24, 23, 10};
_printInitialInputs(C, A, b, 0.01, false); _printInitialInputs(C, A, b, 0.01, false);
auto result = simplex(C, A, b, 0.01, false); auto result = simplex(C, A, b, 0.01, false);
if (!(result.state == bounded)) { if (!(result.state == bounded))
{
std::string state_name; std::string state_name;
switch (result.state) { switch (result.state)
{
case unsolvable: case unsolvable:
state_name = "unsolvable"; state_name = "unsolvable";
break; break;
@@ -198,15 +204,15 @@ int TEST_MINIMIZE_CASE() {
return 0; return 0;
} }
if (!check_eq(result.objective_function_value, -30.75)) { if (!check_eq(result.objective_function_value, -30.75))
{
std::cout << "Incorrect objective function value. Expected -30.75. Got " std::cout << "Incorrect objective function value. Expected -30.75. Got "
<< result.objective_function_value << std::endl; << result.objective_function_value << std::endl;
return 0; return 0;
} }
if (!( check_eq(result.solution[0], 0) && check_eq(result.solution[1], 0.75) if (!(check_eq(result.solution[0], 0) && check_eq(result.solution[1], 0.75) && check_eq(result.solution[2], 5.375)))
&& check_eq(result.solution[2],5.375) {
)) {
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got " std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got "
<< result.solution; << result.solution;
return 0; return 0;
@@ -217,25 +223,26 @@ int TEST_MINIMIZE_CASE() {
return 1; return 1;
} }
int TEST_WITH_SLACK_CASE() { int TEST_WITH_SLACK_CASE()
{
std::cout << "----------------------------RUNNING_TEST_WITH_SLACK_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_WITH_SLACK_CASE----------------------------" << std::endl;
Vector C = {5, 4}; Vector C = {2, -1, 0, -1};
Matrix A = { Matrix A = {
{6, 4, 1}, {1, -2, 1, 0},
{1, 2, 0}, {-2, -1, 0, -2},
{-1, 1, 0}, {3, 2, 0, 1}};
{0, 1, 0} Vector b = {10, 18, 36};
};
Vector b = {24, 6, 1, 2};
_printInitialInputs(C, A, b, 0.01, true); _printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b); auto result = simplex(C, A, b);
if (!(result.state == bounded)) { if (!(result.state == bounded))
{
std::string state_name; std::string state_name;
switch (result.state) { switch (result.state)
{
case unsolvable: case unsolvable:
state_name = "unsolvable"; state_name = "unsolvable";
break; break;
@@ -250,41 +257,45 @@ int TEST_WITH_SLACK_CASE() {
return 0; return 0;
} }
if (result.objective_function_value != 21) { if (result.objective_function_value != 22.25)
std::cout << "Incorrect objective function value. Expected 21. Got " {
std::cout << "Incorrect objective function value. Expected 22.25. Got "
<< result.objective_function_value << std::endl; << result.objective_function_value << std::endl;
return 0; return 0;
} }
if (!((result.solution[0] == 3) || (result.solution[1] == 1.5))) { if (!((result.solution[0] == 11.5) || (result.solution[1] == 0.75) ||
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got " (result.solution[2] == 0) || (result.solution[3] == 0)))
<< result.solution[0] << result.solution[1]; {
std::cout << "Incorrect desire variables. Expected 11.5, 0.75, 0, and 0. Got "
<< result.solution;
return 0; return 0;
} }
printResult(result); printResult(result);
return 1; return 1;
} }
int TEST_UNBOUNDED_CASE() { int TEST_UNBOUNDED_CASE()
{
std::cout << "----------------------------RUNNING_TEST_UNBOUNDED_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_UNBOUNDED_CASE----------------------------" << std::endl;
Vector C = {2, 1}; Vector C = {2, 1};
Matrix A = { Matrix A = {
{1, -1}, {1, -1},
{2, 0} {2, 0}};
};
Vector b = {10, 40}; Vector b = {10, 40};
_printInitialInputs(C, A, b, 0.01, true); _printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b); auto result = simplex(C, A, b);
if (!(result.state == unbounded)) { if (!(result.state == unbounded))
{
std::string state_name; std::string state_name;
switch (result.state) { switch (result.state)
{
case unsolvable: case unsolvable:
state_name = "unsolvable"; state_name = "unsolvable";
break; break;
@@ -304,25 +315,27 @@ int TEST_UNBOUNDED_CASE() {
return 1; return 1;
} }
int TEST_UNSOLVABLE_CASE() { int TEST_UNSOLVABLE_CASE()
{
std::cout << "----------------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------------" << std::endl; std::cout << "----------------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------------" << std::endl;
Vector C = {5, 4}; Vector C = {5, 4, 0, -5, 13};
Matrix A = { Matrix A = {
{6, 4}, {6, 4, 1, 3, 4},
{1, 2}, {1, 2, 0, 0, 2},
{-1, 1}, {-1, 0, 0, 10, 0},
{0, 1} {0, 1, 1, -5, 1}};
};
Vector b = {-24, 6, 1, 2}; Vector b = {-24, 6, 1, 2};
_printInitialInputs(C, A, b, 0.01, true); _printInitialInputs(C, A, b, 0.01, true);
auto result = simplex(C, A, b); auto result = simplex(C, A, b);
if (!(result.state == unsolvable)) { if (!(result.state == unsolvable))
{
std::string state_name; std::string state_name;
switch (result.state) { switch (result.state)
{
case unsolvable: case unsolvable:
state_name = "unsolvable"; state_name = "unsolvable";
break; break;
@@ -342,21 +355,20 @@ int TEST_UNSOLVABLE_CASE() {
return 1; return 1;
} }
int main()
{
int main() {
std::vector<std::function<int(void)>> tests = { std::vector<std::function<int(void)>> tests = {
TEST_GENERAL_CASE, TEST_GENERAL_CASE,
TEST_MINIMIZE_CASE, TEST_MINIMIZE_CASE,
TEST_WITH_SLACK_CASE, TEST_WITH_SLACK_CASE,
TEST_UNBOUNDED_CASE, TEST_UNBOUNDED_CASE,
TEST_UNSOLVABLE_CASE TEST_UNSOLVABLE_CASE};
};
int counter = 0; int counter = 0;
for (auto& test : tests) { for (auto &test : tests)
{
counter += test(); counter += test();
} }
@@ -366,5 +378,3 @@ int main() {
return 0; return 0;
} }
+61 -38
View File
@@ -4,39 +4,48 @@
#include "tools/math.h" #include "tools/math.h"
#include "tools/elimination.h" #include "tools/elimination.h"
enum solver_state { enum solver_state
{
unbounded, unbounded,
bounded, bounded,
unsolvable unsolvable
}; };
struct Result { struct Result
{
solver_state state; solver_state state;
Vector solution; Vector solution;
double objective_function_value; double objective_function_value;
bool maximize; bool maximize;
}; };
void _stopIterating(Matrix& generalMatrix, Vector& C, std::vector<int>& basicVars, solver_state state, Result& result) { void _stopIterating(Matrix &generalMatrix, Vector &C, std::vector<int> &basicVars, solver_state state, Result &result)
{
DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix); DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix);
Matrix _A = destroyedGeneralMatrix.A; Matrix _A = destroyedGeneralMatrix.A;
Vector _C = destroyedGeneralMatrix.C; Vector _C = destroyedGeneralMatrix.C;
Vector _b = destroyedGeneralMatrix.b; Vector _b = destroyedGeneralMatrix.b;
result.state = state; result.state = state;
if (state == bounded) { if (state == bounded)
{
result.solution = Vector(C.size()); result.solution = Vector(C.size());
for (int i = 0; i < C.size(); i++) { for (int i = 0; i < C.size(); i++)
{
result.solution[i] = 0; result.solution[i] = 0;
} }
for (size_t i = 1; i < basicVars.size(); i++) { for (size_t i = 1; i < basicVars.size(); i++)
if (basicVars[i] < C.size()) { {
if (basicVars[i] < C.size())
{
result.solution[basicVars[i]] = _b[i]; result.solution[basicVars[i]] = _b[i];
} }
} }
result.objective_function_value = _b[0]; result.objective_function_value = _b[0];
} else { }
else
{
result.solution = Vector({0}); result.solution = Vector({0});
result.objective_function_value = 0; result.objective_function_value = 0;
} }
@@ -45,76 +54,90 @@ void _stopIterating(Matrix& generalMatrix, Vector& C, std::vector<int>& basicVar
/* /*
Implementation of the Simplex method. Implementation of the Simplex method.
*/ */
Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize=true) { Result simplex(Vector &C, Matrix &A, Vector &b, double eps = 0.01, bool maximize = true)
{
if (maximize == true) { if (maximize == true)
for (int i = 0; i < C.size(); i++) { {
C[i] = -C[i]; for (int i = 0; i < C.size(); i++)
{
C[i] = -C[i];
} }
} }
Result result{}; Result result{};
result.maximize = maximize; result.maximize = maximize;
Matrix generalMatrix = createGeneralMatrix(A, C, b); Matrix generalMatrix = createGeneralMatrix(A, C, b);
std::cout << generalMatrix; std::cout << generalMatrix;
std::vector<int> basicVars(generalMatrix.getRows()); std::vector<int> basicVars(generalMatrix.getRows());
basicVars[0] = -1; basicVars[0] = -1;
for (int i = 0; i < b.size(); ++i) { for (int i = 0; i < b.size(); ++i)
if (b[i] < 0) { {
if (b[i] < 0)
{
_stopIterating(generalMatrix, C, basicVars, unsolvable, result); _stopIterating(generalMatrix, C, basicVars, unsolvable, result);
return result; return result;
} }
} }
for (size_t i = 1; i < basicVars.size(); i++) { for (size_t i = 1; i < basicVars.size(); i++)
{
basicVars[i] = static_cast<int>(basicVars.size()) + i; basicVars[i] = static_cast<int>(basicVars.size()) + i;
} }
int iterationCount = 0; int iterationCount = 0;
while (true) { while (true)
//3 {
// 3
iterationCount++; iterationCount++;
int pivot_column_index = 0; int pivot_column_index = 0;
pivot_column_index = min_index(generalMatrix[0]); pivot_column_index = min_index(generalMatrix[0]);
if (generalMatrix[0][pivot_column_index] >= 0) { if (generalMatrix[0][pivot_column_index] >= 0)
{
_stopIterating(generalMatrix, C, basicVars, bounded, result); _stopIterating(generalMatrix, C, basicVars, bounded, result);
if (!maximize) { if (!maximize)
{
result.objective_function_value = -result.objective_function_value; result.objective_function_value = -result.objective_function_value;
} }
return result; return result;
} }
// 4
//4
Vector ratio_vector(generalMatrix.getRows()); Vector ratio_vector(generalMatrix.getRows());
for (int i = 1; i < generalMatrix.getRows(); i++) { for (int i = 1; i < generalMatrix.getRows(); i++)
if (generalMatrix[i][pivot_column_index] != 0) { {
ratio_vector[i] = generalMatrix[i][generalMatrix.getColumns() - 1] / generalMatrix[i][pivot_column_index]; if (generalMatrix[i][pivot_column_index] != 0)
if (std::abs(ratio_vector[i]) < eps) { {
ratio_vector[i] = generalMatrix[i][generalMatrix.getColumns() - 1] / generalMatrix[i][pivot_column_index];
if (std::abs(ratio_vector[i]) < eps)
{
ratio_vector[i] = 0; ratio_vector[i] = 0;
} }
} else { }
ratio_vector[i] = 0; else
} {
ratio_vector[i] = 0;
}
} }
ratio_vector[0] = 0; ratio_vector[0] = 0;
int pivot_row_index = min_index_positive(ratio_vector); int pivot_row_index = min_index_positive(ratio_vector);
// No leaving variable exists // No leaving variable exists
if (pivot_row_index == -1) { if (pivot_row_index == -1)
{
_stopIterating(generalMatrix, C, basicVars, unbounded, result); _stopIterating(generalMatrix, C, basicVars, unbounded, result);
return result; return result;
} }
basicVars[pivot_row_index] = pivot_column_index; basicVars[pivot_row_index] = pivot_column_index;
//5 // 5
elimination(generalMatrix, pivot_row_index, pivot_column_index); elimination(generalMatrix, pivot_row_index, pivot_column_index);
std::cout << "Iteration "<< iterationCount << " " << std::endl;; std::cout << "Iteration " << iterationCount << " " << std::endl;
;
std::cout << generalMatrix; std::cout << generalMatrix;
} }
return result; return result;
@@ -133,10 +156,10 @@ Steps:
1. Print the optimization problem: 1. Print the optimization problem:
- max (or min) z = C[0] * x1 + C[1] * x2 + ... + C[n] * xn - max (or min) z = C[0] * x1 + C[1] * x2 + ... + C[n] * xn
- subject to the constraints: - subject to the constraints:
- A[0] * x <= b[0] - A[0] * x <= b[0]
- A[1] * x <= b[1] - A[1] * x <= b[1]
- ... - ...
- A[m] * x <= b[m] - A[m] * x <= b[m]
2. Initialize: 2. Initialize:
- Form the initial tableau by introducing slack variables to convert inequalities into equalities. - Form the initial tableau by introducing slack variables to convert inequalities into equalities.
+7 -5
View File
@@ -4,21 +4,23 @@
#include <vector> #include <vector>
#include "tools/matrix.h" #include "tools/matrix.h"
enum solver_state { enum solver_state
{
unbounded, unbounded,
bounded, bounded,
unsolvable unsolvable
}; };
struct Result { struct Result
{
solver_state state; solver_state state;
Vector solution; Vector solution;
double objective_function_value; double objective_function_value;
bool maximize; bool maximize;
}; };
void _printInitialInputs(Vector& C, Matrix& A, Vector& b); void _printInitialInputs(Vector &C, Matrix &A, Vector &b);
void _stopIterating(Matrix& generalMatrix, std::vector<int>& basicVars, Result& result); void _stopIterating(Matrix &generalMatrix, std::vector<int> &basicVars, Result &result);
Result simplex(Vector& C, Matrix& A, Vector& b, double eps = 0.01, bool maximize = true); Result simplex(Vector &C, Matrix &A, Vector &b, double eps = 0.01, bool maximize = true);
#endif // SIMPLEX_H #endif // SIMPLEX_H
+46 -53
View File
@@ -1,9 +1,10 @@
#include "elimination.h" #include "elimination.h"
#include "math.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(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; A = other.A;
C = other.C; C = other.C;
b = other.b; b = other.b;
@@ -12,7 +13,8 @@ FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) {
return *this; return *this;
} }
DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) { DestroyMatrix disassembleGeneralMatrix(Matrix &generalMatrix)
{
int rows = generalMatrix.getRows(); int rows = generalMatrix.getRows();
int cols = generalMatrix.getColumns(); int cols = generalMatrix.getColumns();
@@ -20,16 +22,20 @@ DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
Vector C(cols - 1); Vector C(cols - 1);
Vector b(rows); Vector b(rows);
for (int j = 0; j < cols - 1; ++j) { for (int j = 0; j < cols - 1; ++j)
{
C[j] = generalMatrix[0][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]; b[i] = generalMatrix[i][cols - 1];
} }
for (int i = 1; i < rows; ++i) { for (int i = 1; i < rows; ++i)
for (int j = 0; j < cols - 1; ++j) { {
for (int j = 0; j < cols - 1; ++j)
{
A[i - 1][j] = generalMatrix[i][j]; A[i - 1][j] = generalMatrix[i][j];
} }
} }
@@ -37,89 +43,76 @@ DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix) {
return {A, C, b}; 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 m = A.getRows() + 1;
int n = A.getColumns() + 1; int n = A.getColumns() + A.getRows() + 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;
Matrix generalMatrix(m, n); 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]; generalMatrix[0][i] = C[i];
} else { }
else
{
generalMatrix[0][i] = 0; generalMatrix[0][i] = 0;
} }
} }
// For objective function (j=0) the value is set to zero one step before // For objective function (j=0) the value is set to zero one step before
for (int j = 1; j < m; j++) { for (int j = 1; j < m; j++)
generalMatrix[j][n-1] = b[j-1]; {
generalMatrix[j][n - 1] = b[j - 1];
} }
int k = 0; int k = 0;
for (int i = 0; i < m - 1; i++) { for (int i = 0; i < m - 1; i++)
for (int j = 0; j < n - 1; j++) { {
if (j < A.getColumns()) { for (int j = 0; j < n - 1; j++)
{
if (j < A.getColumns())
{
generalMatrix[i + 1][j] = A[i][j]; generalMatrix[i + 1][j] = A[i][j];
} else { }
else
{
generalMatrix[i + 1][j] = 0; generalMatrix[i + 1][j] = 0;
} }
} }
if (!has_slack[i]) { generalMatrix[i + 1][A.getColumns() + k] = 1;
generalMatrix[i + 1][A.getColumns() + k] = 1; ++k;
++k;
}
} }
return generalMatrix; 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 rows = generalMatrix.getRows();
int cols = generalMatrix.getColumns(); int cols = generalMatrix.getColumns();
double pivotElement = generalMatrix[pivot_row_index][pivot_column_index]; 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; generalMatrix[pivot_row_index][j] /= pivotElement;
} }
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
{
if (i == pivot_row_index) if (i == pivot_row_index)
continue; continue;
double pivotColumnCoefficient = generalMatrix[i][pivot_column_index]; 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]; generalMatrix[i][j] -= pivotColumnCoefficient * generalMatrix[pivot_row_index][j];
} }
} }
} }
+13 -13
View File
@@ -3,28 +3,28 @@
#include "matrix.h" #include "matrix.h"
struct FracturedMatrix
struct FracturedMatrix { {
Matrix A; Matrix A;
Vector C; Vector C;
Vector b; Vector b;
int pivot_column_index; int pivot_column_index;
int pivot_row_index; int pivot_row_index;
FracturedMatrix() = default; 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(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 { struct DestroyMatrix
Matrix A; {
Vector C; Matrix A;
Vector b; Vector C;
Vector b;
}; };
DestroyMatrix disassembleGeneralMatrix(Matrix& generalMatrix); DestroyMatrix disassembleGeneralMatrix(Matrix &generalMatrix);
void elimination(Matrix&, int pivot_row_index, int pivot_column_index); void elimination(Matrix &, int pivot_row_index, int pivot_column_index);
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b); 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" #include "math.h"
double min(Vector vector) { double min(Vector vector)
{
double temp = vector[0]; double temp = vector[0];
for (int j = 0; j < vector.size(); j++) { for (int j = 0; j < vector.size(); j++)
if (vector[j] < temp) { {
if (vector[j] < temp)
{
temp = vector[j]; temp = vector[j];
} }
} }
return temp; return temp;
} }
double max(Vector vector) { double max(Vector vector)
{
double temp = vector[0]; double temp = vector[0];
for (int j = 0; j < vector.size(); j++) { for (int j = 0; j < vector.size(); j++)
if (vector[j] > temp) { {
if (vector[j] > temp)
{
temp = vector[j]; temp = vector[j];
} }
} }
return temp; return temp;
} }
int min_index_positive(Vector vector) { int min_index_positive(Vector vector)
{
int i = 0; int i = 0;
while (i < vector.size() && vector[i] <= 0) { while (i < vector.size() && vector[i] <= 0)
{
++i; ++i;
} }
if (i >= vector.size()) { if (i >= vector.size())
{
// No positive value found -> iterations stop // No positive value found -> iterations stop
return -1; return -1;
} }
int temp_index = i; int temp_index = i;
double temp = vector[i]; double temp = vector[i];
for (int j = i + 1; j < vector.size(); j++) { for (int j = i + 1; j < vector.size(); j++)
if (vector[j] < temp && vector[j] > 0) { {
if (vector[j] < temp && vector[j] > 0)
{
temp_index = j; temp_index = j;
temp = vector[j]; temp = vector[j];
} }
@@ -41,11 +52,14 @@ int min_index_positive(Vector vector) {
return temp_index; return temp_index;
} }
int min_index(Vector vector) { int min_index(Vector vector)
{
int temp_index = 0; int temp_index = 0;
double temp = vector[0]; double temp = vector[0];
for (int j = 0; j < vector.size(); j++) { for (int j = 0; j < vector.size(); j++)
if (vector[j] < temp) { {
if (vector[j] < temp)
{
temp_index = j; temp_index = j;
temp = vector[j]; temp = vector[j];
} }
@@ -53,11 +67,14 @@ int min_index(Vector vector) {
return temp_index; return temp_index;
} }
int max_index(Vector vector) { int max_index(Vector vector)
{
int temp_index = 0; int temp_index = 0;
double temp = vector[0]; double temp = vector[0];
for (int j = 0; j < vector.size(); j++) { for (int j = 0; j < vector.size(); j++)
if (vector[j] > temp) { {
if (vector[j] > temp)
{
temp_index = j; temp_index = j;
temp = vector[j]; temp = vector[j];
} }
@@ -65,31 +82,40 @@ int max_index(Vector vector) {
return temp_index; return temp_index;
} }
double min(std::vector<double> array) { double min(std::vector<double> array)
{
double temp = array[0]; double temp = array[0];
for (size_t i = 1; i < array.size(); i++) { for (size_t i = 1; i < array.size(); i++)
if (array[i] < temp) { {
if (array[i] < temp)
{
temp = array[i]; temp = array[i];
} }
} }
return temp; return temp;
} }
double max(std::vector<double> array) { double max(std::vector<double> array)
{
double temp = array[0]; double temp = array[0];
for (size_t i = 1; i < array.size(); i++) { for (size_t i = 1; i < array.size(); i++)
if (array[i] > temp) { {
if (array[i] > temp)
{
temp = array[i]; temp = array[i];
} }
} }
return temp; return temp;
} }
int min_index(std::vector<double> array) { int min_index(std::vector<double> array)
{
int temp_index = 0; int temp_index = 0;
double temp = array[0]; double temp = array[0];
for (size_t i = 1; i < array.size(); i++) { for (size_t i = 1; i < array.size(); i++)
if (array[i] < temp) { {
if (array[i] < temp)
{
temp_index = i; temp_index = i;
temp = array[i]; temp = array[i];
} }
@@ -97,12 +123,14 @@ int min_index(std::vector<double> array) {
return temp_index; return temp_index;
} }
int max_index(std::vector<double> array)
int max_index(std::vector<double> array) { {
int temp_index = 0; int temp_index = 0;
double temp = array[0]; double temp = array[0];
for (size_t i = 1; i < array.size(); i++) { for (size_t i = 1; i < array.size(); i++)
if (array[i] > temp) { {
if (array[i] > temp)
{
temp_index = i; temp_index = i;
temp = array[i]; temp = array[i];
} }
+130 -67
View File
@@ -1,142 +1,175 @@
#include "matrix.h" #include "matrix.h"
Vector::Vector(int n) { Vector::Vector(int n)
{
rows = n; rows = n;
columns = 1; columns = 1;
vector.resize(n); vector.resize(n);
} }
Vector::Vector(const Vector& other) { Vector::Vector(const Vector &other)
{
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
vector = other.vector; vector = other.vector;
} }
Vector::Vector(std::initializer_list<double> init) { Vector::Vector(std::initializer_list<double> init)
{
rows = init.size(); rows = init.size();
columns = 1; columns = 1;
vector = std::vector<double>(rows); vector = std::vector<double>(rows);
auto it = init.begin(); 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++; vector[i] = *it++;
} }
} }
int Vector::size() const { int Vector::size() const
{
return rows; return rows;
} }
double& Vector::operator[](int row) { double &Vector::operator[](int row)
{
return vector[row]; return vector[row];
} }
Vector& Vector::operator=(const Vector& other) { Vector &Vector::operator=(const Vector &other)
{
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
vector = other.vector; vector = other.vector;
return *this; return *this;
} }
Vector Vector::operator+(Vector& other) { Vector Vector::operator+(Vector &other)
if (rows != other.rows) { {
if (rows != other.rows)
{
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Vector result(rows); Vector result(rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
{
result[i] = vector[i] + other[i]; result[i] = vector[i] + other[i];
} }
return result; return result;
} }
Vector Vector::operator-(Vector& other) { Vector Vector::operator-(Vector &other)
if (rows != other.rows) { {
if (rows != other.rows)
{
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Vector result(rows); Vector result(rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
{
result[i] = vector[i] - other[i]; result[i] = vector[i] - other[i];
} }
return result; return result;
} }
std::istream& operator>>(std::istream& cin, Vector& vectorObj) { std::istream &operator>>(std::istream &cin, Vector &vectorObj)
for (int i = 0; i < vectorObj.rows; ++i) { {
for (int i = 0; i < vectorObj.rows; ++i)
{
cin >> vectorObj[i]; cin >> vectorObj[i];
} }
return cin; return cin;
} }
std::ostream& operator<<(std::ostream& cout, Vector& vectorObj) { std::ostream &operator<<(std::ostream &cout, Vector &vectorObj)
for (int i = 0; i < vectorObj.rows; ++i) { {
if (i == vectorObj.rows - 1) { for (int i = 0; i < vectorObj.rows; ++i)
{
if (i == vectorObj.rows - 1)
{
cout << vectorObj[i] << std::endl; cout << vectorObj[i] << std::endl;
} }
else { else
{
cout << vectorObj[i] << ' '; cout << vectorObj[i] << ' ';
} }
} }
return cout; return cout;
} }
Matrix::Matrix(int n, int m) { Matrix::Matrix(int n, int m)
{
rows = n; rows = n;
columns = m; columns = m;
matrix.resize(n, Vector(m)); matrix.resize(n, Vector(m));
for (auto& row : matrix) { for (auto &row : matrix)
{
row = Vector(m); row = Vector(m);
} }
} }
Matrix::Matrix(const Matrix& other) { Matrix::Matrix(const Matrix &other)
{
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
matrix = other.matrix; matrix = other.matrix;
} }
Matrix::Matrix(std::initializer_list<std::vector<double>> init) { Matrix::Matrix(std::initializer_list<std::vector<double>> init)
{
rows = init.size(); rows = init.size();
auto it = init.begin(); auto it = init.begin();
columns = it->size(); columns = it->size();
matrix = std::vector<Vector>(rows, Vector(columns)); matrix = std::vector<Vector>(rows, Vector(columns));
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
for (int j = 0; j < columns; ++j) { {
for (int j = 0; j < columns; ++j)
{
matrix[i][j] = it->operator[](j); matrix[i][j] = it->operator[](j);
} }
++it; ++it;
} }
} }
int Matrix::getRows() const { int Matrix::getRows() const
{
return rows; return rows;
} }
int Matrix::getColumns() const { int Matrix::getColumns() const
{
return columns; return columns;
} }
Vector& Matrix::operator[](int row) { Vector &Matrix::operator[](int row)
{
return matrix[row]; return matrix[row];
} }
Matrix& Matrix::operator=(const Matrix& other) { Matrix &Matrix::operator=(const Matrix &other)
{
rows = other.rows; rows = other.rows;
columns = other.columns; columns = other.columns;
matrix = other.matrix; matrix = other.matrix;
return *this; return *this;
} }
Matrix Matrix::operator+(Matrix& other) const { Matrix Matrix::operator+(Matrix &other) const
if (rows != other.rows || columns != other.columns) { {
if (rows != other.rows || columns != other.columns)
{
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Matrix result(rows, columns); Matrix result(rows, columns);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
for (int j = 0; j < columns; ++j) { {
for (int j = 0; j < columns; ++j)
{
auto x = matrix[i]; auto x = matrix[i];
auto y = other[i]; auto y = other[i];
result[i][j] = x[j] + y[j]; result[i][j] = x[j] + y[j];
@@ -146,13 +179,17 @@ Matrix Matrix::operator+(Matrix& other) const {
return result; return result;
} }
Matrix Matrix::operator-(Matrix& other) const { Matrix Matrix::operator-(Matrix &other) const
if (rows != other.rows || columns != other.columns) { {
if (rows != other.rows || columns != other.columns)
{
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Matrix result(rows, columns); Matrix result(rows, columns);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
for (int j = 0; j < columns; ++j) { {
for (int j = 0; j < columns; ++j)
{
auto x = matrix[i]; auto x = matrix[i];
auto y = other[i]; auto y = other[i];
result[i][j] = x[j] - y[j]; result[i][j] = x[j] - y[j];
@@ -162,16 +199,21 @@ Matrix Matrix::operator-(Matrix& other) const {
return result; return result;
} }
Matrix Matrix::operator*(Matrix& other) const { Matrix Matrix::operator*(Matrix &other) const
if (columns != other.rows) { {
if (columns != other.rows)
{
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Matrix result(rows, other.columns); Matrix result(rows, other.columns);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
for (int j = 0; j < other.columns; ++j) { {
for (int j = 0; j < other.columns; ++j)
{
result[i][j] = 0; result[i][j] = 0;
for (int k = 0; k < columns; ++k) { for (int k = 0; k < columns; ++k)
{
auto x = matrix[i]; auto x = matrix[i];
auto y = other[k]; auto y = other[k];
result[i][j] += x[k] * y[j]; result[i][j] += x[k] * y[j];
@@ -182,15 +224,19 @@ Matrix Matrix::operator*(Matrix& other) const {
return result; return result;
} }
Vector Matrix::operator*(Vector other) const { Vector Matrix::operator*(Vector other) const
if (columns != other.size()) { {
if (columns != other.size())
{
throw std::runtime_error("Error: the dimensional problem occurred"); throw std::runtime_error("Error: the dimensional problem occurred");
} }
Vector result(rows); Vector result(rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
{
result[i] = 0; result[i] = 0;
for (int k = 0; k < columns; ++k) { for (int k = 0; k < columns; ++k)
{
auto x = matrix[i]; auto x = matrix[i];
result[i] += x[k] * other[k]; result[i] += x[k] * other[k];
} }
@@ -199,10 +245,13 @@ Vector Matrix::operator*(Vector other) const {
return result; return result;
} }
Matrix Matrix::transpose() const { Matrix Matrix::transpose() const
{
Matrix result(columns, rows); Matrix result(columns, rows);
for (int i = 0; i < rows; ++i) { for (int i = 0; i < rows; ++i)
for (int j = 0; j < columns; ++j) { {
for (int j = 0; j < columns; ++j)
{
auto x = matrix[i]; auto x = matrix[i];
result[j][i] = x[j]; result[j][i] = x[j];
} }
@@ -211,51 +260,65 @@ Matrix Matrix::transpose() const {
return result; return result;
} }
std::istream& operator>>(std::istream& cin, Matrix& matrixObj) { std::istream &operator>>(std::istream &cin, Matrix &matrixObj)
for (int i = 0; i < matrixObj.rows; ++i) { {
for (int j = 0; j < matrixObj.columns; ++j) { for (int i = 0; i < matrixObj.rows; ++i)
{
for (int j = 0; j < matrixObj.columns; ++j)
{
cin >> matrixObj[i][j]; cin >> matrixObj[i][j];
} }
} }
return cin; return cin;
} }
std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) { std::ostream &operator<<(std::ostream &cout, Matrix &matrixObj)
{
size_t maxNumberLength = 1; size_t maxNumberLength = 1;
for (int y = 0; y < matrixObj.getRows(); y++) { 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 x = 0; x < matrixObj.getColumns(); x++)
{
if (std::to_string(matrixObj[y][x]).length() > maxNumberLength)
{
maxNumberLength = std::to_string(matrixObj[y][x]).length(); maxNumberLength = std::to_string(matrixObj[y][x]).length();
} }
} }
} }
//std::cout << maxNumberLength << std::endl; // std::cout << maxNumberLength << std::endl;
std::cout << 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 = ""; 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]); std::string strNumber = std::to_string(matrixObj[y][x]);
bool spaceRight = true; bool spaceRight = true;
while (strNumber.length() < maxNumberLength) { while (strNumber.length() < maxNumberLength)
if (spaceRight) { {
if (spaceRight)
{
strNumber += " "; strNumber += " ";
} }
else { else
{
strNumber = " " + strNumber; strNumber = " " + strNumber;
} }
spaceRight = !spaceRight; spaceRight = !spaceRight;
strNumber = "" + strNumber; strNumber = "" + strNumber;
} }
if (matrixObj[y][x] == 0) { if (matrixObj[y][x] == 0)
{
row += "[\033[0m" + strNumber + "\033[0m] "; row += "[\033[0m" + strNumber + "\033[0m] ";
} }
else if(matrixObj[y][x] > 0) { else if (matrixObj[y][x] > 0)
{
row += "[\033[32m" + strNumber + "\033[0m] "; row += "[\033[32m" + strNumber + "\033[0m] ";
}else { }
else
{
row += "[\033[31m" + strNumber + "\033[0m] "; row += "[\033[31m" + strNumber + "\033[0m] ";
} }
} }
std::cout << row << std::endl; std::cout << row << std::endl;
} }
+33 -30
View File
@@ -6,12 +6,12 @@
#include <vector> #include <vector>
/** /**
* Vector is a class to represent * Vector is a class to represent
* a column vector with n rows. * a column vector with n rows.
*/ */
class Vector
class Vector { {
protected: protected:
// Number of rows in vector // Number of rows in vector
int rows; int rows;
@@ -19,39 +19,41 @@ protected:
int columns; int columns;
// Matrix representation as vector of vectors of integers // Matrix representation as vector of vectors of integers
std::vector<double> vector; std::vector<double> vector;
public: public:
Vector(int n); Vector(int n);
Vector(const Vector& other); Vector(const Vector &other);
Vector(std::initializer_list<double>); Vector(std::initializer_list<double>);
/* Getter for the number of rows */ /* Getter for the number of rows */
int size() const; 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 */ /* 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 /* Output operator prints elements of the vector
* in a row separated with a space (no space at the end of the line) * in a row separated with a space (no space at the end of the line)
*/ */
friend std::ostream& operator<<(std::ostream& cout, Vector& vectorObj); friend std::ostream &operator<<(std::ostream &cout, Vector &vectorObj);
}; };
/** /**
* Class Matrix represents * Class Matrix represents
* a matrix of size n x m * a matrix of size n x m
* of type integer. * of type integer.
*/ */
class Matrix { class Matrix
{
protected: protected:
// Number of rows in matrix // Number of rows in matrix
int rows; int rows;
@@ -59,10 +61,11 @@ protected:
int columns; int columns;
// Matrix representation as vector of vectors of integers // Matrix representation as vector of vectors of integers
std::vector<Vector> matrix; std::vector<Vector> matrix;
public: public:
Matrix(int n, int m); Matrix(int n, int m);
Matrix(const Matrix& other); Matrix(const Matrix &other);
Matrix(std::initializer_list<std::vector<double>>); Matrix(std::initializer_list<std::vector<double>>);
@@ -72,15 +75,15 @@ public:
/* Getter for the number of columns */ /* Getter for the number of columns */
int getColumns() const; 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 */ /* Matrix-Vector multiplication */
Vector operator*(Vector other) const; Vector operator*(Vector other) const;
@@ -89,12 +92,12 @@ public:
Matrix transpose() const; Matrix transpose() const;
/* Input operator reads element of matrix row by row */ /* 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 /* Output operator prints elements of the matrix
* row by row separated with a space (no space at the end of each line) * row by row separated with a space (no space at the end of each line)
*/ */
friend std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj); friend std::ostream &operator<<(std::ostream &cout, Matrix &matrixObj);
}; };
#endif // TOOLS_MATRIX_H #endif // TOOLS_MATRIX_H