2014-10-10 78 views
3

我想這個功能轉自鐵血陣列到二維數組轉換,我不能夠將所有的都 原函數:轉換鋸齒狀數組二維數組C#

public static double[][] InvertMatrix(double[][] A) 
{ 
    int n = A.Length; 
    //e will represent each column in the identity matrix 
    double[] e; 
    //x will hold the inverse matrix to be returned 
    double[][] x = new double[n][]; 
    for (int i = 0; i < n; i++) 
    { 
     x[i] = new double[A[i].Length]; 
    } 
    /* 
    * solve will contain the vector solution for the LUP decomposition as we solve 
    * for each vector of x. We will combine the solutions into the double[][] array x. 
    * */ 
    double[] solve; 

    //Get the LU matrix and P matrix (as an array) 
    Tuple<double[][], int[]> results = LUPDecomposition(A); 

    double[][] LU = results.Item1; 
    int[] P = results.Item2; 

    /* 
    * Solve AX = e for each column ei of the identity matrix using LUP decomposition 
    * */ 
    for (int i = 0; i < n; i++) 
    { 
     e = new double[A[i].Length]; 
     e[i] = 1; 
     solve = LUPSolve(LU, P, e); 
     for (int j = 0; j < solve.Length; j++) 
     { 
      x[j][i] = solve[j]; 
     } 
    } 
    return x; 
} 

我已經轉換到現在:

public static double[,] InvertMatrix(double[,] A) 
{ 
    int n = A.Length; 
    //e will represent each column in the identity matrix 
    double[] e; 
    //x will hold the inverse matrix to be returned 
    double[,] x = new double[n][]; 
    for (int i = 0; i < n; i++) 
    { 
     //how to convert this line? 
     x[i] = new double[A[i].Length]; 
    } 
    /* 
    * solve will contain the vector solution for the LUP decomposition as we solve 
    * for each vector of x. We will combine the solutions into the double[][] array x. 
    * */ 
    double[] solve; 

    //Get the LU matrix and P matrix (as an array) 
    Tuple<double[,], int[]> results = LUPDecomposition(A); 

    double[,] LU = results.Item1; 
    int[] P = results.Item2; 

    /* 
    * Solve AX = e for each column ei of the identity matrix using LUP decomposition 
    * */ 
    for (int i = 0; i < n; i++) 
    { 
     //This one too?! 
     e = new double[A[i].Length]; 
     e[i] = 1; 
     solve = LUPSolve(LU, P, e); 
     for (int j = 0; j < solve.Length; j++) 
     { 
      x[j,i] = solve[i,j]; 
     } 
    } 
    return x; 
} 

如何將x [i] = new double [A [i] .Length]轉換爲2D數組?

回答

11

這個片段可以幫助

static T[,] To2D<T>(T[][] source) 
{ 
    try 
    { 
     int FirstDim = source.Length; 
     int SecondDim = source.GroupBy(row => row.Length).Single().Key; // throws InvalidOperationException if source is not rectangular 

     var result = new T[FirstDim, SecondDim]; 
     for (int i = 0; i < FirstDim; ++i) 
      for (int j = 0; j < SecondDim; ++j) 
       result[i, j] = source[i][j]; 

     return result; 
    } 
    catch (InvalidOperationException) 
    { 
     throw new InvalidOperationException("The given jagged array is not rectangular."); 
    } 
} 

用法:

double[][] array = { new double[] { 52, 76, 65 }, new double[] { 98, 87, 93 }, new double[] { 43, 77, 62 }, new double[] { 72, 73, 74 } }; 
double[,] D2 = To2D(array); 
0

只是爲了確保我們在相同的理解,交錯數組是數組的數組。所以,當你做

for (int i = 0; i < n; i++) 
{ 
    //how to convert this line? 
    x[i] = new double[A[i].Length]; 
} 

,所添加的第一個維度的陣列中的每個位置的陣列。

在你的情況下(在鋸齒狀陣列中)A.Length表示數組第一維的長度,而A[i].Length表示包含在第一維的索引(i)中的第二維數組的長度。如果您使用的是二維數組,則A.Length表示兩個維度的長度相乘。雖然對於每個第二維數組,鋸齒可以具有不同的長度,但二維數組在兩個維上的長度必須相同。因此,在你的情況下,你將不得不得到n = A.GetLength(0)(意味着獲得第一維的長度)和m = A.GetLength(1)(意味着獲得第二維的長度)。您將初始化'x'double[,] x = new double[n, m];,您將不再需要for循環。然後

您的代碼應該是這樣的:

public static double[,] InvertMatrix(double[,] A) 
{ 
    int n = A.Length; 
    //e will represent each column in the identity matrix 
    double[] e; 
    //x will hold the inverse matrix to be returned 
    double[,] x = new double[n, m]; 

    /* 
    * solve will contain the vector solution for the LUP decomposition as we solve 
    * for each vector of x. We will combine the solutions into the double[][] array x. 
    * */ 
    double[] solve; 

    //Get the LU matrix and P matrix (as an array) 
    Tuple<double[,], int[]> results = LUPDecomposition(A); 

    double[,] LU = results.Item1; 
    int[] P = results.Item2; 

    /* 
    * Solve AX = e for each column ei of the identity matrix using LUP decomposition 
    * */ 
    for (int i = 0; i < n; i++) 
    { 
     //This one too?! /// this one would become 
     e = new double[m]; 
     e[i] = 1; 
     solve = LUPSolve(LU, P, e); 
     for (int j = 0; j < solve.Length; j++) 
     { 
      x[j,i] = solve[i,j]; 
     } 
    } 
    return x; 
} 

所以,如果我做了什麼是好的,這應該解決您的問題,並回答你的問題。

2

注意:你的參差不齊的數組應該是正交的,因此子數組長度應該都是相等的,否則你不能將它轉換成二維數組。

部分:

double[,] x = new double[n][]; 
for (int i = 0; i < n; i++) 
{ 
    //how to convert this line? 
    x[i] = new double[A[i].Length]; 
} 

僅僅是初始化一個新的鋸齒狀排列,可以方便地與

double[,] x = new double[A.GetLength(0),A.GetLength(1)]; 

//This one too?! 
    e = new double[A[i].Length]; 

你基本上是創建一個數組代替相同長度的子陣iA所以我們可以用它代替它h

e = new double[A.GetLength(1)]; //NOTE: second dimension 

如前所述,所有的子數組長度都是相等的,所以我們可以用第二維長度代替。

,整個方法是:

public static double[,] InvertMatrix2D(double[,] A) 
    { 
     int n = A.Length; 
     //e will represent each column in the identity matrix 
     double[] e; 
     //x will hold the inverse matrix to be returned 
     double[,] x = new double[A.GetLength(0),A.GetLength(1)]; 

     /* 
     * solve will contain the vector solution for the LUP decomposition as we solve 
     * for each vector of x. We will combine the solutions into the double[][] array x. 
     * */ 
     double[] solve; 

     //Get the LU matrix and P matrix (as an array) 
     Tuple<double[,], int[]> results = LUPDecomposition(A); 

     double[,] LU = results.Item1; 
     int[] P = results.Item2; 

     /* 
     * Solve AX = e for each column ei of the identity matrix using LUP decomposition 
     * */ 
     for (int i = 0; i < n; i++) 
     { 
      e = new double[A.GetLength(1)]; //NOTE: second dimension 
      e[i] = 1; 
      solve = LUPSolve(LU, P, e); 
      for (int j = 0; j < solve.Length; j++) 
      { 
       x[j,i] = solve[j]; 
      } 
     } 
     return x; 
    } 
相關問題