![](/img/trans.png)
[英]TypeError: unsupported operand type(s) for +=: 'builtin_function_or_method' and 'int'
[英]Getting TypeError: unsupported operand type(s) for ** or pow(): 'builtin_function_or_method' and 'int' when solving an equation in Python
我在 Mathematica 中为 f(x,t) 创建了一个原始方程:
uExact[x_, t_] := (1 + I*t^2)* Cos[2 * Pi *x];
f[x_, t_] :=
4 * Pi^2 * (1 + I * t^2) * Cos[2* Pi * x] + 2*I*t*Cos[2 Pi*x] -
I * g * log[(t^4 + 1) * Cos^2[2*Pi*x]];
我已将其转换为以下代码:
def u_exact(x, t):
return (1 + I*t**2)* cmath.cos(2 * cmath.pi *x)
#check!!!
def f(x,t):
#1return 4 * cmath.pi**2 * (1 + I * t**2) * cmath.cos(2* cmath.pi * x) + 2*I*t*cmath.cos(2 * cmath.pi*x) - I * g * cmath.log((t**4 + 1) * cmath.cos**2(2*cmath.pi*x))
#alternative
p1 = 4 * pow(cmath.pi,2) * (1 + I * pow(t,2)) * pow(cmath.cos,2)* (2*cmath.pi * x)
p2 = 2*I*t*cmath.cos(2 * cmath.pi*x)
p3 = - I * g * cmath.log(pow(t,4) + 1) * pow(cmath.cos,2) * (2*cmath.pi*x)
return (p1 + p2 + p3)
在 #1 和 #alternative 的情况下,我不断收到相同的错误:** 或 pow() 不支持的操作数类型:'builtin_function_or_method'
可能是什么问题呢? 我错过了什么吗?
cmath.cos是 function。 您正在应用以 function 为基础的战俘 function。
>>> pow(cmath.cos, 2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for ** or pow(): 'builtin_function_or_method' and 'int'
>>> pow(cmath.cos(1), 2)
(0.2919265817264289+0j)
在您的两种选择中,您都试图将电源操作应用于 function ( math.cos
):
cmath.cos**2
pow(cmath.cos,2)
cmath.cos
是 function,如果你想应用电源 function,你需要有一个浮点数,所以你需要调用value
cmath.cos(value)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.