Matrix Class
From WikiKantila
//Filename : matrix.hpp
//Written by : Sudesh Kantila
//Created : 11-Mar-2008
//Last Modified : 12-Mar-2008
#include <iostream>
#include <iomanip>
#include <cstdio>
using namespace std;
namespace secs {
class Matrix {
protected:
int row;
int col;
int **m;
public:
//default constructors
Matrix();
//parameterized dynamic constructors
Matrix(const int,const int);
Matrix(const int,const int,const int *);
Matrix(const int *,const int,const int);
//copy constructor
Matrix(const Matrix &);
//destructor
~Matrix();
//inline member functions
//function to get the value of a matrix element
int element(const unsigned int x,const unsigned int y) {
if(x<=row && y<=col && x>0 && y>0)
return m[x-1][y-1];
else
{
cerr<<"Subscript ["<<x<<"]["<<y<<"] is out of range!"<<endl;
return 0;
}
}
//overloaded function to set the value of a matrix element
void element(const unsigned int x,const unsigned int y,const int v) {
if(x<=row && y<=col && x>0 && y>0)
m[x-1][y-1]=v;
else
cerr<<"Subscript ["<<x<<"]["<<y<<"] is out of range!"<<endl;
}
//function to check whether the matrix is horizontal or not
bool isHorizontal() {
return (row<col)?true:false;
}
//function to check whether the matrix is vertical or not
bool isVertical() {
return (col<row)?true:false;
}
//function to check whether the matrix is square or not
bool isSquare() {
return (row==col)?true:false;
}
//function to check whether the matrix is vector or not
bool isVector() {
return ((row==1 && col>1)|| (col==1 && row>1))?true:false;
}
//function to check whether the matrix is row vector or not
bool isRowVector() {
return (row==1 && col>1)?true:false;
}
//function to check whether the matrix is column vector or not
bool isColumnVector() {
return (row>1 && col==1)?true:false;
}
//non-inline member functions - defined outside the class
void print();
void print(const unsigned int, const char *);
void print(const unsigned int, const unsigned int);
//functions to get various derivatives of matrices
Matrix transpose(); //not implemented yet
Matrix adjoint(); //not implemented yet
Matrix inverse(); //not implemented yet
int determinant(); //not implemented yet
//functions to check special type of matrices
bool isNull();
bool isIdentity();
bool isDiagonal();
bool isScalar();
bool isTriangular();
bool isUpperTriangular();
bool isLowerTriangular();
bool isSymatric();
bool isSkewSymatric();
bool isOrthogonal(); //not implemented yet
bool isSingular(); //not implemented yet
bool isNonSingular(); //not implemented yet
//overloaded member operators to perform arithmatics
Matrix operator=(Matrix); //not implemented yet
Matrix operator+(Matrix &); //not implemented yet
Matrix operator-(Matrix &); //not implemented yet
Matrix operator*(Matrix &); //not implemented yet
Matrix operator*(int); //not implemented yet
Matrix operator/(Matrix &); //not implemented yet
Matrix operator/(int); //not implemented yet
//overloaded operators to perform comparison
bool operator==(Matrix); //not implemented yet
bool operator!=(Matrix); //not implemented yet
}; //body of the class ends here
//Implimentation of member functions
//All constructors in this class are dynamic constructors
//As here are several constructors, these are overloaded
//default constructor defined
Matrix::Matrix() {
row=1;
col=1;
//dynamic memory allocation for matrix
m=new int *[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
//initialization of matrix elements
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]=0;
}
//parameterized constructor (2 arguments) defined
Matrix::Matrix(const int x,const int y) {
row=x;
col=y;
//dynamic memory allocation for matrix
m=new int *[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
//initialization of matrix elements
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]=0;
}
//parameterized constructor (3 arguments) defined
Matrix::Matrix(const int x,const int y,const int *v) {
row=x;
col=y;
//dynamic memory allocation for matrix
m=new int *[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
//initialization of matrix elements
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]=*(v+i*col+j);
}
//parameterized constructor (3 arguments) defined
Matrix::Matrix(const int *v,const int x,const int y) {
row=x;
col=y;
//dynamic memory allocation for matrix
m=new int *[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
//initialization of matrix elements
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]=*(v+i*col+j);
}
//copy constructor defined
Matrix::Matrix(const Matrix &ob) {
row=ob.row;
col=ob.col;
//dynamic memory allocation for matrix
m=new int *[row];
for(int i=0;i<row;i++)
m[i]=new int[col];
//initialization of matrix elements
for(int i=0;i<row;i++)
for(int j=0;j<col;j++)
m[i][j]=ob.m[i][j];
}
//destructor defined
//neccessary to deallocate dynamically allocated memory
Matrix::~Matrix() {
for(int i=0;i<row;i++)
delete m[i];
delete m;
}
void Matrix::print() {
printf("%c",218);
for(int i=0;i<col;i++) cout<<" ";
printf(" %c\n",191);
for(int i=0;i<row;i++)
{
printf("%c",179);
for (int j=0;j<col;j++)
cout<<setw(5)<<m[i][j];
printf(" %c",179);
cout<<endl;
}
printf("%c",192);
for(int i=0;i<col;i++) cout<<" ";
printf(" %c\n",217);
}
//function to print a row or a column of the matrix defined
void Matrix::print(const unsigned int r, const char *c="r") {
if(c[0]=='r' || c[0]=='R')
{
if(r<=row && r>0)
{
cout<<"[";
for(int t=0;t<col;t++)
cout<<setw(5)<<m[r-1][t];
cout<<" ]"<<endl;
}
else
cerr<<"Row "<<r<<" does not exist in matrix!"<<endl;
}
else if(c[0]=='c' || c[0]=='C')
{
if(r<=col && r>0)
{
printf("%c%c%c%c%c%c%c\n",218,196,196,196,196,196,191);
for(int t=0;t<row;t++)
cout<<setw(6)<<m[t][r-1]<<endl;;
printf("%c%c%c%c%c%c%c\n",192,196,196,196,196,196,217);
}
else
cerr<<"Column "<<r<<" does not exist in matrix!"<<endl;
}
}
//function to print a perticular element from the matrix
void Matrix::print(const unsigned int r,const unsigned int c) {
if(r<=row && r>0 && c<=col && c>0)
cout<<setw(5)<<m[r-1][c-1]<<endl;
else
cerr<<"Subscript ["<<r<<"]["<<c<<"] is out of range!"<<endl;
}
//function to check whether the matrix is null matrix
bool Matrix::isNull() {
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if(m[s][t]!=0)
return false;
return true;
}
//function to check whether the matrix is identity matrix
bool Matrix::isIdentity() {
if (!isSquare())
return false;
bool result=true;
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if((s==t && m[s][t]!=1) || (s!=t && m[s][t]!=0))
result=false;
return result;
}
//function to check whether the matrix is diagonal matrix
bool Matrix::isDiagonal() {
if (!isSquare())
return false;
bool result=true;
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if((s==t && m[s][t]==0) || (s!=t && m[s][t]!=0))
result=false;
return result;
}
//function to check whether the matrix is scalar matrix
bool Matrix::isScalar() {
if (!isSquare())
return false;
bool result=true;
int test=m[0][0];
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if((s==t && m[s][t]!=test) || (s!=t && m[s][t]!=0))
result=false;
return result;
}
//function to check whether the matrix is upper triangular matrix
bool Matrix::isUpperTriangular() {
if (!isSquare())
return false;
bool result=true;
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if(s>t && m[s][t]!=0)
result=false;
return result;
}
//function to check whether the matrix is lower triangular matrix
bool Matrix::isLowerTriangular() {
if (!isSquare())
return false;
bool result=true;
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if(s<t && m[s][t]!=0)
result=false;
return result;
}
//function to check whether the matrix is triangular matrix
bool Matrix::isTriangular() {
return isUpperTriangular() || isLowerTriangular();
}
//function to check whether the matrix is symatric matrix
bool Matrix::isSymatric() {
if(!isSquare())
return false;
bool result=true;
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if(m[s][t]!=m[t][s])
result=false;
return result;
}
//function to check whether the matrix is skew-symatric matrix
bool Matrix::isSkewSymatric() {
if(!isSquare())
return false;
bool result=true;
for(int s=0;s<row;s++)
for(int t=0;t<col;t++)
if(m[s][t]!=-m[t][s])
result=false;
return result;
}
}; //namespace closed
Go back to C++ Programs

