我们用的是MathNet.Numerics命名空间里面的Matrix.PseudoInverse()方法计算伪逆,跟Python的numpy.linalg.pinv或者Matlab的pinv是一样的。
double[,] xEX = {
{ 1.0, 0,0 ,0,0,6.0},
{ 0, -1,0 ,0,0,17.0},
{ 0, 0,1 ,-6,17,0},
{ 1.0, 0,0 ,0,0,-6.0},
{ 0, -1,0 ,0,0,17.0},
{ 0, 0,1 ,6,17,0},
{ 1.0, 0,0 ,0,0,6.0},
{ 0, -1,0 ,0,0,-17.0},
{ 0, 0,1 ,-6,-17,0},
{ 1.0, 0,0 ,0,0,-6.0},
{ 0, -1,0 ,0,0,-17.0},
{ 0, 0,1 ,6,-17,0},
};
var matrix6 = DenseMatrix.OfArray(xEX);
var pi = matrix6.PseudoInverse();
var pia = pi.ToArray();
double[,] xEX_PseudoInverse ={
{ 0.2500, 0,0 ,0.2500,0,0,0.2500,0,0,0.2500,0,0},
{ 0, -0.2500,0 ,0,-0.2500,0,0,-0.2500,0,0,-0.2500,0},
{ 0, 0,0.2500 ,0,0,0.2500,0,0,0.2500,0,0,0.2500},
{ 0,0,-0.0417,0,0,0.0417,0,0,-0.0417,0,0,0.0417},
{ 0,0,0.0147,0,0,0.0147,0,0,-0.0147,0,0,-0.0147},
{ 0.0046,0.0131,0,-0.0046,0.0131,0,0.0046,-0.0131,0,-0.0046,-0.0131,0},
};
//可以通过resultStr检查变换结果
int row = pia.GetLength(0); //可以理解为:第一维的长度(即行数)
int col = pia.GetLength(1); //可以理解为:第二维的长度(即列数)
string resultStr = "";
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
resultStr += (pia[i, j].ToString("f4"));
resultStr += "\t";
}
resultStr += "\n";
}