Friday, April 2, 2010

Matrix Class Library

/******** Class Matrix :: Declaration Part ********/
// Matrix.h

template
class matrix
{
public:
//Data
T **mat;
short row,col;

//Constructors
matrix ();
matrix (short,short);
matrix (matrix &);

//Input and Output Stream
friend istream & operator >> (istream &,matrix &);
friend ostream & operator << (ostream &,matrix &);

//Arithmatics Operators
matrix operator + (matrix);
matrix operator - (matrix);
matrix operator - ();
friend matrix operator * (matrix,matrix);
friend matrix operator * (T,matrix &);
friend matrix operator * (matrix &,T);
double operator / (matrix);
friend matrix operator / (matrix &,T);

//Increment and Decrement Operators
matrix operator ++ ();
matrix operator ++ (int);
matrix operator -- ();
matrix operator -- (int);

//Shorthand Operators
void operator += (matrix &);
void operator -= (matrix &);
void operator *= (T);
void operator /= (T);

//Equality Operators
int operator == (matrix);
int operator != (matrix);

//Other Calculations

friend matrix transpose(matrix);
friend double determinant(matrix);
friend float tr(matrix); //Trace of a Matrix
friend int issymmetric(matrix);
friend int isskewsymmetric(matrix);
void putvalue(short,short,T);
T getvalue(short,short);

//Destructor
virtual ~matrix();
};

//Function Definitions
//Matrix.cpp

#include "iostream.h"
#include "conio.h"
#include "stdlib.h"
#include "matrix.h"

/********** Class Matrix :: Implementation Part ***********/

enum _bool {FALSE,TRUE};

template
matrix :: matrix () {} // Default Constructor

template
matrix :: matrix (short m,short n) // Parameterized Constructor
{
row = m;
col = n;

if(row < 0 || row > 10)
{
cout << "\nError: Bad Row";
exit(0);
}

if(col < 0 || col > 10)
{
cout << "\nError: Bad Column";
exit(0);
}

mat = new T *[row];
for(int i = 0;i < row;i++)
mat[i] = new T[col];

}

template
matrix :: matrix (matrix &m) // Copy Constructor
{
row = m.row;
col = m.col;

mat = new T *[row];
for(int i = 0;i < row;i++)
mat[i] = new T[col];

for(i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] = m.mat[i][j];

}

template
void matrix :: putvalue (short i,short j,T f)
{
mat[i][j] = f;
}

template
T matrix :: getvalue (short i,short j)
{
return mat[i][j];
}

template
istream & operator >> (istream & ain,matrix & m)
{
for(int i = 0;i < m.row;i++)
for(int j = 0;j < m.col;j++)
ain >> m.mat[i][j];
return ain;
}

template
ostream & operator << (ostream & aout ,matrix & m)
{
for(int i = 0;i < m.row;i++)
{
for(int j = 0;j < m.col;j++)
aout << m.mat[i][j] <<'\t';
aout << endl;
}

return aout;
}

template
matrix matrix :: operator + (matrix m)
{
if((row != m.row) || (col != m.col))
{
cout << "Error: Order Mismatch.";
exit(0);
}

matrix temp(row,col);
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
temp.mat[i][j] = mat[i][j] + m.mat[i][j];

return temp;
}

template
matrix matrix :: operator - (matrix m)
{
if((row != m.row) || (col != m.col))
{
cout << "Error: Order Mismatch.";
exit(0);
}

matrix temp(row,col);
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
temp.mat[i][j] = mat[i][j] - m.mat[i][j];

return temp;
}

template
matrix matrix :: operator - ()
{
matrix temp(row,col);
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
temp.mat[i][j] = -mat[i][j];

return temp;
}

template
matrix operator * (matrix n,matrix m)
{
if(n.col != m.row)
{
cout << "Error : Column of First Matrix Must be Equal to the Row of Second Matrix\n";
exit (0);
}

matrix temp(n.row,m.col);
matrix x = n;
matrix y = m;

for(int i = 0;i < n.row;i++)
for(int j = 0;j < m.col;j++)
{
temp.mat[i][j] = 0.0;

for(int k = 0;k < m.col;k++)
temp.mat[i][j] += x.mat[i][k] * y.mat[k][j];
}

return temp;
}

template
matrix operator * (T n,matrix &m)
{
matrix temp(m.row,m.col);
for(int i = 0;i < m.row;i++)
for(int j = 0;j < m.col;j++)
temp.mat[i][j] = n * m.mat[i][j];

return temp;
}

template
matrix operator * (matrix &m,T n)
{
matrix temp(m.row,m.col);
for(int i = 0;i < m.row;i++)
for(int j = 0;j < m.col;j++)
temp.mat[i][j] = m.mat[i][j] * n;

return temp;
}

template
matrix operator / (matrix &m,T n)
{
matrix temp(m.row,m.col);
for(int i = 0;i < m.row;i++)
for(int j = 0;j < m.col;j++)
temp.mat[i][j] = m.mat[i][j] / n;

return temp;
}

template
matrix matrix :: operator ++ ()
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
++mat[i][j];

return *this;
}

template
matrix matrix :: operator ++ (int)
{
matrix temp(row,col);
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
temp.mat[i][j] = mat[i][j];

for(i = 0;i < row;i++)
for( j = 0;j < col;j++)
mat[i][j]++;

return temp;
}

template
matrix matrix :: operator -- ()
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
--mat[i][j];

return *this;
}

template
matrix matrix :: operator -- (int)
{
matrix temp(row,col);
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
temp.mat[i][j] = mat[i][j];

for(i = 0;i < row;i++)
for(j = 0;j < col;j++)
mat[i][j]--;

return temp;
}

template
void matrix :: operator += (matrix &m)
{
if((row != m.row) || (col != m.col))
{
cout << "Error: Order Mismatch.";
exit(0);
}

for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] += m.mat[i][j];
}

template
void matrix :: operator -= (matrix &m)
{
if((row != m.row) || (col != m.col))
{
cout << "Error: Order Mismatch.";
exit(0);
}

for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] -= m.mat[i][j];

}

template
void matrix :: operator *= (T n)
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] *= n;
}

template
void matrix :: operator /= (T n)
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] /= n;
}

template
int matrix :: operator == (matrix m)
{
int isequal;
if((row != m.row) || (col != m.col))
isequal = FALSE;

else
{
isequal = TRUE;
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
if(mat[i][j] != m.mat[i][j])
{
isequal = FALSE;
break;
}
}

if(isequal)
return 1;
else
return 0;
}

template
int matrix :: operator != (matrix m)
{
int isequal;
if((row != m.row) || (col != m.col))
isequal = FALSE;

else
{
isequal = TRUE;
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
if(mat[i][j] != m.mat[i][j])
{
isequal = FALSE;
break;
}
}

if(!isequal)
return 1;
else
return 0;
}

template
matrix transpose(matrix m)
{
matrix temp(m.col,m.row);
for(int j = 0;j < m.col;j++)
for(int i = 0;i < m.row;i++)
temp.mat[j][i] = m.mat[i][j];

return temp;

}

template
float tr(matrix m) //Trace of a Matrix
{
float f = 0.0;
if(m.row != m.col)
{
cout << "\nError: Must be Square Matrix to find a Trace";
exit(0);
}

for(int i = 0;i < m.row;i++)
f += m.mat[i][i];

return f;
}

template
int issymmetric(matrix m)
{
int isequal = FALSE;
matrix temp(m.col,m.row);
temp = transpose(m);
if(m == temp)
isequal = TRUE;

if(isequal)
return 1;
else
return 0;
}

template
int isskewsymmetric(matrix m)
{
int isequal = FALSE;
matrix temp(m.col,m.row);
temp = transpose(m);
temp = -temp;
if(m == temp)
isequal = TRUE;

if(isequal)
return 1;
else
return 0;
}

template
double determinant(matrix m)
{
matrix temp;
temp = m;

int i, j, k;
double factor,result = 1.0;

for(k=0; k < temp.row-1; k++)
{
for(i=k+1; i < temp.row; i++)
{
factor = temp.mat[i][k]/temp.mat[k][k];
for(j=0; j < temp.col; j++)
temp.mat[i][j] -= factor*temp.mat[k][j];

}
}

for(i = 0;i < temp.row;i++)
result *= temp.mat[i][i];

return result;
}

template
double matrix :: operator / (matrix m)
{
double num,den,value;
num = determinant(*this);
den = determinant(m);
if(den == 0)
{
cout << "Error : Cannot Divide by Zero";
exit(0);
}

value = double(num)/den;

return value;
}


/*template
matrix :: ~matrix()
{
for(int i = 0;i < row;i++)
delete mat[i];

delete mat;
}*/

template
matrix :: ~matrix() {}

No comments:

Post a Comment