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

📅  最后修改于: 2021-04-17 16:26:58             🧑  作者: 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]计算下一个矩阵,即B [] [] ,其尺寸为(N – 1)x(N – 1) ] [i] – mat [0] [j] * mat [i] [0]
    • 分配垫= 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 )