Compare commits
19
Commits
first_half
...
9b67db3629
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b67db3629 | ||
|
|
8b7f86d5b0 | ||
|
|
50fda70802 | ||
|
|
fa123e0fb0 | ||
|
|
c7bc142081 | ||
|
|
535c63938d | ||
|
|
5ee857a067 | ||
|
|
6e9ce3c894 | ||
|
|
2bfea15fde | ||
|
|
253cf335ce | ||
|
|
98d5fc9da9 | ||
|
|
9cf37ec9e4 | ||
|
|
36b4250ad3 | ||
|
|
6e5dab96b0 | ||
|
|
08eecfd88a | ||
|
|
2aee5013d6 | ||
|
|
ed2163ee9d | ||
|
|
38982b501a | ||
|
|
67a6ca1eae |
-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
|
|
||||||
Submodule
+1
Submodule SimplexTASK added at c7bc142081
@@ -1,19 +0,0 @@
|
|||||||
#include <iostream>
|
|
||||||
#include "tools/matrix.h"
|
|
||||||
#include "tools/math.h"
|
|
||||||
#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];
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
-133
@@ -1,133 +0,0 @@
|
|||||||
#include <algorithm>
|
|
||||||
#include <iostream>
|
|
||||||
#include "tools/matrix.h"
|
|
||||||
#include "tools/math.h"
|
|
||||||
|
|
||||||
enum solver_state {
|
|
||||||
unbounded,
|
|
||||||
bounded
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Result {
|
|
||||||
solver_state state;
|
|
||||||
ColumnVector *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(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize=true) {
|
|
||||||
|
|
||||||
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) {
|
|
||||||
//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);
|
|
||||||
|
|
||||||
//5
|
|
||||||
struct FracturedMatrix fractured_matrix();
|
|
||||||
fractured_matrix = eleminate(A, C, b, pivot_column_index, pivot_row_index);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
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];
|
|
||||||
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
Function_name(C, A, b, eps = eps_default)
|
|
||||||
|
|
||||||
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
|
|
||||||
*/
|
|
||||||
@@ -1,20 +0,0 @@
|
|||||||
#ifndef SIMPLEX_H
|
|
||||||
#define SIMPLEX_H
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include "tools/matrix.h"
|
|
||||||
|
|
||||||
enum solver_state {
|
|
||||||
unbounded,
|
|
||||||
bounded
|
|
||||||
};
|
|
||||||
|
|
||||||
struct Result {
|
|
||||||
solver_state state;
|
|
||||||
ColumnVector *solution;
|
|
||||||
double objective_fucntion_value;
|
|
||||||
};
|
|
||||||
|
|
||||||
Result Simplex(ColumnVector C, Matrix A, ColumnVector b, double eps = 0.01, bool maximize = true);
|
|
||||||
|
|
||||||
#endif // SIMPLEX_H
|
|
||||||
@@ -1,101 +0,0 @@
|
|||||||
#include "elimination.h"
|
|
||||||
#include "matrix.h"
|
|
||||||
#include "math.h"
|
|
||||||
|
|
||||||
struct FracturedMatrix {
|
|
||||||
Matrix A;
|
|
||||||
ColumnVector C;
|
|
||||||
ColumnVector b;
|
|
||||||
int pivot_column_index;
|
|
||||||
int pivot_row_index;
|
|
||||||
};
|
|
||||||
|
|
||||||
struct DestroyMatrix {
|
|
||||||
Matrix A;
|
|
||||||
ColumnVector C;
|
|
||||||
ColumnVector b;
|
|
||||||
};
|
|
||||||
|
|
||||||
DestroyMatrix destroyGeneralMatrix(Matrix& generalMatrix) {
|
|
||||||
int rows = generalMatrix.getRows();
|
|
||||||
int cols = generalMatrix.getColumns();
|
|
||||||
|
|
||||||
Matrix A(rows - 1, cols - 1);
|
|
||||||
ColumnVector C(cols - 1);
|
|
||||||
ColumnVector b(rows - 1);
|
|
||||||
|
|
||||||
for (int j = 0; j < cols - 1; ++j) {
|
|
||||||
C[j] = generalMatrix[0][j];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 1; i < rows; ++i) {
|
|
||||||
b[i - 1] = generalMatrix[i - 1][cols - 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 1; i < rows; ++i) {
|
|
||||||
for (int j = 0; j < cols - 1; ++j) {
|
|
||||||
A[i - 1][j] = generalMatrix[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return {A, C, b};
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix createGeneralMatrix(Matrix& A, ColumnVector& C, ColumnVector& b) {
|
|
||||||
Matrix generalMatrix(A.getRows() + 1, A.getColumns() + 1);
|
|
||||||
for (int i = 0; i < A.getColumns(); i++) {
|
|
||||||
generalMatrix[0][i] = C[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int j = 0; j < A.getRows(); j++) {
|
|
||||||
generalMatrix[j][A.getColumns()] = b[j];
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < A.getRows(); i++) {
|
|
||||||
for (int j = 0; j < A.getColumns(); j++) {
|
|
||||||
generalMatrix[i + 1][j] = A[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return generalMatrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
FracturedMatrix elimination(Matrix A, ColumnVector C, ColumnVector b, int pivot_column_index, int pivot_row_index) {
|
|
||||||
Matrix generalMatrix = createGeneralMatrix(A, C, b);
|
|
||||||
|
|
||||||
int rows = generalMatrix.getRows();
|
|
||||||
int cols = generalMatrix.getColumns();
|
|
||||||
|
|
||||||
double pivotElement = generalMatrix[pivot_row_index][pivot_column_index];
|
|
||||||
|
|
||||||
for (int j = 0; j < cols; ++j) {
|
|
||||||
generalMatrix[pivot_row_index][j] /= pivotElement;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
if (i == pivot_row_index)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
double pivotColumnCoefficient = generalMatrix[i][pivot_column_index];
|
|
||||||
|
|
||||||
for (int j = 0; j < cols; ++j) {
|
|
||||||
generalMatrix[i][j] -= pivotColumnCoefficient * generalMatrix[pivot_row_index][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
DestroyMatrix destroyedMatrix = destroyGeneralMatrix(generalMatrix);
|
|
||||||
|
|
||||||
pivot_column_index = Math::max_index(destroyedMatrix.C);
|
|
||||||
|
|
||||||
ColumnVector ratio_vector(A.getRows());
|
|
||||||
|
|
||||||
for (int i = 0; i < A.getRows(); i++) {
|
|
||||||
ratio_vector[i] = b[i] / A[i][pivot_column_index];
|
|
||||||
}
|
|
||||||
|
|
||||||
int pivot_row_index = Math::min_index(ratio_vector);
|
|
||||||
|
|
||||||
return {A, C, b, pivot_column_index, pivot_row_index};
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,13 +0,0 @@
|
|||||||
#ifndef ELIMINATION_H
|
|
||||||
#define ELIMINATION_H
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Elimination {
|
|
||||||
public:
|
|
||||||
Matrix elimination(Matrix A, ColumnVector C, ColumnVector b, double eps);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#endif //ELIMINATION_H
|
|
||||||
@@ -1,91 +0,0 @@
|
|||||||
#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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
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) {
|
|
||||||
temp_index = j;
|
|
||||||
temp = columnVector[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temp_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
int max_index(ColumnVector columnVector) {
|
|
||||||
int temp_index = 0;
|
|
||||||
double temp = columnVector[0];
|
|
||||||
for (int j = 0; j < columnVector.getColumns(); j++) {
|
|
||||||
if (columnVector[j] > temp) {
|
|
||||||
temp_index = j;
|
|
||||||
temp = columnVector[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temp_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
double min(std::vector<double> array) {
|
|
||||||
double temp = array[0];
|
|
||||||
for (size_t i = 1; i < array.size(); i++) {
|
|
||||||
if (array[i] < temp) {
|
|
||||||
temp = array[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
double max(std::vector<double> array) {
|
|
||||||
double temp = array[0];
|
|
||||||
for (size_t i = 1; i < array.size(); i++) {
|
|
||||||
if (array[i] > temp) {
|
|
||||||
temp = array[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
int min_index(std::vector<double> array) {
|
|
||||||
int temp_index = 0;
|
|
||||||
double temp = array[0];
|
|
||||||
for (size_t i = 1; i < array.size(); i++) {
|
|
||||||
if (array[i] < temp) {
|
|
||||||
temp_index = i;
|
|
||||||
temp = array[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temp_index;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int max_index(std::vector<double> array) {
|
|
||||||
int temp_index = 0;
|
|
||||||
double temp = array[0];
|
|
||||||
for (size_t i = 1; i < array.size(); i++) {
|
|
||||||
if (array[i] > temp) {
|
|
||||||
temp_index = i;
|
|
||||||
temp = array[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return temp_index;
|
|
||||||
}
|
|
||||||
@@ -1,15 +0,0 @@
|
|||||||
#ifndef MATH_H
|
|
||||||
#define MATH_H
|
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
double min(ColumnVector columnVector);
|
|
||||||
double max(ColumnVector columnVector);
|
|
||||||
int min_index(ColumnVector columnVector);
|
|
||||||
int max_index(ColumnVector columnVector);
|
|
||||||
double min(std::vector<double> array);
|
|
||||||
double max(std::vector<double> array);
|
|
||||||
int min_index(std::vector<double> array);
|
|
||||||
int max_index(std::vector<double> array);
|
|
||||||
|
|
||||||
#endif // MATH_H
|
|
||||||
@@ -1,213 +0,0 @@
|
|||||||
#include "matrix.h"
|
|
||||||
|
|
||||||
ColumnVector::ColumnVector(int n) {
|
|
||||||
rows = n;
|
|
||||||
columns = 1;
|
|
||||||
columnVector.resize(n);
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector::ColumnVector(const ColumnVector& other) {
|
|
||||||
rows = other.rows;
|
|
||||||
columns = other.columns;
|
|
||||||
columnVector = other.columnVector;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ColumnVector::getRows() const {
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
int ColumnVector::getColumns() const {
|
|
||||||
return columns;
|
|
||||||
}
|
|
||||||
|
|
||||||
double& ColumnVector::operator[](int row) {
|
|
||||||
return columnVector[row];
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector& ColumnVector::operator=(const ColumnVector& other) {
|
|
||||||
rows = other.rows;
|
|
||||||
columns = other.columns;
|
|
||||||
columnVector = other.columnVector;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector ColumnVector::operator+(ColumnVector& other) {
|
|
||||||
if (rows != other.rows) {
|
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
|
||||||
}
|
|
||||||
ColumnVector result(rows);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
result[i] = columnVector[i] + other[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector ColumnVector::operator-(ColumnVector& other) {
|
|
||||||
if (rows != other.rows) {
|
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
|
||||||
}
|
|
||||||
ColumnVector result(rows);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
result[i] = columnVector[i] - other[i];
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::istream& operator>>(std::istream& cin, ColumnVector& vectorObj) {
|
|
||||||
for (int i = 0; i < vectorObj.rows; ++i) {
|
|
||||||
cin >> vectorObj[i];
|
|
||||||
}
|
|
||||||
return cin;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& cout, ColumnVector& vectorObj) {
|
|
||||||
for (int i = 0; i < vectorObj.rows; ++i) {
|
|
||||||
if (i == vectorObj.rows - 1) {
|
|
||||||
cout << vectorObj[i] << std::endl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cout << vectorObj[i] << ' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cout;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix::Matrix(int n, int m) {
|
|
||||||
rows = n;
|
|
||||||
columns = m;
|
|
||||||
matrix.resize(n, ColumnVector(m));
|
|
||||||
for (auto& row : matrix) {
|
|
||||||
row = ColumnVector(m);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix::Matrix(const Matrix& other) {
|
|
||||||
rows = other.rows;
|
|
||||||
columns = other.columns;
|
|
||||||
matrix = other.matrix;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Matrix::getRows() const {
|
|
||||||
return rows;
|
|
||||||
}
|
|
||||||
|
|
||||||
int Matrix::getColumns() const {
|
|
||||||
return columns;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector& Matrix::operator[](int row) {
|
|
||||||
return matrix[row];
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix& Matrix::operator=(const Matrix& other) {
|
|
||||||
rows = other.rows;
|
|
||||||
columns = other.columns;
|
|
||||||
matrix = other.matrix;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix Matrix::operator+(Matrix& other) const {
|
|
||||||
if (rows != other.rows || columns != other.columns) {
|
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
|
||||||
}
|
|
||||||
Matrix result(rows, columns);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
for (int j = 0; j < columns; ++j) {
|
|
||||||
auto x = matrix[i];
|
|
||||||
auto y = other[i];
|
|
||||||
result[i][j] = x[j] + y[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix Matrix::operator-(Matrix& other) const {
|
|
||||||
if (rows != other.rows || columns != other.columns) {
|
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
|
||||||
}
|
|
||||||
Matrix result(rows, columns);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
for (int j = 0; j < columns; ++j) {
|
|
||||||
auto x = matrix[i];
|
|
||||||
auto y = other[i];
|
|
||||||
result[i][j] = x[j] - y[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix Matrix::operator*(Matrix& other) const {
|
|
||||||
if (columns != other.rows) {
|
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix result(rows, other.columns);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
for (int j = 0; j < other.columns; ++j) {
|
|
||||||
result[i][j] = 0;
|
|
||||||
for (int k = 0; k < columns; ++k) {
|
|
||||||
auto x = matrix[i];
|
|
||||||
auto y = other[k];
|
|
||||||
result[i][j] += x[k] * y[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector Matrix::operator*(ColumnVector other) const {
|
|
||||||
if (columns != other.getRows()) {
|
|
||||||
throw std::runtime_error("Error: the dimensional problem occurred");
|
|
||||||
}
|
|
||||||
|
|
||||||
ColumnVector result(rows);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
result[i] = 0;
|
|
||||||
for (int k = 0; k < columns; ++k) {
|
|
||||||
auto x = matrix[i];
|
|
||||||
result[i] += x[k] * other[k];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Matrix Matrix::transpose() const {
|
|
||||||
Matrix result(columns, rows);
|
|
||||||
for (int i = 0; i < rows; ++i) {
|
|
||||||
for (int j = 0; j < columns; ++j) {
|
|
||||||
auto x = matrix[i];
|
|
||||||
result[j][i] = x[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::istream& operator>>(std::istream& cin, Matrix& matrixObj) {
|
|
||||||
for (int i = 0; i < matrixObj.rows; ++i) {
|
|
||||||
for (int j = 0; j < matrixObj.columns; ++j) {
|
|
||||||
cin >> matrixObj[i][j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cin;
|
|
||||||
}
|
|
||||||
|
|
||||||
std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj) {
|
|
||||||
for (int i = 0; i < matrixObj.rows; ++i) {
|
|
||||||
for (int j = 0; j < matrixObj.columns; ++j) {
|
|
||||||
if (j == matrixObj.columns - 1) {
|
|
||||||
cout << matrixObj[i][j] << std::endl;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
cout << matrixObj[i][j] << ' ';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return cout;
|
|
||||||
}
|
|
||||||
@@ -1,97 +0,0 @@
|
|||||||
#ifndef TOOLS_MATRIX_H
|
|
||||||
#define TOOLS_MATRIX_H
|
|
||||||
|
|
||||||
// some functions for matrix interaction
|
|
||||||
#include <iostream>
|
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
/**
|
|
||||||
* ColumnVector is a class to represent
|
|
||||||
* a column vector with n rows.
|
|
||||||
*/
|
|
||||||
class ColumnVector {
|
|
||||||
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;
|
|
||||||
public:
|
|
||||||
ColumnVector(int n);
|
|
||||||
|
|
||||||
ColumnVector(const ColumnVector& other);
|
|
||||||
|
|
||||||
/* Getter for the number of rows */
|
|
||||||
int getRows() const;
|
|
||||||
|
|
||||||
/* Getter for the number of columns */
|
|
||||||
int getColumns() const;
|
|
||||||
|
|
||||||
double& operator[](int row);
|
|
||||||
|
|
||||||
ColumnVector& operator=(const ColumnVector& other);
|
|
||||||
|
|
||||||
ColumnVector operator+(ColumnVector& other);
|
|
||||||
|
|
||||||
ColumnVector operator-(ColumnVector& other);
|
|
||||||
|
|
||||||
/* Input operator reads element of vector */
|
|
||||||
friend std::istream& operator>>(std::istream& cin, ColumnVector& 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);
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Class Matrix represents
|
|
||||||
* a matrix of size n x m
|
|
||||||
* of type integer.
|
|
||||||
*/
|
|
||||||
class Matrix {
|
|
||||||
protected:
|
|
||||||
// Number of rows in matrix
|
|
||||||
int rows;
|
|
||||||
// Number of columns in matrix
|
|
||||||
int columns;
|
|
||||||
// Matrix representation as vector of vectors of integers
|
|
||||||
std::vector<ColumnVector> matrix;
|
|
||||||
public:
|
|
||||||
Matrix(int n, int m);
|
|
||||||
|
|
||||||
Matrix(const Matrix& other);
|
|
||||||
|
|
||||||
/* Getter for the number of rows */
|
|
||||||
int getRows() const;
|
|
||||||
|
|
||||||
/* Getter for the number of columns */
|
|
||||||
int getColumns() const;
|
|
||||||
|
|
||||||
ColumnVector& operator[](int row);
|
|
||||||
|
|
||||||
Matrix& operator=(const Matrix& other);
|
|
||||||
|
|
||||||
Matrix operator+(Matrix& other) const;
|
|
||||||
|
|
||||||
Matrix operator-(Matrix& other) const;
|
|
||||||
|
|
||||||
Matrix operator*(Matrix& other) const;
|
|
||||||
|
|
||||||
/* Matrix-Vector multiplication */
|
|
||||||
ColumnVector operator*(ColumnVector other) const;
|
|
||||||
|
|
||||||
/* Produces transposed version of the matrix */
|
|
||||||
Matrix transpose() const;
|
|
||||||
|
|
||||||
/* Input operator reads element of matrix row by row */
|
|
||||||
friend std::istream& operator>>(std::istream& cin, Matrix& matrixObj);
|
|
||||||
|
|
||||||
/* Output operator prints elements of the matrix
|
|
||||||
* row by row separated with a space (no space at the end of each line)
|
|
||||||
*/
|
|
||||||
friend std::ostream& operator<<(std::ostream& cout, Matrix& matrixObj);
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif // TOOLS_MATRIX_H
|
|
||||||
Reference in New Issue
Block a user