Matrix class implementation

The concept of matrix and multidimensional array had always been an intriguing topic for me when I was in programming classes. Since computer memory isn’t organized in tabular form, there should be a way to represent it. Today, I am going to show you how to implement a matrix class with a component of an array. It will simply has some basic operations of matrix such as addition, subtraction, and multiplication.

Firstly, I will start off with identifying the attributes of the matrix class. It simply consists of a 1D array, a number of rows, and a number of columns.

Array *A;
int rows = 0, cols = 0;

void deepcopy(const Matrix &other);

A default constructor initializing a matrix consists of one row and one column stores the number zero.

Matrix::Matrix()
{
    allocate_(this->A, 1, 1);
    this->rows = this->cols = 1;
    this->A[0].setElement(0, 0);
}

setElement is a setter from my Array class. It sets a value at an index in an array.

void Array::setElement(int index, int value)
{
    this->A[index] = value;
}

Next, I define the parameterized constructor taking in the number of rows and columns of the matrix a user desires.

Matrix::Matrix(int x, int y)
{
    allocate_(this->A, x, y);
    this->rows = x;
    this->cols = y;
    for (int i = 0; i < this->rows; i++)
    {
        for (int j = 0; j < this->cols; j++)
        {
            this->A[i].setElement(j, 0);
        }
    }
}

This method also initializes a matrix with all zeros.

Now, I would like to input values for the matrix and display it on the screen.

void Matrix::input()
{
    for (int i = 0; i < this->rows; i++)
    {
        this->A[i].set_na(this->cols);
        cin >> A[i];
    }
}

void Matrix::output()
{
    for (int i = 0; i < this->rows; i++)
    {
        for (int j = 0; j < this->cols; j++)
        {
            cout << this->A[i].getElement(j) << " ";
        }
        cout << endl;
    }
}

I could write methods like this and calling them. However, overloading operators seems more professional.

istream &operator>>(istream &in, Matrix &a)
{
    for (int i = 0; i < a.view_rows(); i++)
    {
        a.A[i].set_na(a.view_cols());
        in >> a.A[i];
    }
    return in;
}

ostream &operator<<(ostream &out, Matrix a)
{
    for (int i = 0; i < a.view_rows(); i++)
    {
        for (int j = 0; j < a.view_cols(); j++)
        {
            out << a.A[i].getElement(j) << " ";
        }
        cout << endl;
    }
    return out;
}

Last but not least, I overloaded the addition and subtraction operators.

Matrix operator+(Matrix m1, Matrix m2)
{
    Matrix a(m1.view_rows(), m1.view_cols());
    if (m1.view_rows() != m2.view_rows() || m1.view_cols() != m2.view_cols())
    {
        cout << "Mismatched dimensions!" << endl;
    }
    else
    {
        for (int i = 0; i < m1.view_rows(); i++)
        {
            for (int j = 0; j < m1.view_cols(); j++)
            {
                int temp = m1.getElement(i, j) + m2.getElement(i, j);
                a.setElement(i, j, temp);
            }
        }
    }
    return a;
}

Bao Ca Ca

HCM 17.04.22 11:40 p.m.

matrix.h

matrix_class.cpp

matrix_main.cpp

1 bình luận cho “Matrix class implementation”

Bình luận về bài viết này

Tạo trang giống vầy với WordPress.com
Tham gia