英文:
Making the complex(a, b) command work with functions from the math module
问题
我一直在用Python构建数学工具,但没有使用numpy,我想知道是否必须使用numpy来执行Python中内置的复数的加法和减法之外的其他操作。
我尝试过将复数命令直接嵌入到我的函数中,例如:
import math
def f(a, b):
return math.cos(complex(a, b))
print(f(1, 1))
但它不会计算。
英文:
I have been building math tools in Python without numpy and am wondering if I have to use numpy to do more than add and subtract using the complex numbers built into Python.
I've tried just plugging the complex command into my functions, for example:
import math
def f(a, b):
return math.cos(complex(a, b))
print(f(1, 1))
and it won't calculate it.
答案1
得分: 1
cmath 模块提供了数学模块函数的复数变体:
import cmath
z = 3 + 4j
cmath.sqrt(z) # (2+1j)
cmath.cos(z) # (-27.034945603074224-3.8511533348117775j)
英文:
The cmath module provides complex number variants of the math module functions:
>>> import cmath
>>> z = 3 + 4j
>>> cmath.sqrt(z)
(2+1j)
>>> cmath.cos(z) (-27.034945603074224-3.8511533348117775j)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论