Compare commits
29
Commits
first_half
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
00b91098f6 | ||
|
|
d8735c3d44 | ||
|
|
0920abc31b | ||
|
|
a2c0cedd00 | ||
|
|
60bd99e570 | ||
|
|
a41c15f3ee | ||
|
|
5c2f80f85a | ||
|
|
8007275709 | ||
|
|
bf714eed92 | ||
|
|
262894d00d | ||
|
|
9b67db3629 | ||
|
|
8b7f86d5b0 | ||
|
|
50fda70802 | ||
|
|
fa123e0fb0 | ||
|
|
c7bc142081 | ||
|
|
535c63938d | ||
|
|
5ee857a067 | ||
|
|
6e9ce3c894 | ||
|
|
2bfea15fde | ||
|
|
253cf335ce | ||
|
|
98d5fc9da9 | ||
|
|
9cf37ec9e4 | ||
|
|
36b4250ad3 | ||
|
|
6e5dab96b0 | ||
|
|
08eecfd88a | ||
|
|
2aee5013d6 | ||
|
|
ed2163ee9d | ||
|
|
38982b501a | ||
|
|
67a6ca1eae |
@@ -1,3 +1,4 @@
|
|||||||
|
*.vscode
|
||||||
# Prerequisites
|
# Prerequisites
|
||||||
*.d
|
*.d
|
||||||
CMakeLists.txt
|
CMakeLists.txt
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
GXX := g++ -std=c++20
|
||||||
|
flags := -Wall -fsanitize=address
|
||||||
|
TOOLS := tools
|
||||||
|
BUILD := build
|
||||||
|
|
||||||
|
build:
|
||||||
|
mkdir -p $(BUILD)
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj
|
||||||
|
$(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj
|
||||||
|
$(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj
|
||||||
|
|
||||||
|
$(GXX) $(BUILD)/*.obj $(flags) -o simplex.out
|
||||||
|
|
||||||
|
$(BUILD)/elimination.obj: $(TOOLS)/elimination.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj
|
||||||
|
|
||||||
|
$(BUILD)/math.obj: $(TOOLS)/math.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj
|
||||||
|
|
||||||
|
$(BUILD)/matrix.obj: $(TOOLS)/matrix.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj
|
||||||
|
|
||||||
|
$(BUILD)/simplex.obj: simplex.cpp
|
||||||
|
$(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj
|
||||||
|
|
||||||
|
$(BUILD)/main.obj: main.cpp
|
||||||
|
$(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj
|
||||||
|
|
||||||
|
simplex.out: $(BUILD)/elimination.obj $(BUILD)/math.obj $(BUILD)/matrix.obj $(BUILD)/simplex.obj $(BUILD)/main.obj
|
||||||
|
$(GXX) $(BUILD)/*.obj $(flags) -o simplex.out
|
||||||
|
|
||||||
|
test: simplex.out
|
||||||
|
./simplex.out
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -rf $(BUILD)
|
||||||
|
rm -rf simplex.out
|
||||||
|
|
||||||
|
rebuild:
|
||||||
|
rm -rf $(BUILD)
|
||||||
|
rm -rf simplex.out
|
||||||
|
|
||||||
|
mkdir -p $(BUILD)
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj
|
||||||
|
$(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj
|
||||||
|
$(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj
|
||||||
|
|
||||||
|
$(GXX) $(BUILD)/*.obj $(flags) -o simplex.out
|
||||||
|
|
||||||
|
$(BUILD)/elimination.obj: $(TOOLS)/elimination.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/elimination.cpp -o $(BUILD)/elimination.obj
|
||||||
|
|
||||||
|
$(BUILD)/math.obj: $(TOOLS)/math.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/math.cpp -o $(BUILD)/math.obj
|
||||||
|
|
||||||
|
$(BUILD)/matrix.obj: $(TOOLS)/matrix.cpp
|
||||||
|
$(GXX) -c $(flags) $(TOOLS)/matrix.cpp -o $(BUILD)/matrix.obj
|
||||||
|
|
||||||
|
$(BUILD)/simplex.obj: simplex.cpp
|
||||||
|
$(GXX) -c $(flags) simplex.cpp -o $(BUILD)/simplex.obj
|
||||||
|
|
||||||
|
$(BUILD)/main.obj: main.cpp
|
||||||
|
$(GXX) -c $(flags) main.cpp -o $(BUILD)/main.obj
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
<h1> Instructions for installing </h1>
|
||||||
|
<h2>1. Clone the repository.</h2>
|
||||||
|
<code>git clone https://github.com/emil28092005/SimplexTASK/</code>
|
||||||
|
<h2>2. Build the project (install Make if not present on your system): </h2>
|
||||||
|
<code>make build</code>
|
||||||
|
<h2>3. Run tests via the following command: </h2>
|
||||||
|
<code>make test</code>
|
||||||
@@ -1,19 +1,410 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <functional>
|
||||||
#include "tools/matrix.h"
|
#include "tools/matrix.h"
|
||||||
#include "tools/math.h"
|
#include "tools/math.h"
|
||||||
#include "simplex.h"
|
#include "simplex.h"
|
||||||
|
|
||||||
int main() {
|
void _printInitialInputs(Vector &C, Matrix &A, Vector &b, double eps, bool maximize)
|
||||||
ColumnVector C(6);
|
{
|
||||||
Matrix A(6, 4);
|
std::cout << "Running for the following inputs:" << std::endl
|
||||||
int Carr[C.getColumns()] = {-5, -4, 0, 0, 0, 0};
|
<< std::endl;
|
||||||
for (int i = 0; i < C.getRows(); ++i) {
|
std::cout << "epsilon: " << eps << std::endl;
|
||||||
C[i] = Carr[i];
|
if (maximize)
|
||||||
|
{
|
||||||
|
std::cout << "Maximize" << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Minimize" << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << "z = ";
|
||||||
|
bool previousIsZero = true;
|
||||||
|
bool lastNonZero = false;
|
||||||
|
for (int i = 0; i < C.size(); i++)
|
||||||
|
{
|
||||||
|
bool isNegative = false;
|
||||||
|
|
||||||
|
for (int k = i; k < C.size(); k++)
|
||||||
|
{
|
||||||
|
if (C[k] == 0)
|
||||||
|
{
|
||||||
|
lastNonZero = true;
|
||||||
|
} else{
|
||||||
|
lastNonZero = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!previousIsZero && !lastNonZero){
|
||||||
|
std::cout << " + ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (C[i] != 0){
|
||||||
|
if (C[i] != 1) {
|
||||||
|
if (C[i] < 0){
|
||||||
|
isNegative = true;
|
||||||
|
std::cout << "(";
|
||||||
|
}
|
||||||
|
std::cout << C[i] << " * ";
|
||||||
|
}
|
||||||
|
std::cout << "x" << i + 1;
|
||||||
|
if (isNegative){
|
||||||
|
std::cout << ")";
|
||||||
|
}
|
||||||
|
previousIsZero = false;
|
||||||
|
}else {
|
||||||
|
previousIsZero = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
std::cout << std::endl
|
||||||
|
<< "subject to the constrains:" << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
for (int i = 0; i < b.size(); i++)
|
||||||
|
{
|
||||||
|
bool previousIsZero = true;
|
||||||
|
bool lastNonZero = false;
|
||||||
|
|
||||||
|
for (int j = 0; j < A.getColumns(); j++)
|
||||||
|
{
|
||||||
|
bool isNegative = false;
|
||||||
|
|
||||||
|
for (int k = j; k < A[i].size(); k++)
|
||||||
|
{
|
||||||
|
if (A[i][k] == 0)
|
||||||
|
{
|
||||||
|
lastNonZero = true;
|
||||||
|
} else{
|
||||||
|
lastNonZero = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!previousIsZero && !lastNonZero){
|
||||||
|
std::cout << " + ";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (A[i][j] != 0) {
|
||||||
|
if (A[i][j] != 1) {
|
||||||
|
if (A[i][j] < 0) {
|
||||||
|
isNegative = true;
|
||||||
|
std::cout << "(";
|
||||||
|
}
|
||||||
|
std::cout << A[i][j] << " * ";
|
||||||
|
}
|
||||||
|
std::cout << "x" << j + 1;
|
||||||
|
if (isNegative) {
|
||||||
|
std::cout << ")";
|
||||||
|
}
|
||||||
|
previousIsZero = false;
|
||||||
|
|
||||||
|
}else {
|
||||||
|
previousIsZero = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << " <= " << b[i] << std::endl;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int printResult(Result result)
|
||||||
|
{
|
||||||
|
if (result.state == unsolvable)
|
||||||
|
{
|
||||||
|
std::cout << "The method is not applicable!" << std::endl;
|
||||||
|
}else if (result.state == unbounded ) {
|
||||||
|
std::cout << "Unbounded problem!" << std::endl;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "SOLVED!" << std::endl;
|
||||||
|
std::cout << "Decision variables: [";
|
||||||
|
|
||||||
|
for (int i = 0; i < result.solution.size(); i++)
|
||||||
|
{
|
||||||
|
std::cout << result.solution[i];
|
||||||
|
if (i != result.solution.size() - 1)
|
||||||
|
{
|
||||||
|
std::cout << ", ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << "]";
|
||||||
|
std::cout << std::endl;
|
||||||
|
if (result.maximize)
|
||||||
|
{
|
||||||
|
std::cout << "Maximum ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Minimum ";
|
||||||
|
}
|
||||||
|
std::cout << "objective function value: " << result.objective_function_value << std::endl;
|
||||||
}
|
}
|
||||||
A = {{6, 4, 1, 0, 0, 0}, {1, 2, 0, 1, 0, 0}, {-1, 1, 0, 0, 1, 0}, {0, 1, 0, 0, 0, 1}};
|
return 0;
|
||||||
ColumnVector b(A.getRows());
|
}
|
||||||
b = {24, 6, 1, 2};
|
|
||||||
std::cout << A << std::endl;
|
bool check_eq(double a, double b, double relativeEpsilon = 0.0001)
|
||||||
std::cout << C << std::endl;
|
{
|
||||||
|
double diff = std::abs(a - b);
|
||||||
|
a = std::abs(a);
|
||||||
|
b = std::abs(b);
|
||||||
|
double largest = (b > a) ? b : a;
|
||||||
|
|
||||||
|
return diff <= largest * relativeEpsilon;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TEST_GENERAL_CASE()
|
||||||
|
{
|
||||||
|
std::cout << "----------------------------RUNNING_TEST_GENERAL_CASE----------------------------" << std::endl;
|
||||||
|
Vector C = {5, 4};
|
||||||
|
Matrix A = {
|
||||||
|
{6, 4},
|
||||||
|
{1, 2},
|
||||||
|
{-1, 1},
|
||||||
|
{0, 1}};
|
||||||
|
Vector b = {24, 6, 1, 2};
|
||||||
|
_printInitialInputs(C, A, b, 0.01, true);
|
||||||
|
auto result = simplex(C, A, b);
|
||||||
|
|
||||||
|
if (!(result.state == bounded))
|
||||||
|
{
|
||||||
|
std::string state_name;
|
||||||
|
switch (result.state)
|
||||||
|
{
|
||||||
|
case unsolvable:
|
||||||
|
state_name = "unsolvable";
|
||||||
|
break;
|
||||||
|
case unbounded:
|
||||||
|
state_name = "unbounded";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state_name = "bounded";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Incorrect state type. Expected bounded. Got " << state_name << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!check_eq(result.objective_function_value, 21))
|
||||||
|
{
|
||||||
|
std::cout << "Incorrect objective function value. Expected 21. Got "
|
||||||
|
<< result.objective_function_value << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
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 "
|
||||||
|
<< result.solution;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TEST_MINIMIZE_CASE()
|
||||||
|
{
|
||||||
|
std::cout << "----------------------------RUNNING_TEST_MINIMIZE_CASE----------------------------" << std::endl;
|
||||||
|
Vector C = {-2, 2, -6};
|
||||||
|
Matrix A = {
|
||||||
|
{2, 1, -2},
|
||||||
|
{1, 2, 4},
|
||||||
|
{1, -1, 2}};
|
||||||
|
Vector b = {24, 23, 10};
|
||||||
|
_printInitialInputs(C, A, b, 0.01, false);
|
||||||
|
auto result = simplex(C, A, b, 0.01, false);
|
||||||
|
|
||||||
|
if (!(result.state == bounded))
|
||||||
|
{
|
||||||
|
std::string state_name;
|
||||||
|
switch (result.state)
|
||||||
|
{
|
||||||
|
case unsolvable:
|
||||||
|
state_name = "unsolvable";
|
||||||
|
break;
|
||||||
|
case unbounded:
|
||||||
|
state_name = "unbounded";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state_name = "bounded";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Incorrect state type. Expected bounded. Got " << state_name << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!check_eq(result.objective_function_value, -30.75))
|
||||||
|
{
|
||||||
|
std::cout << "Incorrect objective function value. Expected -30.75. Got "
|
||||||
|
<< result.objective_function_value << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!(check_eq(result.solution[0], 0) && check_eq(result.solution[1], 0.75) && check_eq(result.solution[2], 5.375)))
|
||||||
|
{
|
||||||
|
std::cout << "Incorrect desire variables. Expected 3 and 1.5. Got "
|
||||||
|
<< result.solution;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TEST_WITH_SLACK_CASE()
|
||||||
|
{
|
||||||
|
std::cout << "----------------------------RUNNING_TEST_WITH_SLACK_CASE----------------------------" << std::endl;
|
||||||
|
|
||||||
|
Vector C = {2, -1, 0, -1};
|
||||||
|
Matrix A = {
|
||||||
|
{1, -2, 1, 0},
|
||||||
|
{-2, -1, 0, -2},
|
||||||
|
{3, 2, 0, 1}};
|
||||||
|
Vector b = {10, 18, 36};
|
||||||
|
|
||||||
|
_printInitialInputs(C, A, b, 0.01, true);
|
||||||
|
|
||||||
|
auto result = simplex(C, A, b);
|
||||||
|
|
||||||
|
if (!(result.state == bounded))
|
||||||
|
{
|
||||||
|
std::string state_name;
|
||||||
|
switch (result.state)
|
||||||
|
{
|
||||||
|
case unsolvable:
|
||||||
|
state_name = "unsolvable";
|
||||||
|
break;
|
||||||
|
case unbounded:
|
||||||
|
state_name = "unbounded";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state_name = "bounded";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Incorrect state type. Expected bounded. Got " << state_name << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result.objective_function_value != 22.25)
|
||||||
|
{
|
||||||
|
std::cout << "Incorrect objective function value. Expected 22.25. Got "
|
||||||
|
<< result.objective_function_value << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!((result.solution[0] == 11.5) || (result.solution[1] == 0.75) ||
|
||||||
|
(result.solution[2] == 0) || (result.solution[3] == 0)))
|
||||||
|
{
|
||||||
|
std::cout << "Incorrect desire variables. Expected 11.5, 0.75, 0, and 0. Got "
|
||||||
|
<< result.solution;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TEST_UNBOUNDED_CASE()
|
||||||
|
{
|
||||||
|
std::cout << "----------------------------RUNNING_TEST_UNBOUNDED_CASE----------------------------" << std::endl;
|
||||||
|
|
||||||
|
Vector C = {2, 1};
|
||||||
|
Matrix A = {
|
||||||
|
{1, -1},
|
||||||
|
{2, 0}};
|
||||||
|
Vector b = {10, 40};
|
||||||
|
|
||||||
|
_printInitialInputs(C, A, b, 0.01, true);
|
||||||
|
|
||||||
|
auto result = simplex(C, A, b);
|
||||||
|
|
||||||
|
if (!(result.state == unbounded))
|
||||||
|
{
|
||||||
|
std::string state_name;
|
||||||
|
switch (result.state)
|
||||||
|
{
|
||||||
|
case unsolvable:
|
||||||
|
state_name = "unsolvable";
|
||||||
|
break;
|
||||||
|
case unbounded:
|
||||||
|
state_name = "unbounded";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state_name = "bounded";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Incorrect state type. Expected unbounded. Got " << state_name << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int TEST_UNSOLVABLE_CASE()
|
||||||
|
{
|
||||||
|
std::cout << "----------------------------RUNNING_TEST_UNSOLVABLE_CASE----------------------------" << std::endl;
|
||||||
|
|
||||||
|
Vector C = {5, 4, 0, -5, 13};
|
||||||
|
Matrix A = {
|
||||||
|
{6, 4, 1, 3, 4},
|
||||||
|
{1, 2, 0, 0, 2},
|
||||||
|
{-1, 0, 0, 10, 0},
|
||||||
|
{0, 1, 1, -5, 1}};
|
||||||
|
Vector b = {-24, 6, 1, 2};
|
||||||
|
|
||||||
|
_printInitialInputs(C, A, b, 0.01, true);
|
||||||
|
|
||||||
|
auto result = simplex(C, A, b);
|
||||||
|
|
||||||
|
if (!(result.state == unsolvable))
|
||||||
|
{
|
||||||
|
std::string state_name;
|
||||||
|
switch (result.state)
|
||||||
|
{
|
||||||
|
case unsolvable:
|
||||||
|
state_name = "unsolvable";
|
||||||
|
break;
|
||||||
|
case unbounded:
|
||||||
|
state_name = "unbounded";
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
state_name = "bounded";
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
std::cout << "Incorrect state type. Expected unsolvable. Got " << state_name << std::endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
printResult(result);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
std::vector<std::function<int(void)>> tests = {
|
||||||
|
TEST_GENERAL_CASE,
|
||||||
|
TEST_MINIMIZE_CASE,
|
||||||
|
TEST_WITH_SLACK_CASE,
|
||||||
|
TEST_UNBOUNDED_CASE,
|
||||||
|
TEST_UNSOLVABLE_CASE};
|
||||||
|
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
for (auto &test : tests)
|
||||||
|
{
|
||||||
|
counter += test();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "----------------------------RESULTS----------------------------" << std::endl;
|
||||||
|
std::cout << "Total number of tests: " << tests.size() << std::endl;
|
||||||
|
std::cout << "Total number of passed tests: " << counter << std::endl;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
+122
-111
@@ -2,132 +2,143 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include "tools/matrix.h"
|
#include "tools/matrix.h"
|
||||||
#include "tools/math.h"
|
#include "tools/math.h"
|
||||||
|
#include "tools/elimination.h"
|
||||||
|
|
||||||
enum solver_state {
|
enum solver_state
|
||||||
|
{
|
||||||
unbounded,
|
unbounded,
|
||||||
bounded
|
bounded,
|
||||||
|
unsolvable
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Result {
|
struct Result
|
||||||
|
{
|
||||||
solver_state state;
|
solver_state state;
|
||||||
ColumnVector *solution;
|
Vector solution;
|
||||||
double objective_function_value;
|
double objective_function_value;
|
||||||
|
bool maximize;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct FracturedMatrix {
|
void _stopIterating(Matrix &generalMatrix, Vector &C, std::vector<int> &basicVars, solver_state state, Result &result)
|
||||||
Matrix A;
|
{
|
||||||
ColumnVector C;
|
DestroyMatrix destroyedGeneralMatrix = disassembleGeneralMatrix(generalMatrix);
|
||||||
ColumnVector b;
|
Matrix _A = destroyedGeneralMatrix.A;
|
||||||
int pivot_column_index;
|
Vector _C = destroyedGeneralMatrix.C;
|
||||||
int pivot_row_index;
|
Vector _b = destroyedGeneralMatrix.b;
|
||||||
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(Matrix A, ColumnVector C, ColumnVector 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) {
|
|
||||||
A = other.A;
|
|
||||||
C = other.C;
|
|
||||||
b = other.b;
|
|
||||||
pivot_column_index = other.pivot_column_index;
|
|
||||||
pivot_row_index = other.pivot_row_index;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize=true) {
|
result.state = state;
|
||||||
|
if (state == bounded)
|
||||||
std::cout << "C: " << C << std::endl;
|
{
|
||||||
std::cout << "A: " << A << std::endl;
|
result.solution = Vector(C.size());
|
||||||
std::cout << "b: " << b << std::endl;
|
for (int i = 0; i < C.size(); i++)
|
||||||
|
{
|
||||||
int n = C.getRows();
|
result.solution[i] = 0;
|
||||||
int m = A.getColumns();
|
|
||||||
|
|
||||||
while (b[0] < eps) {
|
|
||||||
//3
|
|
||||||
int pivot_column_index = 0;
|
|
||||||
pivot_column_index = Math::max_index(C);
|
|
||||||
|
|
||||||
//4
|
|
||||||
ColumnVector ratio_vector(m);
|
|
||||||
for (int i = 0; i < m; i++) {
|
|
||||||
ratio_vector[i] = b[i] / A[i][pivot_column_index];
|
|
||||||
}
|
}
|
||||||
int pivot_row_index = Math::min_index(ratio_vector);
|
for (size_t i = 1; i < basicVars.size(); i++)
|
||||||
|
{
|
||||||
//5
|
if (basicVars[i] < C.size())
|
||||||
struct FracturedMatrix fractured_matrix();
|
{
|
||||||
fractured_matrix = eleminate(A, C, b, pivot_column_index, pivot_row_index);
|
result.solution[basicVars[i]] = _b[i];
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Result result;
|
|
||||||
std::vector<int> basicVars(A.getColumns() - A.getRows());
|
|
||||||
basicVars[0] = -1;
|
|
||||||
|
|
||||||
for (int i = 1; i < basicVars.size(); i++) {
|
|
||||||
basicVars[i] = static_cast<int>(basicVars.size()) + i;
|
|
||||||
}
|
|
||||||
|
|
||||||
int kc = 0;
|
|
||||||
double temp = A[0][0];
|
|
||||||
for (int j = 0; j< A.getColumns(); j++) {
|
|
||||||
if (A[0][j] < temp) {
|
|
||||||
temp = A[0][j];
|
|
||||||
kc = j;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (A[0][kc] >= 0) {
|
|
||||||
result.state = unbounded;
|
|
||||||
result.solution = new ColumnVector(C.getRows());
|
|
||||||
for (int i = 0; i < C.getRows(); i++) {
|
|
||||||
result.solution->operator[](i) = 0;
|
|
||||||
}
|
|
||||||
for (int i = 1; i < basicVars.size(); i++) {
|
|
||||||
if (basicVars[i] <= C.getRows()) {
|
|
||||||
(*result.solution)[basicVars[i]] = b.getRows() - 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result.objective_fucntion_value = b[0];
|
|
||||||
|
|
||||||
|
result.objective_function_value = _b[0];
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
result.solution = Vector({0});
|
||||||
|
result.objective_function_value = 0;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Function_name(C, A, b, eps = eps_default)
|
Implementation of the Simplex method.
|
||||||
|
|
||||||
Input:
|
|
||||||
- C: A vector of coefficients of the objective function
|
|
||||||
- A: A matrix of coefficients of the constraint functions
|
|
||||||
- b: A vector of right-hand side values
|
|
||||||
- eps: Approximation accuracy (optional, default = eps_default)
|
|
||||||
|
|
||||||
Steps:
|
|
||||||
1. Print the optimization problem:
|
|
||||||
- max (or min) z = C[0] * x1 + C[1] * x2 + ... + C[n] * xn
|
|
||||||
- subject to the constraints:
|
|
||||||
- A[0] * x <= b[0]
|
|
||||||
- A[1] * x <= b[1]
|
|
||||||
- ...
|
|
||||||
- A[m] * x <= b[m]
|
|
||||||
|
|
||||||
2. Initialize:
|
|
||||||
- Form the initial tableau by introducing slack variables to convert inequalities into equalities.
|
|
||||||
|
|
||||||
3. Iteratively apply the Simplex method:
|
|
||||||
- Step 1: Identify the entering variable (most negative coefficient in the objective row).
|
|
||||||
- Step 2: Identify the leaving variable (smallest positive ratio of RHS to pivot column).
|
|
||||||
- Step 3: Perform pivot operations to update the tableau.
|
|
||||||
|
|
||||||
4. Check for optimality or unboundedness:
|
|
||||||
- If all coefficients in the objective function row are non-negative, the solution is optimal.
|
|
||||||
- If no leaving variable exists, the problem is unbounded.
|
|
||||||
|
|
||||||
5. Return:
|
|
||||||
- solver_state: {solved, unbounded}
|
|
||||||
- x*: Optimal vector of decision variables (if solved)
|
|
||||||
- z: Maximum (or minimum) value of the objective function (if solved)
|
|
||||||
|
|
||||||
End Function
|
|
||||||
*/
|
*/
|
||||||
|
Result simplex(Vector &C, Matrix &A, Vector &b, double eps = 0.01, bool maximize = true)
|
||||||
|
{
|
||||||
|
|
||||||
|
if (maximize == true)
|
||||||
|
{
|
||||||
|
for (int i = 0; i < C.size(); i++)
|
||||||
|
{
|
||||||
|
C[i] = -C[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Result result{};
|
||||||
|
result.maximize = maximize;
|
||||||
|
|
||||||
|
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
||||||
|
std::cout << generalMatrix;
|
||||||
|
std::vector<int> basicVars(generalMatrix.getRows());
|
||||||
|
basicVars[0] = -1;
|
||||||
|
|
||||||
|
for (int i = 0; i < b.size(); ++i)
|
||||||
|
{
|
||||||
|
if (b[i] < 0)
|
||||||
|
{
|
||||||
|
_stopIterating(generalMatrix, C, basicVars, unsolvable, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = 1; i < basicVars.size(); i++)
|
||||||
|
{
|
||||||
|
basicVars[i] = static_cast<int>(basicVars.size()) + i;
|
||||||
|
}
|
||||||
|
int iterationCount = 0;
|
||||||
|
while (true)
|
||||||
|
{
|
||||||
|
// 3
|
||||||
|
iterationCount++;
|
||||||
|
int pivot_column_index = 0;
|
||||||
|
|
||||||
|
pivot_column_index = min_index(generalMatrix[0]);
|
||||||
|
|
||||||
|
if (generalMatrix[0][pivot_column_index] >= 0)
|
||||||
|
{
|
||||||
|
_stopIterating(generalMatrix, C, basicVars, bounded, result);
|
||||||
|
if (!maximize)
|
||||||
|
{
|
||||||
|
result.objective_function_value = -result.objective_function_value;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4
|
||||||
|
Vector ratio_vector(generalMatrix.getRows());
|
||||||
|
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 (std::abs(ratio_vector[i]) < eps)
|
||||||
|
{
|
||||||
|
ratio_vector[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
ratio_vector[i] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
ratio_vector[0] = 0;
|
||||||
|
|
||||||
|
int pivot_row_index = min_index_positive(ratio_vector);
|
||||||
|
|
||||||
|
// No leaving variable exists
|
||||||
|
if (pivot_row_index == -1)
|
||||||
|
{
|
||||||
|
_stopIterating(generalMatrix, C, basicVars, unbounded, result);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
basicVars[pivot_row_index] = pivot_column_index;
|
||||||
|
|
||||||
|
// 5
|
||||||
|
elimination(generalMatrix, pivot_row_index, pivot_column_index);
|
||||||
|
std::cout << "Iteration " << iterationCount << " " << std::endl;
|
||||||
|
;
|
||||||
|
std::cout << generalMatrix;
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
@@ -4,17 +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
|
||||||
};
|
};
|
||||||
|
|
||||||
struct Result {
|
struct Result
|
||||||
|
{
|
||||||
solver_state state;
|
solver_state state;
|
||||||
ColumnVector *solution;
|
Vector solution;
|
||||||
double objective_fucntion_value;
|
double objective_function_value;
|
||||||
|
bool maximize;
|
||||||
};
|
};
|
||||||
|
|
||||||
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize = true);
|
void _printInitialInputs(Vector &C, Matrix &A, Vector &b);
|
||||||
|
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);
|
||||||
|
|
||||||
#endif // SIMPLEX_H
|
#endif // SIMPLEX_H
|
||||||
+72
-55
@@ -1,39 +1,41 @@
|
|||||||
#include "elimination.h"
|
#include "elimination.h"
|
||||||
#include "matrix.h"
|
|
||||||
#include "math.h"
|
#include "math.h"
|
||||||
|
|
||||||
struct FracturedMatrix {
|
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) {}
|
||||||
Matrix A;
|
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) {}
|
||||||
ColumnVector C;
|
FracturedMatrix &FracturedMatrix::operator=(const FracturedMatrix &other)
|
||||||
ColumnVector b;
|
{
|
||||||
int pivot_column_index;
|
A = other.A;
|
||||||
int pivot_row_index;
|
C = other.C;
|
||||||
};
|
b = other.b;
|
||||||
|
pivot_column_index = other.pivot_column_index;
|
||||||
|
pivot_row_index = other.pivot_row_index;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
struct DestroyMatrix {
|
DestroyMatrix disassembleGeneralMatrix(Matrix &generalMatrix)
|
||||||
Matrix A;
|
{
|
||||||
ColumnVector C;
|
|
||||||
ColumnVector b;
|
|
||||||
};
|
|
||||||
|
|
||||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
|
||||||
int rows = generalMatrix.getRows();
|
int rows = generalMatrix.getRows();
|
||||||
int cols = generalMatrix.getColumns();
|
int cols = generalMatrix.getColumns();
|
||||||
|
|
||||||
Matrix A(rows - 1, cols - 1);
|
Matrix A(rows - 1, cols - 1);
|
||||||
ColumnVector C(cols - 1);
|
Vector C(cols - 1);
|
||||||
ColumnVector b(rows - 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];
|
C[j] = generalMatrix[0][j];
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 1; i < rows; ++i) {
|
for (int i = 0; i < rows; ++i)
|
||||||
b[i - 1] = generalMatrix[i - 1][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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -41,61 +43,76 @@ DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
|||||||
return {A, C, b};
|
return {A, C, b};
|
||||||
}
|
}
|
||||||
|
|
||||||
Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
Matrix createGeneralMatrix(Matrix &A, Vector &C, Vector &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++) {
|
int m = A.getRows() + 1;
|
||||||
generalMatrix[j][A.getColumns()] = b[j];
|
int n = A.getColumns() + A.getRows() + 1;
|
||||||
}
|
Matrix generalMatrix(m, n);
|
||||||
|
|
||||||
for (int i = 0; i < A.getRows(); i++) {
|
for (int i = 0; i < n; i++)
|
||||||
for (int j = 0; j < A.getColumns(); j++) {
|
{
|
||||||
generalMatrix[i + 1][j] = A[i][j];
|
if (i < C.size())
|
||||||
|
{
|
||||||
|
generalMatrix[0][i] = C[i];
|
||||||
}
|
}
|
||||||
|
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];
|
||||||
|
}
|
||||||
|
|
||||||
|
int k = 0;
|
||||||
|
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
|
||||||
|
{
|
||||||
|
generalMatrix[i + 1][j] = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
generalMatrix[i + 1][A.getColumns() + k] = 1;
|
||||||
|
++k;
|
||||||
}
|
}
|
||||||
|
|
||||||
return generalMatrix;
|
return generalMatrix;
|
||||||
}
|
}
|
||||||
|
|
||||||
FracturedMatrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) {
|
void elimination(Matrix &generalMatrix, int pivot_row_index, int pivot_column_index)
|
||||||
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
{
|
||||||
|
|
||||||
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];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
+22
-5
@@ -1,13 +1,30 @@
|
|||||||
#ifndef ELIMINATION_H
|
#ifndef ELIMINATION_H
|
||||||
#define ELIMINATION_H
|
#define ELIMINATION_H
|
||||||
|
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
|
struct FracturedMatrix
|
||||||
class Elimination {
|
{
|
||||||
public:
|
Matrix A;
|
||||||
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, double eps);
|
Vector C;
|
||||||
|
Vector b;
|
||||||
|
int pivot_column_index;
|
||||||
|
int pivot_row_index;
|
||||||
|
FracturedMatrix() = default;
|
||||||
|
FracturedMatrix(const FracturedMatrix &other);
|
||||||
|
FracturedMatrix(Matrix A, Vector C, Vector b, int pivot_column_index, int pivot_row_index);
|
||||||
|
FracturedMatrix &operator=(const FracturedMatrix &other);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
#endif //ELIMINATION_H
|
#endif // ELIMINATION_H
|
||||||
|
|||||||
+83
-35
@@ -1,75 +1,121 @@
|
|||||||
#include "math.h"
|
#include "math.h"
|
||||||
#include "matrix.h"
|
|
||||||
|
|
||||||
double min(ColumnVector columnVector) {
|
double min(Vector vector)
|
||||||
double temp = columnVector[0];
|
{
|
||||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
double temp = vector[0];
|
||||||
if (columnVector[j] < temp) {
|
for (int j = 0; j < vector.size(); j++)
|
||||||
temp = columnVector[j];
|
{
|
||||||
|
if (vector[j] < temp)
|
||||||
|
{
|
||||||
|
temp = vector[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
double max(ColumnVector columnVector) {
|
double max(Vector vector)
|
||||||
double temp = columnVector[0];
|
{
|
||||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
double temp = vector[0];
|
||||||
if (columnVector[j] > temp) {
|
for (int j = 0; j < vector.size(); j++)
|
||||||
temp = columnVector[j];
|
{
|
||||||
|
if (vector[j] > temp)
|
||||||
|
{
|
||||||
|
temp = vector[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return temp;
|
return temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
int min_index(ColumnVector columnVector) {
|
int min_index_positive(Vector vector)
|
||||||
int temp_index = 0;
|
{
|
||||||
double temp = columnVector[0];
|
int i = 0;
|
||||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
while (i < vector.size() && vector[i] <= 0)
|
||||||
if (columnVector[j] < temp) {
|
{
|
||||||
|
++i;
|
||||||
|
}
|
||||||
|
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)
|
||||||
|
{
|
||||||
temp_index = j;
|
temp_index = j;
|
||||||
temp = columnVector[j];
|
temp = vector[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return temp_index;
|
return temp_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
int max_index(ColumnVector columnVector) {
|
int min_index(Vector vector)
|
||||||
|
{
|
||||||
int temp_index = 0;
|
int temp_index = 0;
|
||||||
double temp = columnVector[0];
|
double temp = vector[0];
|
||||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
for (int j = 0; j < vector.size(); j++)
|
||||||
if (columnVector[j] > temp) {
|
{
|
||||||
|
if (vector[j] < temp)
|
||||||
|
{
|
||||||
temp_index = j;
|
temp_index = j;
|
||||||
temp = columnVector[j];
|
temp = vector[j];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return temp_index;
|
return temp_index;
|
||||||
}
|
}
|
||||||
|
|
||||||
double min(std::vector<double> array) {
|
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)
|
||||||
|
{
|
||||||
|
temp_index = j;
|
||||||
|
temp = vector[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return temp_index;
|
||||||
|
}
|
||||||
|
|
||||||
|
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];
|
||||||
}
|
}
|
||||||
@@ -77,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];
|
||||||
}
|
}
|
||||||
|
|||||||
+6
-4
@@ -2,11 +2,13 @@
|
|||||||
#define MATH_H
|
#define MATH_H
|
||||||
|
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include "matrix.h"
|
||||||
|
|
||||||
double min(ColumnVector columnVector);
|
double min(Vector vector);
|
||||||
double max(ColumnVector columnVector);
|
double max(Vector vector);
|
||||||
int min_index(ColumnVector columnVector);
|
int min_index(Vector vector);
|
||||||
int max_index(ColumnVector columnVector);
|
int min_index_positive(Vector vector);
|
||||||
|
int max_index(Vector vector);
|
||||||
double min(std::vector<double> array);
|
double min(std::vector<double> array);
|
||||||
double max(std::vector<double> array);
|
double max(std::vector<double> array);
|
||||||
int min_index(std::vector<double> array);
|
int min_index(std::vector<double> array);
|
||||||
|
|||||||
+185
-70
@@ -1,120 +1,175 @@
|
|||||||
#include "matrix.h"
|
#include "matrix.h"
|
||||||
|
|
||||||
ColumnVector::ColumnVector(int n) {
|
Vector::Vector(int n)
|
||||||
|
{
|
||||||
rows = n;
|
rows = n;
|
||||||
columns = 1;
|
columns = 1;
|
||||||
columnVector.resize(n);
|
vector.resize(n);
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnVector::ColumnVector(const ColumnVector& other) {
|
Vector::Vector(const Vector &other)
|
||||||
|
{
|
||||||
rows = other.rows;
|
rows = other.rows;
|
||||||
columns = other.columns;
|
columns = other.columns;
|
||||||
columnVector = other.columnVector;
|
vector = other.vector;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ColumnVector::getRows() const {
|
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)
|
||||||
|
{
|
||||||
|
vector[i] = *it++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Vector::size() const
|
||||||
|
{
|
||||||
return rows;
|
return rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
int ColumnVector::getColumns() const {
|
double &Vector::operator[](int row)
|
||||||
return columns;
|
{
|
||||||
|
return vector[row];
|
||||||
}
|
}
|
||||||
|
|
||||||
double& ColumnVector::operator[](int row) {
|
Vector &Vector::operator=(const Vector &other)
|
||||||
return columnVector[row];
|
{
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector& ColumnVector::operator=(const ColumnVector& other) {
|
|
||||||
rows = other.rows;
|
rows = other.rows;
|
||||||
columns = other.columns;
|
columns = other.columns;
|
||||||
columnVector = other.columnVector;
|
vector = other.vector;
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnVector ColumnVector::operator+(ColumnVector& 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");
|
||||||
}
|
}
|
||||||
ColumnVector result(rows);
|
Vector result(rows);
|
||||||
for (int i = 0; i < rows; ++i) {
|
for (int i = 0; i < rows; ++i)
|
||||||
result[i] = columnVector[i] + other[i];
|
{
|
||||||
|
result[i] = vector[i] + other[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnVector ColumnVector::operator-(ColumnVector& 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");
|
||||||
}
|
}
|
||||||
ColumnVector result(rows);
|
Vector result(rows);
|
||||||
for (int i = 0; i < rows; ++i) {
|
for (int i = 0; i < rows; ++i)
|
||||||
result[i] = columnVector[i] - other[i];
|
{
|
||||||
|
result[i] = vector[i] - other[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::istream& operator>>(std::istream& cin, ColumnVector& 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, ColumnVector& 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, ColumnVector(m));
|
matrix.resize(n, Vector(m));
|
||||||
for (auto& row : matrix) {
|
for (auto &row : matrix)
|
||||||
row = ColumnVector(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Matrix::getRows() const {
|
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)
|
||||||
|
{
|
||||||
|
matrix[i][j] = it->operator[](j);
|
||||||
|
}
|
||||||
|
++it;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int Matrix::getRows() const
|
||||||
|
{
|
||||||
return rows;
|
return rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
int Matrix::getColumns() const {
|
int Matrix::getColumns() const
|
||||||
|
{
|
||||||
return columns;
|
return columns;
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnVector& 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];
|
||||||
@@ -124,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];
|
||||||
@@ -140,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];
|
||||||
@@ -160,15 +224,19 @@ Matrix Matrix::operator*(Matrix& other) const {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnVector Matrix::operator*(ColumnVector other) const {
|
Vector Matrix::operator*(Vector other) const
|
||||||
if (columns != other.getRows()) {
|
{
|
||||||
|
if (columns != other.size())
|
||||||
|
{
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
throw std::runtime_error("Error: the dimensional problem occurred");
|
||||||
}
|
}
|
||||||
|
|
||||||
ColumnVector 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];
|
||||||
}
|
}
|
||||||
@@ -177,10 +245,13 @@ ColumnVector Matrix::operator*(ColumnVector 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];
|
||||||
}
|
}
|
||||||
@@ -189,25 +260,69 @@ 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)
|
||||||
for (int i = 0; i < matrixObj.rows; ++i) {
|
{
|
||||||
for (int j = 0; j < matrixObj.columns; ++j) {
|
size_t maxNumberLength = 1;
|
||||||
if (j == matrixObj.columns - 1) {
|
for (int y = 0; y < matrixObj.getRows(); y++)
|
||||||
cout << matrixObj[i][j] << std::endl;
|
{
|
||||||
}
|
for (int x = 0; x < matrixObj.getColumns(); x++)
|
||||||
else {
|
{
|
||||||
cout << matrixObj[i][j] << ' ';
|
if (std::to_string(matrixObj[y][x]).length() > maxNumberLength)
|
||||||
|
{
|
||||||
|
maxNumberLength = std::to_string(matrixObj[y][x]).length();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// std::cout << maxNumberLength << std::endl;
|
||||||
|
std::cout << std::endl;
|
||||||
|
for (int y = 0; y < matrixObj.getRows(); y++)
|
||||||
|
{
|
||||||
|
std::string row = "";
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
strNumber += " ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
strNumber = " " + strNumber;
|
||||||
|
}
|
||||||
|
spaceRight = !spaceRight;
|
||||||
|
strNumber = "" + strNumber;
|
||||||
|
}
|
||||||
|
if (matrixObj[y][x] == 0)
|
||||||
|
{
|
||||||
|
row += "[\033[0m" + strNumber + "\033[0m] ";
|
||||||
|
}
|
||||||
|
else if (matrixObj[y][x] > 0)
|
||||||
|
{
|
||||||
|
row += "[\033[32m" + strNumber + "\033[0m] ";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
row += "[\033[31m" + strNumber + "\033[0m] ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::cout << row << std::endl;
|
||||||
|
}
|
||||||
|
std::cout << std::endl;
|
||||||
|
|
||||||
return cout;
|
return cout;
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-38
@@ -6,62 +6,68 @@
|
|||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ColumnVector is a class to represent
|
* Vector is a class to represent
|
||||||
* a column vector with n rows.
|
* a column vector with n rows.
|
||||||
*/
|
*/
|
||||||
class ColumnVector {
|
|
||||||
|
class Vector
|
||||||
|
{
|
||||||
protected:
|
protected:
|
||||||
// Number of rows in vector
|
// Number of rows in vector
|
||||||
int rows;
|
int rows;
|
||||||
// Number of columns in vector
|
// Number of columns in vector
|
||||||
int columns;
|
int columns;
|
||||||
// Matrix representation as vector of vectors of integers
|
// Matrix representation as vector of vectors of integers
|
||||||
std::vector<double> columnVector;
|
std::vector<double> vector;
|
||||||
public:
|
|
||||||
ColumnVector(int n);
|
|
||||||
|
|
||||||
ColumnVector(const ColumnVector& other);
|
public:
|
||||||
|
Vector(int n);
|
||||||
|
|
||||||
|
Vector(const Vector &other);
|
||||||
|
|
||||||
|
Vector(std::initializer_list<double>);
|
||||||
|
|
||||||
/* Getter for the number of rows */
|
/* Getter for the number of rows */
|
||||||
int getRows() const;
|
int size() const;
|
||||||
|
|
||||||
/* Getter for the number of columns */
|
double &operator[](int row);
|
||||||
int getColumns() const;
|
|
||||||
|
|
||||||
double& operator[](int row);
|
Vector &operator=(const Vector &other);
|
||||||
|
|
||||||
ColumnVector& operator=(const ColumnVector& other);
|
Vector operator+(Vector &other);
|
||||||
|
|
||||||
ColumnVector operator+(ColumnVector& other);
|
Vector operator-(Vector &other);
|
||||||
|
|
||||||
ColumnVector operator-(ColumnVector& other);
|
|
||||||
|
|
||||||
/* Input operator reads element of vector */
|
/* Input operator reads element of vector */
|
||||||
friend std::istream& operator>>(std::istream& cin, ColumnVector& 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, ColumnVector& 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;
|
||||||
// Number of columns in matrix
|
// Number of columns in matrix
|
||||||
int columns;
|
int columns;
|
||||||
// Matrix representation as vector of vectors of integers
|
// Matrix representation as vector of vectors of integers
|
||||||
std::vector<ColumnVector> 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>>);
|
||||||
|
|
||||||
/* Getter for the number of rows */
|
/* Getter for the number of rows */
|
||||||
int getRows() const;
|
int getRows() const;
|
||||||
@@ -69,29 +75,29 @@ public:
|
|||||||
/* Getter for the number of columns */
|
/* Getter for the number of columns */
|
||||||
int getColumns() const;
|
int getColumns() const;
|
||||||
|
|
||||||
ColumnVector& 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 */
|
||||||
ColumnVector operator*(ColumnVector other) const;
|
Vector operator*(Vector other) const;
|
||||||
|
|
||||||
/* Produces transposed version of the matrix */
|
/* Produces transposed version of the matrix */
|
||||||
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
|
||||||
|
|||||||
Reference in New Issue
Block a user