{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T03:12:26.273991Z", "start_time": "2022-04-16T03:12:26.268549Z" } }, "outputs": [], "source": [ "from IPython.display import Math, HTML\n", "\n", "def load_mathjax_in_cell_output():\n", " display(HTML(\"\"))\n", "get_ipython().events.register('pre_run_cell', load_mathjax_in_cell_output)" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T03:12:37.881284Z", "start_time": "2022-04-16T03:12:37.013651Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sympy import Function, hessian, Matrix, init_printing\n", "init_printing()" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:22:39.068211Z", "start_time": "2022-04-16T04:22:39.062261Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# n=3, m = 4\n", "from sympy.abc import x, y, z" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:23:50.884011Z", "start_time": "2022-04-16T04:23:50.878458Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f = Function('f')(x, y, z)\n", "g = Function('g')(x, y, z)\n", "h = Function('h')(x, y, z)\n", "p = Function('p')(x, y, z)" ] }, { "cell_type": "code", "execution_count": 16, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:24:02.186209Z", "start_time": "2022-04-16T04:24:02.181132Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "multi_f = Matrix([f, g, h, p])" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:24:21.767987Z", "start_time": "2022-04-16T04:24:21.745196Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}\\frac{\\partial}{\\partial x} f{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial y} f{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial z} f{\\left (x,y,z \\right )}\\\\\\frac{\\partial}{\\partial x} g{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial y} g{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial z} g{\\left (x,y,z \\right )}\\\\\\frac{\\partial}{\\partial x} h{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial y} h{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial z} h{\\left (x,y,z \\right )}\\\\\\frac{\\partial}{\\partial x} p{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial y} p{\\left (x,y,z \\right )} & \\frac{\\partial}{\\partial z} p{\\left (x,y,z \\right )}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡∂ ∂ ∂ ⎤\n", "⎢──(f(x, y, z)) ──(f(x, y, z)) ──(f(x, y, z))⎥\n", "⎢∂x ∂y ∂z ⎥\n", "⎢ ⎥\n", "⎢∂ ∂ ∂ ⎥\n", "⎢──(g(x, y, z)) ──(g(x, y, z)) ──(g(x, y, z))⎥\n", "⎢∂x ∂y ∂z ⎥\n", "⎢ ⎥\n", "⎢∂ ∂ ∂ ⎥\n", "⎢──(h(x, y, z)) ──(h(x, y, z)) ──(h(x, y, z))⎥\n", "⎢∂x ∂y ∂z ⎥\n", "⎢ ⎥\n", "⎢∂ ∂ ∂ ⎥\n", "⎢──(p(x, y, z)) ──(p(x, y, z)) ──(p(x, y, z))⎥\n", "⎣∂x ∂y ∂z ⎦" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multi_f.jacobian([x, y, z])" ] }, { "cell_type": "code", "execution_count": 18, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:25:45.025603Z", "start_time": "2022-04-16T04:25:45.020531Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# jacobian, hessian" ] }, { "cell_type": "code", "execution_count": 19, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:26:25.662601Z", "start_time": "2022-04-16T04:26:25.657969Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sympy import sin" ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:26:41.371024Z", "start_time": "2022-04-16T04:26:41.366746Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f1 = x**2*y\n", "f2 = 5*x+sin(y)" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:26:54.987676Z", "start_time": "2022-04-16T04:26:54.982735Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "multi_f = Matrix([f1, f2])" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:27:13.712719Z", "start_time": "2022-04-16T04:27:13.699817Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}2 x y & x^{2}\\\\5 & \\cos{\\left (y \\right )}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ 2 ⎤\n", "⎢2⋅x⋅y x ⎥\n", "⎢ ⎥\n", "⎣ 5 cos(y)⎦" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "multi_f.jacobian([x, y])" ] }, { "cell_type": "code", "execution_count": 26, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:28:25.417289Z", "start_time": "2022-04-16T04:28:25.398308Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}2 y & 2 x\\\\2 x & 0\\end{matrix}\\right]$$" ], "text/plain": [ "⎡2⋅y 2⋅x⎤\n", "⎢ ⎥\n", "⎣2⋅x 0 ⎦" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hessian(Matrix([f1, ]), [x, y])" ] }, { "cell_type": "code", "execution_count": 27, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:29:40.067324Z", "start_time": "2022-04-16T04:29:40.062911Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "# Jacobian 和 Gradient vector" ] }, { "cell_type": "code", "execution_count": 28, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:29:51.701948Z", "start_time": "2022-04-16T04:29:51.696999Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "single_f = Matrix([f1, ])" ] }, { "cell_type": "code", "execution_count": 30, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:30:02.285106Z", "start_time": "2022-04-16T04:30:02.273177Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}2 x y & x^{2}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ 2⎤\n", "⎣2⋅x⋅y x ⎦" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "single_f.jacobian([x, y])" ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:30:11.565985Z", "start_time": "2022-04-16T04:30:11.560948Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sympy import derive_by_array" ] }, { "cell_type": "code", "execution_count": 32, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:30:24.919642Z", "start_time": "2022-04-16T04:30:24.899448Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}\\left[\\left[\\begin{matrix}2 x y\\end{matrix}\\right]\\right] & \\left[\\left[\\begin{matrix}x^{2}\\end{matrix}\\right]\\right]\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ ⎡⎡ 2⎤⎤⎤\n", "⎣[[2⋅x⋅y]] ⎣⎣x ⎦⎦⎦" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "derive_by_array(single_f, [x, y])" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:31:46.066642Z", "start_time": "2022-04-16T04:31:46.057182Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "f = 0.5*(1-x)**2 + (y-x**2)**2" ] }, { "cell_type": "code", "execution_count": 34, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:32:18.968269Z", "start_time": "2022-04-16T04:32:18.923939Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}- 4 x \\left(- x^{2} + y\\right) + 1.0 x - 1.0 & - 2 x^{2} + 2 y\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ ⎛ 2 ⎞ 2 ⎤\n", "⎣- 4⋅x⋅⎝- x + y⎠ + 1.0⋅x - 1.0 - 2⋅x + 2⋅y⎦" ] }, "execution_count": 34, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Matrix([f, ]).jacobian([x, y])" ] }, { "cell_type": "code", "execution_count": 35, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:33:17.308460Z", "start_time": "2022-04-16T04:33:17.291752Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}12 x^{2} - 4 y + 1.0 & - 4 x\\\\- 4 x & 2\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ 2 ⎤\n", "⎢12⋅x - 4⋅y + 1.0 -4⋅x⎥\n", "⎢ ⎥\n", "⎣ -4⋅x 2 ⎦" ] }, "execution_count": 35, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hessian(Matrix([f, ]), [x, y])" ] }, { "cell_type": "code", "execution_count": 44, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:38:05.525793Z", "start_time": "2022-04-16T04:38:05.521680Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" } ], "source": [ "from sympy.abc import w\n", "g = Function('g')(x, y, z)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "ExecuteTime": { "end_time": "2022-04-16T04:38:08.404704Z", "start_time": "2022-04-16T04:38:08.383639Z" } }, "outputs": [ { "data": { "text/html": [ "" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/latex": [ "$$\\left[\\begin{matrix}\\frac{\\partial^{2}}{\\partial x^{2}} g{\\left (x,y,z \\right )} & \\frac{\\partial^{2}}{\\partial x\\partial y} g{\\left (x,y,z \\right )} & \\frac{\\partial^{2}}{\\partial x\\partial z} g{\\left (x,y,z \\right )}\\\\\\frac{\\partial^{2}}{\\partial x\\partial y} g{\\left (x,y,z \\right )} & \\frac{\\partial^{2}}{\\partial y^{2}} g{\\left (x,y,z \\right )} & \\frac{\\partial^{2}}{\\partial y\\partial z} g{\\left (x,y,z \\right )}\\\\\\frac{\\partial^{2}}{\\partial x\\partial z} g{\\left (x,y,z \\right )} & \\frac{\\partial^{2}}{\\partial y\\partial z} g{\\left (x,y,z \\right )} & \\frac{\\partial^{2}}{\\partial z^{2}} g{\\left (x,y,z \\right )}\\end{matrix}\\right]$$" ], "text/plain": [ "⎡ 2 2 2 ⎤\n", "⎢ ∂ ∂ ∂ ⎥\n", "⎢ ───(g(x, y, z)) ─────(g(x, y, z)) ─────(g(x, y, z))⎥\n", "⎢ 2 ∂y ∂x ∂z ∂x ⎥\n", "⎢ ∂x ⎥\n", "⎢ ⎥\n", "⎢ 2 2 2 ⎥\n", "⎢ ∂ ∂ ∂ ⎥\n", "⎢─────(g(x, y, z)) ───(g(x, y, z)) ─────(g(x, y, z))⎥\n", "⎢∂y ∂x 2 ∂z ∂y ⎥\n", "⎢ ∂y ⎥\n", "⎢ ⎥\n", "⎢ 2 2 2 ⎥\n", "⎢ ∂ ∂ ∂ ⎥\n", "⎢─────(g(x, y, z)) ─────(g(x, y, z)) ───(g(x, y, z)) ⎥\n", "⎢∂z ∂x ∂z ∂y 2 ⎥\n", "⎣ ∂z ⎦" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hessian(g, [x, y, z])" ] } ], "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.8" } }, "nbformat": 4, "nbformat_minor": 2 }