在 Python 中返回复数输入的平方
要返回数组输入的逐元素平方,请在 Python 中使用 numpy.square() 方法。该方法返回 x*x 的逐元素结果,其形状和数据类型与 x 相同。如果 x 是标量,则这是一个标量。第一个参数 x 是输入数据。第二个参数 out 是存储结果的位置。如果提供,它必须具有输入广播到的形状。如果未提供或为 None,则返回一个新分配的数组。元组(仅作为关键字参数可能)的长度必须等于输出的数量。
第三个参数 where,此条件在输入上广播。在条件为 True 的位置,out 数组将设置为 ufunc 结果。在其他地方,out 数组将保留其原始值。请注意,如果通过默认的 out=None 创建了一个未初始化的 out 数组,则其中条件为 False 的位置将保持未初始化状态。
步骤
首先,导入所需的库 -
import numpy as np
使用 array() 方法创建一个 numpy 数组。我们添加了复数类型的元素 -
arr = np.array([[3 + 4j, 5 + 7j], [2 + 6j, -2j]])
显示数组 -
print("Our Array...\n",arr)检查维度 -
print("\nDimensions of our Array...\n",arr.ndim)
获取数据类型 -
print("\nDatatype of our Array object...\n",arr.dtype)要返回数组输入的逐元素平方,请在 Python 中使用 numpy.square() 方法。该方法返回 x*x 的逐元素结果,其形状和数据类型与 x 相同。如果 x 是标量,则这是一个标量 -
print("\nResult...\n",np.square(arr))示例
import numpy as np
# Creating a numpy array using the array() method
# We have added elements of complex type
arr = np.array([[3 + 4j, 5 + 7j], [2 + 6j, -2j]])
# 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)
# To return the element-wise square of the array input, use the numpy.square() method in Python
print("\nResult...\n",np.square(arr))输出
Our Array... [[ 3.+4.j 5.+7.j] [ 2.+6.j -0.-2.j]] Dimensions of our Array... 2 Datatype of our Array object... complex128 Result... [[ -7.+24.j -24.+70.j] [-32.+24.j -4. +0.j]]
广告
数据结构
网络
关系数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP