Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Python에서 두 벡터의 내적 반환

<시간/>

두 벡터의 내적을 반환하려면 Python에서 numpy.vdot() 메서드를 사용합니다. vdot(a, b) 함수는 복소수를 dot(a, b)와 다르게 처리합니다. 첫 번째 인수가 복소수이면 첫 번째 인수의 켤레 복소수가 내적 계산에 사용됩니다. vdot는 점과 다르게 다차원 배열을 처리합니다. 행렬 곱을 수행하지 않지만 먼저 1차원 벡터에 대한 입력 인수를 평면화합니다. 따라서 벡터에만 사용해야 합니다.

이 메서드는 및 b의 내적을 반환합니다. 및 b의 유형에 따라 int, float 또는 complex가 될 수 있습니다. 첫 번째 매개변수는 a입니다. 복소수인 경우 내적 계산 전에 복소수 켤레를 취합니다. b는 내적에 대한 두 번째 매개변수입니다.

단계

먼저 필요한 라이브러리를 가져옵니다 -

import numpy as np

array() 메서드를 사용하여 두 개의 numpy 1차원 배열 만들기 -

arr1 = np.array([2+3j,5+6j])
arr2 = np.array([9+10j,11+12j])

배열 표시 -

print("Array1...\n",arr1)
print("\nArray2...\n",arr2)

두 어레이의 차원을 확인하십시오 -

print("\nDimensions of Array1...\n",arr1.ndim)
print("\nDimensions of Array2...\n",arr2.ndim)

두 배열의 모양을 확인하십시오 -

print("\nShape of Array1...\n",arr1.shape)
print("\nShape of Array2...\n",arr2.shape)

두 벡터의 내적을 반환하려면 Python에서 numpy.vdot() 메서드를 사용하십시오 -

print("\nResult...\n",np.vdot(arr1, arr2))

예시

import numpy as np

# Creating two numpy One-Dimensional array using the array() method
arr1 = np.array([2+3j,5+6j])
arr2 = np.array([9+10j,11+12j])

# 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 return the dot product of two vectors, use the numpy.vdot() method in Python.
print("\nResult...\n",np.vdot(arr1, arr2))

출력

Array1...
[2.+3.j 5.+6.j]

Array2...
[ 9.+10.j 11.+12.j]

Dimensions of Array1...
1

Dimensions of Array2...
1

Shape of Array1...
(2,)

Shape of Array2...
(2,)

Result...
(175-13j)