Week 2 be done with beautiful docstrings

This commit is contained in:
2022-02-15 17:00:21 +01:00
parent a8d2a8d775
commit 74594c54f1
2 changed files with 53 additions and 34 deletions

View File

@ -137,8 +137,11 @@
}, },
"outputs": [], "outputs": [],
"source": [ "source": [
"# TODO: Write docstrings, ugh.\n",
"def DFT(yk):\n", "def DFT(yk):\n",
" \"\"\"\n",
" Return discrete fourier transform (DFT) of yk for N discrete frequency intervals.\n",
" \"\"\"\n",
" \n",
" N = len(yk)\n", " N = len(yk)\n",
" xk = 2*np.pi*np.arange(N)/N\n", " xk = 2*np.pi*np.arange(N)/N\n",
" beta = np.dot(yk, np.exp(np.outer(-np.arange(N), xk*1j)))\n", " beta = np.dot(yk, np.exp(np.outer(-np.arange(N), xk*1j)))\n",
@ -293,31 +296,31 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"M = 2 gives\n", "M = 2 gives\n",
"tOut = [0.0003551868721842766, 0.00026587769389152527, 0.0002683410421013832, 0.0002652248367667198, 0.00029958970844745636]\n", "tOut = [0.00035531632602214813, 0.00026446394622325897, 0.0002669980749487877, 0.000263310968875885, 0.0002625705674290657]\n",
"\n", "\n",
"M = 3 gives\n", "M = 3 gives\n",
"tOut = [0.00037261005491018295, 0.0003149900585412979, 0.0002723895013332367, 0.00024654995650053024, 0.000258052721619606]\n", "tOut = [0.0002719983458518982, 0.0002478230744600296, 0.0002496456727385521, 0.00025005731731653214, 0.0008301911875605583]\n",
"\n", "\n",
"M = 4 gives\n", "M = 4 gives\n",
"tOut = [0.00034819450229406357, 0.00033616088330745697, 0.0003195982426404953, 0.00034148991107940674, 0.00035313330590724945]\n", "tOut = [0.000346149317920208, 0.0003256509080529213, 0.00032440759241580963, 0.00031386781483888626, 0.0003223838284611702]\n",
"\n", "\n",
"M = 5 gives\n", "M = 5 gives\n",
"tOut = [0.0007255515083670616, 0.0005129771307110786, 0.0004988498985767365, 0.0004972768947482109, 0.0005336767062544823]\n", "tOut = [0.0007905261591076851, 0.0005042999982833862, 0.0004924368113279343, 0.0004901541396975517, 0.0005300091579556465]\n",
"\n", "\n",
"M = 6 gives\n", "M = 6 gives\n",
"tOut = [0.0025200899690389633, 0.0023925071582198143, 0.0023851143196225166, 0.0023938296362757683, 0.0023842724040150642]\n", "tOut = [0.002741251140832901, 0.002402886748313904, 0.0024533523246645927, 0.0024026362225413322, 0.002384801395237446]\n",
"\n", "\n",
"M = 7 gives\n", "M = 7 gives\n",
"tOut = [0.008896775543689728, 0.00880778580904007, 0.008781216107308865, 0.008825629949569702, 0.008792318403720856]\n", "tOut = [0.009145548567175865, 0.008851788938045502, 0.00881863571703434, 0.008819866925477982, 0.008821901865303516]\n",
"\n", "\n",
"M = 8 gives\n", "M = 8 gives\n",
"tOut = [0.03435589000582695, 0.033995422534644604, 0.034102585166692734, 0.03416615817695856, 0.03535666409879923]\n", "tOut = [0.03434724546968937, 0.03383493982255459, 0.03385954722762108, 0.03491049725562334, 0.03388229105621576]\n",
"\n", "\n",
"M = 9 gives\n", "M = 9 gives\n",
"tOut = [0.1477726474404335, 0.14786271005868912, 0.14967191498726606, 0.1493966607376933, 0.14906080160290003]\n", "tOut = [0.14674979075789452, 0.14636401552706957, 0.1461899448186159, 0.14625835418701172, 0.14628244005143642]\n",
"\n", "\n",
"M = 10 gives\n", "M = 10 gives\n",
"tOut = [0.5873688040301204, 0.5887598115950823, 0.5843716822564602, 0.5818474553525448, 0.5803691502660513]\n", "tOut = [0.5750202471390367, 0.5727888783439994, 0.5727971633896232, 0.5725235631689429, 0.5941787445917726]\n",
"\n" "\n"
] ]
} }
@ -416,9 +419,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"def FFT(yk):\n", "def FFT(yk):\n",
" # TODO: Write a docstring.\n",
" \"\"\"Don't forget to write a docstring ...\n",
" \"\"\"\n", " \"\"\"\n",
" Return the fast fourier transform (FFT) of array yk by considering odd\n",
" and even points and making use of discrete fourier transforms (DFTs).\n",
" \"\"\"\n",
" \n",
" N = len(yk)\n", " N = len(yk)\n",
"\n", "\n",
" # N should be a power of two\n", " # N should be a power of two\n",
@ -535,40 +540,40 @@
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"M = 2 gives\n", "M = 2 gives\n",
"tOutDFT = [0.00026126671582460403, 0.00021027959883213043, 0.0001835385337471962, 0.00023766234517097473, 0.00022200308740139008]\n", "tOutDFT = [0.00021187309175729752, 0.00018186774104833603, 0.0001812456175684929, 0.00018110498785972595, 0.00018095411360263824]\n",
"tOutFFT = [0.0006446670740842819, 0.0007413318380713463, 0.0006805472075939178, 0.0006410712376236916, 0.0006419029086828232]\n", "tOutFFT = [0.0005802353844046593, 0.0005557788535952568, 0.0006145015358924866, 0.0005566608160734177, 0.0005704071372747421]\n",
"\n", "\n",
"M = 3 gives\n", "M = 3 gives\n",
"tOutDFT = [0.00022369623184204102, 0.00022925715893507004, 0.0002404283732175827, 0.00021736416965723038, 0.0002731001004576683]\n", "tOutDFT = [0.00022258423268795013, 0.00020575150847434998, 0.0002390751615166664, 0.00020509026944637299, 0.00020475033670663834]\n",
"tOutFFT = [0.0013865511864423752, 0.0013830652460455894, 0.0013337815180420876, 0.0012838244438171387, 0.0012847669422626495]\n", "tOutFFT = [0.001284896396100521, 0.0013337591663002968, 0.0012675542384386063, 0.0013139024376869202, 0.0012842761352658272]\n",
"\n", "\n",
"M = 4 gives\n", "M = 4 gives\n",
"tOutDFT = [0.0003327140584588051, 0.0003035273402929306, 0.0003030272200703621, 0.00030216481536626816, 0.00030276738107204437]\n", "tOutDFT = [0.0003250986337661743, 0.00035554729402065277, 0.0003026863560080528, 0.00030192453414201736, 0.0003027757629752159]\n",
"tOutFFT = [0.002759365364909172, 0.002286495640873909, 0.0020805923268198967, 0.0020598340779542923, 0.0020735692232847214]\n", "tOutFFT = [0.00277732964605093, 0.002760577015578747, 0.0021515777334570885, 0.002049895003437996, 0.0028353100642561913]\n",
"\n", "\n",
"M = 5 gives\n", "M = 5 gives\n",
"tOutDFT = [0.0008888524025678635, 0.000489652156829834, 0.00048649683594703674, 0.00048589520156383514, 0.0004848325625061989]\n", "tOutDFT = [0.0008856169879436493, 0.0004905443638563156, 0.0004913564771413803, 0.0005037290975451469, 0.0004903236404061317]\n",
"tOutFFT = [0.004292245022952557, 0.00426078587770462, 0.004242740571498871, 0.005865932442247868, 0.006726261228322983]\n", "tOutFFT = [0.004227722063660622, 0.004218194633722305, 0.004212621599435806, 0.006331468001008034, 0.006671415641903877]\n",
"\n", "\n",
"M = 6 gives\n", "M = 6 gives\n",
"tOutDFT = [0.0036992961540818214, 0.002382858656346798, 0.002396935597062111, 0.002390684559941292, 0.002375013194978237]\n", "tOutDFT = [0.0037633972242474556, 0.002388077788054943, 0.0024485038593411446, 0.0023846719413995743, 0.0024057207629084587]\n",
"tOutFFT = [0.011373533867299557, 0.011346021667122841, 0.01132622268050909, 0.011333126574754715, 0.011340481229126453]\n", "tOutFFT = [0.01122717373073101, 0.011149754747748375, 0.011180805042386055, 0.01120598241686821, 0.011219387874007225]\n",
"\n", "\n",
"M = 7 gives\n", "M = 7 gives\n",
"tOutDFT = [0.009215572848916054, 0.008818225935101509, 0.008802506141364574, 0.008820069953799248, 0.00881765503436327]\n", "tOutDFT = [0.00920125376433134, 0.00882750190794468, 0.008815147913992405, 0.00881881546229124, 0.008816582150757313]\n",
"tOutFFT = [0.022970101796090603, 0.023467930033802986, 0.02285583410412073, 0.02295252773910761, 0.022949432022869587]\n", "tOutFFT = [0.022676337510347366, 0.023197690956294537, 0.02264594007283449, 0.022664004936814308, 0.022718658670783043]\n",
"\n", "\n",
"M = 8 gives\n", "M = 8 gives\n",
"tOutDFT = [0.03451558295637369, 0.033903918229043484, 0.03388609457761049, 0.033958752639591694, 0.033889370039105415]\n", "tOutDFT = [0.034523812122642994, 0.033904923126101494, 0.033997087739408016, 0.03390498273074627, 0.03390084486454725]\n",
"tOutFFT = [0.047724733129143715, 0.04677246883511543, 0.04511415120214224, 0.035544090904295444, 0.03561900556087494]\n", "tOutFFT = [0.046751600690186024, 0.04573535453528166, 0.04478597640991211, 0.03446220513433218, 0.034463477320969105]\n",
"\n", "\n",
"M = 9 gives\n", "M = 9 gives\n",
"tOutDFT = [0.12958059646189213, 0.13343970850110054, 0.13339493237435818, 0.1336062252521515, 0.13355298340320587]\n", "tOutDFT = [0.12952119670808315, 0.1336375381797552, 0.13360701967030764, 0.1335998559370637, 0.13358618132770061]\n",
"tOutFFT = [0.09326057601720095, 0.08018506038933992, 0.07027726992964745, 0.07104555331170559, 0.0710984542965889]\n", "tOutFFT = [0.09165043104439974, 0.07926731836050749, 0.06876837275922298, 0.06870093382894993, 0.06888525560498238]\n",
"\n", "\n",
"M = 10 gives\n", "M = 10 gives\n",
"tOutDFT = [0.5566168483346701, 0.5795272067189217, 0.5755985928699374, 0.5750947641208768, 0.5745749343186617]\n", "tOutDFT = [0.5503582386299968, 0.5721757095307112, 0.572254091501236, 0.572665935382247, 0.5729674575850368]\n",
"tOutFFT = [0.1753099588677287, 0.14073420222848654, 0.14395484700798988, 0.14109977800399065, 0.14102207031100988]\n", "tOutFFT = [0.17185171879827976, 0.1377342501655221, 0.13842287193983793, 0.1409124033525586, 0.13889379799365997]\n",
"\n" "\n"
] ]
} }

File diff suppressed because one or more lines are too long