Search code examples
pythonmatrixtypeerrorsympy

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


I have system of differential equations. I must do the Jacobian Matrix, and get it to the method, to find the stability step by using 2/max(abs(eigenvalue)) = hst

I am trying to make it like this



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]])

getting some values from print

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))

I can't make this list values to float

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()) 

getting

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

This is an expression, and I need to make it float, get max(abs(values list), to have stability step.

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

Solution

  • When expressions involving arctg were sympified, unknown functions of that name were created. You should replace them with atan:

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

    Maybe like this:

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