Để nhận được tích ngoài của hai mảng nhiều chiều, hãy sử dụng phương thức numpy.outer () trong Python. Tham số đầu tiên a là vectơ đầu vào đầu tiên. Đầu vào được làm phẳng nếu chưa phải là 1 chiều. Tham số thứ 2 b là vectơ đầu vào thứ hai. Đầu vào được làm phẳng nếu chưa phải là 1 chiều. Tham số thứ 3 là vị trí lưu trữ kết quả.
Cho hai vectơ, a =[a0, a1, ..., aM] và b =[b0, b1, ..., bN], tích ngoài [1] là -
[[a0*b0 a0*b1 ... a0*bN ] [a1*b0 . [ ... . [aM*b0 aM*bN ]]
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 hai mảng hai chiều không gian bằng phương thức array () -
arr1 = np.array([[5, 10], [15, 20]]) arr2 = np.array([[6, 12], [18, 24]])
Hiển thị các mảng -
print("Array1...\n",arr1)
print("\nArray2...\n",arr2) Kiểm tra Kích thước của cả hai mảng -
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim) Kiểm tra Hình dạng của cả hai mảng -
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape) Để nhận được tích ngoài của hai mảng nhiều chiều, hãy sử dụng phương thức numpy.outer () -
print("\nResult (Outer Product)...\n",np.outer(arr1, arr2))
Ví dụ
import numpy as np
# Creating two numpy Two-Dimensional array using the array() method
arr1 = np.array([[5, 10], [15, 20]])
arr2 = np.array([[6, 12], [18, 24]])
# Display the arrays
print("Array1...\n",arr1)
print("\nArray2...\n",arr2)
# Check the Dimensions of both the arrays
print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)
# Check the Shape of both the arrays
print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)
# To get the Outer product of two multi-dimensional arrays, use the numpy.outer() method in Python
print("\nResult (Outer Product)...\n",np.outer(arr1, arr2)) Đầu ra
Array1... [[ 5 10] [15 20]] Array2... [[ 6 12] [18 24]] Dimensions of Array1... 2 Dimensions of Array2... 2 Shape of Array1... (2, 2) Shape of Array2... (2, 2) Result (Outer Product)... [[ 30 60 90 120] [ 60 120 180 240] [ 90 180 270 360] [120 240 360 480]]