bool SMatrix::Jointer_bottom(SMatrix& other) { if(other.nCol()!=nCol()) return FALSE; if(!AddRows(other.nRow(),ERRORVAL)) return FALSE; return Paste(other,nRow()-other.nRow(),0); }
bool SMatrix::Paste(SMatrix& 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 SMatrix::Jointer_Diagonal(SMatrix& 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 SMatrix::CutIn2_Across(SMatrix& Upper,SMatrix& 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 SMatrix::Jointer_Right(SMatrix& other) { if(other.nRow()!=nRow()) return FALSE; if(!AddCols(other.nCol(),ERRORVAL)) return FALSE; Paste(other,0,nCol()-other.nCol()); return TRUE; }
SMatrix operator * (double value ,const SMatrix& other) { int i,j; SMatrix result(other); for (i=0;i<other.nRow();i++) for(j=0;j<other.nCol();j++) result[i][j] *= value; return result; }
SMatrix SMatrix::operator * (const SMatrix& other) const { int i,j,k; SMatrix 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++) result[i][j] += this->GetElem(i,k) * other.GetElem(k,j); return result; }
ELEMTYPE SMatrix::Distance_E(SMatrix& 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); }
SMatrix::SMatrix(const SMatrix& 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; }
SMatrix SMatrix::GetPart(int left,int top,int bottom,int right) const { int i,j; SMatrix 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; }
SMatrix SMatrix::operator - (const SMatrix& other) const { int i,j; SMatrix 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; }