// 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
template
matrix
{
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
{
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
{
mat[i][j] = f;
}
template
T matrix
{
return mat[i][j];
}
template
istream & operator >> (istream & ain,matrix
{
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
{
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
{
if((row != m.row) || (col != m.col))
{
cout << "Error: Order Mismatch.";
exit(0);
}
matrix
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
{
if((row != m.row) || (col != m.col))
{
cout << "Error: Order Mismatch.";
exit(0);
}
matrix
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
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
{
if(n.col != m.row)
{
cout << "Error : Column of First Matrix Must be Equal to the Row of Second Matrix\n";
exit (0);
}
matrix
matrix
matrix
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
{
matrix
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
{
matrix
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
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
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
++mat[i][j];
return *this;
}
template
matrix
{
matrix
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
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
--mat[i][j];
return *this;
}
template
matrix
{
matrix
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
{
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
{
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
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] *= n;
}
template
void matrix
{
for(int i = 0;i < row;i++)
for(int j = 0;j < col;j++)
mat[i][j] /= n;
}
template
int matrix
{
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
{
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
{
matrix
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
{
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
{
int isequal = FALSE;
matrix
temp = transpose(m);
if(m == temp)
isequal = TRUE;
if(isequal)
return 1;
else
return 0;
}
template
int isskewsymmetric(matrix
{
int isequal = FALSE;
matrix
temp = transpose(m);
temp = -temp;
if(m == temp)
isequal = TRUE;
if(isequal)
return 1;
else
return 0;
}
template
double determinant(matrix
{
matrix
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
{
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
{
for(int i = 0;i < row;i++)
delete mat[i];
delete mat;
}*/
template
matrix
No comments:
Post a Comment