更改Python中复数参数的虚部


要返回复数参数的虚部,请在Python中使用numpy.imag()方法。该方法返回复数参数的虚数部分。如果val是实数,则使用val的类型作为输出类型。如果val包含复数元素,则返回类型为浮点数。第一个参数val是输入数组。我们还将使用array.img更新复数参数的虚部。

步骤

首先,导入所需的库:

import numpy as np

使用array()方法创建一个数组:

arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.j])

显示数组:

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

要返回复数参数的虚部,请在Python中使用numpy.imag()方法。该方法返回复数参数的虚数部分。如果val是实数,则使用val的类型作为输出类型。如果val包含复数元素,则返回类型为浮点数。第一个参数val是输入数组:

print("\nImaginary part...\n",np.imag(arr))

更改虚部:

arr.imag = 5
print("\nUpdated result...\n",arr)

示例

import numpy as np

# Create an array using the array() method
arr = np.array([36.+1.j , 27.+2.j , 68.+3.j , 23.+2.j])

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

# To return the imaginary part of the complex argument, use the numpy.imag() method in Python
print("\nImaginary part...\n",np.imag(arr))

# Change the imaginary part
arr.imag = 5
print("\nUpdated result...\n",arr)

输出

Our Array...
[36.+1.j 27.+2.j 68.+3.j 23.+2.j]

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(4,)

Imaginary part...
[1. 2. 3. 2.]

Updated result...
[36.+5.j 27.+5.j 68.+5.j 23.+5.j]

更新于:2022年2月28日

1K+ 次浏览

启动你的职业生涯

完成课程获得认证

开始学习
广告
© . All rights reserved.