Python / Sympy三角方程的三角解

时间:2014-07-14 15:47:36

标签: python sympy complex-numbers symbolic-math equation-solving

1 个答案:

答案 0 :(得分:3)

更新:解决此类公式时,现在可以使用带有根或roots_cubic的关键字trig=True来获得下面给出的解决方案。

这个表单不是我所知道的任何关键字,但编写例程并不难:

>>> def cutrig(a,b,c,d):
...     a,b,c,d = [S(i) for i in (a,b,c,d)]
...     # x = t - b/3/a
...     p = (3*a*c-b**2)/3/a**2
...     q = (2*b**3-9*a*b*c+27*a**2*d)/(27*a**3)
...     D = 18*a*b*c*d-4*b**3*d+b**2*c**2-4*a*c**3-27*a**2*d**2
...     assert D > 0
...     rv = []
...     for k in range(3):
...         rv.append(2*sqrt(-p/3)*cos(acos(3*q/2/p*sqrt(-3/p))/3-k*2*pi/3))
...     return list(sorted([i - b/3/a for i in rv]))
...
>>> print filldedent(cutrig(-1,2,3,-2))

[-2*sqrt(13)*cos(-acos(8*sqrt(13)/169)/3 + pi/3)/3 + 2/3,
-2*sqrt(13)*sin(-acos(8*sqrt(13)/169)/3 + pi/6)/3 + 2/3, 2/3 +
2*sqrt(13)*cos(acos(8*sqrt(13)/169)/3)/3]
>>> 

与默认的解决方案相比:

>>> print filldedent(solve(-x**3+2*x**2+3*x-2))

[2/3 + (-1/2 - sqrt(3)*I/2)*(8/27 + sqrt(237)*I/9)**(1/3) +
13/(9*(-1/2 - sqrt(3)*I/2)*(8/27 + sqrt(237)*I/9)**(1/3)), 2/3 +
13/(9*(-1/2 + sqrt(3)*I/2)*(8/27 + sqrt(237)*I/9)**(1/3)) + (-1/2 +
sqrt(3)*I/2)*(8/27 + sqrt(237)*I/9)**(1/3), 2/3 + 13/(9*(8/27 +
sqrt(237)*I/9)**(1/3)) + (8/27 + sqrt(237)*I/9)**(1/3)]

每个的值都相同:

>>> sorted([w.n(2,chop=True) for w in solve(-x**3+2*x**2+3*x-2)])
[-1.3, 0.53, 2.8]
>>> sorted([w.n(2,chop=True) for w in cutrig(-1,2,3,-2)])
[-1.3, 0.53, 2.8]