返回Python中使用scimath对负输入值求幂的结果

返回Python中使用scimath对负输入值求幂的结果

要返回Python中使用scimath对输入值进行乘方的结果,请使用scimath.power()方法。返回x的p次方,即x**p的结果。如果x和p是标量,那么输出也是标量,否则返回一个数组。

如果x包含负值,则输出将转换为复数域。参数x是输入值。参数p是将x提高到的幂(s)。如果x包含多个值,p必须是标量,或者包含与x相同数量的值。在后一种情况下,结果是x[0]p[0],x[1]p[1],….

步骤

首先,导入所需的库 –

import numpy as np

使用array()方法创建一个numpy数组。数组元素还包括负值−

arr = np.array([2, -4, -8, 16, -32])

显示数组 –

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)

使用scimath.power()方法返回将输入值提高到的幂的结果 –

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

示例

import numpy as np

# Create a numpy array using the array() method
# The array elements also includes negative values
arr = np.array([2, -4, -8, 16, -32])

# 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 return the result of the power to which the input value is raised with scimath, use the scimath.power() method in Python
print("\nResult...\n",np.emath.power(arr, 2))

输出

Our Array...
[ 2 -4 -8 16 -32]

Dimensions of our Array...
1

Datatype of our Array object...
int64

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

Result...
[ 4.+0.j 16.-0.j 64.-0.j 256.+0.j 1024.-0.j]

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程