seVenVo1d
seVenVo1d

Reputation: 426

"Can't convert expression to float" error with sympy

I am trying to solve the equations like this,

from sympy.solvers import solve
from sympy import Symbol
import math

x = Symbol('x')

A, B = 1, 2

print(solve((x) + (A/math.sqrt(x**4)) - (B * math.exp(-x)), x))


Traceback (most recent call last):
  File "C:\Users\****\Desktop\Python Stuff\****\***.py", line 7, in <module>
    print(solve((x) + (A/math.sqrt(x**4)) - (B * math.exp(-x)), x))
  File "C:\Users\****\AppData\Local\Programs\Python\Python37\lib\site-packages\sympy\core\expr.py", line 280, in __float__
    raise TypeError("can't convert expression to float")
TypeError: can't convert expression to float

Why this is happening?

Upvotes: 2

Views: 4416

Answers (1)

Josh Karpel
Josh Karpel

Reputation: 2145

x is a sympy.Symbol, so you can't use it with normal math library functions because they don't know about sympy. Instead, use sympy functions like sympy.sqrt:

from sympy.solvers import solve
import sympy

x = sympy.Symbol('x')

A, B = 1, 2

print(solve((x) + (A / sympy.sqrt(x ** 4)) - (B * sympy.exp(-x)), x))

(This raises another exception, with sympy complaining that it doesn't have an algorithm to solve this problem -- if you have problems with that too, you should post separate question.)

PS: as pointed out in a comment, the actual error you're getting is from a different expression. You'll need to fix this throughout.

Upvotes: 3

Related Questions