更改Python中复数参数的实部


要返回复数参数的实部,请使用 numpy.real() 方法。此方法返回复数参数的实部。如果 val 为实数,则输出使用 val 的类型。如果 val 包含复数元素,则返回类型为浮点数。第一个参数 val 是输入数组。我们还将使用 array.real 更改复数参数的实部。

步骤

首先,导入所需的库:

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.real() 方法。此方法返回复数参数的实部。如果 val 为实数,则输出使用 val 的类型。如果 val 包含复数元素,则返回类型为浮点数。

第一个参数 val 是输入数组:

print("\nReal part...\n",np.real(arr))

更改实部:

arr.real = 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 real part of the complex argument, use the numpy.real() method in Python
print("\nReal part...\n",np.real(arr))

# Change the real part
arr.real = 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,)

Real part...
[36. 27. 68. 23.]

Updated result...
[5.+1.j 5.+2.j 5.+3.j 5.+2.j]

更新于:2022年2月25日

3K+ 次浏览

启动您的职业生涯

完成课程获得认证

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