如何取消Sympy

问题描述 投票:0回答:0
然后我通过一些迭代的代码运行它们,添加衍生物,然后除以

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

,但是我试图通过更复杂的表达方式来使用更高的订单,而我发现Sympy并没有可靠地弄清楚如何弄清楚在每个阶段取消一个
order = 4
(即使我知道可以这样做)。我如何沿正确的方向哄骗它?

我发现

sin(th)
.trigsimp()
有时会产生较高频率的因素,例如
.simplify()
,然后

sin(2th)

无法弄清楚如何消除这些因素。但是,如果我根本不简化,或者只是尝试在末尾简化,那么在运行时和最终复杂性方面,Sympy确实会明显更糟。

	
在这种情况下进行简化似乎有效:
.cancel()
输出
sin(th)
	

python sympy
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.