-2
import scipy as sp 
from scipy import integrate

from sympy.abc import x, i 
from scipy.integrate import dblquad
from sympy import *

eq = dblquad(lambda y, t: (5*exp(-y**2) - 5 * exp(-y**2) * exp(-10/(1+x**2))) * exp(i*x*t) * exp(-i*y*t) / (1 - exp(-10/1+t**2)), -oo,oo,-oo, oo)

print(eq)

I've changed all possible variables from other libraries to Sympy but type error is still appearing.

enter image description here

gremur
  • 1,645
  • 2
  • 7
  • 20
  • Could you also give the error message please ? – Peterrabbit Apr 05 '22 at 17:11
  • 1
    Why are you using `sympy` - symbolic code, with `scipy.integrate`.? `scipy` is a numeric library, built on `numpy`. It's inputs must be numeric arrays and functions that produce such. What does your `lambda `produce for sample `y` and `t` values? I can guess what the error is - something about can't get a float from an expression, but you really should show the full error. – hpaulj Apr 05 '22 at 17:23
  • Sure, i also think so, but i could`t find another way to solve it. – Sultanbek Jakhanov Apr 05 '22 at 17:52
  • Does this answer your question? [Can't convert expression to float](https://stackoverflow.com/questions/44263889/cant-convert-expression-to-float) – esqew Apr 05 '22 at 17:59
  • I need to solve integral, but it has letters which should be constant. – Sultanbek Jakhanov Apr 05 '22 at 18:02

1 Answers1

0

In an isympy session:

In [1]: from sympy.abc import x, i

In [2]: f=lambda y, t: (5*exp(-y**2) - 5 * exp(-y**2) * exp(-10/(1+x**2))) * exp
   ...: (i*x*t) * exp(-i*y*t) / (1 - exp(-10/1+t**2))

In [3]: f
Out[3]: <function __main__.<lambda>(y, t)>

dblquad is going to call this function with specific values of y and t - ones within the bounds. So let's test such a call:

In [4]: f(0,0)
Out[4]: 
                                      -10  
                                     ──────
                                      2    
                                     x  + 1
5.00022700995505 - 5.00022700995505⋅ℯ      

The result is a sympy expression, with symbol x, and sympy exp.

dblquad is a numeric integrator; it expects numbers, not expressions. So it "asks" for that float - but sympy says - " can't do that". That's the TypeError.

In [5]: float(f(0,0))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <cell line: 1>()
----> 1 float(f(0,0))

File /usr/local/lib/python3.8/dist-packages/sympy/core/expr.py:345, in Expr.__float__(self)
    343 if result.is_number and result.as_real_imag()[1]:
    344     raise TypeError("Cannot convert complex to float")
--> 345 raise TypeError("Cannot convert expression to float")

TypeError: Cannot convert expression to float

In short, you get the TypeError because you are trying to use a sympy expression in a numeric integrator.

hpaulj
  • 221,503
  • 14
  • 230
  • 353