add solution for hw 5
This commit is contained in:
@@ -1,15 +1,12 @@
|
||||
#include "Matrix.h"
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
//helper function to allocate memory for a matrix of size r x c and fill it with "fill"
|
||||
void Matrix::allocateMemory(unsigned int r, unsigned int c, double fill) {
|
||||
if(r == 0 || c == 0) {
|
||||
rows = 0;
|
||||
cols = 0;
|
||||
data = nullptr;
|
||||
return;
|
||||
}
|
||||
//-------------------------
|
||||
// Private Helper Functions
|
||||
//-------------------------
|
||||
|
||||
// Allocates memory for an r x c matrix and fills every element with 'fill'
|
||||
void Matrix::allocate(unsigned int r, unsigned int c, double fill) {
|
||||
rows = r;
|
||||
cols = c;
|
||||
data = new double*[rows];
|
||||
@@ -21,149 +18,180 @@ void Matrix::allocateMemory(unsigned int r, unsigned int c, double fill) {
|
||||
}
|
||||
}
|
||||
|
||||
//helper function to deallocate memory
|
||||
void Matrix::deallocateMemory() {
|
||||
if(data != nullptr) {
|
||||
// Deallocates the memory used by the matrix
|
||||
void Matrix::deallocate() {
|
||||
if (data) {
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
delete [] data[i];
|
||||
}
|
||||
delete [] data;
|
||||
data = nullptr;
|
||||
}
|
||||
data = nullptr;
|
||||
rows = 0;
|
||||
cols = 0;
|
||||
}
|
||||
|
||||
//default constructor: creates an empty 0 x 0 matrix
|
||||
Matrix::Matrix() : rows(0), cols(0), data(nullptr) { }
|
||||
//-------------------------
|
||||
// Constructors & Destructor
|
||||
//-------------------------
|
||||
|
||||
//parameterized constructor
|
||||
// Default constructor: creates an empty matrix (0 x 0)
|
||||
Matrix::Matrix() : rows(0), cols(0), data(nullptr) {}
|
||||
|
||||
// Parameterized constructor: creates an r x c matrix filled with 'fill'
|
||||
// If either dimension is 0, an empty matrix is created.
|
||||
Matrix::Matrix(unsigned int r, unsigned int c, double fill) : rows(0), cols(0), data(nullptr) {
|
||||
allocateMemory(r, c, fill);
|
||||
if (r == 0 || c == 0) {
|
||||
// Create an empty matrix.
|
||||
rows = 0;
|
||||
cols = 0;
|
||||
data = nullptr;
|
||||
} else {
|
||||
allocate(r, c, fill);
|
||||
}
|
||||
}
|
||||
|
||||
//copy constructor
|
||||
Matrix::Matrix(const Matrix& other) : rows(0), cols(0), data(nullptr) {
|
||||
allocateMemory(other.rows, other.cols, 0.0);
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
data[i][j] = other.data[i][j];
|
||||
// Copy constructor
|
||||
Matrix::Matrix(const Matrix &other) : rows(0), cols(0), data(nullptr) {
|
||||
if (other.rows == 0 || other.cols == 0) {
|
||||
rows = 0;
|
||||
cols = 0;
|
||||
data = nullptr;
|
||||
} else {
|
||||
allocate(other.rows, other.cols, 0.0);
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
data[i][j] = other.data[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//destructor
|
||||
// Destructor
|
||||
Matrix::~Matrix() {
|
||||
deallocateMemory();
|
||||
deallocate();
|
||||
}
|
||||
|
||||
//assignment operator
|
||||
Matrix& Matrix::operator=(const Matrix& other) {
|
||||
// Assignment operator
|
||||
Matrix& Matrix::operator=(const Matrix &other) {
|
||||
if (this == &other)
|
||||
return *this;
|
||||
deallocateMemory();
|
||||
allocateMemory(other.rows, other.cols, 0.0);
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
data[i][j] = other.data[i][j];
|
||||
|
||||
deallocate();
|
||||
|
||||
if (other.rows == 0 || other.cols == 0) {
|
||||
rows = 0;
|
||||
cols = 0;
|
||||
data = nullptr;
|
||||
} else {
|
||||
allocate(other.rows, other.cols, 0.0);
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
data[i][j] = other.data[i][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
//returns the number of rows
|
||||
//-------------------------
|
||||
// Dimension Accessors & Clear
|
||||
//-------------------------
|
||||
|
||||
unsigned int Matrix::num_rows() const {
|
||||
return rows;
|
||||
}
|
||||
|
||||
//returns the number of columns
|
||||
unsigned int Matrix::num_cols() const {
|
||||
return cols;
|
||||
}
|
||||
|
||||
//clears the matrix by deallocating its memory
|
||||
void Matrix::clear() {
|
||||
deallocateMemory();
|
||||
deallocate();
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
// Safe Accessors & Modifiers
|
||||
//-------------------------
|
||||
|
||||
// get(): If (row,col) is within bounds, set value and return true; otherwise, return false.
|
||||
bool Matrix::get(unsigned int row, unsigned int col, double &value) const {
|
||||
if(row >= rows || col >= cols) {
|
||||
if (row >= rows || col >= cols)
|
||||
return false;
|
||||
}
|
||||
value = data[row][col];
|
||||
return true;
|
||||
}
|
||||
|
||||
//modifier
|
||||
// set(): If (row,col) is valid, assign value and return true; else return false.
|
||||
bool Matrix::set(unsigned int row, unsigned int col, double value) {
|
||||
if(row >= rows || col >= cols) {
|
||||
if (row >= rows || col >= cols)
|
||||
return false;
|
||||
}
|
||||
data[row][col] = value;
|
||||
return true;
|
||||
}
|
||||
|
||||
//equality operator
|
||||
bool Matrix::operator==(const Matrix& other) const {
|
||||
//two matrices are equal if dimensions match
|
||||
//and every element is equal within a small epsilon
|
||||
if(rows != other.rows || cols != other.cols)
|
||||
return false;
|
||||
//-------------------------
|
||||
// Simple Matrix Operations
|
||||
//-------------------------
|
||||
|
||||
// Multiplies every element in the matrix by the given coefficient.
|
||||
void Matrix::multiply_by_coefficient(double coefficient) {
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
if (fabs(data[i][j] - other.data[i][j]) > 1e-10)
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Matrix::operator!=(const Matrix& other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
//multiplies every element by the given coefficient
|
||||
void Matrix::multiply_by_coefficient(double coeff) {
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
data[i][j] *= coeff;
|
||||
data[i][j] *= coefficient;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//swaps two rows of the matrix (by swapping the row pointers)
|
||||
// Swaps the entire contents of row1 and row2 if both indices are valid.
|
||||
bool Matrix::swap_row(unsigned int row1, unsigned int row2) {
|
||||
if(row1 >= rows || row2 >= rows) {
|
||||
if (row1 >= rows || row2 >= rows)
|
||||
return false;
|
||||
}
|
||||
double* temp = data[row1];
|
||||
data[row1] = data[row2];
|
||||
data[row2] = temp;
|
||||
return true;
|
||||
}
|
||||
|
||||
//transposes the matrix in place
|
||||
// Transposes the matrix in place.
|
||||
// For non-square matrices, a new 2D array is allocated, the contents are transposed,
|
||||
// and the old memory is deallocated.
|
||||
void Matrix::transpose() {
|
||||
if(rows == 0 || cols == 0)
|
||||
if (rows == 0 || cols == 0)
|
||||
return;
|
||||
unsigned int newRows = cols;
|
||||
unsigned int newCols = rows;
|
||||
double** newData = new double*[newRows];
|
||||
for (unsigned int i = 0; i < newRows; i++) {
|
||||
newData[i] = new double[newCols];
|
||||
for (unsigned int j = 0; j < newCols; j++) {
|
||||
newData[i][j] = data[j][i];
|
||||
|
||||
// Allocate new array with swapped dimensions.
|
||||
double** newData = new double*[cols];
|
||||
for (unsigned int i = 0; i < cols; i++) {
|
||||
newData[i] = new double[rows];
|
||||
}
|
||||
// Transpose: newData[j][i] becomes data[i][j]
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
newData[j][i] = data[i][j];
|
||||
}
|
||||
}
|
||||
deallocateMemory();
|
||||
rows = newRows;
|
||||
cols = newCols;
|
||||
// Free old data.
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
delete [] data[i];
|
||||
}
|
||||
delete [] data;
|
||||
|
||||
// Swap dimensions.
|
||||
unsigned int temp = rows;
|
||||
rows = cols;
|
||||
cols = temp;
|
||||
data = newData;
|
||||
}
|
||||
|
||||
//adds another matrix to this one element-wise
|
||||
bool Matrix::add(const Matrix& other) {
|
||||
if(rows != other.rows || cols != other.cols)
|
||||
//-------------------------
|
||||
// Binary Matrix Operations
|
||||
//-------------------------
|
||||
|
||||
// Adds the corresponding elements of other to this matrix.
|
||||
// Returns true if dimensions match, else returns false.
|
||||
bool Matrix::add(const Matrix &other) {
|
||||
if (rows != other.rows || cols != other.cols)
|
||||
return false;
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
@@ -173,9 +201,10 @@ bool Matrix::add(const Matrix& other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//subtracts another matrix from this one element-wise
|
||||
bool Matrix::subtract(const Matrix& other) {
|
||||
if(rows != other.rows || cols != other.cols)
|
||||
// Subtracts the corresponding elements of other from this matrix.
|
||||
// Returns true if dimensions match, else returns false.
|
||||
bool Matrix::subtract(const Matrix &other) {
|
||||
if (rows != other.rows || cols != other.cols)
|
||||
return false;
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
@@ -185,11 +214,15 @@ bool Matrix::subtract(const Matrix& other) {
|
||||
return true;
|
||||
}
|
||||
|
||||
//returns a dynamically allocated copy of the specified row
|
||||
//caller must delete[] the returned array
|
||||
//-------------------------
|
||||
// Advanced Accessors
|
||||
//-------------------------
|
||||
|
||||
// Returns a new dynamically allocated array containing the requested row.
|
||||
// Returns nullptr if the row index is out of bounds.
|
||||
double* Matrix::get_row(unsigned int row) const {
|
||||
if(row >= rows)
|
||||
return NULL;
|
||||
if (row >= rows)
|
||||
return nullptr;
|
||||
double* rowArray = new double[cols];
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
rowArray[j] = data[row][j];
|
||||
@@ -197,11 +230,11 @@ double* Matrix::get_row(unsigned int row) const {
|
||||
return rowArray;
|
||||
}
|
||||
|
||||
//returns a dynamically allocated copy of the specified column
|
||||
//caller must delete[] the returned array
|
||||
// Returns a new dynamically allocated array containing the requested column.
|
||||
// Returns nullptr if the column index is out of bounds.
|
||||
double* Matrix::get_col(unsigned int col) const {
|
||||
if(col >= cols)
|
||||
return NULL;
|
||||
if (col >= cols)
|
||||
return nullptr;
|
||||
double* colArray = new double[rows];
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
colArray[i] = data[i][col];
|
||||
@@ -209,84 +242,102 @@ double* Matrix::get_col(unsigned int col) const {
|
||||
return colArray;
|
||||
}
|
||||
|
||||
//divides the matrix into four quadrants and returns a pointer to an array of four matrices
|
||||
//each quadrant size = ceil(rows/2) x ceil(cols/2)
|
||||
//-------------------------
|
||||
// Quarter Operation
|
||||
//-------------------------
|
||||
|
||||
// Splits the matrix into four quadrants (UL, UR, LL, LR) and returns them in a new array.
|
||||
// All four quadrants will have the same dimensions.
|
||||
// If the matrix has fewer than 2 rows or 2 columns, returns four empty matrices.
|
||||
Matrix* Matrix::quarter() const {
|
||||
if (rows == 0 || cols == 0) {
|
||||
return nullptr;
|
||||
Matrix* quadrants = new Matrix[4];
|
||||
if (rows < 2 || cols < 2) {
|
||||
// Return four empty matrices.
|
||||
return quadrants;
|
||||
}
|
||||
// Determine quadrant size so that all four quadrants are identical.
|
||||
// For overlapping, use ceil for both dimensions.
|
||||
unsigned int q_rows = (rows % 2 == 0) ? (rows / 2) : (rows / 2 + 1);
|
||||
unsigned int q_cols = (cols % 2 == 0) ? (cols / 2) : (cols / 2 + 1);
|
||||
|
||||
// For an overlapping quarter, the top quadrants start at row 0,
|
||||
// the bottom quadrants start at floor(rows/2), similarly for columns.
|
||||
unsigned int start_row_bottom = rows / 2;
|
||||
unsigned int start_col_right = cols / 2;
|
||||
// Determine quadrant dimensions.
|
||||
// Using (dim + 1) / 2 ensures that if the dimension is odd the shared middle row/col is included.
|
||||
unsigned int quad_rows = (rows + 1) / 2;
|
||||
unsigned int quad_cols = (cols + 1) / 2;
|
||||
|
||||
Matrix* quadrants = new Matrix[4]{
|
||||
Matrix(q_rows, q_cols, 0.0), // Upper Left
|
||||
Matrix(q_rows, q_cols, 0.0), // Upper Right
|
||||
Matrix(q_rows, q_cols, 0.0), // Lower Left
|
||||
Matrix(q_rows, q_cols, 0.0) // Lower Right
|
||||
};
|
||||
quadrants[0] = Matrix(quad_rows, quad_cols, 0.0); // Upper Left (UL)
|
||||
quadrants[1] = Matrix(quad_rows, quad_cols, 0.0); // Upper Right (UR)
|
||||
quadrants[2] = Matrix(quad_rows, quad_cols, 0.0); // Lower Left (LL)
|
||||
quadrants[3] = Matrix(quad_rows, quad_cols, 0.0); // Lower Right (LR)
|
||||
|
||||
// Fill Upper Left quadrant from original (starting at (0,0)).
|
||||
for (unsigned int i = 0; i < q_rows; i++) {
|
||||
for (unsigned int j = 0; j < q_cols; j++) {
|
||||
double value;
|
||||
if (i < rows && j < cols && get(i, j, value))
|
||||
quadrants[0].set(i, j, value);
|
||||
// Fill UL quadrant: rows 0 .. quad_rows-1, cols 0 .. quad_cols-1.
|
||||
for (unsigned int i = 0; i < quad_rows; i++) {
|
||||
for (unsigned int j = 0; j < quad_cols; j++) {
|
||||
quadrants[0].set(i, j, data[i][j]);
|
||||
}
|
||||
}
|
||||
// Fill Upper Right quadrant from original (starting at (0, start_col_right)).
|
||||
for (unsigned int i = 0; i < q_rows; i++) {
|
||||
for (unsigned int j = 0; j < q_cols; j++) {
|
||||
double value;
|
||||
if (i < rows && (j + start_col_right) < cols && get(i, j + start_col_right, value))
|
||||
quadrants[1].set(i, j, value);
|
||||
|
||||
// Fill UR quadrant: rows 0 .. quad_rows-1, cols (cols - quad_cols) .. (cols - 1).
|
||||
for (unsigned int i = 0; i < quad_rows; i++) {
|
||||
for (unsigned int j = 0; j < quad_cols; j++) {
|
||||
quadrants[1].set(i, j, data[i][(cols - quad_cols) + j]);
|
||||
}
|
||||
}
|
||||
// Fill Lower Left quadrant from original (starting at (start_row_bottom, 0)).
|
||||
for (unsigned int i = 0; i < q_rows; i++) {
|
||||
for (unsigned int j = 0; j < q_cols; j++) {
|
||||
double value;
|
||||
if ((i + start_row_bottom) < rows && j < cols && get(i + start_row_bottom, j, value))
|
||||
quadrants[2].set(i, j, value);
|
||||
|
||||
// Fill LL quadrant: rows (rows - quad_rows) .. (rows - 1), cols 0 .. quad_cols-1.
|
||||
for (unsigned int i = 0; i < quad_rows; i++) {
|
||||
for (unsigned int j = 0; j < quad_cols; j++) {
|
||||
quadrants[2].set(i, j, data[(rows - quad_rows) + i][j]);
|
||||
}
|
||||
}
|
||||
// Fill Lower Right quadrant from original (starting at (start_row_bottom, start_col_right)).
|
||||
for (unsigned int i = 0; i < q_rows; i++) {
|
||||
for (unsigned int j = 0; j < q_cols; j++) {
|
||||
double value;
|
||||
if ((i + start_row_bottom) < rows && (j + start_col_right) < cols &&
|
||||
get(i + start_row_bottom, j + start_col_right, value))
|
||||
quadrants[3].set(i, j, value);
|
||||
|
||||
// Fill LR quadrant: rows (rows - quad_rows) .. (rows - 1), cols (cols - quad_cols) .. (cols - 1).
|
||||
for (unsigned int i = 0; i < quad_rows; i++) {
|
||||
for (unsigned int j = 0; j < quad_cols; j++) {
|
||||
quadrants[3].set(i, j, data[(rows - quad_rows) + i][(cols - quad_cols) + j]);
|
||||
}
|
||||
}
|
||||
|
||||
return quadrants;
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
// Equality Operators
|
||||
//-------------------------
|
||||
|
||||
//overloaded output operator to print the matrix
|
||||
// 4 x 4 matrix:
|
||||
// [ 14 14 14 14
|
||||
// 14 14 14 14
|
||||
// 14 9 14 14
|
||||
// 14 14 14 13 ]
|
||||
std::ostream& operator<<(std::ostream& out, const Matrix& m) {
|
||||
out << m.rows << " x " << m.cols << " matrix:" << std::endl;
|
||||
out << "[ ";
|
||||
for (unsigned int i = 0; i < m.rows; i++) {
|
||||
for (unsigned int j = 0; j < m.cols; j++) {
|
||||
out << m.data[i][j];
|
||||
if(j < m.cols - 1)
|
||||
out << " ";
|
||||
bool Matrix::operator==(const Matrix &other) const {
|
||||
if (rows != other.rows || cols != other.cols)
|
||||
return false;
|
||||
for (unsigned int i = 0; i < rows; i++) {
|
||||
for (unsigned int j = 0; j < cols; j++) {
|
||||
if (data[i][j] != other.data[i][j])
|
||||
return false;
|
||||
}
|
||||
if(i < m.rows - 1)
|
||||
out << std::endl << " ";
|
||||
}
|
||||
out << " ]";
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Matrix::operator!=(const Matrix &other) const {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
//-------------------------
|
||||
// Overloaded Output Operator
|
||||
//-------------------------
|
||||
|
||||
std::ostream& operator<<(std::ostream &out, const Matrix &m) {
|
||||
out << m.rows << " x " << m.cols << " matrix:" << std::endl;
|
||||
out << "[";
|
||||
if (m.rows > 0 && m.cols > 0) {
|
||||
for (unsigned int i = 0; i < m.rows; i++) {
|
||||
out << " ";
|
||||
for (unsigned int j = 0; j < m.cols; j++) {
|
||||
out << m.data[i][j];
|
||||
if (j < m.cols - 1)
|
||||
out << " ";
|
||||
}
|
||||
if (i < m.rows - 1)
|
||||
out << std::endl;
|
||||
}
|
||||
out << " ]";
|
||||
} else {
|
||||
out << " ]";
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user