📄 realmatrix.cpp
字号:
}
void CRealMatrix::GoPrev()
{
if(pCurrent->pPrev!=pHead)
{
pCurrent=pCurrent->pPrev;
pData=pCurrent;
}
}
void CRealMatrix::GoRight()
{
if(pData!=NULL)
pData=pData->pRight;
}
void CRealMatrix::GoDown()
{
if(pData!=NULL)
pData=pData->pDown;
}
int CRealMatrix::RowCount()
{
if(pHead!=NULL)
return pHead->nRow;
return 0;
}
////////////////////////////////////////////////////////////
//architecture operation
////////////////////////////////////////////////////////
CRealMatrix CRealMatrix::operator +(const CRealMatrix &src)
{
CRealMatrix newMatrix;
if(pHead!=NULL&&src.pHead!=NULL&&pHead->nRow==src.pHead->nRow&&pHead->nCol==src.pHead->nCol)
{
newMatrix.Create(pHead->nRow,pHead->nCol);
CNode *plhsCurrent=pHead;
CNode *prhsCurrent=src.pHead;
for(int i=1;i<=pHead->nRow;++i)
{
plhsCurrent=plhsCurrent->pNext;
prhsCurrent=prhsCurrent->pNext;
CNode *plhsData=plhsCurrent->pRight;
CNode *prhsData=prhsCurrent->pRight;
while(plhsData!=NULL&&prhsData!=NULL)
{
if(plhsData->nCol<prhsData->nCol)
{
newMatrix.Store(i,plhsData->nCol,plhsData->data);
plhsData=plhsData->pRight;
}
else if(plhsData->nCol>prhsData->nCol)
{
newMatrix.Store(i,prhsData->nCol,prhsData->data);
prhsData=prhsData->pRight;
}
else
{
double data=plhsData->data+prhsData->data;
if(data!=0)
newMatrix.Store(i,plhsData->nCol,data);
plhsData=plhsData->pRight;
prhsData=prhsData->pRight;
}
}
while(plhsData!=NULL)
{
newMatrix.Store(i,plhsData->nCol,plhsData->data);
plhsData=plhsData->pRight;
}
while(prhsData!=NULL)
{
newMatrix.Store(i,prhsData->nCol,prhsData->data);
prhsData=prhsData->pRight;
}
}
}
return newMatrix;
}
CRealMatrix CRealMatrix::operator -(const CRealMatrix &src)
{
CRealMatrix newMatrix;
if(pHead!=NULL&&src.pHead!=NULL&&pHead->nRow==src.pHead->nRow&&pHead->nCol==src.pHead->nCol)
{
newMatrix.Create(pHead->nRow,pHead->nCol);
CNode *plhsCurrent=pHead;
CNode *prhsCurrent=src.pHead;
for(int i=1;i<=pHead->nRow;++i)
{
plhsCurrent=plhsCurrent->pNext;
prhsCurrent=prhsCurrent->pNext;
CNode *plhsData=plhsCurrent->pRight;
CNode *prhsData=prhsCurrent->pRight;
while(plhsData!=NULL&&prhsData!=NULL)
{
if(plhsData->nCol<prhsData->nCol)
{
newMatrix.Store(i,plhsData->nCol,plhsData->data);
plhsData=plhsData->pRight;
}
else if(plhsData->nCol>prhsData->nCol)
{
newMatrix.Store(i,prhsData->nCol,-prhsData->data);
prhsData=prhsData->pRight;
}
else
{
double data=plhsData->data-prhsData->data;
if(data!=0)
newMatrix.Store(i,plhsData->nCol,data);
plhsData=plhsData->pRight;
prhsData=prhsData->pRight;
}
}
while(plhsData!=NULL)
{
newMatrix.Store(i,plhsData->nCol,plhsData->data);
plhsData=plhsData->pRight;
}
while(prhsData!=NULL)
{
newMatrix.Store(i,prhsData->nCol,-prhsData->data);
prhsData=prhsData->pRight;
}
}
}
return(newMatrix);
}
CRealMatrix CRealMatrix::operator *(const CRealMatrix &src)
{
CRealMatrix newMatrix;
if(pHead->data!=0&&pHead->nRow==0&&pHead->nCol==0)
{
double value=pHead->data;
newMatrix.Create(src.pHead->nRow,src.pHead->nCol);
CNode *psrcCurrent=src.pHead;
for(int i=0;i<src.pHead->nRow;++i)
{
psrcCurrent=psrcCurrent->pNext;
CNode *psrcData=psrcCurrent->pRight;
while(psrcData!=NULL)
{
newMatrix.Store(psrcData->nRow,psrcData->nCol,value*psrcData->data);
psrcData=psrcData->pRight;
}
}
return newMatrix;
}
if(src.pHead->data!=0&&src.pHead->nRow==0&&src.pHead->nCol==0)
{
double value=src.pHead->data;
newMatrix.Create(pHead->nRow,pHead->nCol);
CNode *psrcCurrent=pHead;
for(int i=0;i<pHead->nRow;++i)
{
psrcCurrent=psrcCurrent->pNext;
CNode *psrcData=psrcCurrent->pRight;
while(psrcData!=NULL)
{
newMatrix.Store(psrcData->nRow,psrcData->nCol,value*psrcData->data);
psrcData=psrcData->pRight;
}
}
return newMatrix;
}
if(pHead!=NULL&&src.pHead!=NULL&&pHead->nCol==src.pHead->nRow)
{
newMatrix.Create(pHead->nRow,src.pHead->nCol);
CNode *plhsCurrent=pHead;
for(int i=1;i<=pHead->nRow;++i)
{
plhsCurrent=plhsCurrent->pNext;
CNode *prhsCurrent=src.pHead;
for(int j=1;j<=src.pHead->nCol;++j)
{
prhsCurrent=prhsCurrent->pNext;
CNode *plhsData=plhsCurrent->pRight;
CNode *prhsData=prhsCurrent->pDown;
double data=0;
while(plhsData!=NULL&&prhsData!=NULL)
{
if(plhsData->nCol<prhsData->nRow)
plhsData=plhsData->pRight;
else if(plhsData->nCol>prhsData->nRow)
prhsData=prhsData->pDown;
else
{
data+=plhsData->data*prhsData->data;
plhsData=plhsData->pRight;
prhsData=prhsData->pDown;
}
}
if(data!=0) newMatrix.Store(i,j,data);
}
}
}
return(newMatrix);
}
void CRealMatrix::operator +=(const CRealMatrix &src)
{
*this=*this+src;
}
void CRealMatrix::operator -=(const CRealMatrix &src)
{
*this=*this-src;
}
void CRealMatrix::operator *=(const CRealMatrix &src)
{
*this=*this*src;
}
void CRealMatrix::operator *=(const double value)
{
if(value==0)
{
int row=pHead->nRow;
int col=pHead->nCol;
Empty();
Create(row,col);
return;
}
if(value!=0)
{
CRealMatrix matrixI(pHead->nCol,pHead->nCol);
for(int i=1;i<=pHead->nCol;++i)
matrixI.Store(i,i,value);
*this=*this*value;
}
}
CRealMatrix CRealMatrix::operator -()
{
CRealMatrix newMatrix;
if(pHead!=NULL)
{
newMatrix.Create(pHead->nRow,pHead->nCol);
CNode *psrcCurrent=pHead;
for(int i=0;i<pHead->nRow;++i)
{
psrcCurrent=psrcCurrent->pNext;
CNode *psrcData=psrcCurrent->pRight;
while(psrcData!=NULL)
{
newMatrix.Store(psrcData->nRow,psrcData->nCol,-psrcData->data);
psrcData=psrcData->pRight;
}
}
}
return(newMatrix);
}
CRealMatrix CRealMatrix::operator *(const double value)
{
CRealMatrix newMatrix;
if(value==0)
{
newMatrix.Create(pHead->nRow,pHead->nCol);
return newMatrix;
}
if(pHead!=NULL)
{
newMatrix.Create(pHead->nRow,pHead->nCol);
CNode *psrcCurrent=pHead;
for(int i=0;i<pHead->nRow;++i)
{
psrcCurrent=psrcCurrent->pNext;
CNode *psrcData=psrcCurrent->pRight;
while(psrcData!=NULL)
{
newMatrix.Store(psrcData->nRow,psrcData->nCol,value*psrcData->data);
psrcData=psrcData->pRight;
}
}
}
return(newMatrix);
}
///////////////////////////////////////////////////////////////////
//special operation
////////////////////////////////////////////////////////////////////
CRealMatrix CRealMatrix::Transpose()
{
CRealMatrix newMatrix;
if(pHead!=NULL)
{
newMatrix.Create(pHead->nCol,pHead->nRow);
pCurrent=pHead;
newMatrix.pCurrent=newMatrix.pHead;
pCurrent=pCurrent->pNext;
while(pCurrent!=pTail)
{
newMatrix.pCurrent=newMatrix.pCurrent->pNext;
pData=pCurrent->pRight;
newMatrix.pData=newMatrix.pCurrent;
while(pData!=NULL)
{
//row transpose to col
newMatrix.pData->pDown=new CNode;
newMatrix.pData=newMatrix.pData->pDown;
newMatrix.pData->nRow=pData->nCol;
newMatrix.pData->nCol=pData->nRow;
newMatrix.pData->pRight=NULL;
newMatrix.pData->pDown=NULL;
newMatrix.pData->data=pData->data;
//col transpose to row
CNode *pTemp=newMatrix.pHead;
for(int i=0;i<newMatrix.pData->nRow;++i)
pTemp=pTemp->pNext;
while(pTemp->pRight!=NULL)
pTemp=pTemp->pRight;
pTemp->pRight=newMatrix.pData;
pData=pData->pRight;
}
pCurrent=pCurrent->pNext;
}
}
return(newMatrix);
}
////////////////////////////////////////////
//正定矩阵求逆
//////////////////////////////////////
CRealMatrix CRealMatrix::Inverse()
{
CRealMatrix matrix;
return matrix;
}
BOOL CRealMatrix::Unit()
{
CNode* pTemp=pData;
if(pData->pRight==NULL) return FALSE;
while(pData->pRight!=NULL&&pData->pRight->nCol<=pCurrent->nRow)
pData=pData->pRight;
if(pData->nCol<pCurrent->nRow) return FALSE;
double value=pData->data;
pData->data=1.0;
while(pTemp->pRight!=NULL)
{
pTemp=pTemp->pRight;
pTemp->data/=value;
}
return TRUE;
}
BOOL CRealMatrix::Remove(INT row1,INT row2)
{
pData=pCurrent;
CNode* pTemp=pHead->pNext;
while(pTemp->pNext->nRow<=row2&&pTemp->pNext!=pTail)
pTemp=pTemp->pNext;
CNode* pNode=pTemp;
if(pTemp->pRight==NULL) return FALSE;
while(pTemp->pRight!=NULL&&pTemp->pRight->nCol<=row1)
pTemp=pTemp->pRight;
if(pTemp->nCol!=row1) return TRUE;
double value=pTemp->data;
if(value==0) return TRUE;
pTemp->data=0.0;
while(pData->pRight!=NULL||pNode->pRight!=NULL)
{
if(pData->pRight==NULL&&pNode->pRight!=NULL) return TRUE;
if(pData->pRight!=NULL&&pNode->pRight==NULL)
{
while(pData->pRight!=NULL)
{
pData=pData->pRight;
AddNode(pNode->nRow,pData->nCol,-value*pData->data,pNode);
pNode=pNode->pRight;
}
return TRUE;
}
if(pData->pRight->nCol==pNode->pRight->nCol)
{
pNode=pNode->pRight;
pData=pData->pRight;
pNode->data-=value*pData->data;
}
else if(pData->pRight->nCol>pNode->pRight->nCol)
pNode=pNode->pRight;
else
{
pData=pData->pRight;
AddNode(pNode->nRow,pData->nCol,-value*pData->data,pNode);
pNode=pNode->pRight;
}
}
return TRUE;
}
void CRealMatrix::AddNode(int row,int col,double value,CNode* pTemp)
{
//create a node
CNode* pNode=new CNode;
pNode->nRow=row;
pNode->nCol=col;
pNode->data=value;
pNode->pRight=NULL;
pNode->pDown=NULL;
pNode->pNext=NULL;
pNode->pPrev=NULL;
if(pTemp->pRight==NULL)
pTemp->pRight=pNode;
else
{
CNode* pNew=pTemp->pRight;
pTemp->pRight=pNode;
pNode->pRight=pNew;
}
CNode* pTemp1=pHead->pNext;
while(pTemp1->nRow<=col&&pTemp1!=pTail)
pTemp1=pTemp1->pNext;
while(pTemp1->pDown!=NULL)
pTemp1=pTemp1->pDown;
pTemp1->pDown=pNode;
}
BOOL CRealMatrix::Inverse0()
{
int n=pHead->nRow;
if(pHead->nRow==pHead->nCol&&pHead->nRow!=0)
{
pCurrent=pHead->pNext;
pData=pCurrent;
CNode* psrcCurrent=pCurrent;
CNode* psrcData=pData;
while(psrcCurrent!=pTail)
{
if(!Unit()) return FALSE;//规格化
for(INT i=1;i<=n;++i)
{
if(i==psrcCurrent->nRow) continue;
BOOL lp=Remove(psrcCurrent->nRow,i);//消去
if(!lp) return FALSE;
}
psrcCurrent=psrcCurrent->pNext;
psrcData=psrcCurrent;
pCurrent=psrcCurrent;
pData=pCurrent;
}
return TRUE;
}
return FALSE;
}
void CRealMatrix::DeleteRow(int row)
{
CRealMatrix NewMatrix;
if(pHead!=NULL&&row<=pHead->nRow&&row>0)
{
NewMatrix.Create(pHead->nRow-1,pHead->nCol);//删除后的新矩阵
pData=pHead->pNext;
pCurrent=pData;
while(pData->nRow<row)
{
while(pData->pRight!=NULL)
{
pData=pData->pRight;
if(pData->data==0)continue;
NewMatrix.Store(pData->nRow,pData->nCol,pData->data);
}
pCurrent=pCurrent->pNext;
pData=pCurrent;
}
//删除该行
pCurrent=pCurrent->pNext;
pData=pCurrent;
while(pData->nRow>row&&pData->pRight!=NULL&&pCurrent!=pTail)
{
while(pData->pRight!=NULL)
{
pData=pData->pRight;
if(pData->data==0)continue;
NewMatrix.Store(pData->nRow-1,pData->nCol,pData->data);
}
pCurrent=pCurrent->pNext;
pData=pCurrent;
}
Copy(NewMatrix);
}
}
void CRealMatrix::DeleteCol(int col)
{
CRealMatrix NewMatrix;
if(pHead!=NULL&&col<=pHead->nCol&&col>0)
{
NewMatrix.Create(pHead->nRow,pHead->nCol-1);//删除后的新矩阵
pData=pHead->pNext;
pCurrent=pData;
while(pData->nCol<col)
{
while(pData->pDown!=NULL)
{
pData=pData->pDown;
if(pData->data==0)continue;
NewMatrix.Store(pData->nRow,pData->nCol,pData->data);
}
pCurrent=pCurrent->pNext;
pData=pCurrent;
}
//删除该列
pCurrent=pCurrent->pNext;
pData=pCurrent;
while(pData->nCol>col&&pCurrent!=pTail)
{
while(pData->pDown!=NULL)
{
pData=pData->pDown;
if(pData->data==0)continue;
NewMatrix.Store(pData->nRow,pData->nCol-1,pData->data);
}
pCurrent=pCurrent->pNext;
pData=pCurrent;
}
Copy(NewMatrix);
}
}
void CRealMatrix::Input(char filename[21])
{
int row,col;
float value;
FILE* fp;
fp=fopen(filename,"r");
if(fp!=NULL)
{
while(!feof(fp))
{
fscanf(fp,"%d%d%f",&row,&col,&value);
Store(row,col,value);
}
}
fclose(fp);
}
void CRealMatrix::Output(char filename[21])
{
if(pHead==NULL) return;
int row,col;
double value;
FILE* fp;
fp=fopen(filename,"w");
pCurrent=pHead->pNext;
if(fp!=NULL)
{
for(row=1;row<=pHead->nRow;++row)
{
for(col=1;col<=pHead->nCol;++col)
{
GetData(row,col,value);
if(value==0) continue;
fprintf(fp,"%d\t%d\t%f\t\t",row,col,value);
}
fprintf(fp,"\n");
}
}
fclose(fp);
}
CRealMatrix CRealMatrix::RowExchange(int row1,int row2)
{
CRealMatrix NewMatrix(pHead->nRow,pHead->nCol);
if(pHead==NULL||row1<0||row1>pHead->nRow
||row2<0||row2>pHead->nRow)
return NewMatrix;
int n=row1<row2?row1:row2;
int n1=row1>row2?row1:row2;
CNode* pData1=new CNode;
CNode* pData2=new CNode;
pData1=pCurrent;
if(pData1==pHead)
{
for(int i=0;i<n;++i)
pData1=pData1->pNext;
}
else if(pData1->nRow<n)
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -