GeneralMatrix GeneralMatrix::operator * (const GeneralMatrix& other) const
{
	int i,j,k;
	
	GeneralMatrix result(this->nRow(),other.nCol());
	
	if(other.nRow()!=this->nCol())
	{
		for (i=0;i<result.nRow();i++)
			for (j=0;j<result.nCol();j++)
				result.GetElemP(i,j)->val = ERRORVAL;
			return result;
	}
	
    for (i=0;i<this->nRow();i++)       //矩阵相乘的计算
	{
		for(j=0;j<other.nCol();j++)
		{
			for(k = 0; k < other.nRow(); k++)
			{
				if ((this->GetElem(i,k)!=0)&&(other.GetElem(k,j)!=0))
				{
					result[i][j] += this->GetElem(i,k) * other.GetElem(k,j); 
				}
			}
		}
	}
	
return result;
}
bool GeneralMatrix::Jointer_bottom(GeneralMatrix& other)
{
	if(other.nCol()!=nCol()) return FALSE;
	
	if(!AddRows(other.nRow(),ERRORVAL))
		return FALSE;	
	
return Paste(other,nRow()-other.nRow(),0);
}
bool GeneralMatrix::Paste(GeneralMatrix& other,int top,int left) const
{
 int i,j,iRow,iCol;

 iRow = other.nRow()>(nRow()-top)  ? (nRow()-top) : other.nRow();
 iCol = other.nCol()>(nCol()-left) ? (nCol()-left): other.nCol();
 
 for(i=0;i<iRow;i++)
	 for(j=0;j<iCol;j++)
		 GetElem(i+top,j+left) = other[i][j];

return TRUE;
}
bool GeneralMatrix::Jointer_Diagonal(GeneralMatrix& other,ELEMTYPE Val)
{
	int iCol;
	
	iCol = nRow()==0 ? other.nCol()-1 : other.nCol();

	if(!AddRows(other.nRow(),Val))
		return FALSE;
	if(!AddCols(iCol,Val))
	    return FALSE;

return Paste(other,nRow()-other.nRow(),nCol()-other.nCol()); 
}
bool GeneralMatrix::CutIn2_Across(GeneralMatrix& Upper,GeneralMatrix& Lower,int iRow)
{
	int i,j;
	
	if (nCol()==0)
	{
		Upper.ReSize(0,0);
		Lower.ReSize(0,0);
		return TRUE;
	}
	
	if(iRow>nRow()||iRow<0) return FALSE;
	
    if(!Upper.ReSize(iRow,nCol(),ERRORVAL))
		return FALSE;
	
	if(!Lower.ReSize(nRow()-iRow,nCol(),ERRORVAL))
		return FALSE;
	
	   for(i=0;i<iRow;i++)
		   for(j=0;j<nCol();j++)
               Upper[i][j] = GetElem(i,j);
		   
		   for(i=0;i<Lower.nRow();i++)
			   for(j=0;j<nCol();j++)
				   Lower[i][j] = GetElem(i+iRow,j);
			   
   return TRUE;
}
bool GeneralMatrix::Jointer_Right(GeneralMatrix& other)
{
    if(other.nRow()!=nRow()) return FALSE;
	
	if(!AddCols(other.nCol(),ERRORVAL))
		return FALSE;

	Paste(other,0,nCol()-other.nCol());
   
return TRUE;
}
GeneralMatrix operator * (double value ,const GeneralMatrix& other)
{
	int i,j;
	
	GeneralMatrix result(other);
	
    for (i=0;i<other.nRow();i++)
		for(j=0;j<other.nCol();j++)
				result[i][j] *= value; 

return result;
}
ELEMTYPE GeneralMatrix::Distance_E(GeneralMatrix& other) const
{
	     int i,j; 
	ELEMTYPE result = ERRORVAL;

	if(nRow()!=other.nRow()||nCol()!=other.nCol()) return result;

    for(result=0,i=0;i<nRow();i++)
		for(j=0;j<nCol();j++)
		   result += pow(GetElem(i,j)-other[i][j],2);

return sqrt(result);
}
GeneralMatrix::GeneralMatrix(const GeneralMatrix& other)
{
	pHead = new HeadNode;
	
	 pHead->nRow = 0;
	 pHead->nCol = 0;
   pHead->pFirst = NULL;

	  AddRows(other.nRow(),0.0);
	  AddCols(other.nCol()-1,0.0);

	  *this += other; 
}
GeneralMatrix GeneralMatrix::GetPart(int left,int top,int bottom,int right) const
{
    int i,j;

    GeneralMatrix result;

	if(left>right||top>bottom||right>=nCol()||bottom>=nRow()||left<0||top<0||right<0||bottom<0) return result;

    if(!result.ReSize(bottom-top+1,right-left+1,ERRORVAL))
		return result;
	
    for(i=0;i<result.nRow();i++)
		for(j=0;j<result.nCol();j++)
			result[i][j] = GetElem(i+left,j+top);

return result;
}
//////////////////////////////////////////////////////////////////////
// 重载运算符==,判断矩阵是否相等
//
// 参数:
// 1. const GeneralMatrix& other - 用于比较的矩阵
//
// 返回值:BOOL 型,两个矩阵相等则为TRUE,否则为FALSE
//////////////////////////////////////////////////////////////////////
bool GeneralMatrix::operator==(const GeneralMatrix& other) const
{
	// 首先检查行列数是否相等
	if (this->nCol() != other.nCol() || this->nRow() != other.nRow())
		return FALSE;

	for (int i=0; i<nRow(); ++i)
	{
		for (int j=0; j<nCol(); ++j)
		{
			if (this->GetElem(i,j)!=other.GetElem(i,j))
				return FALSE;
		}
	}

	return TRUE;
}
GeneralMatrix GeneralMatrix::operator - (const GeneralMatrix& other) const
{
	int i,j;
	
	GeneralMatrix result(*this);
	
	if(other.nRow()!=this->nRow()||other.nCol()!=this->nCol())
	{
		for (i=0;i<result.nRow();i++)
			for (j=0;j<result.nCol();j++)
				result.GetElemP(i,j)->val = ERRORVAL;
			return result;
	}
	
	result -= other;
	
return result;
}