Sure. Here is the whole header file:
Code:
//#include "Complex.h"
class Matrix
{
int row,col;
Complex **A;
bool empty, freq;
public:
Matrix();
Matrix(const Matrix&);
Matrix(const int&, const int&);
~Matrix();
Matrix operator*(const Matrix&) const;
Complex& operator()(const int&, const int&) const; //(1)
Matrix operator=(const Matrix&);
operator bool() const;
void LU(const Matrix&, Matrix&);
void SetFreqMode(const bool&);
friend ostream& operator <<(ostream&, Matrix const&);
};
Matrix::Matrix()
{
row = col = 0;
empty = true;
freq = true;
}
Matrix::Matrix(const Matrix &another)
{
A = new Complex* [another.row];
for (int i=0; i<row; i++)
A[i] = new Complex[another.col];
for (i=0; i<row; i++)
{
for (int j=0; j<col; j++)
{
A[i][j] = another(i,j);
}
}
}
Matrix::Matrix(const int &a, const int &b)
{
row = a;
col = b;
empty = false;
A = new Complex* [row];
for (int i=0; i<row; i++)
A[i] = new Complex[col];
for (i=0; i<row; i++)
{
for(int j=0; j<col; j++)
{
A[i][j] = 0; //zero-out the entire matrix;
}
}
}
Matrix::~Matrix()
{
cout<<"Destroying matrix."<<endl;
if(!empty)
{
for (int i=0; i<row; i++)
delete[] A[i];
delete[] A;
}
}
Complex& Matrix::operator() (const int &a, const int &b) const // (2)
{
if(a<=row && b<=col)
return A[a][b];
else
{
cout<<"Error in Indexing. Aborting."<<endl;
return A[0][0]; //just for the sake of returning
}
}
Matrix Matrix::operator=(const Matrix &another)
{
//clearing heap
if(!empty)
{
for (int i=0; i<row; i++)
delete[] A[i];
delete[] A;
}
row = another.row;
col = another.col;
for (int i=0; i<row; i++)
{
for (int j=0; j<col; j++)
{
A[i][j] = another(i,j);
}
}
return *this;
}
Matrix Matrix::operator*(const Matrix &another) const //'const' keyword after => doesn't
{ //modify data members
if (col != another.row)
{
cout<<"Error in matrix multiplication. Matrix dimensions do not agree."<<endl;
return *this;
}
Matrix prod(row,another.col);
Complex sum(0,0);
for(int i=0; i<row; i++) //i: 0 to 3
{
sum = 0;
for(int j=0; j<another.col; j++) //j: 0 to 2
{
for(int k=0; k<row; k++)
{
sum = sum + A[i][k] * another(k,j);
}
//cout<<"prod("<<i<<","<<j<<") = "<<sum<<endl;
prod(i,j) = sum;
}
}
return prod;
}
void Matrix::SetFreqMode(const bool &flag)
{
freq = flag;
}
Matrix::operator bool() const
{
return freq;
}
void Matrix::LU(const Matrix &B, Matrix &X)
{
// forward substitution
Complex sum = 0;
int size = row;
for(int i=2; i<size; i++)
{
sum = B(i,0);
for(int j=1; j<=i-1; j++)
sum = sum - A[i][j]*B(j,0);
B(i,0) = sum;
}
//backward substitution
X(size-1,0) = B(size-1,0)/A[size-1][size-1];
for(int m=size-2; m>=0; m--)
{
sum=0;
for(int n=m+1;n<size;n++)
sum = sum + A[m][n]*X(n,0);
X(m,0) = (B(m,0) - sum)/A[m][m];
}
return;
}
/*
Notes:
(1)'const' after the function header means that the function does not modify data
contents; equivalently, it means that you can call the function on const objects
(2)'const' since it does not modify data members; although you can SET, which
changes data members, this change occurs in the actual line of code where it is
used, NOT in this funcion. This function only returns A[a][b].
*/