Computer >> Máy Tính >  >> Lập trình >> Python

Tính toán định thức cho một chồng ma trận trong đại số tuyến tính bằng Python

Để tính định thức cho một chồng ma trận trong đại số tuyến tính, hãy sử dụng np.linalg.det () trong Python Numpy. Tham số đầu tiên, a là mảng đầu vào để tính toán các định thức. Phương thức trả về định thức của a.

Các bước

Đầu tiên, hãy nhập các thư viện được yêu cầu -

import numpy as np

Tạo một mảng -

arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]] ])

Hiển thị mảng -

print("Our Array...\n",arr)

Kiểm tra các thứ nguyên -

print("\nDimensions of our Array...\n",arr.ndim)

Lấy Datatype -

print("\nDatatype of our Array object...\n",arr.dtype)

Lấy hình dạng -

print("\nShape of our Array object...\n",arr.shape)

Để tính định thức cho một chồng ma trận trong đại số tuyến tính, hãy sử dụng np.linalg.det () trong Python -

print("\nResult (determinant)...\n",np.linalg.det(arr))

Ví dụ

import numpy as np

# Create an array
arr = np.array([ [[1, 2], [3, 4]], [[1, 2], [2, 1]], [[1, 3], [3, 1]]])

# 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 for a stack of matrices in linear algebra, use the np.linalg.det() in Python Numpy.
print("\nResult (determinant)...\n",np.linalg.det(arr))

Đầu ra

Our Array...
[[[1 2]
[3 4]]

[[1 2]
[2 1]]

[[1 3]
[3 1]]]

Dimensions of our Array...
3

Datatype of our Array object...
int64

Shape of our Array object...
(3, 2, 2)

Result (determinant)...
[-2. -3. -8.]