矩阵运算的程序实现以C描述.docx
- 文档编号:4028117
- 上传时间:2022-11-27
- 格式:DOCX
- 页数:7
- 大小:14.91KB
矩阵运算的程序实现以C描述.docx
《矩阵运算的程序实现以C描述.docx》由会员分享,可在线阅读,更多相关《矩阵运算的程序实现以C描述.docx(7页珍藏版)》请在冰豆网上搜索。
矩阵运算的程序实现以C描述
矩阵运算的程序实现——以C#描述
矩阵运算的程序实现——以C#描述
//求去掉某行、某列(lNum)后的矩阵
double[,]minus(inthNum,intlNum,double[,]matr)
{
intm=(int)Math.Sqrt(matr.Length);//获取原行列式的行数
intn=(int)Math.Sqrt(matr.Length);//获取原行列式的列数
double[,]newMatr=newdouble[m-1,m-1];//新建二维数组,用于存储变形后的行列式
if(lNum>m-1||lNum<0||hNum>m-1||hNum<0)
{
MessageBox.Show("不存在该列或行");
}
if(lNum==m-1&&hNum==m-1)//如果去掉的行列都为最后一行一列
{
for(inti=0;i<m-1;i++)
for(intj=0;j<m-1;j++)
{
newMatr[i,j]=matr[i,j];
}
}
if(lNum==m-1&&hNum<m-1&&hNum>=0)//去掉最后一列,中间一行
{
for(intj=0;j<m-1;j++)
{
for(inti=0;i<hNum;i++)
{
newMatr[i,j]=matr[i,j];
}
for(inti=hNum;i<m-1;i++)
{
newMatr[i,j]=matr[i+1,j];
}
}
}
if(hNum==m-1&&lNum<m-1&&lNum>=0)//去掉最后一行,中间一列
{
for(inti=0;i<m-1;i++)
{
for(intj=0;j<lNum;j++)
{
newMatr[i,j]=matr[i,j];
}
for(intj=lNum;j<m-1;j++)
{
newMatr[i,j]=matr[i,j+1];
}
}
}
if(lNum<m-1&&lNum>=0&&hNum<m-1&&hNum>=0)//去掉中间行列
{
for(inti=0;i<hNum;i++)
{
for(intj=0;j<lNum;j++)
{
newMatr[i,j]=matr[i,j];
}
for(intj2=lNum;j2<m-1;j2++)
{
newMatr[i,j2]=matr[i,j2+1];
}
}
//////////
for(inti=hNum;i<m-1;i++)
{
for(intj=0;j<lNum;j++)
{
newMatr[i,j]=matr[i+1,j];
}
for(intj2=lNum;j2<m-1;j2++)
{
newMatr[i,j2]=matr[i+1,j2+1];
}
}
}
returnnewMatr;
}
//求行列式
publicdoublevalueHls(double[,]matr)
{
doublevalue=0;//用于存储最后计算值
doublee=-1;//-1的次数
if(matr.Length==1)
{
value=matr[0,0];
}
//if(matr.Length>1)//if不套for
//{
for(intj=0;j<Math.Sqrt(matr.Length)&&matr.Length>1;j++)
{
e=e*(-1);
double[,]B=minus(0,j,matr);
value+=e*matr[0,j]*valueHls(B);
}
//}
returnvalue;
}
//矩阵求逆
publicdouble[,]matrInver(double[,]matr)
{
intm=(int)Math.Sqrt(matr.Length);//获取原行列式的行数
intn=(int)Math.Sqrt(matr.Length);//获取原行列式的列数
double[,]newMatr=newdouble[m,m];//新建二维数组,用于存储变形后的行列式
doubleAValue=0;
doublee=-1;//-1的次数
AValue=1/valueHls(matr);
for(inti=0;i<m;i++)
for(intj=0;j<m;j++)
{
e*=-1;
newMatr[i,j]=e*AValue*valueHls(minus(j,i,matr));
}
returnnewMatr;
}
//矩阵相乘
publicdouble[,]matrMult(doublea,double[,]matr)
{
double[,]newMatr=newdouble[matr.GetLength(0),matr.GetLength
(1)];
for(inti=0;i<matr.GetLength(0);i++)
for(intj=0;j<matr.GetLength
(1);j++)
{
newMatr[i,j]=matr[i,j]*a;
}
returnnewMatr;
}
//矩阵相乘2
publicdouble[,]matrMult(double[,]matr1,double[,]matr2)
{
if(matr1.GetLength
(1)==matr2.GetLength(0))
{
double[,]newMatr=newdouble[matr1.GetLength(0),matr2.GetLength
(1)];
for(inti=0;i<matr1.GetLength(0);i++)
for(intj=0;j<matr2.GetLength
(1);j++)
{
for(intk=0;k<matr2.GetLength(0);k++)
{
newMatr[i,j]+=matr1[i,k]*matr2[k,j];
}
}
returnnewMatr;
}
else{
MessageBox.Show("两个行列式不能相乘");
returnnull;
}
}
//矩阵相加
publicdouble[,]matrAdd(double[,]matr1,double[,]matr2)
{
if(matr1.GetLength(0)==matr2.GetLength(0)&&matr1.GetLength
(1)==matr2.GetLength
(1))
{
double[,]newMatr=new
double[matr1.GetLength(0),matr1.GetLength
(1)];
for(inti=0;i<matr1.GetLength(0);i++)
for(intj=0;j<matr1.GetLength
(1);j++)
{
newMatr[i,j]=matr1[i,j]+matr2[i,j];
}
returnnewMatr;
}
else
{
MessageBox.Show("两个行列式不能相加");
returnnull;
}
}
//矩阵转置
publicdouble[,]matrTrans(double[,]matr)
{
double[,]newMatr=newdouble[matr.GetLength
(1),matr.GetLength(0)];
for(inti=0;i<matr.GetLength
(1);i++)
for(intj=0;j<matr.GetLength(0);j++)
{
newMatr[i,j]=matr[j,i];
}
returnnewMatr;
}
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 矩阵 运算 程序 实现 描述