update matrix functionality
This commit is contained in:
+213
-1
@@ -1 +1,213 @@
|
||||
// some functions for matrix interaction
|
||||
#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);
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,97 @@
|
||||
#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