使用Python中的scimath计算复杂输入的平方根

使用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

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程