From 9477dbe667f250ecd23f8fc0d56b942191526421 Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Thu, 25 Feb 2021 14:42:55 +0100 Subject: Stare semestry, niepoukladane --- Semestr 3/anm/pracownia1/prog/program.html | 16056 ++++++++++++++++++++++++++ Semestr 3/anm/pracownia1/prog/program.ipynb | 2042 ++++ Semestr 3/anm/pracownia1/prog/program.jl | 247 + 3 files changed, 18345 insertions(+) create mode 100644 Semestr 3/anm/pracownia1/prog/program.html create mode 100644 Semestr 3/anm/pracownia1/prog/program.ipynb create mode 100644 Semestr 3/anm/pracownia1/prog/program.jl (limited to 'Semestr 3/anm/pracownia1/prog') diff --git a/Semestr 3/anm/pracownia1/prog/program.html b/Semestr 3/anm/pracownia1/prog/program.html new file mode 100644 index 0000000..66e1c19 --- /dev/null +++ b/Semestr 3/anm/pracownia1/prog/program.html @@ -0,0 +1,16056 @@ + + + + + +program + + + + + + + + + + + + + + + + + + + + + + +
+ + + + +
+ + + + +
+ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + +
+ + + + + + + + + diff --git a/Semestr 3/anm/pracownia1/prog/program.ipynb b/Semestr 3/anm/pracownia1/prog/program.ipynb new file mode 100644 index 0000000..e5d7c6a --- /dev/null +++ b/Semestr 3/anm/pracownia1/prog/program.ipynb @@ -0,0 +1,2042 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 89, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "set_cordic_iterations (generic function with 1 method)" + ] + }, + "execution_count": 89, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "include(\"program.jl\")" + ] + }, + { + "cell_type": "code", + "execution_count": 90, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Plots.PlotlyBackend()" + ] + }, + "execution_count": 90, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "using Plots\n", + "using Random\n", + "using Distributions\n", + "\n", + "plotly()" + ] + }, + { + "cell_type": "code", + "execution_count": 91, + "metadata": {}, + "outputs": [], + "source": [ + "# Zadanie 10, Franiszek malinka, Kacper Solecki\n", + "\n", + "# instrukcja:\n", + "# Nasz program udostępnia funkcje \n", + "\n", + "# -> taylor_sin(a, b) - sinus liczby a+bi liczony za pomocą szeregu Taylora\n", + "# -> taylor_cos(a, b) - cosinus liczby a+bi liczony za pomocą szeregu Taylora\n", + "# -> taylor_sinh(x) - sinus hiperboliczny liczby x liczony za pomocą szeregu Taylora\n", + "# -> taylor_cosh(x) - cosinus hiperboliczny liczby x liczony za pomocą szeregu Taylora\n", + "# -> cordic_sin(x) - sinus (rzeczywistej) liczby x liczony za pomocą algorytmu Cordic\n", + "# -> cordic_cos(x) - cosinus (rzeczywistej) liczby x liczony za pomocą algorytmu Cordic" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.9092974268256817" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# porównianie na sin(2), cos(2)\n", + "sin(2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(0.9092974268256817, -0.0)" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "taylor_sin(2.0, 0.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "0.9092974280938506" + ] + }, + "execution_count": 94, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cordic_sin(2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-0.4161468365471424" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cos(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(-0.41614683654714246, -0.0)" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "taylor_cos(2.0, 0.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-0.4161468353122473" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cordic_cos(2.0)" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "-5991.431207677988 - 9240.89014825243im" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# porównianie na sin(10 + 10i)\n", + "sin(10 + 10im)" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(-5991.431207678, -9240.890148252452)" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "taylor_sin(10, 10)" + ] + }, + { + "cell_type": "code", + "execution_count": 73, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "rel_error (generic function with 1 method)" + ] + }, + "execution_count": 73, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "# w ten sposób liczony jest błąd względny zarówno dla liczb rzeczywistych jak i zespolonych\n", + "function rel_error(x, y)\n", + " if x == 0\n", + " return 0\n", + " end\n", + " return abs((x-y)/x)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "taylor_error_of_iterations (generic function with 1 method)" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function taylor_error_of_iterations(x)\n", + " set_taylor_iterations(x)\n", + " return rel_error(sin(100+100im), taylor_sin(100, 100)[1] + taylor_sin(100, 100)[2]*im)\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "cordic_error_of_iterations (generic function with 1 method)" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function cordic_error_of_iterations(x)\n", + " set_cordic_iterations(x)\n", + " return rel_error(sin(100), cordic_sin(100.0))\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "taylor_error_of_iterations2 (generic function with 1 method)" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function taylor_error_of_iterations2(x)\n", + " set_taylor_iterations(x)\n", + " return rel_error(sin(100), taylor_sin(100.0, 0.0)[1])\n", + "end" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "1:20" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "X = 1:20" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Przykładowe błędy w zależności od liczby iteracji:" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.plotly.v1+json": { + "data": [ + { + "colorbar": { + "title": "" + }, + "legendgroup": "y1", + "line": { + "color": "rgba(0, 154, 250, 1.000)", + "dash": "solid", + "shape": "linear", + "width": 1 + }, + "mode": "lines", + "name": "y1", + "showlegend": true, + "type": "scatter", + "x": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20 + ], + "xaxis": "x", + "y": [ + 0.9999999999947897, + 0.6604336394521374, + 0.02055234411557763, + 0.00022080130144359062, + 1.4713452359772989e-06, + 6.728428696183563e-09, + 2.238852117625799e-11, + 7.408349671662484e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14, + 2.066101872553143e-14 + ], + "yaxis": "y", + "zmax": null, + "zmin": null + } + ], + "layout": { + "annotations": [ + { + "font": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 20 + }, + "rotation": 0, + "showarrow": false, + "text": "Taylor relative error calculating sin(100+100i)", + "x": 0.5423611111111111, + "xanchor": "center", + "xref": "paper", + "y": 1, + "yanchor": "top", + "yref": "paper" + } + ], + "height": 400, + "legend": { + "bgcolor": "rgba(255, 255, 255, 1.000)", + "bordercolor": "rgba(0, 0, 0, 1.000)", + "borderwidth": 1, + "font": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tracegroupgap": 0, + "traceorder": "normal", + "x": 1, + "xanchor": "auto", + "y": 1, + "yanchor": "auto" + }, + "margin": { + "b": 20, + "l": 0, + "r": 0, + "t": 20 + }, + "paper_bgcolor": "rgba(255, 255, 255, 1.000)", + "plot_bgcolor": "rgba(255, 255, 255, 1.000)", + "showlegend": true, + "width": 600, + "xaxis": { + "anchor": "y", + "domain": [ + 0.09128390201224845, + 0.9934383202099738 + ], + "gridcolor": "rgba(0, 0, 0, 0.100)", + "gridwidth": 0.5, + "linecolor": "rgba(0, 0, 0, 1.000)", + "mirror": false, + "range": [ + 0.43000000000000005, + 20.57 + ], + "showgrid": true, + "showline": true, + "showticklabels": true, + "tickangle": 0, + "tickcolor": "rgb(0, 0, 0)", + "tickfont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tickmode": "array", + "ticks": "inside", + "ticktext": [ + "5", + "10", + "15", + "20" + ], + "tickvals": [ + 5, + 10, + 15, + 20 + ], + "title": "iterations", + "titlefont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 15 + }, + "type": "-", + "visible": true, + "zeroline": false, + "zerolinecolor": "rgba(0, 0, 0, 1.000)" + }, + "yaxis": { + "anchor": "x", + "domain": [ + 0.07581474190726165, + 0.9415463692038496 + ], + "gridcolor": "rgba(0, 0, 0, 0.100)", + "gridwidth": 0.5, + "linecolor": "rgba(0, 0, 0, 1.000)", + "mirror": false, + "range": [ + -0.029999999999822412, + 1.0299999999946328 + ], + "showgrid": true, + "showline": true, + "showticklabels": true, + "tickangle": 0, + "tickcolor": "rgb(0, 0, 0)", + "tickfont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tickmode": "array", + "ticks": "inside", + "ticktext": [ + "0.00", + "0.25", + "0.50", + "0.75", + "1.00" + ], + "tickvals": [ + 0, + 0.25, + 0.5, + 0.75, + 1 + ], + "title": "relative error", + "titlefont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 15 + }, + "type": "-", + "visible": true, + "zeroline": false, + "zerolinecolor": "rgba(0, 0, 0, 1.000)" + } + } + }, + "text/html": [ + "\n", + "\n", + " \n", + " Plots.jl\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " \n", + "\n", + " \n", + "\n" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "\n", + "plot(taylor_error_of_iterations, X, title=\"Taylor relative error calculating sin(100+100i)\", xguide = \"iterations\", yguide = \"relative error\")" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.plotly.v1+json": { + "data": [ + { + "colorbar": { + "title": "" + }, + "legendgroup": "y1", + "line": { + "color": "rgba(0, 154, 250, 1.000)", + "dash": "solid", + "shape": "linear", + "width": 1 + }, + "mode": "lines", + "name": "y1", + "showlegend": true, + "type": "scatter", + "x": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20 + ], + "xaxis": "x", + "y": [ + 0.04858006105965706, + 0.0006898745020126097, + 4.643725178799385e-06, + 1.8207599251359285e-08, + 4.6699841760941685e-11, + 8.485100007739351e-14, + 2.1925323017414343e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16, + 4.3850646034828687e-16 + ], + "yaxis": "y", + "zmax": null, + "zmin": null + } + ], + "layout": { + "annotations": [ + { + "font": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 20 + }, + "rotation": 0, + "showarrow": false, + "text": "Taylor relative error calculating sin(100)", + "x": 0.5423611111111111, + "xanchor": "center", + "xref": "paper", + "y": 1, + "yanchor": "top", + "yref": "paper" + } + ], + "height": 400, + "legend": { + "bgcolor": "rgba(255, 255, 255, 1.000)", + "bordercolor": "rgba(0, 0, 0, 1.000)", + "borderwidth": 1, + "font": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tracegroupgap": 0, + "traceorder": "normal", + "x": 1, + "xanchor": "auto", + "y": 1, + "yanchor": "auto" + }, + "margin": { + "b": 20, + "l": 0, + "r": 0, + "t": 20 + }, + "paper_bgcolor": "rgba(255, 255, 255, 1.000)", + "plot_bgcolor": "rgba(255, 255, 255, 1.000)", + "showlegend": true, + "width": 600, + "xaxis": { + "anchor": "y", + "domain": [ + 0.09128390201224845, + 0.9934383202099738 + ], + "gridcolor": "rgba(0, 0, 0, 0.100)", + "gridwidth": 0.5, + "linecolor": "rgba(0, 0, 0, 1.000)", + "mirror": false, + "range": [ + 0.43000000000000005, + 20.57 + ], + "showgrid": true, + "showline": true, + "showticklabels": true, + "tickangle": 0, + "tickcolor": "rgb(0, 0, 0)", + "tickfont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tickmode": "array", + "ticks": "inside", + "ticktext": [ + "5", + "10", + "15", + "20" + ], + "tickvals": [ + 5, + 10, + 15, + 20 + ], + "title": "iterations", + "titlefont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 15 + }, + "type": "-", + "visible": true, + "zeroline": false, + "zerolinecolor": "rgba(0, 0, 0, 1.000)" + }, + "yaxis": { + "anchor": "x", + "domain": [ + 0.07581474190726165, + 0.9415463692038496 + ], + "gridcolor": "rgba(0, 0, 0, 0.100)", + "gridwidth": 0.5, + "linecolor": "rgba(0, 0, 0, 1.000)", + "mirror": false, + "range": [ + -0.0014574018317894857, + 0.05003746289144676 + ], + "showgrid": true, + "showline": true, + "showticklabels": true, + "tickangle": 0, + "tickcolor": "rgb(0, 0, 0)", + "tickfont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tickmode": "array", + "ticks": "inside", + "ticktext": [ + "0.00", + "0.01", + "0.02", + "0.03", + "0.04", + "0.05" + ], + "tickvals": [ + 0, + 0.01, + 0.02, + 0.03, + 0.04, + 0.05 + ], + "title": "relative error", + "titlefont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 15 + }, + "type": "-", + "visible": true, + "zeroline": false, + "zerolinecolor": "rgba(0, 0, 0, 1.000)" + } + } + }, + "text/html": [ + "\n", + "\n", + " \n", + " Plots.jl\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " \n", + "\n", + " \n", + "\n" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot(taylor_error_of_iterations2, X, title=\"Taylor relative error calculating sin(100)\", xguide = \"iterations\", yguide = \"relative error\")" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": {}, + "outputs": [ + { + "data": { + "application/vnd.plotly.v1+json": { + "data": [ + { + "colorbar": { + "title": "" + }, + "legendgroup": "y1", + "line": { + "color": "rgba(0, 154, 250, 1.000)", + "dash": "solid", + "shape": "linear", + "width": 1 + }, + "mode": "lines", + "name": "y1", + "showlegend": true, + "type": "scatter", + "x": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20 + ], + "xaxis": "x", + "y": [ + 0.19923803206067645, + 0.40038098396966176, + 0.04933327667366985, + 0.15678575968442748, + 0.04552832522388985, + 0.008453488065297705, + 0.018071367045314653, + 0.004685838026992587, + 0.0019769599175434157, + 0.001346845796143942, + 0.0003169671004323271, + 0.0005144620678263349, + 9.862885340066103e-05, + 0.00010925740652706494, + 5.322553095504949e-06, + 4.664671284967571e-05, + 2.066207987708538e-05, + 7.669763390790217e-06, + 1.1736051476426335e-06, + 2.074473973931158e-06 + ], + "yaxis": "y", + "zmax": null, + "zmin": null + } + ], + "layout": { + "annotations": [ + { + "font": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 20 + }, + "rotation": 0, + "showarrow": false, + "text": "Cordic relative error calculating sin(100)", + "x": 0.5349537037037038, + "xanchor": "center", + "xref": "paper", + "y": 1, + "yanchor": "top", + "yref": "paper" + } + ], + "height": 400, + "legend": { + "bgcolor": "rgba(255, 255, 255, 1.000)", + "bordercolor": "rgba(0, 0, 0, 1.000)", + "borderwidth": 1, + "font": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tracegroupgap": 0, + "traceorder": "normal", + "x": 1, + "xanchor": "auto", + "y": 1, + "yanchor": "auto" + }, + "margin": { + "b": 20, + "l": 0, + "r": 0, + "t": 20 + }, + "paper_bgcolor": "rgba(255, 255, 255, 1.000)", + "plot_bgcolor": "rgba(255, 255, 255, 1.000)", + "showlegend": true, + "width": 600, + "xaxis": { + "anchor": "y", + "domain": [ + 0.07646908719743364, + 0.9934383202099737 + ], + "gridcolor": "rgba(0, 0, 0, 0.100)", + "gridwidth": 0.5, + "linecolor": "rgba(0, 0, 0, 1.000)", + "mirror": false, + "range": [ + 0.43000000000000005, + 20.57 + ], + "showgrid": true, + "showline": true, + "showticklabels": true, + "tickangle": 0, + "tickcolor": "rgb(0, 0, 0)", + "tickfont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tickmode": "array", + "ticks": "inside", + "ticktext": [ + "5", + "10", + "15", + "20" + ], + "tickvals": [ + 5, + 10, + 15, + 20 + ], + "title": "iterations", + "titlefont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 15 + }, + "type": "-", + "visible": true, + "zeroline": false, + "zerolinecolor": "rgba(0, 0, 0, 1.000)" + }, + "yaxis": { + "anchor": "x", + "domain": [ + 0.07581474190726165, + 0.9415463692038496 + ], + "gridcolor": "rgba(0, 0, 0, 0.100)", + "gridwidth": 0.5, + "linecolor": "rgba(0, 0, 0, 1.000)", + "mirror": false, + "range": [ + -0.012010220705787781, + 0.41239237828059716 + ], + "showgrid": true, + "showline": true, + "showticklabels": true, + "tickangle": 0, + "tickcolor": "rgb(0, 0, 0)", + "tickfont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 11 + }, + "tickmode": "array", + "ticks": "inside", + "ticktext": [ + "0.0", + "0.1", + "0.2", + "0.3", + "0.4" + ], + "tickvals": [ + 0, + 0.1, + 0.2, + 0.30000000000000004, + 0.4 + ], + "title": "relative error", + "titlefont": { + "color": "rgba(0, 0, 0, 1.000)", + "family": "sans-serif", + "size": 15 + }, + "type": "-", + "visible": true, + "zeroline": false, + "zerolinecolor": "rgba(0, 0, 0, 1.000)" + } + } + }, + "text/html": [ + "\n", + "\n", + " \n", + " Plots.jl\n", + " \n", + " \n", + " \n", + " \n", + "
\n", + " \n", + "\n", + " \n", + "\n" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "plot(cordic_error_of_iterations, X, title=\"Cordic relative error calculating sin(100)\", xguide = \"iterations\", yguide = \"relative error\")" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": {}, + "outputs": [], + "source": [ + "# Poniżej znajdują się funkcje testujące, na podstawie których powstała tabelka z błędami w sprawozdaniu" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "MersenneTwister(UInt32[0x00003039], Random.DSFMT.DSFMT_state(Int32[-870096391, 1072918504, -1812426662, 1073255081, -733866021, 1073404543, 807620846, 1073368448, 1919433844, 1072852359 … -362113007, 1073100625, -166402106, 1073460158, -1907020342, 721295190, -750225566, -1300227565, 382, 0]), [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], UInt128[0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000 … 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000, 0x00000000000000000000000000000000], 1002, 0)" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "TESTS = 100000000\n", + "\n", + "Random.seed!(12345)" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "taylor_test_error_real (generic function with 3 methods)" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function taylor_test_error_real(l::Float64=floatmin(), r::Float64=floatmax())\n", + " res = BigFloat(0)\n", + " abs_res = BigFloat(0)\n", + " maksi_rel = BigFloat(0)\n", + " maksi_abs = BigFloat(0)\n", + " for i = 1:TESTS\n", + " if i % 100000 == 0\n", + " println(i)\n", + " end\n", + " x = rand(Uniform(l, r))\n", + " lib_sin = sin(x)\n", + " if lib_sin == 0\n", + " continue\n", + " end\n", + " my_sin = taylor_sin(x, 0)\n", + " error = rel_error(lib_sin, my_sin[1])\n", + " abs_error = abs(my_sin[1] - lib_sin)\n", + " res += error\n", + " abs_res += abs_error\n", + " maksi_rel = max(maksi_rel, error)\n", + " maksi_abs = max(maksi_abs, abs_error)\n", + " end\n", + " return (res/TESTS, maksi_rel, abs_res/TESTS, maksi_abs)\n", + "end\n", + "\n", + "# (floatmin(), floatmax()):\n", + "# (1.887844299668514797145972383393008309519973773948872165524132116232181033410611e-15, \n", + "# 3.16719187748669057932019506480803006098767582443542778491973876953125e-08,\n", + "# 1.1794041986528804301572959036155385792454808324691839516162872314453125e-16,\n", + "# 8.8817841970012523233890533447265625e-16)\n", + "\n", + "# (-pi/2, pi/2):\n", + "# (1.471587646915289673578957365178574707202863924359834292944840261618821841693717e-15, \n", + "# 1.1848604479598457485905096801294400510329296594136394560337066650390625e-08, \n", + "# 9.765754183892570637182101557852154094518937199609354138374328613281249999999994e-17, \n", + "# 5.5511151231257827021181583404541015625e-16)\n", + "\n", + "# (0, 1):\n", + "# (8.693695902799099432701533207691913249153884601349429181102457242502623557811573e-17,\n", + "# 6.661260307992334044328275268948192015174572739102942797728701407322660088539124e-16,\n", + "# 4.293257315426284893844499634951716871000826358795166015624999999999999999999994e-17,\n", + "# 4.44089209850062616169452667236328125e-16)" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "taylor_test_error_complex (generic function with 3 methods)" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function taylor_test_error_complex(l::Float64=-100.0, r::Float64=100.0)\n", + " res = BigFloat(0)\n", + " abs_res = BigFloat(0)\n", + " maksi_rel = BigFloat(0)\n", + " maksi_abs = BigFloat(0)\n", + " for i = 1:TESTS\n", + " if i % 100000 == 0\n", + " println(i)\n", + " end\n", + " x = rand(Uniform(l, r))\n", + " y = rand(Uniform(max(l, -Float64(√(BigFloat(r)*r - BigFloat(x)*x))), \n", + " Float64(√(BigFloat(r)*r - BigFloat(x)*x))))\n", + " lib_sin = sin(x + y*im)\n", + " my_sin = taylor_sin(x, y)\n", + " error = rel_error(lib_sin, my_sin[1] + my_sin[2]*im)\n", + " abs_error = abs(lib_sin - (my_sin[1] + my_sin[2]*im))\n", + " res += error\n", + " abs_res += abs_error\n", + " maksi_rel = max(maksi_rel, error)\n", + " maksi_abs = max(maksi_abs, abs_error)\n", + " end\n", + " return (res/TESTS, maksi_rel, abs_res/TESTS, maksi_abs)\n", + "end\n", + "\n", + "# (-100, 100):\n", + "# (4.932205036590292360305897845543684560590114030155004375572792447173773555907229e-15, \n", + "# 1.3111008357751143737471652583705182364137709072338111582212150096893310546875e-13, \n", + "# 1.688623533003329462861070079404255492323042928202526655997186385923664654746476e+26, \n", + "# 5.89784569029861503624382775296e+29)\n", + "\n", + "# (-2pi, 2pi):\n", + "# (4.338436856498561167962902801400526155223569336855327458414068651872587652334067e-16, \n", + "# 1.48720543982594402760972427363260419015678071019692652043886482715606689453125e-11, \n", + "# 1.364745868545483273874507699553481910023596725366415789061836204439613629002538e-14, \n", + "# 8.7095846425677781478128738959826782468909289747216462274082005023956298828125e-13)\n", + "\n", + "# (0, 1):\n", + "# (1.596935223079780368874812440778376297707878344605454825588075017177200118204992e-16, \n", + "# 1.098997011961567777204023105931451003520679665648174250236479565501213073730469e-15, \n", + "# 1.124298405324025732059699593805301650508046127888472394113736655893442950571177e-16, \n", + "# 1.110569915127177230816030746289393434073728902933275719533412484452128410339355e-15)" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "cordic_test_error (generic function with 3 methods)" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function cordic_test_error(l::Float64=floatmin(), r::Float64=floatmax())\n", + " res = BigFloat(0)\n", + " abs_res = BigFloat(0)\n", + " maksi_rel = BigFloat(0)\n", + " maksi_abs = BigFloat(0)\n", + " for i = 1:TESTS\n", + " if i % 100000 == 0\n", + " println(i)\n", + " end\n", + " x = rand(Uniform(l, r))\n", + " lib_sin = sin(x)\n", + " my_sin = cordic_sin(x)\n", + " error = rel_error(lib_sin, my_sin)\n", + " abs_error = abs(lib_sin - my_sin)\n", + " res += error\n", + " abs_res += abs_error\n", + " if error > maksi_rel\n", + " worst_rel = x\n", + " end\n", + " maksi_rel = max(maksi_rel, error)\n", + " maksi_abs = max(maksi_abs, abs_error)\n", + " end\n", + " return (res/TESTS, maksi_rel, abs_res/TESTS, maksi_abs)\n", + "end\n", + "\n", + "# (floatmin(), floatmax()):\n", + "# (3.099880824631376815575307358441341907045753361742215192539218280437518515668677e-08, \n", + "# 0.457561153670805575988111968399607576429843902587890625, \n", + "# 2.459716652636021482355597144179802356154379561203882076370064169168472290039072e-09, \n", + "# 0.0006041780891818948617810747236944735050201416015625)\n", + "\n", + "# (-2pi, 2pi):\n", + "# (2.769658715752475495709394998775060901506630522496771093654899307916206208091117e-08, \n", + "# 0.11834204003306579566778822254491387866437435150146484375, \n", + "# 2.532059440779907667675144447194875727078638982803227008844260126352310180664052e-09,\n", + "# 0.00552917548107156875403234153054654598236083984375)\n", + "\n", + "# (0, 1):\n", + "# (4.176404604808155838824592152607760760141260709650975486490997166423577713345588e-08, \n", + "# 0.091828765031669201679420666550868190824985504150390625, \n", + "# 2.613683444981852927700279986835644064485650872597943816799670457839965820312493e-09, \n", + "# 0.00052619288922584050993691562325693666934967041015625)" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "taylor_without_reduction_test_error (generic function with 3 methods)" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "function taylor_without_reduction_test_error(l::Float64=-100.0, r::Float64=100.0)\n", + " res = BigFloat(0)\n", + " abs_res = BigFloat(0)\n", + " maksi_rel = BigFloat(0)\n", + " maksi_abs = BigFloat(0)\n", + " for i = 1:TESTS\n", + " if i % 1000000 == 0\n", + " println(i)\n", + " end\n", + " x = rand(Uniform(l, r))\n", + " y = rand(Uniform(max(l, -Float64(√(BigFloat(r)*r - BigFloat(x)*x))), \n", + " Float64(√(BigFloat(r)*r - BigFloat(x)*x))))\n", + " lib_sin = sin(x + y*im)\n", + " my_sin = taylor_sin_no_reduction(x, y)\n", + " error = rel_error(lib_sin, my_sin[1] + my_sin[2]*im)\n", + " abs_error = abs(lib_sin - (my_sin[1] + my_sin[2]*im))\n", + " res += error\n", + " abs_res += abs_error\n", + " maksi_rel = max(maksi_rel, error)\n", + " maksi_abs = max(maksi_abs, abs_error)\n", + " end\n", + " return (res/TESTS, maksi_rel, abs_res/TESTS, maksi_abs)\n", + "end\n", + "\n", + "# (-100, 100)\n", + "# (4.774091809397734982069398193189465079787514988283523440828527859306283137571149e+23, \n", + "# 4.48814142545670189837451264e+26, \n", + "# 7.758560481134976967771949796127369173267383351574525337904198599731007318070319e+40, \n", + "# 2.20832987186165589366506156220211970162294784e+44)\n", + "\n", + "# (-2pi, 2pi)\n", + "# (0.6332711088634405192103194531076134843075526902544601426097735760298574150340518, \n", + "# 1.0, \n", + "# 23.44057586605533515691829807979128873527513778367553433852055381911453864572971, \n", + "# 267.74654227273646256435313262045383453369140625)\n", + "\n", + "# (0, 1)\n", + "# (1.589482169544726703219739509256918022523030217883325972454504856003547167066932e-16, \n", + "# 1.291897416767691567199962520855285151964115327068161054313577551511116325855255e-15, \n", + "# 1.118367257755837281340217148887719929595000777959128862241583039814976641146415e-16, \n", + "# 1.115760330918745818020084658567032229219617364690542160587938269600272178649902e-15)" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.4.1", + "language": "julia", + "name": "julia-1.4" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.4.1" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/Semestr 3/anm/pracownia1/prog/program.jl b/Semestr 3/anm/pracownia1/prog/program.jl new file mode 100644 index 0000000..8e2d8b4 --- /dev/null +++ b/Semestr 3/anm/pracownia1/prog/program.jl @@ -0,0 +1,247 @@ +using Printf + +global C_ITERATIONS = 30 +global CORDIC_MUL_POW = 30 +global CORDIC_MUL = 2.0^CORDIC_MUL_POW +global CORDIC_ATANS = [843314857, 497837829, 263043837, 133525159, 67021687, 33543516, 16775851, + 8388437, 4194283, 2097149, 1048576, 524288, 262144, 131072, 65536, 32768, 16384, 8192, 4096, + 2048, 1024, 512, 256, 128, 64, 32, 16, 8, 4, 2] +global CORDIC_F = 1768195363 +global CORDIC_F_INV = 652032874 + +global T_ITERATIONS = 15 +global HYPERBOLIC_MAX = 1 + +# liczenie szeregu Taylora +function series(x, parity, change_sign, iterations) + res = zero(x) + elem = one(x) + if parity == 1 + elem = x + end + i = parity + 1 + while i <= 2*iterations + parity + res += elem + elem *= change_sign*x*x/(i*(i+1)) + i += 2 + end + return res +end + +# generyczna funkcja stosująca wzory redukcyjne, licząca sinusa +function gen_sin(x, iterations, sin_fun, cos_fun) + if x < 0 + return -gen_sin(-x, iterations, sin_fun, cos_fun) + end + x = mod2pi(x) + if x > pi + return -gen_sin(x-pi, iterations, sin_fun, cos_fun) + end + if x > pi/2 + return gen_cos(x-pi/2, iterations, sin_fun, cos_fun) + end + if x > pi/4 + return gen_cos(pi/2-x, iterations, sin_fun, cos_fun) + end + return sin_fun(x, iterations) +end + +# generyczna funkcja stosująca wzory redukcyjne, licząca cosinusa +function gen_cos(x, iterations, sin_fun, cos_fun) + if x < 0 + return gen_cos(-x, iterations, sin_fun, cos_fun) + end + x = mod2pi(x) + if x > pi + return -gen_cos(x-pi, iterations, sin_fun, cos_fun) + end + if x > pi/2 + return -gen_sin(x-pi/2, iterations, sin_fun, cos_fun) + end + if x > pi/4 + return gen_sin(pi/2-x, iterations, sin_fun, cos_fun) + end + return cos_fun(x, iterations) +end + +# sin dla liczb rzeczywistych [taylor] +function real_sin(r, iterations) + return series(r, 1, -1, iterations) +end + +# cos dla liczb rzeczywistych [taylor] +function real_cos(r, iterations) + return series(r, 0, -1, iterations) +end + +# sinh [taylor] +function real_sinh(r, iterations) + if r > 1000 + return Inf + end + if r < -1000 + return -Inf + end + if r == 0 + return Float64(0) + end + if abs(r) > HYPERBOLIC_MAX + return 2*real_sinh(r/2, iterations)*real_cosh(r/2, iterations) + end + return series(r, 1, 1, iterations) +end + +# cosh [taylor] +function real_cosh(r, iterations) + if abs(r) > 1000 + return Inf + end + if r == 1 + return Float64(1) + end + if abs(r) > HYPERBOLIC_MAX + s = real_sinh(r/2, iterations) + c = real_cosh(r/2, iterations) + return s*s+c*c + end + return series(r, 0, 1, iterations) +end + +# sin dla liczb zespolonych [taylor] +function complex_sin(a, b, iterations) + return (gen_sin(a, iterations, real_sin, real_cos)*real_cosh(b, iterations), + gen_cos(a, iterations, real_sin, real_cos)*real_sinh(b, iterations)) +end + +# cos dla liczb zespolonych [taylor] +function complex_cos(a, b, iterations) + return (real_cos(a, iterations)*real_cosh(b, iterations), + -real_sin(a, iterations)*real_sinh(b, iterations)) +end + +# funkcja dla użytkownika [taylor] +function taylor_sin(a, b) + return complex_sin(a, b, T_ITERATIONS) +end + +# funkcja dla użytkownika [taylor] +function taylor_cos(a, b) + return complex_cos(a, b, T_ITERATIONS) +end + +# funkcja dla użytkownika [taylor] +function taylor_sinh(r) + return real_sinh(r, T_ITERATIONS) +end + +# funkcja dla użytkownika [taylor] +function taylor_cosh(r) + return real_cosh(r, T_ITERATIONS) +end + +# preprocesing [cordic] +function preprocess_atan(iterations) + global CORDIC_MUL + atan2pow = Array{Float64}(undef, iterations) + @printf("CORDIC_ATANS = [") + for i in 1:iterations + atan2pow[i] = round(atan(1.0 / Float64(BigInt(2)^(i - 1))) * CORDIC_MUL) + @printf("%d", atan2pow[i]) + if i < iterations + @printf(", ") + end + end + @printf("]\n") +end + + +# preprocesing [cordic] +function preprocess_scaling_factor(iterations) + CORDIC_F = 1.0 + for i in 0:iterations + CORDIC_F *= sqrt(1. + 1. / Float64(BigInt(2)^(2 * i))) + end + @printf("CORDIC_F = %d\nCORDIC_F_INV = %d\n", round(CORDIC_F * CORDIC_MUL), round(CORDIC_MUL / CORDIC_F)) +end + + +# funkcja licząca zarówno sin oraz cos [cordic] +function approx_trig(x, iterations) + global CORDIC_ATANS + global CORDIC_F_INV + X = CORDIC_F_INV + Y = 0 + Z = round(x * CORDIC_MUL) + s = 1 + for i in 0:(iterations - 1) + tempX = X + if Z == 0 + break + end + if Z >= 0 + X -= s * (Y >> i) + Y += s * (tempX >> i) + Z -= s * CORDIC_ATANS[i + 1] + else + X += s * (Y >> i) + Y -= s * (tempX >> i) + Z += s * CORDIC_ATANS[i + 1] + end + end + + return (Float64(X) / CORDIC_MUL, Float64(Y) / CORDIC_MUL) +end + +# wyciąganie sin z approx_trig [cordic] +function approx_sin(x, iterations) + return approx_trig(x, iterations)[2] +end + + +# wyciąganie cos z approx_trig [cordic] +function approx_cos(x, iterations) + return approx_trig(x, iterations)[1] +end + +# funkcja dla użytkownika [cordic] +function cordic_sin(x) + return gen_sin(x, C_ITERATIONS, approx_sin, approx_cos) +end + +# funkcja dla użytkownika [cordic] +function cordic_cos(x) + return gen_cos(x, C_ITERATIONS, approx_sin, approx_cos) +end + +# uruchamianie preprocesingu [cordic] +function preprocess_cordic() + println("Preprocessing CORDIC constants.") + preprocess_atan(CORDIC_MUL_POW) + preprocess_scaling_factor(CORDIC_MUL_POW) +end + +# sinh bez stosowania wzorów redukcyjnych [taylor] +function sinh_no_reduction(x, iterations) + return series(x, 1, 1, iterations) +end + +# cosh bez stosowania wzorów redukcyjnych [taylor] +function cosh_no_reduction(x, iterations) + return series(x, 0, 1, iterations) +end + +# sin bez stosowania wzorów redukcyjnych [taylor] +function taylor_sin_no_reduction(x, y) + return (real_sin(x, 10*round(x)+10) * cosh_no_reduction(y, 10*round(y)+10), + real_cos(x, 10*round(x)+10) * sinh_no_reduction(y, 10*round(x)+10)) +end + +# zmiana liczby iteracji [taylor] +function set_taylor_iterations(x) + global T_ITERATIONS = x +end + +# zmiana liczby iteracji [cordic] +function set_cordic_iterations(x) + global C_ITERATIONS = x +end \ No newline at end of file -- cgit v1.2.3