矩阵的LU分解———学习笔记 ,转载自chinaunix

/**
 * @(#)LUanalyzeTest.java
 *
 *
 * @author 坚持到底
 * @version 1.00
 */
public class LUanalyzeTest
{
     /**
      * LU分解
      * @param L_matrix L矩阵(单位下三角阵)
      * @param U_matrix U矩阵(上三角阵)
      * @param matrix LU分解之前的系数矩阵
      * @param num 系数矩阵的阶数 
      */
     public void LUanalyze(double[][] L_matrix, double[][] U_matrix, double[][] matrix, int num)
     {
          int i, j, k, r;
          double sum;
          //初始化L矩阵(L为单位下三角阵)

          for (i = 0; i < num; i++)
          for (j = 0; j < num; j++)
          {
               if (i == j)
                    L_matrix[j][i] = 1;
               //对角线元素置1

               else
               {
                    L_matrix[j][i] = 0;
               }
          }
          //初始化U矩阵(所有元素置0)

          for (i = 0; i < num; i++)
               for (j = 0; j < num; j++)
                    U_matrix[j][i] = 0;
          //求U矩阵的第一行

          for (i = 0; i < num; i++)
               U_matrix[0][i] = matrix[0][i];
          //求L矩阵的第一列

          for (i = 0; i < num; i++)
               L_matrix[i][0] = matrix[i][0] / U_matrix[0][0];
          for (r = 1; r < num; r++)
          {
               //求U的第r行的元素

               for (i = r; i < num; i++)
               {
                    sum = 0.0;
                    for (k = 0; k < r; k++)
                    {
                         sum += L_matrix[r][k] * U_matrix[k][i];
                    }
                    U_matrix[r][i] = matrix[r][i] - sum;
               }
               //求L的第r列的元素

               for (i = r; i < num; i++)
               {
                    sum = 0.0;
                    for (k = 0; k < r; k++)
                    {
                         sum += L_matrix[i][k] * U_matrix[k][r];
                    }
                    L_matrix[i][r] = (matrix[i][r] - sum) / U_matrix[r][r];
               }
          }
     }
     //打印行列式

     public void printMatrix(double[][] matrixTest, int row, int column)
     {
          for (int i = 0; i <= row - 1; i++)
          {
               System.out.println('\n');
               for (int j = 0; j <= column - 1; j++)
                    System.out.print(matrixTest[i][j] + " ");
          }
     }
     public static void main(String[] args)
     {
          int num = 4; //矩阵的阶数

          //double[][] matrix={{1,2,6},{2,5,15},{6,15,46}};

          //double[][] matrix={{1,2,3},{2,5,2},{3,1,5}};    

          double[][] matrix = {{2, 4, 2, 6}, {4, 9, 6, 15}, {2, 6, 9, 18}, {6, 15, 18, 40}};
          double[][] L_matrix = new double[num][num];
          double[][] U_matrix = new double[num][num];
          LUanalyzeTest lu = new LUanalyzeTest();
          System.out.println("\n原矩阵:");
          lu.printMatrix(matrix, num, num);
          lu.LUanalyze(L_matrix, U_matrix, matrix, num);
          System.out.println("\nL矩阵:");
          lu.printMatrix(L_matrix, num, num);
          System.out.println("\nU矩阵:");
          lu.printMatrix(U_matrix, num, num);
     }
}

你可能感兴趣的:(JAVA)