更改Python中複數引數的實部


要返回複數引數的實部,請使用 numpy.real() 方法。此方法返回複數引數的實部。如果 val 為實數,則輸出使用 val 的型別。如果 val 包含複數元素,則返回型別為浮點數。第一個引數 val 是輸入陣列。我們還將使用 array.real 更改複數引數的實部。

步驟

首先,匯入所需的庫:

import numpy as np

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

arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.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("\nReal part...\n",np.real(arr))

更改實部:

arr.real = 5
print("\nUpdated result...\n",arr)

示例

import numpy as np

# Create an array using the array() method
arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.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("\nReal part...\n",np.real(arr))

# Change the real part
arr.real = 5
print("\nUpdated result...\n",arr)

輸出

Our Array...
[36.+1.j 27.+2.j 68.+3.j 23.+2.j]

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(4,)

Real part...
[36. 27. 68. 23.]

Updated result...
[5.+1.j 5.+2.j 5.+3.j 5.+2.j]

更新於:2022年2月25日

3K+ 次瀏覽

啟動您的職業生涯

完成課程獲得認證

開始學習
廣告
© . All rights reserved.