np.linalg.det:什么是 Numpy linalg det() 方法

要使用数值包 NumPy 在 Python 中计算行列式的值,请使用 np.linalg.det() 函数。 但是矩阵的行列式是什么:它是通过两个对角线元素(左对角线 – 右对角线)的乘积相减计算得出的。

np.linalg.det

np.linalg.det() 是一个 numpy 库函数,用于确定方阵的行列式。 np.linalg.det() 函数将数组作为参数并返回给定数组的行列式。

例如,如果我们有 2×2 的矩阵 [ [1, 2], [2, 4]]那么答案将是 (4*1)-(2*2) = 0。

行列式是线性代数中的一个工具值。 它是根据方阵的对角项计算得出的。 对于 2×2 矩阵,它只是左上项和右下项的乘积与其他两项的乘积相减。

句法

numpy.linalg.det(array)

np.linalg.det() 函数只接受一个参数作为参数,即数组名称。

返回值

np.linalg.det() 函数返回给定数组的行列式。 返回值将采用浮点数据类型。

编程示例

显示 linalg.det() 在 2×2 矩阵中的工作的程序:

# Importing numpy
import numpy as np

# This will create a 2D array of shape 2x2 with values 5 to 8
arr = np.arange(5, 9).reshape(2, 2)
print("The array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))

# Now we will print determinant using det() function
print("Determinant of the given array: ", np.linalg.det(arr))

# Verify with the manual caculation
detr = (5*8)-(7*6)
print("Determinant using manual method: ", detr)

输出

The array is:
 [[5 6]
 [7 8]]
Shape of the array is :  (2, 2)
Determinant of the given array:  -2.000000000000005
Determinant using manual method:  -2

解释:

在这个例子中,我们首先制作了一个 2×2 的形状数组,然后打印出来。 然后我们调用了 numpy.linalg.det() 函数来计算给定数组的行列式。

然后我们手动计算行列式。 我们可以看到这两个值几乎相同。

在 3×3 矩阵中显示 linalg.det() 工作的程序:

请参阅以下代码。

# Importing numpy
import numpy as np

# This will create a 2D array of shape 3x3 with values 1 to 9
arr = np.arange(1, 10).reshape(3, 3)
print("The array is:\n", arr)
print("Shape of the array is : ", np.shape(arr))

# Now we will print determinant using det() function
print("Determinant of the given array: ", np.linalg.det(arr))

# Verify with the manual caculation
detr = 1*(5*9 - 6*8) + 2*(4*9 - 6*7) - 3*(4*8 - 5*7)
print("Determinant using manual method: ", detr)

输出

The array is:
 [[1 2 3]
 [4 5 6]
 [7 8 9]]
Shape of the array is :  (3, 3)
Determinant of the given array:  0.0
Determinant using manual method:  -6

解释

在这个例子中,我们首先制作了一组 3×3 的形状,然后打印出来。 然后我们调用了 numpy.linalg.det() 函数来计算给定数组的行列式。 然后我们手动计算行列式。 我们可以看到这两个值几乎相同。

结论

换句话说,对于一个矩阵 [[w,x], [c,d]],行列式计算为“ad-bc”。 较大的方阵被认为是 2×2 矩阵的组合。 numpy.linalg.det() 函数计算输入矩阵的行列式。

而已。

相关文章

np.linalg.norm

np.linalg.inv

np.linalg.svd

帖子 np.linalg.det: What is Numpy linalg det() Method 首先出现在 AppDividend 上。

资讯来源:由0x资讯编译自APPDIVIDEND,版权归作者Ankit Lathiya所有,未经许可,不得转载
你可能还喜欢