三角関数#

三角比#

直角三角形は、内角\(\theta\)の値を固定するとすべて相似な形となるため、\(x/r\)\(y/r\)などの辺の長さ同士の比は一定になる。

Hide code cell source
import numpy as np
import matplotlib.pyplot as plt

base = [0, 0]
x = [2, 0]
r = [2, 1]

fig, ax = plt.subplots()
ax.plot([base[0], x[0]], [base[1], x[1]], label="x")
ax.plot([base[0], r[0]], [base[1], r[1]], label="r")
ax.plot([x[0], r[0]], [x[1], r[1]], label="y")
ax.legend()
fig.show()
../../../_images/6db378804057bbe31eda3c24300f16b01c0a9b22b71c33ff63c5ab5ef0e5f071.png

この値を三角比と呼び、角度\(\theta\)の値によって決まるものとなる。

\[\begin{split} \sin \theta = \frac{y}{r}\\ \cos \theta = \frac{x}{r}\\ \tan \theta = \frac{y}{x} = \frac{\sin \theta}{\cos \theta} \end{split}\]

三角関数#

三角比の定義では、直角三角形の内角となりうる\(0^\circ\)から\(90^\circ\)の間の値しかとれないが、それ以外の値もとれるように定義を拡張したものが三角関数である。

\(r=1\)の場合、\(\sin \theta = \frac{y}{1} = y, \cos \theta = \frac{x}{1} = x\)となり扱いやすい。これを利用して、半径1、中心が原点の円(単位円)の円周上の点、\(x\)軸の正の向きからの角度が\(\theta\)の点の\(y\)座標を\(\sin \theta\)\(x\)座標を\(\cos \theta\)と定義する。

Hide code cell source
theta = (np.arange(0, 360) * np.pi / 180)
y = np.sin(theta)
x = np.cos(theta)

# base
fig, ax = plt.subplots(figsize=[4, 4])
ax.plot(x, y)
ax.axhline(0, color="gray")
ax.axvline(0, color="gray")

# line
theta = (120 * np.pi / 180)
y = np.sin(theta)
x = np.cos(theta)
ax.plot([0, x], [0, y], color="darkorange")
ax.text(x - 0.1, y + 0.1, r"($\cos \theta, \sin \theta$)", color="darkorange", ha="center")

fig.show()
../../../_images/1c66a0f3be4018e4525a2f900f96ceb670344225880fb920103c511469295c7b.png
Hide code cell source
fig, ax = plt.subplots(figsize=[6,2])

x = np.linspace(-10, 10, 100)
ax.plot(x, np.sin(x), label=r"$\sin x$")
ax.plot(x, np.cos(x), label=r"$\cos x$")
ax.legend()
fig.show()
../../../_images/fa0c85e500169e4390a1bb726622b2615821b4b469f1c9fc5034a401cc2c2f66.png
Hide code cell source
fig, ax = plt.subplots(figsize=[6,2])

r = np.array([0, 0.5, 1, 1.5, 2])
r = (-r).tolist() + r.tolist()
ticks = [c * np.pi for c in r]

x = np.linspace(-6.5, 6.5, 100)
ax.plot(x, np.tan(x), label=r"$\tan x$")
ax.set(xticks=ticks)
ax.grid(True)
ax.legend()
fig.show()
../../../_images/8d0d51cb2c36f34bb33010c37317f7beb18892238cd3e21dcb4a19c7f57a7cd2.png

逆三角関数#

三角関数の逆関数のことを逆三角関数という。

\[\begin{split} \arcsin x = \sin^{-1} x\\ \arccos x = \cos^{-1} x\\ \arctan x = \tan^{-1} x\\ \end{split}\]
Hide code cell source
fig, ax = plt.subplots(figsize=[2,3])

x = np.linspace(-np.pi, np.pi, 100)
ax.plot(np.sin(x), x, label=r"$x = \sin y$")
ax.plot(np.cos(x), x, label=r"$x = \cos y$")
ax.legend()
fig.show()
../../../_images/0731718d920dce69ff9f68db94ac892e9d8510b8e3d3732fe8216936063cc07a.png

Note

内積やコサイン類似度との関係 三角比の定義式

\[\begin{split} \sin \theta = \frac{y}{r}\\ \cos \theta = \frac{x}{r}\\ \end{split}\]

の両辺に\(r\)を掛けると、

\[\begin{split} y = r \sin \theta \\ x = r \cos \theta \\ \end{split}\]

となる。

2つのベクトル\(a, b\)のなす角度を\(\theta\)とすると、内積\(\boldsymbol{a} \cdot \boldsymbol{b}\)

\[ \boldsymbol{a} \cdot \boldsymbol{b} = ||\boldsymbol{a}|| \ ||\boldsymbol{b}|| \cos \theta \]

と定義される。

Hide code cell source
a = np.array([4, 2])
b = np.array([1, 2])

fig, ax = plt.subplots()
ax.plot([0, a[0]], [0, a[1]], color="steelblue")
ax.plot([0, b[0]], [0, b[1]], color="steelblue")
ax.text(a[0] * 1.01, a[1] * 1.01, f"a={a}", color="steelblue")
ax.text(b[0] * 1.01, b[1] * 1.01, f"b={b}", color="steelblue")

b_ = ((np.linalg.norm(b) / np.linalg.norm(a)) * a).astype(int)
ax.plot([0, b_[0]], [0, b_[1]], color="darkorange")
ax.plot([b[0], b_[0]], [b[1], b_[1]], color="darkorange", linestyle=":")
ax.text(1.5, 1.5, "|b| sin θ", color="dimgray")

ax.text(b_[0] * 1.07, b_[1] * 0.95, f"b'={b_}", color="darkorange")
ax.text(*(b_ * 0.5), "|b| cos θ ?", color="dimgray")


fig.show()
../../../_images/71e82a87a2515d427d2ba9e9883215c6f7ce372f2679b4e6635ea1a2dae77053.png

bからaへ垂線をたらして射影したものの長さは\(||b|| \sin \theta\)となる。これは単位円における\(y = r \sin \theta\)に相当する(ノルム\(||b||\)はベクトルの長さのため)

同様に\(x = r \cos \theta\)から\(y = ||b|| \cos \theta\)となる

はずだが…?

cos_theta = (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))  # cos θ
b_length = np.linalg.norm(b) * cos_theta # |b| cos θ
print(b_length, np.linalg.norm(b_)) ## 一致しない
1.7888543819998315 2.23606797749979
cos_theta = (a @ b) / (np.linalg.norm(a) * np.linalg.norm(b))  # cos θ
theta = np.arccos(cos_theta)
b_length = np.linalg.norm(b) * np.sin(theta) # |b| sin θ
print(b_length, np.linalg.norm(b - b_)) ## 一致しない
1.3416407864998743 1.4142135623730951

三角関数の基本的性質#

性質1

\[\begin{split} \begin{aligned} & \sin (-x)=-\sin x\\ & \cos (-x)=\cos x \\ & \tan (-x)=-\tan x \end{aligned} \end{split}\]

一般に \(f(-x)=-f(x)\) となる関数を 奇関数\(f(-x)=f(x)\) となる関数を 偶関数 という。

\(\sin x, \tan x\) は奇関数, \(\cos x\) は偶関数である

性質2

\[ \sin ^2 x+\cos ^2 x=1 \]
\[ \sin^2 \theta = \left(\frac{y}{r}\right)^2 = \frac{y^2}{r^2} ,\quad \cos^2 \theta = \left(\frac{x}{r}\right)^2 = \frac{x^2}{r^2} \]
\[ \sin^2 \theta + \cos^2 \theta = \frac{y^2}{r^2} + \frac{x^2}{r^2} = \frac{x^2 + y^2}{r^2} \]

三平方の定理より\(x^2 + y^2 = r^2\)なので

\[ \sin^2 \theta + \cos^2 \theta = \frac{x^2 + y^2}{r^2} = 1 \]

性質3 (加法定理)

\[\begin{split} \begin{aligned} & \sin (x \pm y)=\sin x \cos y \pm \cos x \sin y \\ & \cos (x \pm y)=\cos x \cos y \mp \sin x \sin y \\ & \tan (x \pm y)=\frac{\tan x \pm \tan y}{1 \mp \tan x \tan y} \end{aligned} \end{split}\]