2011-12-20 121 views
1

我無法弄清楚如何使用MathNet獲得生成矩陣的值,任何人都可以請幫忙嗎?我有一個4列,3行矩陣,它代表3個多項式,我試圖求解a =,b =,c =。 row1表示a,2b,3c。我無法弄清楚如何使用MathNet的功能來獲取這些值,或者如果它甚至可以。如何在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); 

編輯:

如果您想嘗試,你可以設置的矩陣解決方案=到

  double[,] matrix = new double[3, 4]{ 
      { 1, 2, -1, -4 }, 
      { 2, 3, -1, -11 }, 
      { -2, 0, -3, 22 } 
     }; 

回答

1

解決甚至不使用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++; 
     }