C# 如何计算矩阵行列式?n*n或只是5*5

C# 如何计算矩阵行列式?n*n或只是5*5,c#,matrix,determinants,C#,Matrix,Determinants,各位。我需要找到矩阵n*n(或5*5)行列式。我有一个从Pascal翻译过来的函数,但是有索引超出范围异常。谁能帮帮我吗 这是我的密码: public static double DET(double[,] a, int n) { int i, j, k; double det = 0; for (i = 0; i < n - 1; i++) { for (j = i + 1; j <

各位。我需要找到矩阵
n*n
(或
5*5
)行列式。我有一个从Pascal翻译过来的函数,但是有
索引超出范围异常
。谁能帮帮我吗

这是我的密码:

public static double DET(double[,] a, int n)
    {
        int i, j, k;
        double det = 0;
        for (i = 0; i < n - 1; i++)
        {   
            for (j = i + 1; j < n + 1; j++)
            {
                det = a[j, i] / a[i, i];
                for (k = i; k < n; k++)
                    a[j, k] = a[j, k] - det * a[i, k]; // Here's exception
            }
        }
        det = 1;
        for (i = 0; i < n; i++)
            det = det * a[i, i];
            return det;
    }
公共静态双数据(双[,]a,int n)
{
int i,j,k;
双det=0;
对于(i=0;i
谢谢你的帮助。

for(j=i+1;jfor (j = i + 1; j < n + 1; j++)

最后一个J值将大于数组大小。因此,您必须重新检查数组大小以及所有索引是如何从pascal转换而来的。

既然可以下载工作的C代码,为什么还要费心翻译呢


对于较大的矩阵,您可能需要运行Bareiss算法来计算行列式:


我认为这种算法不好,至少对于5x5矩阵的计算是这样。即使我们纠正了这一点

对于(j=i+1;j 像这样

对于(j=i+1;j 然后编写完整的代码,如:

using System;

public class Matrix
{
    private int row_matrix; //number of rows for matrix
    private int column_matrix; //number of colums for matrix
    private double[,] matrix; //holds values of matrix itself

    //create r*c matrix and fill it with data passed to this constructor
    public Matrix(double[,] double_array)
    {
        matrix = double_array;
        row_matrix = matrix.GetLength(0);
        column_matrix = matrix.GetLength(1);
        Console.WriteLine("Contructor which sets matrix size {0}*{1} and fill it with initial data executed.", row_matrix, column_matrix);
    }

    //returns total number of rows
    public int countRows()
    {
        return row_matrix;
    }

    //returns total number of columns
    public int countColumns()
    {
        return column_matrix;
    }

    //returns value of an element for a given row and column of matrix
    public double readElement(int row, int column)
    {
        return matrix[row, column];
    }

    //sets value of an element for a given row and column of matrix
    public void setElement(double value, int row, int column)
    {
        matrix[row, column] = value;
    }

    public double deterMatrix()
    {
        double det = 0;
        double value = 0;
        int i, j, k;

        i = row_matrix;
        j = column_matrix;
        int n = i;

        if (i != j)
        {
            Console.WriteLine("determinant can be calculated only for sqaure matrix!");
            return det;
        }

        for (i = 0; i < n - 1; i++)
        {
            for (j = i + 1; j < n; j++)
            {
                det = (this.readElement(j, i) / this.readElement(i, i));

                for (k = i; k < n; k++)
                {
                    value = this.readElement(j, k) - det * this.readElement(i, k);

                    this.setElement(value, j, k);
                }
            }
        }
        det = 1;
        for (i = 0; i < n; i++)
            det = det * this.readElement(i, i);

        return det;
    }
}

internal class Program
{
    private static void Main(string[] args)
    {
        Matrix mat03 = new Matrix(new[,]
        {
            {1.0, 2.0, -1.0},
            {-2.0, -5.0, -1.0},
            {1.0, -1.0, -2.0},
        });

        Matrix mat04 = new Matrix(new[,]
        {
            {1.0, 2.0, 1.0, 3.0},
            {-2.0, -5.0, -2.0, 1.0},
            {1.0, -1.0, -3.0, 2.0},
            {4.0, -1.0, -3.0, 1.0},
        });

        Matrix mat05 = new Matrix(new[,]
        {
            {1.0, 2.0, 1.0, 2.0, 3.0},
            {2.0, 1.0, 2.0, 2.0, 1.0},
            {3.0, 1.0, 3.0, 1.0, 2.0},
            {1.0, 2.0, 4.0, 3.0, 2.0},
            {2.0, 2.0, 1.0, 2.0, 1.0},
        });

        double determinant = mat03.deterMatrix();
        Console.WriteLine("determinant is: {0}", determinant);

        determinant = mat04.deterMatrix();
        Console.WriteLine("determinant is: {0}", determinant);

        determinant = mat05.deterMatrix();
        Console.WriteLine("determinant is: {0}", determinant);
    }
}
使用系统;
公共类矩阵
{
private int row_matrix;//矩阵的行数
private int column_matrix;//矩阵的列数
私有双精度[,]矩阵;//保存矩阵本身的值
//创建r*c矩阵并用传递给此构造函数的数据填充它
公共矩阵(双[,]双_数组)
{
矩阵=双_数组;
row_matrix=matrix.GetLength(0);
列_矩阵=矩阵.GetLength(1);
WriteLine(“设置矩阵大小{0}*{1}并用执行的初始数据填充它的构造函数。”,行\矩阵,列\矩阵);
}
//返回总行数
公共整数countRows()
{
返回行_矩阵;
}
//返回总列数
公共int countColumns()
{
返回列_矩阵;
}
//返回给定矩阵行和列的元素值
公共双读元素(int行,int列)
{
返回矩阵[行,列];
}
//为矩阵的给定行和列设置元素的值
公共void setElement(双值、整行、整列)
{
矩阵[行,列]=值;
}
公共双矩阵()
{
双det=0;
双值=0;
int i,j,k;
i=行_矩阵;
j=列_矩阵;
int n=i;
如果(i!=j)
{
WriteLine(“行列式只能为sqaure矩阵计算!”);
返回数据;
}
对于(i=0;i
结果是: 行列式为:-8 行列式为:-142 行列式为:-NaN

NaN的出现是因为被零除(我调试过) 对于一些非常特定的输入,这可能是可行的,但在一般情况下,这不是一个好的算法

因此,它适用于3x3和4x4,但不适用于5x5


我写这封信给任何可能遇到这个问题的人,以避免在尝试实现或修复某个算法错误时浪费几个小时。

计算n*n行列式的工作解决方案如下所示:

using System;

internal class MatrixDecompositionProgram
{
    private static void Main(string[] args)
    {
        float[,] m = MatrixCreate(4, 4);
        m[0, 0] = 3.0f; m[0, 1] = 7.0f; m[0, 2] = 2.0f; m[0, 3] = 5.0f;
        m[1, 0] = 1.0f; m[1, 1] = 8.0f; m[1, 2] = 4.0f; m[1, 3] = 2.0f;
        m[2, 0] = 2.0f; m[2, 1] = 1.0f; m[2, 2] = 9.0f; m[2, 3] = 3.0f;
        m[3, 0] = 5.0f; m[3, 1] = 4.0f; m[3, 2] = 7.0f; m[3, 3] = 1.0f;

        int[] perm;
        int toggle;

        float[,] luMatrix = MatrixDecompose(m, out perm, out toggle);

        float[,] lower = ExtractLower(luMatrix);
        float[,] upper = ExtractUpper(luMatrix);

        float det = MatrixDeterminant(m);

        Console.WriteLine("Determinant of m computed via decomposition = " + det.ToString("F1"));
    }

    // --------------------------------------------------------------------------------------------------------------
    private static float[,] MatrixCreate(int rows, int cols)
    {
        // allocates/creates a matrix initialized to all 0.0. assume rows and cols > 0
        // do error checking here
        float[,] result = new float[rows, cols];
        return result;
    }

    // --------------------------------------------------------------------------------------------------------------
    private static float[,] MatrixDecompose(float[,] matrix, out int[] perm, out int toggle)
    {
        // Doolittle LUP decomposition with partial pivoting.
        // rerturns: result is L (with 1s on diagonal) and U; perm holds row permutations; toggle is +1 or -1 (even or odd)
        int rows = matrix.GetLength(0);
        int cols = matrix.GetLength(1);

        //Check if matrix is square
        if (rows != cols)
            throw new Exception("Attempt to MatrixDecompose a non-square mattrix");

        float[,] result = MatrixDuplicate(matrix); // make a copy of the input matrix

        perm = new int[rows]; // set up row permutation result
        for (int i = 0; i < rows; ++i) { perm[i] = i; } // i are rows counter

        toggle = 1; // toggle tracks row swaps. +1 -> even, -1 -> odd. used by MatrixDeterminant

        for (int j = 0; j < rows - 1; ++j) // each column, j is counter for coulmns
        {
            float colMax = Math.Abs(result[j, j]); // find largest value in col j
            int pRow = j;
            for (int i = j + 1; i < rows; ++i)
            {
                if (result[i, j] > colMax)
                {
                    colMax = result[i, j];
                    pRow = i;
                }
            }

            if (pRow != j) // if largest value not on pivot, swap rows
            {
                float[] rowPtr = new float[result.GetLength(1)];

                //in order to preserve value of j new variable k for counter is declared
                //rowPtr[] is a 1D array that contains all the elements on a single row of the matrix
                //there has to be a loop over the columns to transfer the values
                //from the 2D array to the 1D rowPtr array.
                //----tranfer 2D array to 1D array BEGIN

                for (int k = 0; k < result.GetLength(1); k++)
                {
                    rowPtr[k] = result[pRow, k];
                }

                for (int k = 0; k < result.GetLength(1); k++)
                {
                    result[pRow, k] = result[j, k];
                }

                for (int k = 0; k < result.GetLength(1); k++)
                {
                    result[j, k] = rowPtr[k];
                }

                //----tranfer 2D array to 1D array END

                int tmp = perm[pRow]; // and swap perm info
                perm[pRow] = perm[j];
                perm[j] = tmp;

                toggle = -toggle; // adjust the row-swap toggle
            }

            if (Math.Abs(result[j, j]) < 1.0E-20) // if diagonal after swap is zero . . .
                return null; // consider a throw

            for (int i = j + 1; i < rows; ++i)
            {
                result[i, j] /= result[j, j];
                for (int k = j + 1; k < rows; ++k)
                {
                    result[i, k] -= result[i, j] * result[j, k];
                }
            }
        } // main j column loop

        return result;
    } // MatrixDecompose

    // --------------------------------------------------------------------------------------------------------------
    private static float MatrixDeterminant(float[,] matrix)
    {
        int[] perm;
        int toggle;
        float[,] lum = MatrixDecompose(matrix, out perm, out toggle);
        if (lum == null)
            throw new Exception("Unable to compute MatrixDeterminant");
        float result = toggle;
        for (int i = 0; i < lum.GetLength(0); ++i)
            result *= lum[i, i];

        return result;
    }

    // --------------------------------------------------------------------------------------------------------------
    private static float[,] MatrixDuplicate(float[,] matrix)
    {
        // allocates/creates a duplicate of a matrix. assumes matrix is not null.
        float[,] result = MatrixCreate(matrix.GetLength(0), matrix.GetLength(1));
        for (int i = 0; i < matrix.GetLength(0); ++i) // copy the values
            for (int j = 0; j < matrix.GetLength(1); ++j)
                result[i, j] = matrix[i, j];
        return result;
    }

    // --------------------------------------------------------------------------------------------------------------
    private static float[,] ExtractLower(float[,] matrix)
    {
        // lower part of a Doolittle decomposition (1.0s on diagonal, 0.0s in upper)
        int rows = matrix.GetLength(0); int cols = matrix.GetLength(1);
        float[,] result = MatrixCreate(rows, cols);
        for (int i = 0; i < rows; ++i)
        {
            for (int j = 0; j < cols; ++j)
            {
                if (i == j)
                    result[i, j] = 1.0f;
                else if (i > j)
                    result[i, j] = matrix[i, j];
            }
        }
        return result;
    }

    // --------------------------------------------------------------------------------------------------------------
    private static float[,] ExtractUpper(float[,] matrix)
    {
        // upper part of a Doolittle decomposition (0.0s in the strictly lower part)
        int rows = matrix.GetLength(0); int cols = matrix.GetLength(1);
        float[,] result = MatrixCreate(rows, cols);
        for (int i = 0; i < rows; ++i)
        {
            for (int j = 0; j < cols; ++j)
            {
                if (i <= j)
                    result[i, j] = matrix[i, j];
            }
        }
        return result;
    }
}
使用系统;
内部类MatrixDecompositionProgram
{
私有静态void Main(字符串[]args)
{
float[,]m=MatrixCreate(4,4);
m[0,0]=3.0f;m[0,1]=7.0f;m[0,2]=2.0f;m[0,3]=5.0f;
m[1,0]=1.0f;m[1,1]=8.0f;m[1,2]=4.0f;m[1,3]=2.0f;
m[2,0]=2.0f;m[2,1]=1.0f;m[2,2]=9.0f;m[2,3]=3.0f;
m[3,0]=5.0f;m[3,1]=4.0f;m[3,2]=7.0f;m[3,3]=1.0f;
int[]perm;
int切换;
float[,]luMatrix=MatrixDecompose(m,out perm,out toggle);
浮动[,]下限=提取下限(luMatrix);
浮动[,]上限=提取上限(luMatrix);
浮点数=矩阵确定数(m);
Console.WriteLine(“通过分解计算的m行列式=“+det.ToString(“F1”)”);
}
// ----------------------------