计算矩阵行列式的下一步。

3

我正在编写一个方法,用于计算包含双精度浮点数的矩阵(这里是二维数组)的行列式。以下是我的代码:

/// <summary>
/// Checks to see if a matrix is square, and then computes its determinant
/// </summary>
/// <returns></returns>
public double Determinant()
{
    // Check to make sure the matrix is square.  Only square matrices 
    // have determinants.
    if (!this.isSquare())
    {
        throw new Exception("The matrix does not have a determinant");
    }

    // Check to see if the matrix has dimensions 1x1.  
    // The determinant of a 1x1 matrix is equal to the value stored at (0,0).
    if (this.NumberOfRows == 1)
    {
        return this.GetElement(0, 0);
    }

    double determinant = 0;

    // Loop through the top row of the matrix.
    for (int columnIndex = 0; columnIndex < this.NumberOfColumns; columnIndex++)
    {
        Matrix cofactor = new Matrix(this.NumberOfRows - 1, this.NumberOfColumns - 1);

        //fill cofactor
        //I dont Know what to do here?


        determinant += this.GetElement(1, columnIndex) * cofactor.Determinant();
    }

    return determinant;
}

我不确定在fill cofactor这一行应该填什么。
有人能否建议我在那里做些什么?基本上,从原矩阵中添加元素到余子式的最佳方法是什么,同时忽略出现在当前位置所在行或列中的元素?

Matrix.Determinant 不能满足你的需求,有什么原因吗?(http://msdn.microsoft.com/en-us/library/system.windows.media.matrix.determinant(v=vs.110).aspx) - Gjeltema
我们正在编写自己的库以最大化灵活性。 - tjahrenholz
我们编写了那个矩阵类,但它还没有行列式方法。 - tjahrenholz
@Gjeltema 使用 Matrix.Determinant 需要应用程序始终能够访问 Silverlight 命名空间,截至 .NET 4.5 版本。若要查看不可移植代码的示例,请将 DLL 加载到 Xamarin Scan 中。 - jth41
@Gjeltema等人:显然那不是那个Matrix类。 - Andrew Morton
显示剩余2条评论
1个回答

2

您只需要删除不需要的第一行(零行)和列。以下内容可能对您有用:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApplication1
{
    class Program
    {
        /// <summary>
        /// Helper to show array.
        /// </summary>
        /// <param name="mat"></param>
        static void ShowArray(double[,] mat)
        {
            int ubound = mat.GetUpperBound(0);

            for (int row = 0; row <= ubound; row++)
            {
                for (int col = 0; col <= ubound; col++)
                {
                    Console.Write(string.Format("{0,2} ", mat[col, row]));
                }
                Console.WriteLine();
            };

            Console.WriteLine();

        }

        /// <summary>
        /// Get an array without the zeroth row and without a specified column.
        /// </summary>
        /// <param name="mat">The square array to remove items from.</param>
        /// <param name="knockoutCol">The column to eliminate.</param>
        /// <returns>A square array of size one less than the input array.</returns>
        static double[,] SubMatrix(double[,] mat, int knockoutCol)
        {
            if (mat.GetUpperBound(0) != mat.GetUpperBound(1))
            {
                throw new ArgumentException("Array is not square.");
            }

            int ubound = mat.GetUpperBound(0);
            double[,] m = new double[ubound, ubound];

            int mCol = 0;
            int mRow = 0;

            for (int row = 1; row <= ubound; row++)
            {
                mCol = 0;
                for (int col = 0; col <= ubound; col++)
                {
                    if (col == knockoutCol)
                    {
                        continue;
                    }
                    else
                    {
                        m[mCol, mRow] = mat[col, row];
                        mCol += 1;
                    }
                }
                mRow += 1;

            };

            return m;
        }

        static void Main(string[] args)
        {
            int arraySize = 4;
            double[,] mat = new double[arraySize, arraySize];
            int ubound = mat.GetUpperBound(0);

            // Initialise array for inspection.
            for (int row = 0; row <= ubound; row++)
            {
                for (int col = 0; col <= ubound; col++)
                {
                    mat[col, row] = (arraySize * row) + col;
                }
            };

            ShowArray(mat);

            ShowArray(SubMatrix(mat, 0));
            ShowArray(SubMatrix(mat, 1));
            ShowArray(SubMatrix(mat, 2));

            Console.ReadLine();

        }
    }
}

输出:

 0  1  2  3
 4  5  6  7
 8  9 10 11
12 13 14 15

 5  6  7
 9 10 11
13 14 15

 4  6  7
 8 10 11
12 14 15

 4  5  7
 8  9 11
12 13 15

如果我一开始考虑得更仔细,可能会交换行和列。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接