使命令”Making the complex(a, b)”与来自math模块的函数配合使用。

huangapple go评论65阅读模式
英文:

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)

huangapple
  • 本文由 发表于 2023年5月15日 04:43:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249593.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定