How makes all low values in the symbolic calculation become zero?

前端 未结 4 1766
长情又很酷
长情又很酷 2021-01-24 23:47

How can I make all low values in a SymPy expression zero? For example, my result is:

1.0*a1*cos(q1) - 6.12e-17*(a2*sin(q2) + a3*sin(q2 + q3) + a4*sin(q2 + q3 + q         


        
4条回答
  •  既然无缘
    2021-01-25 00:22

    A direct way to do this is to replace such numbers with 0. A naive eq.subs(small, 0) will fail because small that you enter is not likely to be exactly the same as the number. But eq.atoms(Float) will give you the set of such numbers:

    >>> eq.xreplace(dict([(n,0) for n in eq.atoms(Float) if abs(n) < 1e-12]))
    1.0*a1*cos(q1) + (1.0*a2*cos(q2) + 1.0*a3*cos(q2 + q3) + 1.0*a4*cos(q2 + q3 + q4))*cos(q1)
    

提交回复
热议问题