Ejemplo n.º 1
0
        /// <summary>
        /// Computes the determinant of the matrix.
        /// </summary>
        /// <param name="matrix">The matrix.</param>
        /// <returns>The determinant of the matrix.</returns>
        /// <exception cref="System.ArgumentNullException">The matrix is null.</exception>
        /// <exception cref="System.ArgumentException">The matrix is not square.</exception>
        public static Double Determinant(this Matrix matrix)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException(nameof(matrix));
            }
            if (matrix.NumberOfRows != matrix.NumberOfColumns)
            {
                throw new ArgumentException(NumericsMessages.MatrixIsNotSquare, nameof(matrix));
            }

            if (matrix.NumberOfRows == 0)
            {
                return(0);
            }

            if (matrix.NumberOfRows == 1)
            {
                return(matrix[0, 0]);
            }

            if (matrix.NumberOfRows == 2)
            {
                return(matrix[0, 0] * matrix[1, 1] - matrix[0, 1] * matrix[1, 0]);
            }

            return(LUDecomposition.ComputeDeterminant(matrix));
        }