我不知道如何使用MathNet获得结果矩阵的值,有人可以帮助吗?我有一个4列3行矩阵它代表3个多项式我试着解出a= b= c=。row1代表a, 2 b, 3 c.我不知道如何使用MathNet的功能来获得这些值,或者如果它甚至这样做。
double X1 = Convert.ToDouble(x1);
double Y1 = Convert.ToDouble(y1);
double X2 = Convert.ToDouble(x2);
double Y2 = Convert.ToDouble(y2);
double X3 = Convert.ToDouble(x3);
double Y3 = Convert.ToDouble(y3);
var a = new DenseMatrix(3, 4);
double[] row1 = new double[2];
row1[0] = X1;
row1[1] = Y1;
double[] row2 = new double[2];
row2[0] = X2;
row2[1] = Y2;
double[] row3 = new double[2];
row3[0] = X3;
row3[1] = Y3;
a.SetRow(0, row1);
a.SetRow(1, row2);
a.SetRow(2, row3);
编辑:如果您想尝试解决方案,您可以将matrix =设置为
double[,] matrix = new double[3, 4]{
{ 1, 2, -1, -4 },
{ 2, 3, -1, -11 },
{ -2, 0, -3, 22 }
};
不用MathNet就解决了:
int lead = 0, rowCount = 3, columnCount = 4;
for (int r = 0; r < rowCount; r++)
{
if (columnCount <= lead) break;
int i = r;
while (matrix[i, lead] == 0)
{
i++;
if (i == rowCount)
{
i = r;
lead++;
if (columnCount == lead)
{
lead--;
break;
}
}
}
for (int j = 0; j < columnCount; j++)
{
double temp = matrix[r, j];
matrix[r, j] = matrix[i, j];
matrix[i, j] = temp;
}
double div = matrix[r, lead];
for (int j = 0; j < columnCount; j++) matrix[r, j] /= div;
for (int j = 0; j < rowCount; j++)
{
if (j != r)
{
double sub = matrix[j, lead];
for (int k = 0; k < columnCount; k++) matrix[j, k] -= (sub * matrix[r, k]);
}
}
lead++;
}