pythonequationunsupportedoperation

Getting TypeError: unsupported operand type(s) for ** or pow(): 'builtin_function_or_method' and 'int' when solving an equation in Python


I have created an original equation for f(x,t) in Mathematica:

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]];

I have converted it into following code:

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)

In case #1 and #alternative, I keep getting the same error: unsupported operand type(s) for ** or pow(): 'builtin_function_or_method'

What could be the problem? have I missed something?


Solution

  • cmath.cos is a function. You are applying the pow function with a function as a base.

    >>> 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)