如何使符号计算中的所有低值都为零? [英] How makes all low values in the symbolic calculation become zero?

查看:33
本文介绍了如何使符号计算中的所有低值都为零?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使 SymPy 表达式中的所有低值都为零?比如我的结果是:

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)

并且我想将第二个学期(从 6.12e-17 开始)更改为零.

解决方案

一个直接的方法是用 0 替换这些数字.一个简单的 eq.subs(small, 0) 将会失败因为您输入的 small 不太可能与数字完全相同.但是 eq.atoms(Float) 会给你一组这样的数字:

<预><代码>>>>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)

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 + q4))*sin(q1) + 1.0*(a2*cos(q2) + a3*cos(q2 + q3) + a4*cos(q2 + q3 + q4))*cos(q1)

and I want to change second term (starting with 6.12e-17) to zero.

解决方案

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)

这篇关于如何使符号计算中的所有低值都为零?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆