简体   繁体   English

Sympy:TypeError:无法将表达式转换为浮点型

[英]Sympy: TypeError: can't convert expression to float

I am trying to solve a simple system of equations in sympy but I'm getting the error: "TypeError: can't convert expression to float". 我正在尝试在sympy中解决一个简单的方程组系统,但出现错误:“ TypeError:无法将表达式转换为float”。

import sympy as sy
q0,q1,x,y = sy.symbols('q_0,q_1,x,y')
s = sy.Matrix([sy.Eq(x-sy.cos(q0)-sy.cos(q0+q1),0),
            sy.Eq(y-sy.sin(q0)-sy.sin(q0+q1),0)]);
sol = sy.solve(s.subs({x:2,y:0}),q0,q1)

The solution should be (0,0). 解应为(0,0)。 I am using sympy version: 0.7.6-git. 我正在使用sympy版本:0.7.6-git。

Here is a partial solution: 这是部分解决方案:

import sympy as sy

q0, q1, x, y = sy.symbols('q_0,q_1,x,y', real=True, positive=True)
cq0, cq1, sq0, sq1 = sy.symbols('cq0, cq1, sq0, sq1', real=True)
s = sy.Matrix([sy.Eq(x - sy.cos(q0) - sy.cos(q0 + q1), 0),
               sy.Eq(y - sy.sin(q0) - sy.sin(q0 + q1), 0)])
# Matrix([
# [x - cos(q_0) - cos(q_0 + q_1) == 0],
# [y - sin(q_0) - sin(q_0 + q_1) == 0]])

s2 = sy.expand_trig(s).subs({
    sy.cos(q0) : cq0
    , sy.cos(q1) : cq1
    , sy.sin(q0) : sy.sqrt(1-cq0**2)
    , sy.sin(q1) : sy.sqrt(1-cq1**2)})

solns = set(sy.solve(s2.subs({x: 2, y: 0}), cq0, cq1))
for soln in solns:
    print(soln)

yields 产量

(1, 1)

If you rewrite the matrix entries in terms of exp and don't check the answer, it works well: 如果您用exp重写矩阵项,并且不检查答案,那么效果很好:

>>> print(filldedent(
... solve(s.applyfunc(lambda x:x.rewrite(exp)).subs({x:2,y:0}), q0, q1, check=0)))

[(0, 0), (zoo, pi), (pi, 0), (-I*log(-sqrt(exp(zoo))), zoo),
(-I*log(sqrt(exp(zoo))), zoo)]

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

相关问题 TypeError:无法将表达式转换为使用Sympy浮动 - TypeError: can't convert expression to float with Sympy sympy TypeError:无法将表达式转换为浮点数 - sympy TypeError: can't convert expression to float Python:sympy TypeError:无法将表达式转换为浮点数 - Python : sympy TypeError: can't convert expression to float TypeError:无法将表达式转换为浮点数 - 定积分 sympy - TypeError: can't convert expression to float - definite integral sympy Sympy“无法将表达式转换为浮点数”错误 - “Can't convert expression to float” error with sympy 无法将表达式转换为浮点数 - 使用 sympy - Can't convert expression to float - using sympy 非线性方程求解Sympy Python用于液压 - 需要解决TypeError(“无法将表达式转换为浮点数”) - Non-linear equation solving Sympy Python for hydraulics - Need resolve TypeError(“can't convert expression to float”) TypeError:无法将表达式转换为浮点型 - TypeError: can't convert expression to float 类型错误:无法将表达式转换为浮点数,使用符号 x,多项式插值 - TypeError: can't convert expression to float, with symbolic x, polynomial interpolation sympy的plot_parametric说TypeError:无法将复数转换为浮点数 - sympy's plot_parametric says TypeError: can't convert complex to float
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM