#include #include "MatrixT.h" #include using namespace std; MatrixDT::MatrixDT(size_t r, size_t c ) { rows = r; cols = c; data = new double[rows * cols]; } MatrixDT::MatrixDT(const MatrixDT & other) { rows = other.Rows(); cols = other.Cols(); data = new double[rows * cols]; for(size_t r = 0; r < rows; ++r) { for(size_t c = 0; c < cols; ++c) { data[r*cols + c] = other[r,c]; } } } MatrixDT::MatrixDT(const MatrixVT & other) { rows = other.Rows(); cols = other.Cols(); data = new double[rows * cols]; for(size_t r = 0; r < rows; ++r) { for(size_t c = 0; c < cols; ++c) { data[r*cols + c] = other[r,c]; } } } MatrixDT::~MatrixDT() { delete[] data; } size_t MatrixDT::Rows() const { return rows; } size_t MatrixDT::Cols() const { return cols; } double & MatrixDT::operator [] (size_t r, size_t c) { if (r > rows or c > cols) { cout << r << " " << c << endl; throw (out_of_range("Index out of bounds")); } return data[r*cols+c]; } const double & MatrixDT::operator [] (size_t r, size_t c) const { if (r > rows or c > cols) { cout << r << " " << c << endl; throw (out_of_range("Index out of bounds")); } return data[r*cols+c]; } MatrixDT MatrixDT::operator * (const MatrixDT & other) { if (cols != other.rows) { throw (range_error("Matrix sizes don't match.")); } MatrixDT rv(rows, other.cols); for(size_t r = 0; r < rows; ++r) { for(size_t c = 0; c < other.cols; c++) { for(size_t i = 0; i < other.rows; ++i) { rv[r,c] += data[r*cols+i] * other[i,c]; } } } return rv; } MatrixVT::MatrixVT(size_t r, size_t c) : rows{r}, cols{c}, data{rows, std::vector(cols, 0.0d)} {} MatrixVT::MatrixVT(const MatrixDT & other): rows{other.Rows()}, cols{other.Cols()}, data{rows, std::vector(cols, 0.0d)} { for(size_t r = 0; r < rows; ++r) { for(size_t c = 0; r < cols; ++r) { data[r][c] = other[r,c]; } } } size_t MatrixVT::Rows() const { return rows; } size_t MatrixVT::Cols() const { return cols; } double & MatrixVT::operator [] (size_t r, size_t c) { if (r > rows or c > cols) { cout << r << " " << c << endl; throw (out_of_range("Index out of bounds")); } return data[r][c]; } const double & MatrixVT::operator [] (size_t r, size_t c) const { if (r > rows or c > cols) { cout << r << " " << c << endl; throw (out_of_range("Index out of bounds")); } return data[r][c]; } MatrixVT MatrixVT::operator * (const MatrixVT & other) { if (cols != other.rows) { throw (range_error("Matrix sizes don't match.")); } MatrixVT rv(rows, other.cols); for(size_t r = 0; r < rows; ++r) { for(size_t c = 0; c < other.cols; c++) { for(size_t i = 0; i < other.rows; ++i) { rv[r,c] += data[r][i] * other.data[i][c]; } } } return rv; }