在 Python 线性代数中计算二维数组的行列式
要在线性代数中计算 2D 数组的行列式,请在 Python Numpy 中使用 np.linalg.det()。第一个参数 a 是要计算行列式的输入数组。此方法返回 a 的行列式。
步骤
首先,导入必要的库 -
import numpy as np
创建一个数组 -
arr = np.array([[ 5, 10], [12, 18]])
显示数组 -
print("Our Array...\n",arr)
检查维度 -
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 -
print("\nDatatype of our Array object...\n",arr.dtype)
获取形状 -
print("\nShape of our Array object...\n",arr.shape)
要在线性代数中计算 2D 数组的行列式,请在 Python 中使用 np.linalg.det() -
print("\nResult...\n",np.linalg.det(arr))
示例
import numpy as np # Create an array arr = np.array([[ 5, 10], [12, 18]]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array object...\n",arr.shape) # To compute the determinant of a 2D array in linear algebra, use the np.linalg.det() in Python Numpy. # The 1st parameter, a is the input array to compute determinants for. # The method returns the determinant of a. print("\nResult...\n",np.linalg.det(arr))
输出
Our Array... [[ 5 10] [12 18]] Dimensions of our Array... 2 Datatype of our Array object... int64 Shape of our Array object... (2, 2) Result... -30.000000000000014
广告