使用 Python 中的 emath 计算负数的平方根


要计算输入的平方根,请在 Python Numpy 中使用 scimath.sqrt() 方法。该方法返回 x 的平方根。如果 x 是标量,则输出也是标量,否则返回数组。参数 x 是输入值。对于负输入元素,将返回复数值。

步骤

首先,导入所需的库 -

import numpy as np

使用 array() 方法创建 NumPy 数组 -

arr = np.array([1, -4, -9, 16, -25, 36])

显示数组 -

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 object...\n",arr.shape)

要计算输入的平方根,请在 Python Numpy 中使用 scimath.sqrt() 方法。该方法返回 x 的平方根。如果 x 是标量,则输出也是标量,否则返回数组 -

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

示例

Open Compiler
import numpy as np # Creating a numpy array using the array() method arr = np.array([1, -4, -9, 16, -25, 36]) # 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 object...\n",arr.shape) # To compute the square root of input, use the scimath.sqrt() method in Python Numpy print("\nResult...\n",np.emath.sqrt(arr))

Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.

输出

Our Array...
[ 1 -4 -9 16 -25 36]

Dimensions of our Array...
1

Datatype of our Array object...
int64

Shape of our Array object...
(6,)

Result...
[1.+0.j 0.+2.j 0.+3.j 4.+0.j 0.+5.j 6.+0.j]

更新于: 2022-03-01

188 次浏览

开启你的 职业生涯

通过完成课程获得认证

开始学习
广告