Compare commits
1
Commits
first_half
..
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2749b9dc0b |
-33
@@ -1,33 +0,0 @@
|
||||
# Prerequisites
|
||||
*.d
|
||||
CMakeLists.txt
|
||||
.idea
|
||||
# Compiled Object files
|
||||
*.slo
|
||||
*.lo
|
||||
*.o
|
||||
*.obj
|
||||
|
||||
# Precompiled Headers
|
||||
*.gch
|
||||
*.pch
|
||||
|
||||
# Compiled Dynamic libraries
|
||||
*.so
|
||||
*.dylib
|
||||
*.dll
|
||||
|
||||
# Fortran module files
|
||||
*.mod
|
||||
*.smod
|
||||
|
||||
# Compiled Static libraries
|
||||
*.lai
|
||||
*.la
|
||||
*.a
|
||||
*.lib
|
||||
|
||||
# Executables
|
||||
*.exe
|
||||
*.out
|
||||
*.app
|
||||
@@ -4,16 +4,36 @@
|
||||
#include "simplex.h"
|
||||
|
||||
int main() {
|
||||
ColumnVector C(6);
|
||||
Matrix A(6, 4);
|
||||
int Carr[C.getColumns()] = {-5, -4, 0, 0, 0, 0};
|
||||
for (int i = 0; i < C.getRows(); ++i) {
|
||||
C[i] = Carr[i];
|
||||
// TODO: Initially should be positive
|
||||
|
||||
Vector C = {-5, -4, 0, 0, 0, 0};
|
||||
|
||||
Matrix 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}
|
||||
};
|
||||
|
||||
Vector b = {24, 6, 1, 2};
|
||||
|
||||
//Matrix test = {{1, -1, -2}, {1, 1, -2}, {1, -1, 2}};
|
||||
|
||||
//showMatrix(test);
|
||||
|
||||
|
||||
Result result = Simplex(C, A, b);
|
||||
if(result.state == bounded) {
|
||||
|
||||
if(result.solution == nullptr) {
|
||||
// TODO: error
|
||||
}
|
||||
std::cout << *result.solution << std::endl;
|
||||
std::cout << result.objective_fucntion_value << std::endl;
|
||||
|
||||
delete result.solution;
|
||||
}
|
||||
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}};
|
||||
ColumnVector b(A.getRows());
|
||||
b = {24, 6, 1, 2};
|
||||
std::cout << A << std::endl;
|
||||
std::cout << C << std::endl;
|
||||
//std::cout << "asdasd" << std::endl;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
+58
-35
@@ -2,6 +2,7 @@
|
||||
#include <iostream>
|
||||
#include "tools/matrix.h"
|
||||
#include "tools/math.h"
|
||||
#include "tools/elimination.h"
|
||||
|
||||
enum solver_state {
|
||||
unbounded,
|
||||
@@ -10,55 +11,77 @@ enum solver_state {
|
||||
|
||||
struct Result {
|
||||
solver_state state;
|
||||
ColumnVector *solution;
|
||||
Vector *solution;
|
||||
double objective_function_value;
|
||||
};
|
||||
|
||||
struct FracturedMatrix {
|
||||
Matrix A;
|
||||
ColumnVector C;
|
||||
ColumnVector b;
|
||||
int pivot_column_index;
|
||||
int 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(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(Vector C, Matrix A, Vector b, double eps = 0.01, bool maximize=true) {
|
||||
if (maximize == false) {
|
||||
for (int i = 0; i < C.size(); i++) {
|
||||
C[i] = -C[i];
|
||||
}
|
||||
}
|
||||
};
|
||||
Result result{};
|
||||
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
||||
std::cout << "Before:" << std::endl;
|
||||
showMatrix(generalMatrix);
|
||||
std::vector<int> basicVars(generalMatrix.getRows());
|
||||
basicVars[0] = -1;
|
||||
|
||||
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize=true) {
|
||||
for (size_t i = 1; i < basicVars.size(); i++) {
|
||||
basicVars[i] = static_cast<int>(basicVars.size()) + i;
|
||||
}
|
||||
|
||||
std::cout << "C: " << C << std::endl;
|
||||
std::cout << "A: " << A << std::endl;
|
||||
std::cout << "b: " << b << std::endl;
|
||||
|
||||
int n = C.getRows();
|
||||
int m = A.getColumns();
|
||||
|
||||
while (b[0] < eps) {
|
||||
while (true) {
|
||||
//3
|
||||
int pivot_column_index = 0;
|
||||
pivot_column_index = Math::max_index(C);
|
||||
pivot_column_index = min_index(generalMatrix[0]);
|
||||
|
||||
if (generalMatrix[0][pivot_column_index] >= 0) {
|
||||
DestroyMatrix destroyedGeneralMatrix = destroyGeneralMatrix(generalMatrix);
|
||||
Matrix _A = destroyedGeneralMatrix.A;
|
||||
Vector _C = destroyedGeneralMatrix.C;
|
||||
Vector _b = destroyedGeneralMatrix.b;
|
||||
|
||||
result.state = bounded;
|
||||
result.solution = new Vector(C.size());
|
||||
for (int i = 0; i < C.size(); i++) {
|
||||
result.solution->operator[](i) = 0;
|
||||
}
|
||||
for (size_t i = 1; i < basicVars.size(); i++) {
|
||||
if (basicVars[i] <= C.size()) {
|
||||
result.solution->operator[](basicVars[i]) = _b[i];
|
||||
}
|
||||
}
|
||||
result.objective_function_value = b[0];
|
||||
return result;
|
||||
}
|
||||
|
||||
//4
|
||||
ColumnVector ratio_vector(m);
|
||||
for (int i = 0; i < m; i++) {
|
||||
ratio_vector[i] = b[i] / A[i][pivot_column_index];
|
||||
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];
|
||||
} else {
|
||||
ratio_vector[i] = 0;
|
||||
}
|
||||
}
|
||||
int pivot_row_index = Math::min_index(ratio_vector);
|
||||
ratio_vector[0] = 0;
|
||||
|
||||
int pivot_row_index = min_index_positive(ratio_vector);
|
||||
|
||||
basicVars[pivot_row_index] = pivot_column_index;
|
||||
|
||||
//5
|
||||
struct FracturedMatrix fractured_matrix();
|
||||
fractured_matrix = eleminate(A, C, b, pivot_column_index, pivot_row_index);
|
||||
elimination(generalMatrix, pivot_row_index, pivot_column_index);
|
||||
std::cout << "After:" << std::endl;
|
||||
showMatrix(generalMatrix);
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
/*
|
||||
Result result;
|
||||
std::vector<int> basicVars(A.getColumns() - A.getRows());
|
||||
@@ -79,7 +102,7 @@ Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool
|
||||
|
||||
if (A[0][kc] >= 0) {
|
||||
result.state = unbounded;
|
||||
result.solution = new ColumnVector(C.getRows());
|
||||
result.solution = new Vector(C.getRows());
|
||||
for (int i = 0; i < C.getRows(); i++) {
|
||||
result.solution->operator[](i) = 0;
|
||||
}
|
||||
|
||||
@@ -11,10 +11,10 @@ enum solver_state {
|
||||
|
||||
struct Result {
|
||||
solver_state state;
|
||||
ColumnVector *solution;
|
||||
Vector *solution;
|
||||
double objective_fucntion_value;
|
||||
};
|
||||
|
||||
Result Simplex(ColumnVector C, Matrix A, ColumnVector 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
|
||||
+17
-36
@@ -1,28 +1,24 @@
|
||||
#include "elimination.h"
|
||||
#include "matrix.h"
|
||||
#include "math.h"
|
||||
|
||||
struct FracturedMatrix {
|
||||
Matrix A;
|
||||
ColumnVector C;
|
||||
ColumnVector b;
|
||||
int pivot_column_index;
|
||||
int pivot_row_index;
|
||||
};
|
||||
|
||||
struct DestroyMatrix {
|
||||
Matrix A;
|
||||
ColumnVector C;
|
||||
ColumnVector b;
|
||||
};
|
||||
FracturedMatrix::FracturedMatrix(const FracturedMatrix& other) : A(other.A), C(other.C), b(other.b), pivot_column_index(other.pivot_column_index), pivot_row_index(other.pivot_row_index) {}
|
||||
FracturedMatrix::FracturedMatrix(Matrix A, Vector C, Vector b, int pivot_column_index, int pivot_row_index) : A(A), C(C), b(b), pivot_column_index(pivot_column_index), pivot_row_index(pivot_row_index) {}
|
||||
FracturedMatrix& FracturedMatrix::operator=(const FracturedMatrix& other) {
|
||||
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;
|
||||
}
|
||||
|
||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
||||
int rows = generalMatrix.getRows();
|
||||
int cols = generalMatrix.getColumns();
|
||||
|
||||
Matrix A(rows - 1, cols - 1);
|
||||
ColumnVector C(cols - 1);
|
||||
ColumnVector b(rows - 1);
|
||||
Vector C(cols - 1);
|
||||
Vector b(rows - 1);
|
||||
|
||||
for (int j = 0; j < cols - 1; ++j) {
|
||||
C[j] = generalMatrix[0][j];
|
||||
@@ -41,14 +37,15 @@ DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
||||
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++) {
|
||||
generalMatrix[j][A.getColumns()] = b[j];
|
||||
// For objective function (j=0) the value is set to zero automatically
|
||||
for (int j = 1; j < A.getRows() + 1; j++) {
|
||||
generalMatrix[j][A.getColumns()] = b[j-1];
|
||||
}
|
||||
|
||||
for (int i = 0; i < A.getRows(); i++) {
|
||||
@@ -60,8 +57,7 @@ Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
||||
return generalMatrix;
|
||||
}
|
||||
|
||||
FracturedMatrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) {
|
||||
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
||||
void elimination(Matrix& generalMatrix, int pivot_row_index, int pivot_column_index) {
|
||||
|
||||
int rows = generalMatrix.getRows();
|
||||
int cols = generalMatrix.getColumns();
|
||||
@@ -82,20 +78,5 @@ FracturedMatrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_
|
||||
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};
|
||||
}
|
||||
|
||||
|
||||
|
||||
+20
-3
@@ -1,13 +1,30 @@
|
||||
#ifndef ELIMINATION_H
|
||||
#define ELIMINATION_H
|
||||
|
||||
#include "matrix.h"
|
||||
|
||||
|
||||
class Elimination {
|
||||
public:
|
||||
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, double eps);
|
||||
struct FracturedMatrix {
|
||||
Matrix A;
|
||||
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 destroyGeneralMatrix(Matrix& generalMatrix);
|
||||
void elimination(Matrix&, int pivot_row_index, int pivot_column_index);
|
||||
Matrix createGeneralMatrix(Matrix& A, Vector& C, Vector& b);
|
||||
|
||||
|
||||
#endif //ELIMINATION_H
|
||||
|
||||
+41
-22
@@ -1,45 +1,64 @@
|
||||
#include "math.h"
|
||||
#include "matrix.h"
|
||||
|
||||
double min(ColumnVector columnVector) {
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] < temp) {
|
||||
temp = columnVector[j];
|
||||
double min(Vector vector) {
|
||||
double temp = vector[0];
|
||||
for (int j = 0; j < vector.size(); j++) {
|
||||
if (vector[j] < temp) {
|
||||
temp = vector[j];
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
double max(ColumnVector columnVector) {
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] > temp) {
|
||||
temp = columnVector[j];
|
||||
double max(Vector vector) {
|
||||
double temp = vector[0];
|
||||
for (int j = 0; j < vector.size(); j++) {
|
||||
if (vector[j] > temp) {
|
||||
temp = vector[j];
|
||||
}
|
||||
}
|
||||
return temp;
|
||||
}
|
||||
|
||||
int min_index(ColumnVector columnVector) {
|
||||
int temp_index = 0;
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] < temp) {
|
||||
int min_index_positive(Vector vector) {
|
||||
int i = 0;
|
||||
while (i < vector.size() && vector[i] <= 0) {
|
||||
++i;
|
||||
}
|
||||
if (i >= vector.size()) {
|
||||
throw std::runtime_error("No positive min found");
|
||||
}
|
||||
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 = columnVector[j];
|
||||
temp = vector[j];
|
||||
}
|
||||
}
|
||||
return temp_index;
|
||||
}
|
||||
|
||||
int max_index(ColumnVector columnVector) {
|
||||
int min_index(Vector vector) {
|
||||
int temp_index = 0;
|
||||
double temp = columnVector[0];
|
||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
||||
if (columnVector[j] > temp) {
|
||||
double temp = vector[0];
|
||||
for (int j = 0; j < vector.size(); j++) {
|
||||
if (vector[j] < temp) {
|
||||
temp_index = j;
|
||||
temp = columnVector[j];
|
||||
temp = vector[j];
|
||||
}
|
||||
}
|
||||
return temp_index;
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
+6
-4
@@ -2,11 +2,13 @@
|
||||
#define MATH_H
|
||||
|
||||
#include <vector>
|
||||
#include "matrix.h"
|
||||
|
||||
double min(ColumnVector columnVector);
|
||||
double max(ColumnVector columnVector);
|
||||
int min_index(ColumnVector columnVector);
|
||||
int max_index(ColumnVector columnVector);
|
||||
double min(Vector vector);
|
||||
double max(Vector vector);
|
||||
int min_index(Vector vector);
|
||||
int min_index_positive(Vector vector);
|
||||
int max_index(Vector vector);
|
||||
double min(std::vector<double> array);
|
||||
double max(std::vector<double> array);
|
||||
int min_index(std::vector<double> array);
|
||||
|
||||
+89
-27
@@ -1,68 +1,116 @@
|
||||
#include "matrix.h"
|
||||
|
||||
ColumnVector::ColumnVector(int n) {
|
||||
void showMatrix(Matrix matrix) {
|
||||
int maxNumberLength = 1;
|
||||
for (size_t y = 0; y < matrix.getRows(); y++) {
|
||||
for (size_t x = 0; x < matrix.getColumns(); x++) {
|
||||
if (std::to_string(matrix[y][x]).length() > maxNumberLength) {
|
||||
maxNumberLength = std::to_string(matrix[y][x]).length();
|
||||
}
|
||||
}
|
||||
}
|
||||
std::cout << maxNumberLength << std::endl;
|
||||
for (size_t y = 0; y < matrix.getRows(); y++) {
|
||||
std::string row = "";
|
||||
for (size_t x = 0; x < matrix.getColumns(); x++) {
|
||||
std::string strNumber = std::to_string(matrix[y][x]);
|
||||
bool spaceRight = true;
|
||||
while (strNumber.length() < maxNumberLength) {
|
||||
if (spaceRight) {
|
||||
strNumber += " ";
|
||||
}
|
||||
else {
|
||||
strNumber = " " + strNumber;
|
||||
}
|
||||
spaceRight = !spaceRight;
|
||||
strNumber = "" + strNumber;
|
||||
|
||||
}
|
||||
if (matrix[y][x] == 0) {
|
||||
row += "[\033[0m" + strNumber + "\033[0m] ";
|
||||
}
|
||||
else if(matrix[y][x] > 0) {
|
||||
row += "[\033[32m" + strNumber + "\033[0m] ";
|
||||
}else {
|
||||
row += "[\033[31m" + strNumber + "\033[0m] ";
|
||||
}
|
||||
|
||||
}
|
||||
std::cout << row << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
Vector::Vector(int n) {
|
||||
rows = n;
|
||||
columns = 1;
|
||||
columnVector.resize(n);
|
||||
vector.resize(n);
|
||||
}
|
||||
|
||||
ColumnVector::ColumnVector(const ColumnVector& other) {
|
||||
Vector::Vector(const Vector& other) {
|
||||
rows = other.rows;
|
||||
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;
|
||||
}
|
||||
|
||||
int ColumnVector::getColumns() const {
|
||||
return columns;
|
||||
double& Vector::operator[](int row) {
|
||||
return vector[row];
|
||||
}
|
||||
|
||||
double& ColumnVector::operator[](int row) {
|
||||
return columnVector[row];
|
||||
}
|
||||
|
||||
ColumnVector& ColumnVector::operator=(const ColumnVector& other) {
|
||||
Vector& Vector::operator=(const Vector& other) {
|
||||
rows = other.rows;
|
||||
columns = other.columns;
|
||||
columnVector = other.columnVector;
|
||||
vector = other.vector;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ColumnVector ColumnVector::operator+(ColumnVector& other) {
|
||||
Vector Vector::operator+(Vector& other) {
|
||||
if (rows != other.rows) {
|
||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
||||
}
|
||||
ColumnVector result(rows);
|
||||
Vector result(rows);
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
result[i] = columnVector[i] + other[i];
|
||||
result[i] = vector[i] + other[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
ColumnVector ColumnVector::operator-(ColumnVector& other) {
|
||||
Vector Vector::operator-(Vector& other) {
|
||||
if (rows != other.rows) {
|
||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
||||
}
|
||||
ColumnVector result(rows);
|
||||
Vector result(rows);
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
result[i] = columnVector[i] - other[i];
|
||||
result[i] = vector[i] - other[i];
|
||||
}
|
||||
|
||||
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) {
|
||||
cin >> vectorObj[i];
|
||||
}
|
||||
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) {
|
||||
cout << vectorObj[i] << std::endl;
|
||||
@@ -77,9 +125,9 @@ std::ostream& operator<<(std::ostream& cout, ColumnVector& vectorObj) {
|
||||
Matrix::Matrix(int n, int m) {
|
||||
rows = n;
|
||||
columns = m;
|
||||
matrix.resize(n, ColumnVector(m));
|
||||
matrix.resize(n, Vector(m));
|
||||
for (auto& row : matrix) {
|
||||
row = ColumnVector(m);
|
||||
row = Vector(m);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -89,6 +137,20 @@ Matrix::Matrix(const Matrix& other) {
|
||||
matrix = other.matrix;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -97,7 +159,7 @@ int Matrix::getColumns() const {
|
||||
return columns;
|
||||
}
|
||||
|
||||
ColumnVector& Matrix::operator[](int row) {
|
||||
Vector& Matrix::operator[](int row) {
|
||||
return matrix[row];
|
||||
}
|
||||
|
||||
@@ -160,12 +222,12 @@ Matrix Matrix::operator*(Matrix& other) const {
|
||||
return result;
|
||||
}
|
||||
|
||||
ColumnVector Matrix::operator*(ColumnVector other) const {
|
||||
if (columns != other.getRows()) {
|
||||
Vector Matrix::operator*(Vector other) const {
|
||||
if (columns != other.size()) {
|
||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
||||
}
|
||||
|
||||
ColumnVector result(rows);
|
||||
Vector result(rows);
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
result[i] = 0;
|
||||
for (int k = 0; k < columns; ++k) {
|
||||
|
||||
+22
-17
@@ -6,43 +6,44 @@
|
||||
#include <vector>
|
||||
|
||||
/**
|
||||
* ColumnVector is a class to represent
|
||||
* Vector is a class to represent
|
||||
* a column vector with n rows.
|
||||
*/
|
||||
class ColumnVector {
|
||||
|
||||
|
||||
class Vector {
|
||||
protected:
|
||||
// Number of rows in vector
|
||||
int rows;
|
||||
// Number of columns in vector
|
||||
int columns;
|
||||
// Matrix representation as vector of vectors of integers
|
||||
std::vector<double> columnVector;
|
||||
std::vector<double> vector;
|
||||
public:
|
||||
ColumnVector(int n);
|
||||
Vector(int n);
|
||||
|
||||
ColumnVector(const ColumnVector& other);
|
||||
Vector(const Vector& other);
|
||||
|
||||
Vector(std::initializer_list<double>);
|
||||
|
||||
/* Getter for the number of rows */
|
||||
int getRows() const;
|
||||
|
||||
/* Getter for the number of columns */
|
||||
int getColumns() const;
|
||||
int size() const;
|
||||
|
||||
double& operator[](int row);
|
||||
|
||||
ColumnVector& operator=(const ColumnVector& other);
|
||||
Vector& operator=(const Vector& other);
|
||||
|
||||
ColumnVector operator+(ColumnVector& other);
|
||||
Vector operator+(Vector& other);
|
||||
|
||||
ColumnVector operator-(ColumnVector& other);
|
||||
Vector operator-(Vector& other);
|
||||
|
||||
/* 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
|
||||
* 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);
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -57,19 +58,21 @@ protected:
|
||||
// Number of columns in matrix
|
||||
int columns;
|
||||
// Matrix representation as vector of vectors of integers
|
||||
std::vector<ColumnVector> matrix;
|
||||
std::vector<Vector> matrix;
|
||||
public:
|
||||
Matrix(int n, int m);
|
||||
|
||||
Matrix(const Matrix& other);
|
||||
|
||||
Matrix(std::initializer_list<std::vector<double>>);
|
||||
|
||||
/* Getter for the number of rows */
|
||||
int getRows() const;
|
||||
|
||||
/* Getter for the number of columns */
|
||||
int getColumns() const;
|
||||
|
||||
ColumnVector& operator[](int row);
|
||||
Vector& operator[](int row);
|
||||
|
||||
Matrix& operator=(const Matrix& other);
|
||||
|
||||
@@ -80,7 +83,7 @@ public:
|
||||
Matrix operator*(Matrix& other) const;
|
||||
|
||||
/* Matrix-Vector multiplication */
|
||||
ColumnVector operator*(ColumnVector other) const;
|
||||
Vector operator*(Vector other) const;
|
||||
|
||||
/* Produces transposed version of the matrix */
|
||||
Matrix transpose() const;
|
||||
@@ -94,4 +97,6 @@ public:
|
||||
friend std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj);
|
||||
};
|
||||
|
||||
void showMatrix(Matrix);
|
||||
|
||||
#endif // TOOLS_MATRIX_H
|
||||
|
||||
Reference in New Issue
Block a user