在 Python 中返回复数参数的实部


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

步骤

首先,导入所需的库 -

import numpy as np

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

arr = np.array([56.+0.j , 27.+0.j , 68.+0.j , 23.+0.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("\nResult (Real part)...\n",np.real(arr))

示例

import numpy as np

# Create an array using the array() method
arr = np.array([56.+0.j , 27.+0.j , 68.+0.j , 23.+0.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("\nResult (Real part)...\n",np.real(arr))

输出

Our Array...
[56.+0.j 27.+0.j 68.+0.j 23.+0.j]

Dimensions of our Array...
1

Datatype of our Array object...
complex128

Shape of our Array...
(4,)

Result (Real part)...
[56. 27. 68. 23.]

更新于: 2022年3月1日

222 次查看

启动您的 职业生涯

通过完成课程获得认证

开始
广告

© . All rights reserved.