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

Python에서 사분면을 올바르게 선택하는 x1/x2의 요소별 아크 탄젠트 계산

<시간/>

사분면은 arctan2(x1, x2)가 원점에서 끝나고 점(1,0)을 통과하는 광선과 원점에서 끝나고 점(x2, x1)을 통과하는 광선 사이의 부호 있는 각도(라디안)가 되도록 선택됩니다. ).

첫 번째 매개변수는 y 좌표입니다. 두 번째 매개변수는 x 좌표입니다. x1.shape !=x2.shape이면 공통 모양으로 브로드캐스트할 수 있어야 합니다. 이 메서드는 [-pi, pi] 범위의 각도 인라디안 배열을 반환합니다. x1과 x2가 모두 스칼라이면 이것은 스칼라입니다.

단계

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

import numpy as np

array() 메서드를 사용하여 배열 만들기. 이것은 서로 다른 사분면에 있는 4개의 점입니다 -

x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])

array1 표시 -

print("Array1 (x coordinates)...\n", x)

array2 표시 -

print("\nArray2 (y coordinates)...\n", y)

사분면을 올바르게 선택하여 x1/x2의 요소별 아크 탄젠트를 계산하려면 Python에서 numpy,arctan2() 메서드를 사용하십시오 -

print("\nResult...",np.arctan2(y, x) * 180 / np.pi)

예시

import numpy as np

# The quadrant is chosen so that arctan2(x1, x2) is the signed angle in radians between the ray
# ending at the origin and passing through the point (1,0), and the ray ending at the origin and
# passing through the point (x2, x1).

# Creating arrays using the array() method
# These are four points in different quadrants
x = np.array([-1, +1, +1, -1])
y = np.array([-1, -1, +1, +1])

# Display the array1
print("Array1 (x coordinates)...\n", x)

# Display the array2
print("\nArray2 (y coordinates)...\n", y)

# To compute element-wise arc tangent of x1/x2 choosing the quadrant correctly, use the numpy, arctan2() method in Python
print("\nResult...",np.arctan2(y, x) * 180 / np.pi)

출력

Array1 (x coordinates)...
[-1 1 1 -1]

Array2 (y coordinates)...
[-1 -1 1 1]

Result... [-135. -45. 45. 135.]