Skip to content

Instantly share code, notes, and snippets.

@ceptreee
Last active March 14, 2018 08:13
Show Gist options
  • Save ceptreee/1d84e4721609620b32ee9375a89ce147 to your computer and use it in GitHub Desktop.
Save ceptreee/1d84e4721609620b32ee9375a89ce147 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np \n",
"import matplotlib.pyplot as plt \n",
"\n",
"# 複素数の出力の調整\n",
"np.set_printoptions(formatter={'complexfloat':' {0.real:4.2f}{0.imag:+4.2f}i'.format})\n",
"\n",
"# 個数\n",
"N = 6\n",
"\n",
"# 入力\n",
"f = np.sin(np.arange(N))\n",
"\n",
"# 回転子\n",
"w = np.exp(- 2 * np.pi / N * 1j)"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.18+0.00i -0.28-3.00i 0.12-0.12i 0.13+0.00i 0.12+0.12i -0.28+3.00i]\n"
]
}
],
"source": [
"# 1 : for loop\n",
"F = np.zeros(N, dtype=complex)\n",
"\n",
"for k in range(N):\n",
" for n in range(N):\n",
" F[k] += f[n] * w **(n * k)\n",
"print(F)"
]
},
{
"cell_type": "code",
"execution_count": 46,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.18+0.00i -0.28-3.00i 0.12-0.12i 0.13+0.00i 0.12+0.12i -0.28+3.00i]\n"
]
}
],
"source": [
"# 2 : Matrix(1)\n",
"# 変換行列\n",
"W = np.zeros([N,N], dtype=complex)\n",
"\n",
"for n in range(N):\n",
" for k in range(N):\n",
" W[n, k] = w **( n * k)\n",
" \n",
"F = W @ f\n",
"print(F)"
]
},
{
"cell_type": "code",
"execution_count": 47,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.18+0.00i -0.28-3.00i 0.12-0.12i 0.13+0.00i 0.12+0.12i -0.28+3.00i]\n"
]
}
],
"source": [
"# 3 : Matrix(2)\n",
"k = np.tile(np.arange(N),[N,1])\n",
"n = k.T\n",
"W = w**(n*k)\n",
"\n",
"F = W @ f\n",
"print(F)"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[ 0.18+0.00i -0.28-3.00i 0.12-0.12i 0.13-0.00i 0.12+0.12i -0.28+3.00i]\n"
]
}
],
"source": [
"print(np.fft.fft(f))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment