1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

SymPyで三角関数を操る

Posted at

はじめに

SymPyでは、三角関数を扱える。何も考えなくとも、.expand(trig=True)あるいはsimplify()を使えばよしなにしてくれる。一方で、「この変換をしたいんだけど」という、痒い所に手を届かせようと思ったときには使い方を工夫しなければならない。

使い方

倍角の公式

公式に用意されている方法で$\sin(2x), \cos(2x)$などは、.expand(trig=True)で展開できる(一般に$x$の整数倍で可能である)。なお、trigはデフォルトではFalseなのでTrueを指定しないと展開してくれない。

>>> sin(2*x).expand(trig=True)
2*sin(x)*cos(x)
>>> cos(2*x).expand(trig=True)
2*cos(x)**2 - 1

ここからは、パブリックな方法ではなく、simplify用の内部関数を直接見にいく。

$\cos(2x)=2\cos^2(x)-1$より、$\cos^2(x) = (\cos(2x)+1)/2$が得られる。

>>> from sympy.simplify.fu import TR7
>>> TR7(cos(x)**2)
cos(2*x)/2 + 1/2

sin(x)^2 + cos(x)^2 = 1

$\sin^2(x) + \cos^2(x) = 1$という関係式を使って、$\sin^n(x)$あるいは$\cos^n(x)$を変形する。

>>> from sympy.simplify.fu import TR5, TR6
>>> TR5(sin(x)**2)
1 - cos(x)**2
>>> TR6(cos(x)**2)
1 - sin(x)**2

なお、TR5, TR6にはそれぞれmaxpowというオプションがある。

積和の公式

>>> from sympy.simplify.fu import TR8
>>> TR8(sin(x)*cos(y))
sin(x - y)/2 + sin(x + y)/2
>>> TR8(sin(x)*sin(y))
cos(x - y)/2 - cos(x + y)/2
>>> TR8(cos(x)*cos(y))
cos(x - y)/2 + cos(x + y)/2

和積の公式

>>> from sympy.simplify.fu import TR9
>>> TR9(sin(x) + sin(y))
2*sin(x/2 + y/2)*cos(x/2 - y/2)
>>> TR9(cos(x) + cos(y))
2*cos(x/2 - y/2)*cos(x/2 + y/2)

加法定理

>>> from sympy.simplify.fu import TR10
>>> TR10(sin(x + y))
sin(x)*cos(y) + sin(y)*cos(x)
>>> TR10(cos(x + y))
-sin(x)*sin(y) + cos(x)*cos(y)

tanTR12を使う

>>> from sympy.simplify.fu import TR12
>>> TR12(tan(x + y))
(tan(x) + tan(y))/(-tan(x)*tan(y) + 1)

逆変換はそれぞれTR10i, TR12i

>>> from sympy.simplify.fu import TR10i
>>> TR10i(sin(x)*cos(y) + sin(y)*cos(x))
sin(x + y)
>>> TR10i(-sin(x)*sin(y) + cos(x)*cos(y))
cos(x + y)
>>> from sympy.simplify.fu import TR12i
>>> TR12i((tan(x) + tan(y))/(-tan(x)*tan(y) + 1))
tan(x + y)

モリーの法則

>>> from sympy.simplify.fu import TRmorrie
>>> TRmorrie(cos(pi/9)*cos(2*pi/9)*cos(3*pi/9)*cos(4*pi/9))
1/16
1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?