简体   繁体   English

Sympy“无法将表达式转换为浮点数”错误

[英]“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? 为什么会这样呢?

x is a sympy.Symbol , so you can't use it with normal math library functions because they don't know about sympy . xsympy.Symbol ,因此您不能将其与普通的math库函数一起使用,因为它们不了解sympy Instead, use sympy functions like sympy.sqrt : 而是使用sympy函数,例如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.) (这引发了另一个异常, sympy抱怨它没有解决该问题的算法-如果您对此也有问题,则应该发布单独的问题。)

PS: as pointed out in a comment, the actual error you're getting is from a different expression. PS:正如评论中指出的那样,您得到的实际错误是来自其他表达式。 You'll need to fix this throughout. 您将需要在整个过程中进行修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM