📜  如何使用 NumPy 计算矩阵的行列式?

📅  最后修改于: 2022-05-13 01:55:30.829000             🧑  作者: Mango

如何使用 NumPy 计算矩阵的行列式?

可以从方阵计算出的特殊数字称为方阵的行列式。 Numpy 为我们提供了使用numpy.linalg.det()函数计算方阵行列式的功能。

句法:

numpy.linalg.det(array)

示例 1:使用2X2 Numpy 矩阵计算行列式 numpy.linalg.det()函数

Python3
# importing Numpy package
import numpy as np
  
# creating a 2X2 Numpy matrix
n_array = np.array([[50, 29], [30, 44]])
  
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
  
# calculating the determinant of matrix
det = np.linalg.det(n_array)
  
print("\nDeterminant of given 2X2 matrix:")
print(int(det))


Python3
# importing Numpy package
import numpy as np
  
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15],
                    [30, 44, 2],
                    [11, 45, 77]])
  
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
  
# calculating the determinant of matrix
det = np.linalg.det(n_array)
  
print("\nDeterminant of given 3X3 square matrix:")
print(int(det))


Python3
# importing Numpy package
import numpy as np
  
# creating a 5X5 Numpy matrix
n_array = np.array([[5, 2, 1, 4, 6],
                    [9, 4, 2, 5, 2],
                    [11, 5, 7, 3, 9],
                    [5, 6, 6, 7, 2],
                    [7, 5, 9, 3, 3]])
  
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
  
# calculating the determinant of matrix
det = np.linalg.det(n_array)
  
print("\nDeterminant of given 5X5 square matrix:")
print(int(det))


输出:

在上面的例子中,我们计算了 2X2 方阵的行列式。

示例 2:使用 numpy.linalg.det()函数计算3X3 Numpy 矩阵的行列式

Python3

# importing Numpy package
import numpy as np
  
# creating a 3X3 Numpy matrix
n_array = np.array([[55, 25, 15],
                    [30, 44, 2],
                    [11, 45, 77]])
  
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
  
# calculating the determinant of matrix
det = np.linalg.det(n_array)
  
print("\nDeterminant of given 3X3 square matrix:")
print(int(det))

输出:

在上面的例子中,我们计算了 3X3 方阵的行列式。

示例 3:使用 numpy.linalg.det()函数计算5X5 Numpy 矩阵的行列式

Python3

# importing Numpy package
import numpy as np
  
# creating a 5X5 Numpy matrix
n_array = np.array([[5, 2, 1, 4, 6],
                    [9, 4, 2, 5, 2],
                    [11, 5, 7, 3, 9],
                    [5, 6, 6, 7, 2],
                    [7, 5, 9, 3, 3]])
  
# Displaying the Matrix
print("Numpy Matrix is:")
print(n_array)
  
# calculating the determinant of matrix
det = np.linalg.det(n_array)
  
print("\nDeterminant of given 5X5 square matrix:")
print(int(det))

输出:

在上面的例子中,我们计算了 5X5 方阵的行列式。