Python/Sympy 三次方程的三角解法
我可以用Python/Sympy得到三角函数的符号解来解决三次方程吗?
http://en.wikipedia.org/wiki/Casus_irreducibilis http://en.wikipedia.org/wiki/Cubic_function#Trigonometric_.28and_hyperbolic.29_method
1 个回答
3
更新:下面提供的解决方案现在可以通过在求解这样的方程时使用关键字 trig=True
来实现,适用于 roots 或 roots_cubic。
这种形式没有我知道的任何关键字,但写一个程序来实现这个并不难:
>>> 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]