首页 > 解决方案 > Python 函数从复指数中查找 sin(x) 和 cos(x) 的值

问题描述

我正在阅读 Stack-overflow 上不同 python 问题的答案。当我遇到一个答案时,在答案中,他使用函数 imag 和 real 来查找 sinx 和 cosx 的值,如下所示

>>> e = 2.718281828459045
>>> X = 0.1
>>> (e**(X*1j)).imag   # sin(X)
0.09983341664682815
>>> (e**(X*1j)).real   # cos(X)
0.9950041652780258

所以我对想象和真实的功能感到困惑。

标签: python

解决方案


numpy.real()函数返回复数参数的实部。

Syntax: numpy.real(arr)

Parameters :
arr : [array_like] Input array.

Return : [ndarray or scalar] The real component of the complex argument. If val is real, the type of val is used for the output. If val has complex elements, the returned type is float.

基本示例:

import numpy as np
   
arr = np.array([1 + 3j, 5 + 7j, 9 + 11j])       
gfg = arr.real       
print (gfg)

输出 :

[1. 5. 9.]

因此,在您的代码中,它试图找到值的虚部和实部。

这是您的代码的一般数学公式e^ix = cos(x)+isin(x)。实数是 Cosx,虚数是 sinx。


推荐阅读