Skip to content

Instantly share code, notes, and snippets.

@macroxela
Last active April 15, 2020 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save macroxela/301ae7d9b3dad68095e7 to your computer and use it in GitHub Desktop.
Save macroxela/301ae7d9b3dad68095e7 to your computer and use it in GitHub Desktop.
A matrix header file that allows for matrix addition, subtraction, and multiplication with method calls & operator overloading
#ifndef _MATRIX
#define _MATRIX
/*
Matrix Header File
Matrix library that allows creation, addition,
subtraction, and multiplication of matrices.
2 Dimensional matrices only
Alex Marroquin
DATE: 7/14/2015
*/
#include<vector>
#include<iostream>
using namespace std;
class Matrix {
public:
int rows, col;
vector< vector<double> > mat;
Matrix();
Matrix(int, int);
void print();
Matrix add(Matrix, Matrix);
Matrix sub(Matrix, Matrix);
Matrix multiply(Matrix, Matrix);
Matrix& operator+(const Matrix&);
Matrix& operator=(const Matrix&);
Matrix& operator-(const Matrix&);
Matrix& operator*(const Matrix&);
};
// Initialize with a default 2 X 2 matrix
Matrix::Matrix()
{
rows = 2;
col = 2;
for (int i = 0; i < rows; i++)
{
vector<double> rw;
for (int j = 0; j < col; j++)
rw.push_back(0);
mat.push_back(rw);
}
}
// Create an r X c matrix
Matrix::Matrix(int r, int c)
{
rows = r;
col = c;
for (int i = 0; i < rows; i++)
{
vector<double> rw;
for (int j = 0; j < col; j++)
rw.push_back(0);
mat.push_back(rw);
}
}
// Display the matrix on screen
void Matrix::print()
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < col; j++)
cout << mat[i][j] << " ";
cout << endl;
}
}
// Add 2 matrices w/o operator overloading
Matrix Matrix::add(Matrix A, Matrix B)
{
if (A.rows != B.rows || A.col != B.col) // sizes do not match
{
Matrix Z(0, 0);
return Z;
}
Matrix C(A.rows, A.col); // Creates new matrix to store values
for (int i = 0; i < C.rows; i++)
{
for (int j = 0; j < C.col; j++)
C.mat[i][j] = A.mat[i][j] + B.mat[i][j];
}
return C;
}
// Subtract 2 matrices
Matrix Matrix::sub(Matrix A, Matrix B)
{
if (A.rows != B.rows || A.col != B.col)
{
Matrix Z(0, 0);
return Z;
}
Matrix C(A.rows, A.col);
for (int i = 0; i < C.rows; i++)
{
for (int j = 0; j < C.col; j++)
C.mat[i][j] = A.mat[i][j] - B.mat[i][j];
}
return C;
}
Matrix Matrix::multiply(Matrix A, Matrix B)
{
Matrix C(A.rows, B.col);
if (A.col != B.rows)
return C;
for (int i = 0; i < C.rows; i++)
{
for (int j = 0; j < C.col; j++)
{
for (int k = 0; k < A.col; k++)
C.mat[i][j] += A.mat[i][k] * B.mat[k][j];
}
}
return C;
}
//Overloaded operators based one the ones above
Matrix& Matrix::operator=(const Matrix&M)
{
mat.clear();
rows = M.rows;
col = M.col;
for (int i = 0; i < rows; i++)
{
vector<double> rw;
for (int j = 0; j < col; j++)
rw.push_back(M.mat[i][j]);
mat.push_back(rw);
}
return *this;
}
Matrix& Matrix::operator+(const Matrix &M)
{
if (M.rows != rows || M.col != col)
return *this;
Matrix *C = new Matrix(M.rows, M.col);
for (int i = 0; i < M.rows; i++)
{
for (int j = 0; j < M.col; j++)
C->mat[i][j] = this->mat[i][j] + M.mat[i][j];
}
return *C;
}
Matrix& Matrix::operator-(const Matrix &M)
{
// *this is the 1st operand while M is the 2nd operand
if (M.rows != rows || M.col != col)
return *this;
Matrix *C = new Matrix(M.rows, M.col);
for (int i = 0; i < M.rows; i++)
{
for (int j = 0; j < M.col; j++)
C->mat[i][j] = this->mat[i][j] - M.mat[i][j];
}
return *C;
}
Matrix& Matrix::operator*(const Matrix &M)
{
// *this is the 1st operand while M is the 2nd operand
Matrix *C = new Matrix(rows, M.col);
if (col != M.rows) //Check that the columns and rows match
return *C;
for (int i = 0; i < C->rows; i++)
{
for (int j = 0; j < C->col; j++)
{
for (int k = 0; k < M.col; k++)
C->mat[i][j] += mat[i][k] * M.mat[k][j];
}
}
return *C;
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment