计算矩阵行列式的下一步

本文关键字:下一步 行列式 计算 | 更新日期: 2023-09-27 18:10:21

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

/// <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行应该做什么。

谁能建议我在那里应该做什么?基本上,从原始矩阵中添加元素到协因式,同时忽略那些出现在矩阵当前位置的行或列中的元素的最佳方法是什么?

计算矩阵行列式的下一步

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

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

如果我在开始的时候考虑的更仔细一些,我可能已经交换了行和列。