在 Python 中返回複數引數的實部
要返回複數引數的實部,請在 Python 中使用 numpy.real() 方法。該方法返回複數引數的實部。如果 val 是實數,則輸出使用 val 的型別。如果 val 包含複數元素,則返回型別為浮點數。第一個引數 val 是輸入陣列
步驟
首先,匯入所需的庫 -
import numpy as np
使用 array() 方法建立陣列 -
arr = np.array([56.+0.j , 27.+0.j , 68.+0.j , 23.+0.j])
顯示陣列 -
print("Our Array...\n",arr)
檢查維度 -
print("\nDimensions of our Array...\n",arr.ndim)
獲取資料型別 -
print("\nDatatype of our Array object...\n",arr.dtype)
獲取形狀 -
print("\nShape of our Array...\n",arr.shape)
要返回複數引數的實部,請在 Python 中使用 numpy.real() 方法。該方法返回複數引數的實部。如果 val 是實數,則輸出使用 val 的型別。如果 val 包含複數元素,則返回型別為浮點數。第一個引數 val 是輸入陣列 -
print("\nResult (Real part)...\n",np.real(arr))
示例
import numpy as np # Create an array using the array() method arr = np.array([56.+0.j , 27.+0.j , 68.+0.j , 23.+0.j]) # Display the array print("Our Array...\n",arr) # Check the Dimensions print("\nDimensions of our Array...\n",arr.ndim) # Get the Datatype print("\nDatatype of our Array object...\n",arr.dtype) # Get the Shape print("\nShape of our Array...\n",arr.shape) # To return the real part of the complex argument, use the numpy.real() method in Python print("\nResult (Real part)...\n",np.real(arr))
輸出
Our Array... [56.+0.j 27.+0.j 68.+0.j 23.+0.j] Dimensions of our Array... 1 Datatype of our Array object... complex128 Shape of our Array... (4,) Result (Real part)... [56. 27. 68. 23.]
廣告