使用 Python 中的 scimath 計算複數輸入的平方根


要計算輸入的平方根,請使用 Python Numpy 中的 emath.sqrt() 方法。該方法返回 x 的平方根。如果 x 是一個標量,那麼輸出也是,否則返回一個數組。引數 x 是輸入值。對於負輸入元素,返回一個複數值

步驟

首先,匯入所需的庫 −

import numpy as np

使用 complex() 方法明確指定複數輸入 −

a = complex(-9.0, 0.0)

顯示陣列 −

print("Display the complex value...\n",a)

要計算輸入的平方根,請使用 Python Numpy 中的 emath.sqrt() 方法 −

print("\nResult...\n",np.emath.sqrt(a))

例項

import numpy as np

# Explicity using complex() method
a = complex(-9.0, 0.0)

# Display the array
print("Display the complex value...\n",a)

# To compute the square root of input, use the emath.sqrt() method in Python Numpy
# The method returns the square root of x. If x was a scalar, so is out, otherwise an array is returned.
print("\nResult...\n",np.emath.sqrt(a))

輸出

Display the complex value...
(-9+0j)

Result...
3j

更新於: 2022 年 3 月 1 日

141 次瀏覽

開啟您的 職業 生涯

完成課程以獲得認證

開始
廣告