Để tính nghịch đảo (inverse) của mảng 3D (tensor), dùng numpy.linalg.tensorinv(). Kết quả là nghịch đảo tensor dot: tensordot(tensorinv(a), a, ind) cho tensor "identity". Hình dạng kết quả: a.shape[ind:] + a.shape[:ind].
Tham số
- a (array_like): Tensor cần nghịch đảo. Hình dạng phải "vuông":
prod(a.shape[:ind]) == prod(a.shape[ind:]). - ind (int, tùy chọn): Số chỉ mục đầu tham gia tổng nghịch đảo. Mặc định 2.
Các bước thực hiện
Nhập thư viện, tạo mảng identity và reshape:
import numpy as np
from numpy.linalg import inv
# Tạo ma trận đơn vị 24x24
arr = np.eye(4*6)
# Reshape thành (24, 8, 3)
arr.shape = (24, 8, 3)
print("Mảng của chúng ta...\n", arr)
print("\nSố chiều...\n", arr.ndim)
print("\nKiểu dữ liệu...\n", arr.dtype)
print("\nHình dạng...\n", arr.shape)
# Tính nghịch đảo tensor với ind=1
print("\nKết quả...\n", np.linalg.tensorinv(arr, ind=1))
Ví dụ hoàn chỉnh
import numpy as np
from numpy.linalg import inv
arr = np.eye(4*6)
arr.shape = (24, 8, 3)
print("Mảng của chúng ta...\n", arr)
print("\nSố chiều...\n", arr.ndim)
print("\nKiểu dữ liệu...\n", arr.dtype)
print("\nHình dạng...\n", arr.shape)
print("\nKết quả...\n", np.linalg.tensorinv(arr, ind=1))
Kết quả đầu ra
Kết quả là tensor shape (8, 3, 24) - ma trận đơn vị block-wise. Xem output gốc để chi tiết đầy đủ.