Code for The Matrix class (C + +)

Source: Internet
Author: User

The Codes of Matrix Class

Matrix.h:
#ifndef matrix_h
#define MATRIX_H
# Include<iostream>
#include <iomanip>
#include <cassert>
#include <cmath>
# Include "MatrixTypedef.h" //declare typedef ' s header file  
Class Matrix {
public:
Matrix (const un_int &R = 0, const un_int &c = 0, const double &number = 0); //the Constructor with the parameters
Matrix (const matrix &mat); //the copy constructor  
~matrix (); The destructor  
Un_int getRows (void) const {return m_rows;} //accessor, get the value of rows  
Un_int getcols (void) const { return m_cols; }//accessor,get the value of cols

void Resize (const un_int &r, const un_int &c, const double &number); //Reseting The values of rows, cols and matrix members

BOOL IsEmpty (void) const; // The judgment if matrix is empty
vec& operator [] (const un_int &i); //Overloading T He subscript operator[]

Const vec& operator [] (const un_int &i) const;//overloading the subscript operator[]
Friend istream& operator >> (IStream &sin, Matrix &mat);//Overloading the operator >>
Friend ostream& operator << (ostream &sout, const Matrix &mat);//Overloading the operator <<
Friend Matrix operator + (const matrix &l, const matrix &AMP;R);//overloading matrix addtion
Friend Matrix operator-(const matrix &AMP;L, const matrix &AMP;R);//overloading matrix subtraction
Friend Matrix operator * (const double &x, const Matrix &mat);//overloading matrix multiplication
Friend Matrix operator * (const matrix &mat, const double &x);// overloading matrix multiplication
Friend Matrix operator * (const matrix &AMP;L, const matrix &AMP;R);// overloading matrix multiplication
Matrix operator! (void);//Solving the inverse of a matrix
Matrix operator ~ (void);//Solving the transpose of the Matrix
Double Det (void);//Solving the determinant

Private:
Un_int M_rows, M_cols; //The values of rows and cols
Mat data;//The values of the Matrix
void Resize (Mat &mat, const un_int &r, const un_int &c); //resetting vector<vector<double>> ' s size

Double Getdet (const Mat &mat, const un_int &n); // solving the Determinan
Double getcofactor (const un_int m, const un_int N); //Solving algebraic cofactor
};
#endif

Matrix.cpp:
#include "Matrix.h"

The constructor with the parameters

Matrix::matrix (const un_int &r, const un_int &c, const double &number) {
IF cols and rows is greater than or equal to 0
ASSERT (r >= 0);
ASSERT (c >= 0);

M_rows = R;m_cols = C;

//resetting and initialising The matrix

Resize (data, m_rows, m_cols);
for (Un_int i = 0;i < M_rows;++i) {
for (Un_int j = 0;j < M_cols;++j) {
DATA[I][J] = number;

}

}

}

//The destructor
Matrix::~matrix (void) {
Using swap function Forcing to release the memory space
for (Un_int i = 0;i < M_rows;++i) {
VEC (). Swap (data[i]);

}
Mat (). swap (data);

}

//The copy constructor
Matrix::matrix (const Matrix &mat) {
if (!mat.isempty ()) {
M_rows = Mat.getrows ();
M_cols = Mat.getcols ();

//resetting matrix ' s size and values
Resize (data, m_rows, m_cols);
for (Un_int i = 0;i < M_rows;++i) {
for (Un_int j = 0;j < M_cols;++j) {
DATA[I][J] = Mat[i][j];

}

}

}

}

The definition of resize function
void Matrix::resize (const un_int &r, const un_int &c, const double &number) {
//IF cols and rows is greater than or equal to 0
ASSERT (r >= 0);
ASSERT (c >= 0);

M_rows = R;
M_cols = C;
//resetting matrix ' s size and values
Resize (data, m_rows, m_cols);
for (Un_int i = 0;i < M_rows;++i) {
for (Un_int j = 0;j < M_cols;++j) {
DATA[I][J] = number;

}

}

}
If Matrix is empty

BOOL Matrix::isempty (void) const{
BOOL flag = TRUE; //If All of the member's matrix is empty
for (Un_int i = 0;i < M_rows;++i) {
if (!data[i].empty ()) {

Flag = false; Break

}

}
return flag;

}

overloading the subscript operator[]
vec& Matrix::operator [] (const un_int &i) {
If mEmory accessing is out of bounds

ASSERT (i >= 0);
ASSERT (I < m_rows);
return data[i];

}

overloading the subscript operator[]
Const vec& Matrix::operator [] (const un_int &i) Const {
If mEmory accessing is out of bounds
ASSERT (i >= 0);
ASSERT (I < m_rows);
return data[i];

}

//Overloading the operator >>
istream& operator >> (IStream &sin, Matrix &mat) {
Sin >> mat.m_rows >> mat.m_cols;
IF cols and rows are greater than 0
ASSERT (Mat.m_rows > 0);
ASSERT (Mat.m_cols > 0);
Resetting matrix ' s size and values
Mat.resize (mat.m_rows, mat.m_cols, 0);
for (Un_int i = 0;i < Mat.m_rows;++i) {
for (Un_int j = 0;j < Mat.m_cols;++j) {
Sin >> mat[i][j];

}

}
return sin;

}

//Overloading the operator <<
ostream& operator << (ostream &sout, const Matrix &mat) {
IF cols and rows are greater than 0
ASSERT (Mat.m_rows > 0);
ASSERT (Mat.m_cols > 0);
Outputting matrix
for (Un_int i = 0;i < Mat.m_rows;++i) {
for (Un_int j = 0;j < Mat.m_cols;++j) {
SOUT.SETF (ios::fixed);
Sout.precision (3);
Sout << Right << setw (6) << Mat[i][j] << (j = = (mat.m_cols-1)? ' \ n ': ');

}

}
cout << Endl;
return sout;

}

//overloading matrix multiplication
Matrix operator * (const double &x, const Matrix &mat) {
IF cols and rows are greater than 0
ASSERT (Mat.m_rows > 0);
ASSERT (Mat.m_cols > 0);
Matrix temp (mat.m_rows, mat.m_cols, 0);
for (Un_int i = 0;i < Mat.m_rows;++i) {
for (Un_int j = 0;j < Mat.m_cols;++j) {
TEMP[I][J] = x * Mat[i][j];

}

}
return temp;

}

//overloading matrix multiplication
Matrix operator * (const matrix &mat, const double &x) {
IF cols and rows are greater than 0
ASSERT (Mat.m_rows > 0);
ASSERT (Mat.m_cols > 0);
Matrix temp (mat.m_rows, mat.m_cols, 0);
for (Un_int i = 0;i < Mat.m_rows;++i) {
for (Un_int j = 0;j < Mat.m_cols;++j) {
TEMP[I][J] = x * Mat[i][j];

}

}
return temp;

}

Overloading Matrix Addtion
Matrix operator + (const matrix &l, const matrix &R) {
If the rows and cols between the Matrixs are equal
ASSERT (L.m_rows = = r.m_rows);
ASSERT (L.m_cols = = R.m_cols);
Matrix temp (l.m_rows, r.m_cols, 0);
for (Un_int i = 0;i < L.m_rows;++i) {
for (Un_int j = 0;j < L.m_cols;++j) {
TEMP[I][J] = L[i][j] + r[i][j];

}

}
return temp;

}

//overloading matrix subtraction
Matrix operator-(const matrix &L, const matrix &R) {
//If the rows and cols between the Matrixs are Equal
ASSERT (L.m_rows = = r.m_rows);
ASSERT (L.m_cols = = R.m_cols);
Matrix temp (l.m_rows, r.m_cols, 0);
for (Un_int i = 0;i < L.m_rows;++i) {
for (Un_int j = 0;j < L.m_cols;++j) {
TEMP[I][J] = l[i][j]-r[i][j];

}

}
return temp;

}

//overloading matrix multiplication
Matrix operator * (const matrix &L, const matrix &R) {
If the left operand ' s cols are equal to the right operand ' s rows
ASSERT (L.m_cols = = r.m_rows);
//Using formula solving matrix multiplication
Matrix temp (l.m_rows, r.m_cols, 0);
for (Un_int i = 0;i < L.m_rows;++i) {
for (Un_int j = 0;j < R.m_cols;++j) {
for (un_int k = 0;k < L.m_cols;++k) {
TEMP[I][J] + = (l[i][k] * r[k][j]);

}

}

}
return temp;

}

//Solving the inverse of a matrix
Matrix Matrix::operator! (void) {
Const double det = det (); // solving the Determinan
If Determinan is equal to 0 and matrix ' s rows are equal to COLS
Assert (Fabs (det) > EPS);
ASSERT (M_rows = = M_cols);
// Solving the inverse of a matrix
Mat temp = data;for (Un_int i = 0,k = 0;i < M_rows;++i, ++k) {
for (Un_int j = 0, L = 0;j < M_cols;++j, ++l) {
Double n = getcofactor (k, L)/det;

if (Fabs (n) < EPS) n = 0.0; //If Double value is equal to 0
Data[j][i] = n;

}

}
return *this;

}

// solving the transpose of the Matrix
Matrix matrix::operator ~ (void) {
IF cols and rows is greater than or equal to 0
ASSERT (M_rows > 0);
ASSERT (M_cols > 0);

If rows are equal to COLS

if (m_rows = = M_cols) {
for (Un_int i = 1;i < M_rows;++i) {
for (Un_int j = 0;j <= i;++j) {
Swap (data[i][j],data[j][i]); } }
return *this;

}

If rows are diffierent from cols
else {Matrix temp (m_cols, m_rows, 0);
for (Un_int i = 0;i < M_cols;++i) {
for (Un_int j = 0;j < M_rows;++j) {
TEMP[I][J] = Data[j][i];

}

}
return temp;

}

}

//Solving the determinant
Double Matrix::D et (void) {
If rows are equal to COLS
ASSERT (M_rows = = M_cols);
return Getdet (data, m_rows);

}
The definition of Resize function
void Matrix::resize (Mat &mat, const un_int &r, const un_int &c) {

resetting cols
Mat.resize (R);
Resetting rows
for (Un_int i = 0;i < R;++i) {mat[i].resize (c);}}
The definition of Getdet function

Getdet double Matrix::getdet (const Mat &mat,const un_int &n) {double ans = 0;
Solving the determinant
if (n = = 1) {return mat[0][0];}
else {Mat temp; temp.resize (n);
for (Un_int i = 0;i < N;++i) {temp[i].resize (n);}
for (Un_int i = 0;i < N;++i) { //Getting algebraic cofactor of first row,j+1st col
Un_int p = 0; for (Un_int j = 0;j < N;++j) {
if (J! = 0) {Un_int q = 0;
for (un_int k = 0;k < N;++k) {
if (k! = i) {Temp[p][q] = mat[j][k]; ++q;

}

}
++p;

}

}
Ans + = POW ( -1, i) *mat[0][i]*getdet (temp, n-1); }
return ans;

}

}

The definition of getcofactor function
Double matrix::getcofactor (const un_int m, const un_int N) {
Matrix temp (m_rows-1, m_cols-1, 0);
Getting algebraic cofactor of M th row,n th col
for (Un_int i = 0, k = 0, L = 0;i < M_rows;++i) {
for (Un_int j = 0;j < M_cols;++j) {

if (i! = m && J! = N) {
TEMP[K][L] = Data[i][j]; ++l;
if (L = = m_cols-1) {L = 0; ++k;

}

}

}

}
const INT sign = ((((m + n + 2) & 1) = = 0?1:-1);
Return sign * temp. Det (); //Getting cofactor ' s determinant

}

MatrixTypedef.h:
#ifndef Matrixtypedef_h
#define Matrixtypedef_h
#include <vector>
using namespace Std;
typedef vector<double> VEC;
typedef vector<vec> MAT;
typedef unsigned int un_int;
Const double EPS = 1e-6;
#endif

Original link: Http://user.qzone.qq.com/1043480007/main

Code for The Matrix class (C + +)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.