Created
October 2, 2022 09:31
-
-
Save HajimeKawahara/a636fe49fe7a7b7578cfaa47094d7765 to your computer and use it in GitHub Desktop.
Minimum Automatic Differentiation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Minimum Automatic Differentiation" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"def mul(x, y):\n", | |
" a, b = x\n", | |
" c, d = y\n", | |
" return a*c, a*d + b*c\n", | |
"\n", | |
"def add(x, y):\n", | |
" a, b = x\n", | |
" c, d = y\n", | |
" return a + c, b + d\n", | |
"\n", | |
"def cos(x):\n", | |
" a, b = x\n", | |
" return np.cos(a), - np.sin(a)*b\n", | |
"\n", | |
"def sin(x):\n", | |
" a, b = x\n", | |
" return np.sin(a), np.cos(a)*b\n", | |
" \n", | |
"def log(x):\n", | |
" a, b = x\n", | |
" return np.log(a), b/a " | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"Consider \n", | |
"$f(x) = \\log{(\\cos{x} \\sin{x})} + \\sin{x}$\n", | |
"and derive its derivative at $x=1.0$" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"f = lambda x: add(log(mul(cos(x),sin(x))),sin(x))\n", | |
"df = lambda x: f([x,1.0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 9, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(0.05324076815279066, -0.37501280285243144)" | |
] | |
}, | |
"execution_count": 9, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"df(1.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 13, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"g = lambda x: f(f(x))\n", | |
"dg = lambda x: g([x,1.0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 14, | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"data": { | |
"text/plain": [ | |
"(-2.8816056725768977, -7.391555094461485)" | |
] | |
}, | |
"execution_count": 14, | |
"metadata": {}, | |
"output_type": "execute_result" | |
} | |
], | |
"source": [ | |
"dg(1.0)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": {}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3.8.8 ('base')", | |
"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.8.8" | |
}, | |
"orig_nbformat": 4, | |
"vscode": { | |
"interpreter": { | |
"hash": "72bc7f8b1808a6f5ada3c6a20601509b8b1843160436d276d47f2ba819b3753b" | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment