python - 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
所以我对想象和真实的功能感到困惑。
解决方案
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。
推荐阅读
- c - 使用 valgrind 查找 C 内存泄漏会冻结程序
- antlr - 将 W3C 的 EBNF 转换为 ANTLR
- c# - SQLite 与 Dapper C# - Windows 窗体:使用 ComboBox Lisl 属性通过外键检索数据库数据
- python - Python3:删除具有相同坐标(x,y)的数组元素
- google-cloud-firestore - 如果字段存在于另一个集合中,如何仅针对文档查询具有 2 个相关集合的 FireStore 数据模型
- typescript - 将表达式从三元运算符转换为逻辑运算符
- python-3.x - 断言使用规范创建的模拟已被调用引发错误
- java - 弹性搜索弹簧启动
- java - 是否可以在抽象 Java 类中重载抽象方法,但在子类中只实现其中一种重载方法?
- javascript - 如何使用反应根据日期对对象内的数据进行排序?