如何引导SymPy可靠消去三角函数表达式中的sin(th)因子以完成迭代简化计算
如何引导SymPy可靠消去三角函数表达式中的sin(th)因子以完成迭代简化计算
我有这么一组表达式:
deque([-6*cos(th)**3 - 9*cos(th), (11*cos(th)**2 + 4)*sin(th), -6*sin(th)**2*cos(th), sin(th)**3])
接着我运行了一段迭代求导、相加再除以sin(th)的代码:
import sympy as sp th = sp.symbols('th') order = 4 for nu in range(order + 1, 2*order): # iterate order-1 more times to reach the constants q = 0 for mu in range(1, nu): # Terms come from the previous derivative, so there are nu - 1 of them here. p = exprs.popleft() term = q + sp.diff(p, th) exprs.append(sp.cancel(term/sp.sin(th))) q = p exprs.append(sp.cancel(q/sp.sin(th))) print(nu, exprs)
但输出的结果一团糟:
5 deque([18*cos(th)**2 + 9, (-22*sin(th)**2*cos(th) + 5*cos(th)**3 - 5*cos(th))/sin(th), 6*sin(th)**2 - cos(th)**2 + 4, -3*sin(th)*cos(th), sin(th)**2]) 6 deque([-36*cos(th), (22*sin(th)**4 - 19*sin(th)**2*cos(th)**2 + 14*sin(th)**2 - 5*cos(th)**4 + 5*cos(th)**2)/sin(th)**3, (-8*sin(th)**2*cos(th) + 5*cos(th)**3 - 5*cos(th))/sin(th)**2, (9*sin(th)**2 - 4*cos(th)**2 + 4)/sin(th), -cos(th), sin(th)]) 7 deque([36, (24*sin(th)**4*cos(th) + 39*sin(th)**2*cos(th)**3 - 24*sin(th)**2*cos(th) + 15*cos(th)**5 - 15*cos(th)**3)/sin(th)**5, (30*sin(th)**4 - 34*sin(th)**2*cos(th)**2 + 19*sin(th)**2 - 15*cos(th)**4 + 15*cos(th)**2)/sin(th)**4, (9*sin(th)**2*cos(th) + 9*cos(th)**3 - 9*cos(th))/sin(th)**3, (10*sin(th)**2 - 4*cos(th)**2 + 4)/sin(th)**2, 0, 1])
而我预期的结果应该是随着迭代步骤逐渐简化,最终得到一组常数,正确的输出示例是这样的:
5 deque([9*cos(2*th) + 18, -27*sin(2*th)/2, 7*sin(th)**2 + 3, -3*sin(2*th)/2, sin(th)**2]) 6 deque([-36*cos(th), 36*sin(th), -13*cos(th), 13*sin(th), -cos(th), sin(th)]) 7 deque([36, 0, 49, 0, 14, 0, 1])
我试过添加各种.simplify()调用,在order = 4时还能凑效,但面对更复杂的表达式和更高阶的情况就不行了。我发现SymPy总是不能可靠地消去每一步中的sin(th)因子——尽管我知道理论上是可以消掉的。
我也试过用.trigsimp()和.simplify(),但它们有时候会生成更高频的项(比如sin(2th)),然后.cancel()就没法消去低频的sin(th)了;可如果完全不做简化,或者只在最后一步简化,SymPy的运行时间会变得极长,最终结果的复杂度也高得离谱。
下面是更高阶的表达式队列,我需要一个能处理所有这些情况的解决方案,而不只是阶数4的简单例子:
- 阶数5:
deque([-24*cos(th)**4 - 72*cos(th)**2 - 9, (50*cos(th)**3 + 55*cos(th))*sin(th), (-35*cos(th)**2 - 10)*sin(th)**2, 10*sin(th)**3*cos(th), -sin(th)**4]) - 阶数6:
deque([-120*cos(th)**5 - 600*cos(th)**3 - 225*cos(th), (274*cos(th)**4 + 607*cos(th)**2 + 64)*sin(th), (-225*cos(th)**3 - 195*cos(th))*sin(th)**2, (85*cos(th)**2 + 20)*sin(th)**3, -15*sin(th)**4*cos(th), sin(th)**5]) - 阶数7:
deque([-720*cos(th)**6 - 5400*cos(th)**4 - 4050*cos(th)**2 - 225, (1764*cos(th)**5 + 6552*cos(th)**3 + 2079*cos(th))*sin(th), (-1624*cos(th)**4 - 2842*cos(th)**2 - 259)*sin(th)**2, (735*cos(th)**3 + 525*cos(th))*sin(th)**3, (-175*cos(th)**2 - 35)*sin(th)**4, 21*sin(th)**5*cos(th), -sin(th)**6]) - 阶数8:
deque([-5040*cos(th)**7 - 52920*cos(th)**5 - 66150*cos(th)**3 - 11025*cos(th), (13068*cos(th)**6 + 73188*cos(th)**4 + 46575*cos(th)**2 + 2304)*sin(th), (-13132*cos(th)**5 - 38626*cos(th)**3 - 10612*cos(th))*sin(th)**2, (6769*cos(th)**4 + 9772*cos(th)**2 + 784)*sin(th)**3, (-1960*cos(th)**3 - 1190*cos(th))*sin(th)**4, (322*cos(th)**2 + 56)*sin(th)**5, -28*sin(th)**6*cos(th), sin(th)**7])
备注:内容来源于stack exchange,提问作者Pavel Komarov
相关产品推荐
相关产品推荐

