如何将 "arctg(5)+..." 等 Sympy 公式转换为列表中的浮点值?

How to convert Sympy formulas like "arctg(5)+..." to float values in list?

我有微分方程组。我必须做雅可比矩阵,并将其用于方法,通过使用 2/max(abs(eigenvalue)) = hst

找到稳定性步骤

我正在努力做到这样



x, y, z = symbols("x y z")


J = Function('J')(x, y, z)
#Setting the Jacobian matrix elements


f1_swpx_angle_ball_step1 = 'arctg(x)'  
print(f1_swpx_angle_ball_step1)
f1_swpy_angle_ball_step1 = 'y/(x*x+1)' 
print(f1_swpx_angle_ball_step1)
f1_swpz_angle_ball_step1 = '0'  
print(f1_swpx_angle_ball_step1)
f2_swpx_angle_ball_step1 = '-3*arctg(x)' 
print(f2_swpx_angle_ball_step1)
f2_swpy_angle_ball_step1 = '-3*(x+3.27)/(y*y+1)'  
print(f2_swpy_angle_ball_step1)
f2_swpz_angle_ball_step1 = '0'      
print(f2_swpy_angle_ball_step1)
f3_swpx_angle_ball_step1 = "1/(x*x+1)"       
print(f2_swpy_angle_ball_step1)
f3_swpy_angle_ball_step1 = "0"       
print(f2_swpy_angle_ball_step1)
f3_swpz_angle_ball_step1 = "(0)"       
print(f3_swpz_angle_ball_step1)


#Setting the Jacobian matrix elements
F3_switchball_angle_step1 = Matrix([[f1_swpx_angle_ball_step1, f1_swpy_angle_ball_step1, f1_swpz_angle_ball_step1],
                                    [f2_swpx_angle_ball_step1, f2_swpy_angle_ball_step1, f2_swpz_angle_ball_step1],
                                    [f3_swpx_angle_ball_step1, f3_swpy_angle_ball_step1, f3_swpz_angle_ball_step1]])

从打印中获取一些值

jaceiglist = list(JacobianF_falling_angle_ball_step1.eigenvals())
print(jaceiglist)

[-0.5*sqrt(1.0*arctg(5)**2 - 0.399230769230769*arctg(5) + 0.910556360946746) + 0.5*arctg(5) - 0.477115384615385, 0.5*sqrt(1.0*arctg(5)**2 - 0.399230769230769*arctg(5) + 0.910556360946746) + 0.5*arctg(5) - 0.477115384615385, 0]
#trying to make a method, that get differentiable variables, and substituting the values, will calculate the current Jacobi matrix and the stability step

def hstabilitygetting_angle_ball_step1(MatrixForYacobian, values):
    print("Matrix Shape ",MatrixForYacobian.shape)
    JacobianF_falling_angle_ball_step1 = Matrix(
    MatrixForYacobian.subs([(x, values[0]), (y, values[1]), (z, values[2])]))
    print("Jacobian Matrix shape after substitute", JacobianF_falling_angle_ball_step1.shape)

    jaceiglist = list(JacobianF_falling_angle_ball_step1.eigenvals())
    print("Eigenvalues",jaceiglist)
    return 2 / (reduce(lambda x, y: abs(x) if abs(x) > abs(y) else abs(y), jaceiglist))

我无法使此列表值浮动

JacobianF_falling_angle_ball_step1 = sympy.Matrix(F3_switchball_angle_step1.subs([(x,5),(y,5),(z,0)])) 
jaceiglist = list(JacobianF_falling_angle_ball_step1.eigenvals()) print(jaceiglist) eq=sympy.S(str(jaceiglist[0])) eq.subs(sympy.Function('arctg'), sympy.atan ) eq.subs(sympy.Function('sqrt'), math.sqrt) 
print((eq).evalf(),'Value') 
eq = ((eq).evalf()) 

得到

-0.5*(1.0*arctg(5)**2 - 0.399230769230769*arctg(5) + 0.910556360946746)**0.5 + 0.5*arctg(5) - 0.477115384615385

这是一个表达式,我需要让它浮动,获取 max(abs(values list),以获得稳定步骤。

print(float((eq).evalf()),'Value') TypeError: Cannot convert expression to float

当涉及 arctg 的表达式被简化时,创建了该名称的未知函数。您应该将它们替换为 atan:

>>> eq=S('-3*arctg(x)')
>>> eq.subs(Function('arctg'), atan)
-3*atan(x)

可能是这样的:

>>> jaceiglist = Tuple(*JacobianF_falling_angle_ball_step1.eigenvals())
...  .subs(Function('arctg'), atan)