用C#来实现矩阵相乘

来源:百度知道 编辑:UC知道 时间:2024/05/28 18:32:06
急求用C#来实现两个矩阵相乘的代码,哪位C#高手帮帮忙,解决一下!非常急!本人在此多谢了!
就是用C#语言来编写一个程序,来实现两个矩阵相乘,那两个矩阵是通过手动输入,按下确定键后就显示出一个新的矩阵,就是要得到的结果! 第二个回答“ operator* (Matrix lhs,Matrix rhs)”有错误!

//binary multiple 矩阵乘
public static Matrix operator* (Matrix lhs,Matrix rhs)
{
if(lhs.Col != rhs.Row) //异常
{
System.Exception e = new Exception("相乘的两个矩阵的行列数不匹配");
throw e;
}

Matrix ret = new Matrix (lhs.Row,rhs.Col);
double temp;
for(int i=0;i<lhs.Row;i++)
{
for(int j=0;j<rhs.Col;j++)
{
temp = 0;
for(int k=0;k<lhs.Col;k++)
{
temp += lhs[i,k] * rhs[k,j];
}
ret[i,j] = temp;
}
}

return ret;
}