Files
CSCI-1200/hws/matrix_class/Matrix.h
2025-03-12 10:01:26 -04:00

65 lines
2.0 KiB
C++

#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
class Matrix {
public:
// Constructors & Destructor
Matrix(); // Default constructor (creates an empty 0 x 0 matrix)
Matrix(unsigned int rows, unsigned int cols, double fill);
Matrix(const Matrix &other);
~Matrix();
Matrix& operator=(const Matrix &other);
// Accessors for dimensions
unsigned int num_rows() const;
unsigned int num_cols() const;
// Clears the matrix (deallocates any memory and sets size to 0 x 0)
void clear();
// Safe accessor and modifier methods
bool get(unsigned int row, unsigned int col, double &value) const;
bool set(unsigned int row, unsigned int col, double value);
// Simple matrix operations
void multiply_by_coefficient(double coefficient);
bool swap_row(unsigned int row1, unsigned int row2);
void transpose();
// Binary matrix operations (modifies this matrix)
bool add(const Matrix &other);
bool subtract(const Matrix &other);
// Advanced accessors: returns a dynamic array with the requested row or column.
// The caller is responsible for deleting the returned array.
double* get_row(unsigned int row) const;
double* get_col(unsigned int col) const;
// Quarter the matrix into four equally sized quadrants.
// The four matrices are returned in a dynamically allocated array in the order:
// UL, UR, LL, LR.
// If the matrix is too small (i.e. less than 2 rows or 2 cols), returns four empty matrices.
Matrix* quarter() const;
// Equality operators
bool operator==(const Matrix &other) const;
bool operator!=(const Matrix &other) const;
// Friend overloaded output operator for printing the matrix.
friend std::ostream& operator<<(std::ostream &out, const Matrix &m);
private:
unsigned int rows;
unsigned int cols;
double** data;
// Helper functions to allocate and deallocate the 2D array.
void allocate(unsigned int r, unsigned int c, double fill);
void deallocate();
};
#endif