Last active
November 18, 2021 20:26
-
-
Save DrSplinter/6ea10a1eedc0cf56874fcd2a1b3982c2 to your computer and use it in GitHub Desktop.
matika - machy
This file contains hidden or 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": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from sympy import *\n", | |
| "from IPython.display import display\n", | |
| "\n", | |
| "def latex_double_dollar(cl):\n", | |
| " f = cl._repr_latex_\n", | |
| " cl._repr_latex_ = lambda *args, **kwargs : f\"${f(*args,**kwargs)}$\"\n", | |
| "\n", | |
| "if 'times' not in globals():\n", | |
| " for c in [Eq, Set, Matrix]:\n", | |
| " latex_double_dollar(c)\n", | |
| "times = True" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Zadani, pro funkci $$f$$ najdete Hessovu matici a gradient" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle f{\\left(x_{1},x_{2},x_{3} \\right)} = x_{1}^{2} + x_{1} x_{2} - 6 x_{1} + 1.5 x_{2}^{2} - 4 x_{2} + 3 x_{3}^{2} + x_{3}$$" | |
| ], | |
| "text/plain": [ | |
| "Eq(f(x1, x2, x3), x1**2 + x1*x2 - 6*x1 + 1.5*x2**2 - 4*x2 + 3*x3**2 + x3)" | |
| ] | |
| }, | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "syms = symbols('x1:4')\n", | |
| "f = Function('f')\n", | |
| "x1, x2, x3 = syms\n", | |
| "fun = x1**2 + 1.5*x2**2 + 3*x3**2 + x1*x2 + x3 - 4*x2 - 6*x1\n", | |
| "Eq(f(x1,x2,x3), fun)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Krok 1 - Vypocet parcialnich derivaci (gradient)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle \\left[\\begin{matrix}2 x_{1} + x_{2} - 6\\\\x_{1} + 3.0 x_{2} - 4\\\\6 x_{3} + 1\\end{matrix}\\right]$$" | |
| ], | |
| "text/plain": [ | |
| "Matrix([\n", | |
| "[ 2*x1 + x2 - 6],\n", | |
| "[x1 + 3.0*x2 - 4],\n", | |
| "[ 6*x3 + 1]])" | |
| ] | |
| }, | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "grad = Matrix(derive_by_array(fun, syms))\n", | |
| "grad\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Krok 2 - Vypocet druhych parcialnich derivaci (Hessova matice)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle \\left[\\begin{matrix}2 & 1 & 0\\\\1 & 3.0 & 0\\\\0 & 0 & 6\\end{matrix}\\right]$$" | |
| ], | |
| "text/plain": [ | |
| "Matrix([\n", | |
| "[2, 1, 0],\n", | |
| "[1, 3.0, 0],\n", | |
| "[0, 0, 6]])" | |
| ] | |
| }, | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "hess = Matrix(derive_by_array(derive_by_array(fun, syms), syms))\n", | |
| "hess" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Krok 3 - Pozitivne definitni" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle \\left|{\\left[\\begin{matrix}2\\end{matrix}\\right]}\\right| = 2$$" | |
| ], | |
| "text/plain": [ | |
| "Eq(Determinant(Matrix([[2]])), 2)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| }, | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle \\left|{\\left[\\begin{matrix}2 & 1\\\\1 & 3.0\\end{matrix}\\right]}\\right| = 5.0$$" | |
| ], | |
| "text/plain": [ | |
| "Eq(Determinant(Matrix([\n", | |
| "[2, 1],\n", | |
| "[1, 3.0]])), 5.0)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| }, | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle \\left|{\\left[\\begin{matrix}2 & 1 & 0\\\\1 & 3.0 & 0\\\\0 & 0 & 6\\end{matrix}\\right]}\\right| = 30.0$$" | |
| ], | |
| "text/plain": [ | |
| "Eq(Determinant(Matrix([\n", | |
| "[2, 1, 0],\n", | |
| "[1, 3.0, 0],\n", | |
| "[0, 0, 6]])), 30.0)" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| }, | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "True" | |
| ] | |
| }, | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "for i in range(len(syms)):\n", | |
| " s = hess[:i+1,:i+1]\n", | |
| " display(Eq(Determinant(s), s.det(),evaluate=False))\n", | |
| "hess.is_positive_definite" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "Krok 4 - stacionarni body" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/latex": [ | |
| "$$\\displaystyle \\left\\{\\left( 2.8, \\ 0.4, \\ -0.166666666666667\\right)\\right\\}$$" | |
| ], | |
| "text/plain": [ | |
| "{(2.8, 0.4, -0.166666666666667)}" | |
| ] | |
| }, | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "linsolve(grad,syms)" | |
| ] | |
| } | |
| ], | |
| "metadata": { | |
| "interpreter": { | |
| "hash": "aee8b7b246df8f9039afb4144a1f6fd8d2ca17a180786b69acc140d282b71a49" | |
| }, | |
| "kernelspec": { | |
| "display_name": "Python 3.9.7 64-bit", | |
| "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.9.7" | |
| }, | |
| "orig_nbformat": 4 | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment