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

前端 未结 4 1740
长情又很酷
长情又很酷 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

    What about more details? I guess you want replace part of a symbolic calculation string, Regular expression in Python could be helpful, you can code like this:

    In [1]: import re
    
    In [2]: s = '1.0*a1*cos(q1) - 6.12e-17*(a2*sin(q2) + a3*sin(q2 + q3) + ' \
       ...:     'a4*sin(q2 + q3 + q4))sin(q1) + 1.0(a2*cos(q2) + ' \
       ...:     'a3*cos(q2 + q3) + a4*cos(q2 + q3 + q4))*cos(q1)'
    
    In [3]: s = re.sub(r'[+-/*/]\s\S*e-[1-9]\d+\S*\s', '', s)
    
    In [4]: s
    Out[4]: '1.0*a1*cos(q1) + a3*sin(q2 + q3) + a4*sin(q2 + q3 + q4))sin(q1) + 1.0(a2*cos(q2) + a3*cos(q2 + q3) + a4*cos(q2 + q3 + q4))*cos(q1)'
    

    First argument of re.sub() function decide what you want to reduce, e-[1-9]\d+ represent a number lower than e-10 which you can modify, I hope it helps.

提交回复
热议问题