0

嗨,我有一个任务,我需要使用高斯消元法求解一个线性方程组。

到目前为止,这是我的代码:

 private static double[][] equation = new double[][]
 {
  new double[] {0.0, 8.0, 2.0, -7.0},   
  new double[] {3.0, 5.0, 2.0, 8.0},
  new double[] {6.0, 2.0, 8.0, 26.0}    
 };

 private const int rowSize = 3;
 private const int columnSize = 4;
 private static int pivotRow;
 private static int curentRowIndex;

 public static void GetTriangularMatrix()
 public static void GetTriangularMatrix()
    {
        PrintMatrix();

        for (int k = 0; k <= columnSize - 2; k++)
        {
            curentRowIndex = k;
            double pivot = GetPivot(k);
            SwapCurentRowWithPivotRow(pivot);
            FindTriangularMatrix();
        }
    }

    public static double GetPivot(int curentPivot)
    {
        double pivot = equation[curentPivot][curentPivot];
        pivotRow = curentPivot;

        for (int i = curentPivot; i <= rowSize - 1; i++)
        {
            if (pivot < equation[i][curentPivot])
            {
                pivot = equation[i][curentPivot];
                pivotRow = i;
            }
        }

        Console.WriteLine("Pivot Row is " + (pivotRow + 1));
        Console.WriteLine("Pivot is " + pivot);

        return pivot;
    }

    private static void SwapCurentRowWithPivotRow(double pivot)
    { 
        if(pivot != equation[curentRowIndex][curentRowIndex])
        {
            double temp;
            for (int i = 0; i <= columnSize - 1; i++)
            {
                temp = equation[curentRowIndex][i];
                equation[curentRowIndex][i] = equation[pivotRow][i];
                equation[pivotRow][i] = temp;
            }
        }

        PrintMatrix();
    }

    public static void FindTriangularMatrix()
    {
        double[][] trangularMatrix = new double[rowSize][];

        for (int j = curentRowIndex + 1; j <= rowSize - 1; j++)
        {
            trangularMatrix[j][curentRowIndex] = equation[j][curentRowIndex] / equation[curentRowIndex][curentRowIndex];
        }
    }

FindTriangularMatrix()方法中,当我尝试为我分配一些值时,triangularMatrix我得到了一个空引用异常。

我相信这更像是一个语法问题,它可能是我缺少的东西,因为我以前从未使用过二维数组。

谁能告诉我我做错了什么,我该如何纠正?

4

0 回答 0