Skip to content

Instantly share code, notes, and snippets.

@reouno
Created December 1, 2020 22:45
Show Gist options
  • Save reouno/5e1381b5749abc407fc5aa4dd151c7bb to your computer and use it in GitHub Desktop.
Save reouno/5e1381b5749abc407fc5aa4dd151c7bb to your computer and use it in GitHub Desktop.
ストラング線形代数イントロダクション 1.2 長さと内積 練習問題32 単位ベクトル同士の内積平均
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ストラング線形代数イントロダクション 1.2 長さと内積 問題32\n",
"\n",
"- 単位ベクトルを1個生成($\\mathbf{u}$)\n",
"- 単位ベクトルをランダムに30個生成($\\mathbf{U}$)\n",
"- $ |\\mathbf{u} \\cdot \\mathbf{U}| $の平均を求める"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 単位ベクトルを1個生成"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"単位ベクトル u =\n",
"[[ 0.87148942]\n",
" [ 0.47712807]\n",
" [-0.11337984]]\n",
"uの長さ =\n",
"0.9999999999999999\n"
]
}
],
"source": [
"v = np.random.randn(3, 1)\n",
"u = v / np.linalg.norm(v)\n",
"print('単位ベクトル u =')\n",
"print(u)\n",
"print('uの長さ =')\n",
"print(np.linalg.norm(u))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 単位ベクトルをランダムにN個生成"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vの最初の列ベクトル =\n",
"[-2.15380297 -0.15116081 -0.37320251]\n",
"Uの最初の列ベクトル =\n",
"[-0.98296999 -0.06898799 -0.17032517]\n"
]
}
],
"source": [
"# 個数\n",
"N = 30\n",
"\n",
"# 行列の各列が1つのベクトル\n",
"V = np.random.randn(3, N) # 3xN行列として\n",
"print('Vの最初の列ベクトル =')\n",
"print(V[:,0])\n",
"V_lengths = np.apply_along_axis(np.linalg.norm, 0, V) # 各列ベクトルの長さ\n",
"U = V / V_lengths\n",
"print('Uの最初の列ベクトル =')\n",
"print(U[:,0])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 内積の絶対値の平均を求める"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"各列ベクトルとの内積 =\n",
"[[-0.87025261 -0.93521217 -0.92616723 -0.20324186 -0.47228492 -0.18479246\n",
" -0.28899362 0.30278933 0.44196373 -0.01457608 0.94871511 0.16772202\n",
" 0.04550446 0.18094622 0.33200627 -0.25600423 -0.15252904 -0.44812875\n",
" -0.5997203 -0.14956317 -0.12887674 -0.69469828 -0.57043962 0.66246135\n",
" -0.78135679 -0.01099612 0.44186798 0.37187519 -0.253802 -0.97761432]]\n",
"内積の絶対値の平均 = 0.427170065350003\n"
]
}
],
"source": [
"# uとUの各列ベクトルとの内積\n",
"#uU = np.apply_along_axis(lambda x: u.dot(), 0, U)\n",
"uU = u.T.dot(U)\n",
"print('各列ベクトルとの内積 =')\n",
"print(uU)\n",
"print('内積の絶対値の平均 =', np.abs(uU).mean())"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.6366197723675814"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 本によれば、内積の絶対値の平均は以下の値になるはず\n",
"2 / np.pi"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment