Python에서 NumPy를 사용하여 주어진 두 벡터의 외부 곱을 계산합니다.
두 벡터의 외적은 벡터 A의 각 요소와 벡터 B의 각 요소를 곱하여 얻은 행렬입니다. 벡터 a와 b의 외적은 a ⊗ b로 제공됩니다. 다음은 외적을 계산하는 수학 공식입니다.
a ⊗ b = [a[0] * b, a[1] * b, ..., a[m-1] * b]
어디,
a, b는 벡터입니다.
는 두 벡터의 요소별 곱셈을 나타냅니다.
외적의 출력은 i와 j가 행렬의 요소인 행렬이며, 여기서 i번째 행은 벡터 'a'의 i번째 요소와 벡터 'b'의 i번째 요소를 곱하여 얻은 벡터입니다.
Numpy를 사용하여 외부 제품 계산
Numpy에는 두 벡터의 외부 곱을 계산하기 위한 external()이라는 함수가 있습니다.
통사론
아래는 external() 함수의 구문입니다.
np.outer(array1, array2)
어디,
외부는 기능입니다.
array1과 array2는 입력 배열입니다.
예
다음 예에서는 external() 함수를 사용하여 두 개의 numpy 배열의 외부 곱을 계산하려고 합니다.
import numpy as np
a = np.array([34,23,90,34])
b = np.array([90,34,43,23])
print("The input arrays:",a,b)
outer_product = np.outer(a,b)
print("The Outer product of the given input arrays:",outer_product)
산출
The input arrays: [34 23 90 34] [90 34 43 23]
The Outer product of the given input arrays: [[3060 1156 1462 782]
[2070 782 989 529]
[8100 3060 3870 2070]
[3060 1156 1462 782]]
예
external() 함수를 사용하여 2D 배열의 외부 곱을 계산하는 또 다른 예를 살펴보겠습니다.
import numpy as np
a = np.array([[34,23],[90,34]])
b = np.array([[90,34],[43,23]])
print("The input arrays:",a,b)
outer_product = np.outer(a,b)
print("The Outer product of the given input arrays:",outer_product)
산출
다음은 두 배열의 외부 곱의 출력입니다.
The input arrays: [[34 23]
[90 34]] [[90 34]
[43 23]]
The Outer product of the given input arrays: [[3060 1156 1462 782]
[2070 782 989 529]
[8100 3060 3870 2070]
[3060 1156 1462 782]]
예
이제 3D 배열의 외적을 계산해 보겠습니다.
import numpy as np
a = np.array([[[34,23],[90,34]],[[12,5],[14,5]]])
b = np.array([[[90,34],[43,23]],[[1,22],[7,2]]])
print("The input arrays:",a,b)
outer_product = np.outer(a,b)
print("The Outer product of the given input arrays:",outer_product)
산출
The input arrays: [[[34 23]
[90 34]]
[[12 5]
[14 5]]] [[[90 34]
[43 23]]
[[ 1 22]
[ 7 2]]]
The Outer product of the given input arrays: [[3060 1156 1462 782 34 748 238 68]
[2070 782 989 529 23 506 161 46]
[8100 3060 3870 2070 90 1980 630 180]
[3060 1156 1462 782 34 748 238 68]
[1080 408 516 276 12 264 84 24]
[ 450 170 215 115 5 110 35 10]
[1260 476 602 322 14 308 98 28]
[ 450 170 215 115 5 110 35 10]]