在Python中返回複數引數的角度(以弧度為單位)


要返回複數引數的角度,請在Python中使用numpy.angle()方法。該方法返回複平面上從正實軸逆時針旋轉的角度,範圍為(-pi, pi],資料型別為numpy.float64。第一個引數z,一個複數或複數序列。第二個引數deg,如果為True,則返回角度的度數;如果為False(預設),則返回弧度。

步驟

首先,匯入所需的庫:

import numpy as np

使用array()方法建立一個數組:

arr = np.array([1.0, 1.0j, 1+1j])

顯示陣列:

print("Array...\n", arr)

獲取陣列的型別:

print("\nOur Array type...\n", arr.dtype)

獲取陣列的維度:

print("\nOur Array Dimension...\n",arr.ndim)

獲取陣列的形狀:

print("\nOur Array Shape...\n",arr.shape)

要在Python Numpy中返回複數引數的角度,請使用numpy.angle()方法。該方法返回複平面上從正實軸逆時針旋轉的角度,範圍為(-pi, pi],資料型別為numpy.float64:

print("\nResult (radians)...\n", np.angle(arr))

示例

import numpy as np

# Create an array using the array() method
arr = np.array([1.0, 1.0j, 1+1j])

# Display the array
print("Array...\n", arr)

# Get the type of the array
print("\nOur Array type...\n", arr.dtype)

# Get the dimensions of the Array
print("\nOur Array Dimension...\n",arr.ndim)

# Get the shape of the Array
print("\nOur Array Shape...\n",arr.shape)

# To return the angle of the complex argument, use the numpy.angle() method in Python Numpy
# The method returns the counterclockwise angle from the positive real axis on the complex plane in the range (-pi, pi], with dtype as numpy.float64.
print("\nResult (radians)...\n", np.angle(arr))

輸出

Array...
[1.+0.j 0.+1.j 1.+1.j]

Our Array type...
complex128

Our Array Dimension...
1

Our Array Shape...
(3,)

Result (radians)...
[0. 1.57079633 0.78539816]

更新於:2022年3月1日

115 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始學習
廣告