📜  使用关键凝聚法计算矩阵的行列式

📅  最后修改于: 2021-09-03 03:47:08             🧑  作者: Mango

给定一个维度为N 的方阵mat[][] ,任务是使用主元凝聚法找到矩阵的行列式。

例子:

方法:想法是使用 Pivotal Condensation 方法来计算矩阵mat[][]的行列式。以下是所提出方法的详细说明:

在这种计算维度N × N行列式的方法中,方阵:

  • 首先,维度为N*N的矩阵A[][]被简化为维度为(N – 1)*(N – 1) 的矩阵B[][] ,使得:
  • 然后可以使用公式从矩阵B[][] 中找到A[][]的行列式值,
  • 现在进一步将矩阵简化为(N – 2)*(N – 2)并计算矩阵B[][]的行列式。
  • 并重复上述过程,直到矩阵变为维度2*2
  • 然后使用公式det(A) = ad-bc计算矩阵的2×2维矩阵的行列式 A[][]{{a, b}, {c, d}}

请按照以下步骤解决问题:

  • 初始化一个变量,比如D ,以存储矩阵的行列式。
  • N大于2 时进行迭代并检查以下情况:
    • 检查mat[0][0]是否为0 ,然后使用矩阵的属性将当前行与下一行交换,使得mat[i][0] > 0
    • 否则,如果没有找到满足mat[i][0] > 0 的行,则打印零。
    • 现在,将D乘以pow(1 / mat[0][0], N – 2)
    • 使用公式b[i – 1][j – 1] = mat[0][0 * mat[i ]计算维度为(N – 1) x (N – 1)的下一个矩阵B[][] ][i] – mat[0][j] * mat[i][0]
    • 分配mat = B
  • D乘以维度为2×2的矩阵mat[][]的行列式,即mat[0][0] * mat[1][1] – mat[0][j] * mat[i][0] ]。
  • 最后,打印存储在D 中的值。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to swap values
void swap(float& i, float& j)
{
    float temp = i;
    i = j;
    j = temp;
}
 
// Function to find the determinant
// of matrix M[][]
float determinantOfMatrix(
    vector > mat, int N)
{
    float mul = 1;
 
    // Iterate over N while N > 2
    while (N > 2) {
 
        // Store the reduced matrix
        // of dimension (N-1)x(N-1)
        float M[N - 1][N - 1];
 
        int next_index = 1;
 
        // Check if first element
        // of first row is zero
        while (mat[0][0] == 0) {
 
            if (mat[next_index][0] > 0) {
 
                // For swapping
                for (int k = 0; k < N; k++) {
                    swap(mat[0][k],
                         mat[next_index][k]);
                }
 
                // Update mul
                mul = mul * pow((-1),
                                (next_index));
            }
 
            else if (next_index == (N - 1))
                return 0;
            next_index++;
        }
 
        // Store the first element
        // of the matrix
        float p = mat[0][0];
 
        // Multiply the mul by
        // (1/p) to the power n-2
        mul = mul * pow(1 / p, N - 2);
 
        // Calculate the next matrix
        // of dimension (N-1) x (N-1)
        for (int i = 1; i < N; i++) {
            for (int j = 1; j < N; j++) {
 
                // Calculate each element of
                // the matrix from previous
                // matrix
                M[i - 1][j - 1] = mat[0][0]
                                      * mat[i][j]
                                  - mat[i][0]
                                        * mat[0][j];
            }
        }
 
        // Copy elements of the matrix
        // M into mat to use it in
        // next iteration
        for (int i = 0;
             i < (N - 1); i++) {
 
            for (int j = 0;
                 j < (N - 1); j++) {
 
                mat[i][j] = M[i][j];
            }
        }
 
        // Decrement N by one
        N--;
    }
 
    // Calculate the determinant
    // of reduced 2x2 matrix and
    // multiply it with factor mul
    float D = mul * (mat[0][0]
                         * mat[1][1]
                     - mat[0][1]
                           * mat[1][0]);
 
    // Print the determinant
    cout << D;
}
 
// Driver Code
int main()
{
    // Given matrix
    vector > mat = { { 1, 0, 2, -1 },
                                   { 3, 0, 0, 5 },
                                   { 2, 1, 4, -3 },
                                   { 1, 0, 5, 0 } };
 
    // Size of the matrix
    int N = mat.size();
 
    // Function Call
    determinantOfMatrix(mat, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
class GFG
{
 
// Function to find the determinant
// of matrix M[][]
static void determinantOfMatrix(int[][] mat, int N)
{
    int mul = 1;
 
    // Iterate over N while N > 2
    while (N > 2)
    {
 
        // Store the reduced matrix
        // of dimension (N-1)x(N-1)
        int [][]M = new int[N - 1][N - 1];
        int next_index = 1;
 
        // Check if first element
        // of first row is zero
        while (mat[0][0] == 0)
        {
            if (mat[next_index][0] > 0)
            {
 
                // For swapping
                for (int k = 0; k < N; k++)
                {
                    int temp = mat[0][k];
                    mat[0][k] = mat[next_index][k];
                    mat[next_index][k] = temp;
 
                }
 
                // Update mul
                mul = (int) (mul * Math.pow((-1),
                                (next_index)));
            }
            else if (next_index == (N - 1))
                return;
            next_index++;
        }
 
        // Store the first element
        // of the matrix
        int p = mat[0][0];
 
        // Multiply the mul by
        // (1/p) to the power n-2
        mul = (int) (mul * Math.pow(1 / p, N - 2));
 
        // Calculate the next matrix
        // of dimension (N-1) x (N-1)
        for (int i = 1; i < N; i++)
        {
            for (int j = 1; j < N; j++)
            {
 
                // Calculate each element of
                // the matrix from previous
                // matrix
                M[i - 1][j - 1] = mat[0][0]
                                      * mat[i][j]
                                  - mat[i][0]
                                        * mat[0][j];
            }
        }
 
        // Copy elements of the matrix
        // M into mat to use it in
        // next iteration
        for (int i = 0;
             i < (N - 1); i++)
        {
            for (int j = 0;
                 j < (N - 1); j++)
            {
                mat[i][j] = M[i][j];
            }
        }
 
        // Decrement N by one
        N--;
    }
 
    // Calculate the determinant
    // of reduced 2x2 matrix and
    // multiply it with factor mul
    int D = mul * (mat[0][0]
                         * mat[1][1]
                     - mat[0][1]
                           * mat[1][0]);
 
    // Print the determinant
    System.out.print(D);
}
 
// Driver Code
public static void main(String[] args)
{
   
    // Given matrix
    int[][] mat = { { 1, 0, 2, -1 },
                                   { 3, 0, 0, 5 },
                                   { 2, 1, 4, -3 },
                                   { 1, 0, 5, 0 } };
 
    // Size of the matrix
    int N = mat.length;
 
    // Function Call
    determinantOfMatrix(mat, N);
}
}
 
// This code is contributed by 29AjayKumar


Python3
# Pyhton 3 program for the above approach
 
# Function to find the determinant
# of matrix M[][]
def determinantOfMatrix(mat, N):
    mul = 1
 
    # Iterate over N while N > 2
    while (N > 2):
        # Store the reduced matrix
        # of dimension (N-1)x(N-1)
        M = [[0 for i in range(N-1)] for j in range(N-1)]
 
        next_index = 1
 
        # Check if first element
        # of first row is zero
        while (mat[0][0] == 0):
            if (mat[next_index][0] > 0):
                # For swapping
                for k in range(N):
                    temp = mat[0][k]
                    mat[0][k] = mat[next_index][k]
                    mat[next_index][k] = temp
 
                # Update mul
                mul = mul * pow((-1),(next_index))
 
            elif (next_index == (N - 1)):
                return 0;
            next_index += 1
 
        # Store the first element
        # of the matrix
        p = mat[0][0]
 
        # Multiply the mul by
        # (1/p) to the power n-2
        mul = mul * pow(1 / p, N - 2)
 
        # Calculate the next matrix
        # of dimension (N-1) x (N-1)
        for i in range(1,N):
            for j in range(1,N,1):
                # Calculate each element of
                # the matrix from previous
                # matrix
                M[i - 1][j - 1] = mat[0][0] * mat[i][j] - mat[i][0] * mat[0][j]
 
        # Copy elements of the matrix
        # M into mat to use it in
        # next iteration
        for i in range(N - 1):
            for j in range(N - 1):
                mat[i][j] = M[i][j]
 
        # Decrement N by one
        N -= 1
 
    # Calculate the determinant
    # of reduced 2x2 matrix and
    # multiply it with factor mul
    D = mul * (mat[0][0] * mat[1][1] - mat[0][1] * mat[1][0])
 
    # Print the determinant
    print(int(D))
 
# Driver Code
if __name__ == '__main__':
    # Given matrix
    mat = [[1, 0, 2, -1],[3, 0, 0, 5], [2, 1, 4, -3], [1, 0, 5, 0]]
 
    # Size of the matrix
    N = len(mat)
 
    # Function Call
    determinantOfMatrix(mat, N)
     
    # This code is contributed by bgangwar59.


C#
// C# program for the above approach
using System;
 
public class GFG
{
 
// Function to find the determinant
// of matrix [,]M
static void determinantOfMatrix(int[,] mat, int N)
{
    int mul = 1;
 
    // Iterate over N while N > 2
    while (N > 2)
    {
 
        // Store the reduced matrix
        // of dimension (N-1)x(N-1)
        int [,]M = new int[N - 1,N - 1];
        int next_index = 1;
 
        // Check if first element
        // of first row is zero
        while (mat[0,0] == 0)
        {
            if (mat[next_index,0] > 0)
            {
 
                // For swapping
                for (int k = 0; k < N; k++)
                {
                    int temp = mat[0,k];
                    mat[0,k] = mat[next_index,k];
                    mat[next_index,k] = temp;
 
                }
 
                // Update mul
                mul = (int) (mul * Math.Pow((-1),
                                (next_index)));
            }
            else if (next_index == (N - 1))
                return;
            next_index++;
        }
 
        // Store the first element
        // of the matrix
        int p = mat[0,0];
 
        // Multiply the mul by
        // (1/p) to the power n-2
        mul = (int) (mul * Math.Pow(1 / p, N - 2));
 
        // Calculate the next matrix
        // of dimension (N-1) x (N-1)
        for (int i = 1; i < N; i++)
        {
            for (int j = 1; j < N; j++)
            {
 
                // Calculate each element of
                // the matrix from previous
                // matrix
                M[i - 1,j - 1] = mat[0,0]
                                      * mat[i,j]
                                  - mat[i,0]
                                        * mat[0,j];
            }
        }
 
        // Copy elements of the matrix
        // M into mat to use it in
        // next iteration
        for (int i = 0;
             i < (N - 1); i++)
        {
            for (int j = 0;
                 j < (N - 1); j++)
            {
                mat[i,j] = M[i,j];
            }
        }
 
        // Decrement N by one
        N--;
    }
 
    // Calculate the determinant
    // of reduced 2x2 matrix and
    // multiply it with factor mul
    int D = mul * (mat[0,0]
                         * mat[1,1]
                     - mat[0,1]
                           * mat[1,0]);
 
    // Print the determinant
    Console.Write(D);
}
 
// Driver Code
public static void Main(String[] args)
{
   
    // Given matrix
    int[,] mat = { { 1, 0, 2, -1 },
                                   { 3, 0, 0, 5 },
                                   { 2, 1, 4, -3 },
                                   { 1, 0, 5, 0 } };
 
    // Size of the matrix
    int N = mat.GetLength(0);
 
    // Function Call
    determinantOfMatrix(mat, N);
}
}
 
// This code is contributed by Rajput-Ji


输出:
30

时间复杂度: O(N 3 )
辅助空间: O(N 2 )

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live